/* [<][>][^][v][top][bottom][index][help] */
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 <array.h>
40 #include <spinlock.h>
41 #include <threadlist.h>
42
43 struct cpu;
44
45 /* get machine-dependent defs */
46 #include <machine/thread.h>
47
48
49 /* Size of kernel stacks; must be power of 2 */
50 #define STACK_SIZE 4096
51
52 /* Mask for extracting the stack base address of a kernel stack pointer */
53 #define STACK_MASK (~(vaddr_t)(STACK_SIZE-1))
54
55 /* Macro to test if two addresses are on the same kernel stack */
56 #define SAME_STACK(p1, p2) (((p1) & STACK_MASK) == ((p2) & STACK_MASK))
57
58
59 /* States a thread can be in. */
60 typedef enum {
61 S_RUN, /* running */
62 S_READY, /* ready to run */
63 S_SLEEP, /* sleeping */
64 S_ZOMBIE, /* zombie; exited but not yet deleted */
65 } threadstate_t;
66
67 /* Thread structure. */
68 struct thread {
69 /*
70 * These go up front so they're easy to get to even if the
71 * debugger is messed up.
72 */
73 char *t_name; /* Name of this thread */
74 const char *t_wchan_name; /* Name of wait channel, if sleeping */
75 threadstate_t t_state; /* State this thread is in */
76
77 /*
78 * Thread subsystem internal fields.
79 */
80 struct thread_machdep t_machdep; /* Any machine-dependent goo */
81 struct threadlistnode t_listnode; /* Link for run/sleep/zombie lists */
82 void *t_stack; /* Kernel-level stack */
83 struct switchframe *t_context; /* Saved register context (on stack) */
84 struct cpu *t_cpu; /* CPU thread runs on */
85 struct proc *t_proc; /* Process thread belongs to */
86
87 /*
88 * Interrupt state fields.
89 *
90 * t_in_interrupt is true if current execution is in an
91 * interrupt handler, which means the thread's normal context
92 * of execution is stopped somewhere in the middle of doing
93 * something else. This makes assorted operations unsafe.
94 *
95 * See notes in spinlock.c regarding t_curspl and t_iplhigh_count.
96 *
97 * Exercise for the student: why is this material per-thread
98 * rather than per-cpu or global?
99 */
100 bool t_in_interrupt; /* Are we in an interrupt? */
101 int t_curspl; /* Current spl*() state */
102 int t_iplhigh_count; /* # of times IPL has been raised */
103
104 /*
105 * Public fields
106 */
107
108 /* add more here as needed */
109 };
110
111 /*
112 * Array of threads.
113 */
114 #ifndef THREADINLINE
115 #define THREADINLINE INLINE
116 #endif
117
118 DECLARRAY(thread);
119 DEFARRAY(thread, THREADINLINE);
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 thread
135 * will belong to the process "proc", or to the current thread's
136 * process if "proc" is null. The "data" arguments (one pointer, one
137 * number) are passed to the function. The current thread is used as a
138 * prototype for creating the new one. Returns an error code. The
139 * thread structure for the new thread is not returned; it is not in
140 * general safe to refer to it as the new thread may exit and
141 * disappear at any time without notice.
142 */
143 int thread_fork(const char *name, struct proc *proc,
144 void (*func)(void *, unsigned long),
145 void *data1, unsigned long data2);
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_ */