Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/include/thread.h
2093 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
#ifndef _THREAD_H_
31
#define _THREAD_H_
32
33
/*
34
* Definition of a thread.
35
*
36
* Note: curthread is defined by <current.h>.
37
*/
38
39
#include <spinlock.h>
40
#include <threadlist.h>
41
#include <proc.h>
42
43
struct addrspace;
44
struct cpu;
45
struct vnode;
46
47
/* get machine-dependent defs */
48
#include <machine/thread.h>
49
50
51
/* Size of kernel stacks; must be power of 2 */
52
#define STACK_SIZE 4096
53
54
/* Mask for extracting the stack base address of a kernel stack pointer */
55
#define STACK_MASK (~(vaddr_t)(STACK_SIZE-1))
56
57
/* Macro to test if two addresses are on the same kernel stack */
58
#define SAME_STACK(p1, p2) (((p1) & STACK_MASK) == ((p2) & STACK_MASK))
59
60
61
/* States a thread can be in. */
62
typedef enum {
63
S_RUN, /* running */
64
S_READY, /* ready to run */
65
S_SLEEP, /* sleeping */
66
S_ZOMBIE, /* zombie; exited but not yet deleted */
67
} threadstate_t;
68
69
/* Thread structure. */
70
struct thread {
71
/*
72
* These go up front so they're easy to get to even if the
73
* debugger is messed up.
74
*/
75
char *t_name; /* Name of this thread */
76
const char *t_wchan_name; /* Name of wait channel, if sleeping */
77
threadstate_t t_state; /* State this thread is in */
78
79
/*
80
* Thread subsystem internal fields.
81
*/
82
struct thread_machdep t_machdep; /* Any machine-dependent goo */
83
struct threadlistnode t_listnode; /* Link for run/sleep/zombie lists */
84
void *t_stack; /* Kernel-level stack */
85
struct switchframe *t_context; /* Saved register context (on stack) */
86
struct cpu *t_cpu; /* CPU thread runs on */
87
88
/*
89
* Interrupt state fields.
90
*
91
* t_in_interrupt is true if current execution is in an
92
* interrupt handler, which means the thread's normal context
93
* of execution is stopped somewhere in the middle of doing
94
* something else. This makes assorted operations unsafe.
95
*
96
* See notes in spinlock.c regarding t_curspl and t_iplhigh_count.
97
*
98
* Exercise for the student: why is this material per-thread
99
* rather than per-cpu or global?
100
*/
101
bool t_in_interrupt; /* Are we in an interrupt? */
102
int t_curspl; /* Current spl*() state */
103
int t_iplhigh_count; /* # of times IPL has been raised */
104
int t_vmp_count; /* # of vmpages locks */
105
int t_clone;
106
107
/*
108
* Public fields
109
*/
110
111
/* VM */
112
struct addrspace *t_addrspace; /* virtual address space */
113
114
/* VFS */
115
struct vnode *t_cwd; /* current working directory */
116
117
/* modifications for ASST2 */
118
struct proc *td_proc; /* process associated with this thread */
119
};
120
121
/* Call once during system startup to allocate data structures. */
122
void thread_bootstrap(void);
123
124
/* Call late in system startup to get secondary CPUs running. */
125
void thread_start_cpus(void);
126
127
/* Call during panic to stop other threads in their tracks */
128
void thread_panic(void);
129
130
/* Call during system shutdown to offline other CPUs. */
131
void thread_shutdown(void);
132
133
/*
134
* Make a new thread, which will start executing at "func". The "data"
135
* arguments (one pointer, one number) are passed to the function. The
136
* current thread is used as a prototype for creating the new one. If
137
* "ret" is non-null, the thread structure for the new thread is
138
* handed back. (Note that using said thread structure from the parent
139
* thread should be done only with caution, because in general the
140
* child thread might exit at any time.) Returns an error code.
141
*/
142
int thread_fork(const char *name,
143
void (*func)(void *, unsigned long),
144
void *data1, unsigned long data2,
145
struct thread **ret);
146
147
/*
148
* Cause the current thread to exit.
149
* Interrupts need not be disabled.
150
*/
151
void thread_exit(void);
152
153
/*
154
* Cause the current thread to yield to the next runnable thread, but
155
* itself stay runnable.
156
* Interrupts need not be disabled.
157
*/
158
void thread_yield(void);
159
160
/*
161
* Reshuffle the run queue. Called from the timer interrupt.
162
*/
163
void schedule(void);
164
165
/*
166
* Potentially migrate ready threads to other CPUs. Called from the
167
* timer interrupt.
168
*/
169
void thread_consider_migration(void);
170
171
172
#endif /* _THREAD_H_ */
173
174