FreeBSD/Linux Kernel Cross Reference
sys/kern/thread.h
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993-1987 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * HISTORY
28 * $Log: thread.h,v $
29 * Revision 2.19 93/08/10 15:12:01 mrt
30 * Conditionalized atm hooks.
31 * [93/07/30 cmaeda]
32 * Included hook for network interface.
33 * [93/06/09 15:45:03 jcb]
34 *
35 * Revision 2.18 93/01/27 09:33:10 danner
36 * Break latent include circularity by introduction of ipc_kmsg_queue.h.
37 *
38 *
39 *
40 * Revision 2.17 93/01/24 13:20:32 danner
41 * Added sample_control to indicate that this threads is to
42 * have its pc's sampled periodically.
43 * [93/01/12 rvb]
44 *
45 * Revision 2.16 93/01/21 14:04:00 danner
46 * Typo correction.
47 *
48 * Revision 2.15 93/01/19 09:01:07 danner
49 * Correct arguments to natural_t.
50 * [93/01/19 danner]
51 *
52 * Revision 2.14 93/01/14 17:37:12 danner
53 * Moved actual declaration of struct thread and thread_t to
54 * kern/kern_types.h, to permit mutually recursive structure
55 * definitions. Added ANSI function prototypes.
56 * [92/12/28 dbg]
57 * Swapped some fields for better alignment.
58 * [92/12/01 af]
59 *
60 * Revision 2.13 91/08/28 11:14:53 jsb
61 * Added ith_seqno.
62 * [91/08/10 rpd]
63 *
64 * Revision 2.12 91/07/31 17:49:44 dbg
65 * Consolidated interruptible, swap_state, halted into state field.
66 * Revised state machine.
67 * [91/07/30 17:06:37 dbg]
68 *
69 * Revision 2.11 91/05/18 14:34:28 rpd
70 * Added depress_timer.
71 * [91/03/31 rpd]
72 *
73 * Replaced swap_privilege with stack_privilege.
74 * [91/03/30 rpd]
75 *
76 * Revision 2.10 91/05/14 16:48:57 mrt
77 * Correcting copyright
78 *
79 * Revision 2.9 91/03/16 14:52:58 rpd
80 * Removed ith_saved.
81 * [91/02/16 rpd]
82 * Added save-state fields for page faults.
83 * [91/02/05 rpd]
84 * Added NCPUS to active_threads declaration.
85 * Added active_stacks.
86 * [91/01/28 rpd]
87 * Added swap_privilege.
88 * [91/01/18 rpd]
89 *
90 * Revision 2.8 91/02/05 17:30:34 mrt
91 * Changed to new Mach copyright
92 * [91/02/01 16:19:58 mrt]
93 *
94 * Revision 2.7 91/01/08 15:18:11 rpd
95 * Added saved-state fields for exceptions.
96 * [90/12/23 rpd]
97 * Added swap_func.
98 * [90/11/20 rpd]
99 *
100 * Revision 2.6 90/08/27 22:04:23 dbg
101 * Remove import of thread_modes.h (unneeded).
102 * [90/07/17 dbg]
103 *
104 * Revision 2.5 90/08/07 17:59:15 rpd
105 * Put last_processor field under NCPUS > 1.
106 * Removed tmp_address and tmp_object fields.
107 * [90/08/07 rpd]
108 *
109 * Revision 2.4 90/06/02 14:57:07 rpd
110 * Converted to new IPC.
111 * [90/03/26 22:25:34 rpd]
112 *
113 * Revision 2.3 90/02/22 20:04:18 dbg
114 * Add per-thread global VM variables (tmp_address, tmp_object).
115 * [89/04/29 mwyoung]
116 *
117 * Revision 2.2 89/09/08 11:26:59 dbg
118 * Added simple_rpc_kmsg [rfr]. Set its size to size of
119 * small Kmsg. Moved all IPC data structures to end of
120 * thread structure.
121 * [89/08/16 dbg]
122 *
123 * Revision 2.6 88/10/11 10:26:00 rpd
124 * Added ipc_data to the thread structure.
125 * [88/10/10 08:00:16 rpd]
126 *
127 * Revision 2.5 88/08/24 02:47:53 mwyoung
128 * Adjusted include file references.
129 * [88/08/17 02:24:57 mwyoung]
130 *
131 * 1-Sep-88 David Black (dlb) at Carnegie-Mellon University
132 * Change all usage and delta fields to unsigned to prevent
133 * negative priorities.
134 *
135 * 11-Aug-88 David Black (dlb) at Carnegie-Mellon University
136 * Split exit_code field into ast and halted fields.
137 * Changed thread_should_halt() macro.
138 *
139 * 9-Aug-88 David Black (dlb) at Carnegie-Mellon University
140 * Replaced preempt_pri field with first_quantum.
141 *
142 * 19-Aug-88 David Golub (dbg) at Carnegie-Mellon University
143 * Changed 'struct pcb *' to 'pcb_t' to pry this file loose from
144 * old data structures. Removed include of 'machine/pcb.h' - the
145 * structure definition should be moved to 'machine/thread.h'.
146 *
147 * Revision 2.4 88/08/06 19:22:05 rpd
148 * Declare external variables as "extern".
149 * Added macros ipc_thread_lock(), ipc_thread_unlock().
150 *
151 * Revision 2.3 88/07/17 18:55:10 mwyoung
152 * .
153 *
154 * Revision 2.2.1.1 88/06/28 20:53:40 mwyoung
155 * Reorganized. Added thread_t->vm_privilege.
156 *
157 * Added current_task() declaration.
158 *
159 *
160 * 6-Jul-88 David Golub (dbg) at Carnegie-Mellon University
161 * Remove compatibility data structures. Add per-thread timeout
162 * element.
163 *
164 * 21-Jun-88 Michael Young (mwyoung) at Carnegie-Mellon University
165 * Reorganized. Added thread_t->vm_privilege.
166 *
167 * 4-May-88 David Golub (dbg) at Carnegie-Mellon University
168 * Remove vax-specific field (pcb physical address).
169 *
170 * 19-Apr-88 Michael Young (mwyoung) at Carnegie-Mellon University
171 * Added current_task() declaration.
172 *
173 * 7-Apr-88 David Black (dlb) at Carnegie-Mellon University
174 * MACH_TIME_NEW is now standard.
175 *
176 * 4-Mar-88 David Black (dlb) at Carnegie-Mellon University
177 * Added usage_save and preempt_pri fields.
178 *
179 * 19-Feb-88 David Black (dlb) at Carnegie-Mellon University
180 * Deleted unused ticks field. Rearranged and added MACH_TIME_NEW
181 * fields for scheduler interface. user_ticks and system_ticks are
182 * not needed under MACH_TIME_NEW. Change wait_time to sched_stamp.
183 *
184 * 21-Jan-88 David Golub (dbg) at Carnegie-Mellon University
185 * Replaced swappable boolean with swap_state field. Swap states
186 * are defined in sys/thread_swap.h.
187 *
188 * 17-Jan-88 David Golub (dbg) at Carnegie-Mellon University
189 * Added declarations of new routines.
190 *
191 * 29-Dec-87 David Golub (dbg) at Carnegie-Mellon University
192 * Added declarations of thread_halt() and thread_halt_self().
193 *
194 * 21-Dec-87 David Golub (dbg) at Carnegie-Mellon University
195 * Added thread_should_halt macro.
196 *
197 * 9-Dec-87 David Golub (dbg) at Carnegie-Mellon University
198 * Added exit_code for thread termination and interrupt.
199 * Removed ipc_message_waiting and ipc_timer_set.
200 *
201 * 3-Dec-87 David Black (dlb) at Carnegie-Mellon University
202 * Added ipc_kernel field to indicate when message buffer is in
203 * kernel address space. Added exception_clear_port to cache
204 * reply port for reuse in exc rpc.
205 *
206 * 2-Dec-87 David Black (dlb) at Carnegie-Mellon University
207 * Removed conditionals, purged history.
208 */
209 /*
210 * File: thread.h
211 * Author: Avadis Tevanian, Jr.
212 *
213 * This file contains the structure definitions for threads.
214 *
215 */
216
217 #ifndef _KERN_THREAD_H_
218 #define _KERN_THREAD_H_
219
220 #include <mach_ipc_compat.h>
221 #include <hw_footprint.h>
222 #include <mach_fixpri.h>
223 #include <mach_host.h>
224 #include <net_atm.h>
225
226 #include <mach/boolean.h>
227 #include <mach/thread_info.h>
228 #include <mach/thread_status.h>
229 #include <mach/machine/vm_types.h>
230 #include <mach/message.h>
231 #include <mach/port.h>
232 #include <mach/pc_sample.h>
233 #include <mach/vm_prot.h>
234 #include <kern/ast.h>
235 #include <kern/cpu_number.h>
236 #include <kern/queue.h>
237 #include <kern/processor.h>
238 #include <kern/sched_prim.h> /* event_t, continuation_t */
239 #include <kern/time_out.h>
240 #include <kern/timer.h>
241 #include <kern/lock.h>
242 #include <kern/sched.h>
243 #include <kern/task.h> /* for current_space(), current_map() */
244 #include <machine/thread.h>
245 #include <ipc/ipc_kmsg_queue.h>
246
247 struct thread {
248 /* Run queues */
249 queue_chain_t links; /* current run queue links */
250 run_queue_t runq; /* run queue p is on SEE BELOW */
251 /*
252 * NOTE: The runq field in the thread structure has an unusual
253 * locking protocol. If its value is RUN_QUEUE_NULL, then it is
254 * locked by the thread_lock, but if its value is something else
255 * (i.e. a run_queue) then it is locked by that run_queue's lock.
256 */
257
258 /* Task information */
259 task_t task; /* Task to which I belong */
260 queue_chain_t thread_list; /* list of threads in task */
261
262 /* Thread bookkeeping */
263 queue_chain_t pset_threads; /* list of all threads in proc set*/
264
265 /* Self-preservation */
266 decl_simple_lock_data(,lock)
267 int ref_count; /* number of references to me */
268
269 /* Hardware state */
270 pcb_t pcb; /* hardware pcb & machine state */
271 vm_offset_t kernel_stack; /* accurate only if the thread is
272 not swapped and not executing */
273 vm_offset_t stack_privilege;/* reserved kernel stack */
274
275 /* Swapping information */
276 void (*swap_func)(); /* start here after swapin */
277
278 /* Blocking information */
279 event_t wait_event; /* event we are waiting on */
280 int suspend_count; /* internal use only */
281 kern_return_t wait_result; /* outcome of wait -
282 may be examined by this thread
283 WITHOUT locking */
284 boolean_t wake_active; /* someone is waiting for this
285 thread to become suspended */
286 int state; /* Thread state: */
287 /*
288 * Thread states [bits or'ed]
289 */
290 #define TH_WAIT 0x01 /* thread is queued for waiting */
291 #define TH_SUSP 0x02 /* thread has been asked to stop */
292 #define TH_RUN 0x04 /* thread is running or on runq */
293 #define TH_UNINT 0x08 /* thread is waiting uninteruptibly */
294 #define TH_HALTED 0x10 /* thread is halted at clean point ? */
295
296 #define TH_IDLE 0x80 /* thread is an idle thread */
297
298 #define TH_SCHED_STATE (TH_WAIT|TH_SUSP|TH_RUN|TH_UNINT)
299
300 #define TH_SWAPPED 0x0100 /* thread has no kernel stack */
301 #define TH_SW_COMING_IN 0x0200 /* thread is waiting for kernel stack */
302
303 #define TH_SWAP_STATE (TH_SWAPPED | TH_SW_COMING_IN)
304
305 /* Scheduling information */
306 int priority; /* thread's priority */
307 int max_priority; /* maximum priority */
308 int sched_pri; /* scheduled (computed) priority */
309 #if MACH_FIXPRI
310 int sched_data; /* for use by policy */
311 int policy; /* scheduling policy */
312 #endif /* MACH_FIXPRI */
313 int depress_priority; /* depressed from this priority */
314 unsigned int cpu_usage; /* exp. decaying cpu usage [%cpu] */
315 unsigned int sched_usage; /* load-weighted cpu usage [sched] */
316 unsigned int sched_stamp; /* last time priority was updated */
317
318 /* VM global variables */
319
320 vm_offset_t recover; /* page fault recovery (copyin/out) */
321 boolean_t vm_privilege; /* Can use reserved memory? */
322
323 /* User-visible scheduling state */
324 int user_stop_count; /* outstanding stops */
325
326 /* IPC data structures */
327 struct thread *ith_next, *ith_prev;
328 mach_msg_return_t ith_state;
329 union {
330 mach_msg_size_t msize; /* max size for recvd msg */
331 struct ipc_kmsg *kmsg; /* received message */
332 } data;
333 mach_port_seqno_t ith_seqno; /* seqno of recvd message */
334
335 struct ipc_kmsg_queue ith_messages;
336
337 decl_simple_lock_data(, ith_lock_data)
338 struct ipc_port *ith_self; /* not a right, doesn't hold ref */
339 struct ipc_port *ith_sself; /* a send right */
340 struct ipc_port *ith_exception; /* a send right */
341 #if MACH_IPC_COMPAT
342 struct ipc_port *ith_reply; /* a send right */
343 #endif /* MACH_IPC_COMPAT */
344
345 mach_port_t ith_mig_reply; /* reply port for mig */
346 struct ipc_port *ith_rpc_reply; /* reply port for kernel RPCs */
347
348 /* State saved when thread's stack is discarded */
349 union {
350 struct {
351 mach_msg_header_t *msg;
352 mach_msg_option_t option;
353 mach_msg_size_t rcv_size;
354 mach_msg_timeout_t timeout;
355 mach_port_t notify;
356 struct ipc_object *object;
357 struct ipc_mqueue *mqueue;
358 } receive;
359 struct {
360 struct ipc_port *port;
361 int exc;
362 int code;
363 int subcode;
364 } exception;
365 void *other; /* catch-all for other state */
366 } saved;
367
368 /* Timing data structures */
369 timer_data_t user_timer; /* user mode timer */
370 timer_data_t system_timer; /* system mode timer */
371 timer_save_data_t user_timer_save; /* saved user timer value */
372 timer_save_data_t system_timer_save; /* saved sys timer val. */
373 unsigned int cpu_delta; /* cpu usage since last update */
374 unsigned int sched_delta; /* weighted cpu usage since update */
375
376 /* Time-outs */
377 timer_elt_data_t timer; /* timer for thread */
378 timer_elt_data_t depress_timer; /* timer for priority depression */
379
380 /* Ast/Halt data structures */
381 boolean_t active; /* how alive is the thread */
382 int ast; /* ast's needed. See ast.h */
383
384 /* Processor data structures */
385 processor_set_t processor_set; /* assigned processor set */
386 processor_t bound_processor; /* bound to processor ?*/
387
388 sample_control_t pc_sample;
389
390 #if MACH_HOST
391 boolean_t may_assign; /* may assignment change? */
392 boolean_t assign_active; /* someone waiting for may_assign */
393 #endif /* MACH_HOST */
394
395 #if NCPUS > 1
396 processor_t last_processor; /* processor this last ran on */
397 #endif /* NCPUS > 1 */
398
399 #if NET_ATM
400 nw_ep_owned_t nw_ep_waited;
401 #endif /* NET_ATM */
402 };
403
404 #define ith_msize data.msize
405 #define ith_kmsg data.kmsg
406 #define ith_wait_result wait_result
407
408 #define ith_msg saved.receive.msg
409 #define ith_option saved.receive.option
410 #define ith_rcv_size saved.receive.rcv_size
411 #define ith_timeout saved.receive.timeout
412 #define ith_notify saved.receive.notify
413 #define ith_object saved.receive.object
414 #define ith_mqueue saved.receive.mqueue
415
416 #define ith_port saved.exception.port
417 #define ith_exc saved.exception.exc
418 #define ith_exc_code saved.exception.code
419 #define ith_exc_subcode saved.exception.subcode
420
421 #define ith_other saved.other
422
423 #ifndef _KERN_KERN_TYPES_H_
424 typedef struct thread *thread_t;
425
426 #define THREAD_NULL ((thread_t) 0)
427
428 typedef mach_port_t *thread_array_t;
429 #endif /* _KERN_KERN_TYPES_H_ */
430
431
432 extern thread_t active_threads[NCPUS]; /* active threads */
433 extern vm_offset_t active_stacks[NCPUS]; /* active kernel stacks */
434
435 /*
436 * User routines
437 */
438
439 extern kern_return_t thread_create(
440 task_t parent_task,
441 thread_t *child_thread);
442 extern kern_return_t thread_terminate(
443 thread_t thread);
444 extern kern_return_t thread_suspend(
445 thread_t thread);
446 extern kern_return_t thread_resume(
447 thread_t thread);
448 extern kern_return_t thread_abort(
449 thread_t thread);
450 extern kern_return_t thread_get_state(
451 thread_t thread,
452 int flavor,
453 thread_state_t old_state,
454 natural_t *old_state_count);
455 extern kern_return_t thread_set_state(
456 thread_t thread,
457 int flavor,
458 thread_state_t new_state,
459 natural_t new_state_count);
460 extern kern_return_t thread_get_special_port(
461 thread_t thread,
462 int which,
463 struct ipc_port **portp);
464 extern kern_return_t thread_set_special_port(
465 thread_t thread,
466 int which,
467 struct ipc_port *port);
468 extern kern_return_t thread_info(
469 thread_t thread,
470 int flavor,
471 thread_info_t thread_info_out,
472 natural_t *thread_info_count);
473 extern kern_return_t thread_assign(
474 thread_t thread,
475 processor_set_t new_pset);
476 extern kern_return_t thread_assign_default(
477 thread_t thread);
478
479 /*
480 * Kernel-only routines
481 */
482
483 extern void thread_init(void);
484 extern void thread_reference(thread_t);
485 extern void thread_deallocate(thread_t);
486 extern void thread_hold(thread_t);
487 extern kern_return_t thread_dowait(
488 thread_t thread,
489 boolean_t must_halt);
490 extern void thread_release(thread_t);
491 extern kern_return_t thread_halt(
492 thread_t thread,
493 boolean_t must_halt);
494 extern void thread_halt_self(void);
495 extern void thread_force_terminate(thread_t);
496 extern void thread_set_own_priority(
497 int priority);
498 extern thread_t kernel_thread(
499 task_t task,
500 void (*start)(void),
501 void * arg);
502
503 extern void reaper_thread(void);
504
505 #if MACH_HOST
506 extern void thread_freeze(
507 thread_t thread);
508 extern void thread_doassign(
509 thread_t thread,
510 processor_set_t new_pset,
511 boolean_t release_freeze);
512 extern void thread_unfreeze(
513 thread_t thread);
514 #endif /* MACH_HOST */
515
516 /*
517 * Macro-defined routines
518 */
519
520 #define thread_pcb(th) ((th)->pcb)
521
522 #define thread_lock(th) simple_lock(&(th)->lock)
523 #define thread_unlock(th) simple_unlock(&(th)->lock)
524
525 #define thread_should_halt(thread) \
526 ((thread)->ast & (AST_HALT|AST_TERMINATE))
527
528 /*
529 * Machine specific implementations of the current thread macro
530 * designate this by defining CURRENT_THREAD.
531 */
532 #ifndef CURRENT_THREAD
533 #define current_thread() (active_threads[cpu_number()])
534 #endif /* CURRENT_THREAD */
535
536 #define current_stack() (active_stacks[cpu_number()])
537
538 #define current_task() (current_thread()->task)
539 #define current_space() (current_task()->itk_space)
540 #define current_map() (current_task()->map)
541
542 #endif /* _KERN_THREAD_H_ */
Cache object: 3022e88c0c994cdaba641c7a2222b96f
|