|
|
1.1 root 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 [email protected]
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: * File: thread.h
28: * Author: Avadis Tevanian, Jr.
29: *
30: * This file contains the structure definitions for threads.
31: *
32: */
33:
34: #ifndef _KERN_THREAD_H_
35: #define _KERN_THREAD_H_
36:
37: #include <mach/boolean.h>
38: #include <mach/thread_info.h>
39: #include <mach/thread_status.h>
40: #include <mach/machine/vm_types.h>
41: #include <mach/message.h>
42: #include <mach/port.h>
43: #include <mach/vm_prot.h>
44: #include <kern/ast.h>
45: #include <kern/cpu_number.h>
1.1.1.3 root 46: #include <kern/mach_clock.h>
1.1 root 47: #include <kern/queue.h>
48: #include <kern/pc_sample.h>
49: #include <kern/processor.h>
50: #include <kern/sched_prim.h> /* event_t, continuation_t */
51: #include <kern/timer.h>
52: #include <kern/lock.h>
53: #include <kern/sched.h>
54: #include <kern/task.h> /* for current_space(), current_map() */
55: #include <machine/thread.h>
56: #include <ipc/ipc_kmsg_queue.h>
57:
58: struct thread {
59: /* Run queues */
60: queue_chain_t links; /* current run queue links */
61: run_queue_t runq; /* run queue p is on SEE BELOW */
62: /*
63: * NOTE: The runq field in the thread structure has an unusual
64: * locking protocol. If its value is RUN_QUEUE_NULL, then it is
65: * locked by the thread_lock, but if its value is something else
66: * (i.e. a run_queue) then it is locked by that run_queue's lock.
67: */
68:
69: /* Task information */
70: task_t task; /* Task to which I belong */
71: queue_chain_t thread_list; /* list of threads in task */
72:
1.1.1.4 root 73: /* Flags */
74: /* The flags are grouped here, but documented at the original
75: position. */
76: union {
77: struct {
78: unsigned state:16;
79: unsigned wake_active:1;
80: unsigned active:1;
81: };
82: event_t event_key;
83: /* These keys can be used with thread_wakeup and friends. */
84: #define TH_EV_WAKE_ACTIVE(t) ((event_t) (&(t)->event_key + 0))
85: #define TH_EV_STATE(t) ((event_t) (&(t)->event_key + 1))
86: };
87:
1.1 root 88: /* Thread bookkeeping */
89: queue_chain_t pset_threads; /* list of all threads in proc set*/
90:
91: /* Self-preservation */
92: decl_simple_lock_data(,lock)
93: int ref_count; /* number of references to me */
94:
95: /* Hardware state */
96: pcb_t pcb; /* hardware pcb & machine state */
97: vm_offset_t kernel_stack; /* accurate only if the thread is
98: not swapped and not executing */
99: vm_offset_t stack_privilege;/* reserved kernel stack */
100:
101: /* Swapping information */
1.1.1.5 root 102: continuation_t swap_func; /* start here after swapin */
1.1 root 103:
104: /* Blocking information */
105: event_t wait_event; /* event we are waiting on */
106: int suspend_count; /* internal use only */
107: kern_return_t wait_result; /* outcome of wait -
108: may be examined by this thread
109: WITHOUT locking */
1.1.1.4 root 110: /* Defined above */
111: /* boolean_t wake_active; someone is waiting for this
1.1 root 112: thread to become suspended */
1.1.1.4 root 113: /* int state; Thread state: */
1.1 root 114: /*
115: * Thread states [bits or'ed]
116: */
117: #define TH_WAIT 0x01 /* thread is queued for waiting */
118: #define TH_SUSP 0x02 /* thread has been asked to stop */
119: #define TH_RUN 0x04 /* thread is running or on runq */
120: #define TH_UNINT 0x08 /* thread is waiting uninteruptibly */
121: #define TH_HALTED 0x10 /* thread is halted at clean point ? */
122:
123: #define TH_IDLE 0x80 /* thread is an idle thread */
124:
125: #define TH_SCHED_STATE (TH_WAIT|TH_SUSP|TH_RUN|TH_UNINT)
126:
127: #define TH_SWAPPED 0x0100 /* thread has no kernel stack */
128: #define TH_SW_COMING_IN 0x0200 /* thread is waiting for kernel stack */
129:
130: #define TH_SWAP_STATE (TH_SWAPPED | TH_SW_COMING_IN)
131:
132: /* Scheduling information */
133: int priority; /* thread's priority */
134: int max_priority; /* maximum priority */
135: int sched_pri; /* scheduled (computed) priority */
136: #if MACH_FIXPRI
137: int sched_data; /* for use by policy */
138: int policy; /* scheduling policy */
139: #endif /* MACH_FIXPRI */
140: int depress_priority; /* depressed from this priority */
141: unsigned int cpu_usage; /* exp. decaying cpu usage [%cpu] */
142: unsigned int sched_usage; /* load-weighted cpu usage [sched] */
143: unsigned int sched_stamp; /* last time priority was updated */
144:
145: /* VM global variables */
146:
147: vm_offset_t recover; /* page fault recovery (copyin/out) */
1.1.1.6 ! root 148: unsigned int vm_privilege; /* Can use reserved memory?
! 149: Implemented as a counter */
1.1 root 150:
151: /* User-visible scheduling state */
152: int user_stop_count; /* outstanding stops */
153:
154: /* IPC data structures */
155: struct thread *ith_next, *ith_prev;
156: mach_msg_return_t ith_state;
157: union {
158: mach_msg_size_t msize; /* max size for recvd msg */
159: struct ipc_kmsg *kmsg; /* received message */
160: } data;
161: mach_port_seqno_t ith_seqno; /* seqno of recvd message */
162:
163: /* This queue is used only when destroying messages:
164: it prevents nasty recursion problems when destroying one message
165: causes other messages to be destroyed.
166: This queue should always be empty under normal circumstances.
167: See ipc_kmsg_destroy() for more details. */
168: struct ipc_kmsg_queue ith_messages;
169:
170: decl_simple_lock_data(, ith_lock_data)
171: struct ipc_port *ith_self; /* not a right, doesn't hold ref */
172: struct ipc_port *ith_sself; /* a send right */
173: struct ipc_port *ith_exception; /* a send right */
174:
175: mach_port_t ith_mig_reply; /* reply port for mig */
176: struct ipc_port *ith_rpc_reply; /* reply port for kernel RPCs */
177:
178: /* State saved when thread's stack is discarded */
179: union {
180: struct {
181: mach_msg_header_t *msg;
182: mach_msg_option_t option;
183: mach_msg_size_t rcv_size;
184: mach_msg_timeout_t timeout;
185: mach_port_t notify;
186: struct ipc_object *object;
187: struct ipc_mqueue *mqueue;
188: } receive;
189: struct {
190: struct ipc_port *port;
191: int exc;
192: int code;
193: int subcode;
194: } exception;
195: void *other; /* catch-all for other state */
196: } saved;
197:
198: /* Timing data structures */
199: timer_data_t user_timer; /* user mode timer */
200: timer_data_t system_timer; /* system mode timer */
201: timer_save_data_t user_timer_save; /* saved user timer value */
202: timer_save_data_t system_timer_save; /* saved sys timer val. */
203: unsigned int cpu_delta; /* cpu usage since last update */
204: unsigned int sched_delta; /* weighted cpu usage since update */
205:
1.1.1.2 root 206: /* Creation time stamp */
207: time_value_t creation_time;
208:
1.1 root 209: /* Time-outs */
210: timer_elt_data_t timer; /* timer for thread */
211: timer_elt_data_t depress_timer; /* timer for priority depression */
212:
213: /* Ast/Halt data structures */
1.1.1.4 root 214: /* Defined above */
215: /* boolean_t active; how alive is the thread */
1.1 root 216: int ast; /* ast's needed. See ast.h */
217:
218: /* Processor data structures */
219: processor_set_t processor_set; /* assigned processor set */
220: processor_t bound_processor; /* bound to processor ?*/
221:
222: sample_control_t pc_sample;
223:
224: #if MACH_HOST
225: boolean_t may_assign; /* may assignment change? */
226: boolean_t assign_active; /* someone waiting for may_assign */
227: #endif /* MACH_HOST */
228:
229: #if NCPUS > 1
230: processor_t last_processor; /* processor this last ran on */
231: #endif /* NCPUS > 1 */
232: };
233:
234: /* typedef of thread_t is in kern/kern_types.h */
235: typedef struct thread_shuttle *thread_shuttle_t;
236: #define THREAD_NULL ((thread_t) 0)
237: #define THREAD_SHUTTLE_NULL ((thread_shuttle_t)0)
238:
239: #define ith_msize data.msize
240: #define ith_kmsg data.kmsg
241: #define ith_wait_result wait_result
242:
243: #define ith_msg saved.receive.msg
244: #define ith_option saved.receive.option
245: #define ith_rcv_size saved.receive.rcv_size
246: #define ith_timeout saved.receive.timeout
247: #define ith_notify saved.receive.notify
248: #define ith_object saved.receive.object
249: #define ith_mqueue saved.receive.mqueue
250:
251: #define ith_port saved.exception.port
252: #define ith_exc saved.exception.exc
253: #define ith_exc_code saved.exception.code
254: #define ith_exc_subcode saved.exception.subcode
255:
256: #define ith_other saved.other
257:
258: #ifndef _KERN_KERN_TYPES_H_
259: typedef struct thread *thread_t;
260:
261: #define THREAD_NULL ((thread_t) 0)
262:
263: typedef mach_port_t *thread_array_t;
264: #endif /* _KERN_KERN_TYPES_H_ */
265:
266:
267: extern thread_t active_threads[NCPUS]; /* active threads */
268: extern vm_offset_t active_stacks[NCPUS]; /* active kernel stacks */
269:
270: #ifdef KERNEL
271: /*
272: * User routines
273: */
274:
275: extern kern_return_t thread_create(
276: task_t parent_task,
277: thread_t *child_thread);
278: extern kern_return_t thread_terminate(
279: thread_t thread);
1.1.1.4 root 280: extern kern_return_t thread_terminate_release(
281: thread_t thread,
282: task_t task,
283: mach_port_t thread_name,
284: mach_port_t reply_port,
285: vm_offset_t address,
286: vm_size_t size);
1.1 root 287: extern kern_return_t thread_suspend(
288: thread_t thread);
289: extern kern_return_t thread_resume(
290: thread_t thread);
291: extern kern_return_t thread_abort(
292: thread_t thread);
1.1.1.3 root 293: extern void thread_start(
294: thread_t thread,
295: continuation_t start);
296: extern thread_t kernel_thread(
297: task_t task,
298: continuation_t start,
299: void *arg);
300: extern kern_return_t thread_priority(
301: thread_t thread,
302: int priority,
303: boolean_t set_max);
304: extern void thread_set_own_priority(
305: int priority);
306: extern kern_return_t thread_max_priority(
307: thread_t thread,
308: processor_set_t pset,
309: int max_priority);
310: extern kern_return_t thread_policy(
311: thread_t thread,
312: int policy,
313: int data);
314: extern void consider_thread_collect(
315: void);
316: extern void stack_privilege(
317: thread_t thread);
1.1 root 318: extern kern_return_t thread_get_state(
319: thread_t thread,
320: int flavor,
321: thread_state_t old_state,
322: natural_t *old_state_count);
323: extern kern_return_t thread_set_state(
324: thread_t thread,
325: int flavor,
326: thread_state_t new_state,
327: natural_t new_state_count);
328: extern kern_return_t thread_get_special_port(
329: thread_t thread,
330: int which,
331: struct ipc_port **portp);
332: extern kern_return_t thread_set_special_port(
333: thread_t thread,
334: int which,
335: struct ipc_port *port);
336: extern kern_return_t thread_info(
337: thread_t thread,
338: int flavor,
339: thread_info_t thread_info_out,
340: natural_t *thread_info_count);
341: extern kern_return_t thread_assign(
342: thread_t thread,
343: processor_set_t new_pset);
344: extern kern_return_t thread_assign_default(
345: thread_t thread);
1.1.1.3 root 346: extern void stack_collect(void);
1.1 root 347: #endif
348:
349: /*
350: * Kernel-only routines
351: */
352:
353: extern void thread_init(void);
354: extern void thread_reference(thread_t);
355: extern void thread_deallocate(thread_t);
356: extern void thread_hold(thread_t);
357: extern kern_return_t thread_dowait(
358: thread_t thread,
359: boolean_t must_halt);
360: extern void thread_release(thread_t);
361: extern kern_return_t thread_halt(
362: thread_t thread,
363: boolean_t must_halt);
1.1.1.5 root 364: extern void thread_halt_self(continuation_t);
1.1 root 365: extern void thread_force_terminate(thread_t);
366: extern thread_t kernel_thread(
367: task_t task,
368: void (*start)(void),
369: void * arg);
370:
1.1.1.4 root 371: extern void reaper_thread(void) __attribute__((noreturn));
1.1 root 372:
373: #if MACH_HOST
374: extern void thread_freeze(
375: thread_t thread);
376: extern void thread_doassign(
377: thread_t thread,
378: processor_set_t new_pset,
379: boolean_t release_freeze);
380: extern void thread_unfreeze(
381: thread_t thread);
382: #endif /* MACH_HOST */
383:
384: /*
385: * Macro-defined routines
386: */
387:
388: #define thread_pcb(th) ((th)->pcb)
389:
390: #define thread_lock(th) simple_lock(&(th)->lock)
391: #define thread_unlock(th) simple_unlock(&(th)->lock)
392:
393: #define thread_should_halt(thread) \
394: ((thread)->ast & (AST_HALT|AST_TERMINATE))
395:
396: /*
397: * Machine specific implementations of the current thread macro
398: * designate this by defining CURRENT_THREAD.
399: */
400: #ifndef CURRENT_THREAD
401: #define current_thread() (active_threads[cpu_number()])
402: #endif /* CURRENT_THREAD */
403:
404: #define current_stack() (active_stacks[cpu_number()])
405:
406: #define current_task() (current_thread()->task)
407: #define current_space() (current_task()->itk_space)
408: #define current_map() (current_task()->map)
409:
1.1.1.4 root 410: #if MACH_DEBUG
411: void stack_init(vm_offset_t stack);
412: void stack_finalize(vm_offset_t stack);
413: #endif /* MACH_DEBUG */
414:
1.1 root 415: #endif /* _KERN_THREAD_H_ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.