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