2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * @OSF_FREE_COPYRIGHT@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr.
64 * This file contains the structure definitions for threads.
68 * Copyright (c) 1993 The University of Utah and
69 * the Computer Systems Laboratory (CSL). All rights reserved.
71 * Permission to use, copy, modify and distribute this software and its
72 * documentation is hereby granted, provided that both the copyright
73 * notice and this permission notice appear in all copies of the
74 * software, derivative works or modified versions, and any portions
75 * thereof, and that both notices appear in supporting documentation.
77 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
78 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
79 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
81 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
82 * improvements that they make and grant CSL redistribution rights.
86 #ifndef _KERN_THREAD_H_
87 #define _KERN_THREAD_H_
89 #include <mach/kern_return.h>
90 #include <mach/mach_types.h>
91 #include <mach/message.h>
92 #include <mach/boolean.h>
93 #include <mach/vm_param.h>
94 #include <mach/thread_info.h>
95 #include <mach/thread_status.h>
96 #include <mach/exception_types.h>
98 #include <kern/kern_types.h>
100 #include <sys/cdefs.h>
102 #ifdef MACH_KERNEL_PRIVATE
104 #include <cputypes.h>
106 #include <mach_assert.h>
107 #include <mach_host.h>
108 #include <mach_prof.h>
109 #include <mach_ldebug.h>
111 #include <ipc/ipc_types.h>
113 #include <mach/port.h>
114 #include <kern/cpu_number.h>
115 #include <kern/queue.h>
116 #include <kern/timer.h>
117 #include <kern/lock.h>
118 #include <kern/locks.h>
119 #include <kern/sched.h>
120 #include <kern/sched_prim.h>
121 #include <kern/thread_call.h>
122 #include <kern/timer_call.h>
123 #include <kern/task.h>
124 #include <kern/exception.h>
126 #include <ipc/ipc_kmsg.h>
128 #include <machine/cpu_data.h>
129 #include <machine/thread.h>
133 * NOTE: The runq field in the thread structure has an unusual
134 * locking protocol. If its value is RUN_QUEUE_NULL, then it is
135 * locked by the thread_lock, but if its value is something else
136 * (i.e. a run_queue) then it is locked by that run_queue's lock.
138 * When the thread is on a wait queue, these first three fields
139 * are treated as an unofficial union with a wait_queue_element.
140 * If you change these, you must change that definition as well
141 * (kern/wait_queue.h).
143 /* Items examined often, modified infrequently */
144 queue_chain_t links
; /* run/wait queue links */
145 run_queue_t runq
; /* run queue thread is on SEE BELOW */
146 wait_queue_t wait_queue
; /* wait queue we are currently on */
147 event64_t wait_event
; /* wait queue event */
148 integer_t options
; /* options set by thread itself */
149 #define TH_OPT_INTMASK 0x03 /* interrupt / abort level */
150 #define TH_OPT_VMPRIV 0x04 /* may allocate reserved memory */
151 #define TH_OPT_DELAYIDLE 0x08 /* performing delayed idle */
152 #define TH_OPT_CALLOUT 0x10 /* executing as callout */
154 /* Data updated during assert_wait/thread_wakeup */
155 decl_simple_lock_data(,sched_lock
) /* scheduling lock (thread_lock()) */
156 decl_simple_lock_data(,wake_lock
) /* covers wake_active (wake_lock())*/
157 boolean_t wake_active
; /* Someone is waiting for this */
158 int at_safe_point
; /* thread_abort_safely allowed */
159 ast_t reason
; /* why we blocked */
160 wait_result_t wait_result
; /* outcome of wait -
161 * may be examined by this thread
163 thread_continue_t continuation
; /* continue here next dispatch */
164 void *parameter
; /* continuation parameter */
166 /* Data updated/used in thread_invoke */
167 struct funnel_lock
*funnel_lock
; /* Non-reentrancy funnel */
169 #define TH_FN_OWNED 0x1 /* we own the funnel */
170 #define TH_FN_REFUNNEL 0x2 /* re-acquire funnel on dispatch */
172 vm_offset_t kernel_stack
; /* current kernel stack */
173 vm_offset_t reserved_stack
; /* reserved kernel stack */
178 * Thread states [bits or'ed]
180 #define TH_WAIT 0x01 /* queued for waiting */
181 #define TH_SUSP 0x02 /* stopped or requested to stop */
182 #define TH_RUN 0x04 /* running or on runq */
183 #define TH_UNINT 0x08 /* waiting uninteruptibly */
184 #define TH_TERMINATE 0x10 /* halted at termination */
186 #define TH_ABORT 0x20 /* abort interruptible waits */
187 #define TH_ABORT_SAFELY 0x40 /* ... but only those at safe point */
189 #define TH_IDLE 0x80 /* processor idle thread */
191 #define TH_SCHED_STATE (TH_WAIT|TH_SUSP|TH_RUN|TH_UNINT)
193 /* Scheduling information */
194 integer_t sched_mode
; /* scheduling mode bits */
195 #define TH_MODE_REALTIME 0x0001 /* time constraints supplied */
196 #define TH_MODE_TIMESHARE 0x0002 /* use timesharing algorithm */
197 #define TH_MODE_PREEMPT 0x0004 /* can preempt kernel contexts */
198 #define TH_MODE_FAILSAFE 0x0008 /* fail-safe has tripped */
199 #define TH_MODE_PROMOTED 0x0010 /* sched pri has been promoted */
200 #define TH_MODE_DEPRESS 0x0020 /* normal depress yield */
201 #define TH_MODE_POLLDEPRESS 0x0040 /* polled depress yield */
202 #define TH_MODE_ISDEPRESSED (TH_MODE_DEPRESS | TH_MODE_POLLDEPRESS)
204 integer_t sched_pri
; /* scheduled (current) priority */
205 integer_t priority
; /* base priority */
206 integer_t max_priority
; /* max base priority */
207 integer_t task_priority
; /* copy of task base priority */
209 integer_t promotions
; /* level of promotion */
210 integer_t pending_promoter_index
;
211 void *pending_promoter
[2];
213 integer_t importance
; /* task-relative importance */
215 /* real-time parameters */
216 struct { /* see mach/thread_policy.h */
218 uint32_t computation
;
220 boolean_t preemptible
;
225 uint32_t current_quantum
; /* duration of current quantum */
227 /* Data used during setrun/dispatch */
228 timer_data_t system_timer
; /* system mode timer */
229 processor_set_t processor_set
; /* assigned processor set */
230 processor_t bound_processor
; /* bound to a processor? */
231 processor_t last_processor
; /* processor last dispatched on */
232 uint64_t last_switch
; /* time of last context switch */
234 /* Fail-safe computation since last unblock or qualifying yield */
235 uint64_t computation_metered
;
236 uint64_t computation_epoch
;
237 integer_t safe_mode
; /* saved mode during fail-safe */
238 natural_t safe_release
; /* when to release fail-safe */
240 /* Statistics and timesharing calculations */
241 natural_t sched_stamp
; /* last scheduler tick */
242 natural_t sched_usage
; /* timesharing cpu usage [sched] */
243 natural_t pri_shift
; /* usage -> priority from pset */
244 natural_t cpu_usage
; /* instrumented cpu usage [%cpu] */
245 natural_t cpu_delta
; /* accumulated cpu_usage delta */
247 /* Timing data structures */
248 timer_data_t user_timer
; /* user mode timer */
249 uint64_t system_timer_save
; /* saved system timer value */
250 uint64_t user_timer_save
; /* saved user timer value */
252 /* Timed wait expiration */
253 timer_call_data_t wait_timer
;
254 integer_t wait_timer_active
;
255 boolean_t wait_timer_is_set
;
257 /* Priority depression expiration */
258 timer_call_data_t depress_timer
;
259 integer_t depress_timer_active
;
261 /* Various bits of stashed state */
264 mach_msg_return_t state
; /* receive state */
265 ipc_object_t object
; /* object received on */
266 mach_vm_address_t msg_addr
; /* receive buffer pointer */
267 mach_msg_size_t msize
; /* max size for recvd msg */
268 mach_msg_option_t option
; /* options for receive */
269 mach_msg_size_t slist_size
; /* scatter list size */
270 struct ipc_kmsg
*kmsg
; /* received message */
271 mach_port_seqno_t seqno
; /* seqno of recvd message */
272 mach_msg_continue_t continuation
;
275 struct semaphore
*waitsemaphore
; /* semaphore ref */
276 struct semaphore
*signalsemaphore
; /* semaphore ref */
277 int options
; /* semaphore options */
278 kern_return_t result
; /* primary result */
279 mach_msg_continue_t continuation
;
282 int option
; /* switch option */
284 int misc
; /* catch-all for other state */
287 /* IPC data structures */
288 struct ipc_kmsg_queue ith_messages
;
289 mach_port_t ith_rpc_reply
; /* reply port for kernel RPCs */
291 /* Ast/Halt data structures */
292 vm_offset_t recover
; /* page fault recover(copyin/out) */
293 int ref_count
; /* number of references to me */
295 /* Processor set info */
296 queue_chain_t pset_threads
; /* list of all threads in pset */
298 boolean_t may_assign
; /* may assignment change? */
299 boolean_t assign_active
; /* waiting for may_assign */
300 #endif /* MACH_HOST */
303 queue_chain_t task_threads
;
305 /*** Machine-dependent state ***/
306 struct machine_thread machine
;
308 /* Task membership */
312 decl_mutex_data(,mutex
)
314 /* Kernel holds on this thread */
317 /* User level suspensions */
320 /* Pending thread ast(s) */
323 /* Miscellaneous bits guarded by mutex */
325 /* Indicates that the thread has not been terminated */
328 /* Indicates that the thread has been started after creation */
333 struct ReturnHandler
{
334 struct ReturnHandler
*next
;
336 struct ReturnHandler
*rh
,
337 struct thread
*thread
);
338 } *handlers
, special_handler
;
340 /* Ports associated with this thread */
341 struct ipc_port
*ith_self
; /* not a right, doesn't hold ref */
342 struct ipc_port
*ith_sself
; /* a send right */
343 struct exception_action exc_actions
[EXC_TYPES_COUNT
];
345 /* Owned ulocks (a lock set element) */
346 queue_head_t held_ulocks
;
351 boolean_t profiled_own
;
352 struct prof_data
*profil_buffer
;
353 #endif /* MACH_PROF */
360 #define ith_state saved.receive.state
361 #define ith_object saved.receive.object
362 #define ith_msg_addr saved.receive.msg_addr
363 #define ith_msize saved.receive.msize
364 #define ith_option saved.receive.option
365 #define ith_scatter_list_size saved.receive.slist_size
366 #define ith_continuation saved.receive.continuation
367 #define ith_kmsg saved.receive.kmsg
368 #define ith_seqno saved.receive.seqno
370 #define sth_waitsemaphore saved.sema.waitsemaphore
371 #define sth_signalsemaphore saved.sema.signalsemaphore
372 #define sth_options saved.sema.options
373 #define sth_result saved.sema.result
374 #define sth_continuation saved.sema.continuation
376 extern void thread_bootstrap(void);
378 extern void thread_init(void);
380 extern void thread_daemon_init(void);
382 #define thread_reference_internal(thread) \
383 hw_atomic_add(&(thread)->ref_count, 1)
385 #define thread_deallocate_internal(thread) \
386 hw_atomic_sub(&(thread)->ref_count, 1)
388 #define thread_reference(thread) \
390 if ((thread) != THREAD_NULL) \
391 thread_reference_internal(thread); \
394 extern void thread_deallocate(
397 extern void thread_terminate_self(void);
399 extern kern_return_t
thread_terminate_internal(
402 extern void thread_terminate_enqueue(
405 extern void thread_stack_enqueue(
408 extern void thread_hold(
411 extern void thread_release(
414 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
415 #define thread_lock(th) simple_lock(&(th)->sched_lock)
416 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
417 #define thread_lock_try(th) simple_lock_try(&(th)->sched_lock)
419 #define thread_should_halt_fast(thread) (!(thread)->active)
421 #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
422 #define wake_lock(th) simple_lock(&(th)->wake_lock)
423 #define wake_unlock(th) simple_unlock(&(th)->wake_lock)
424 #define wake_lock_try(th) simple_lock_try(&(th)->wake_lock)
426 extern void stack_alloc(
429 extern void stack_free(
432 extern void stack_free_stack(
435 extern boolean_t
stack_alloc_try(
438 extern void stack_collect(void);
440 extern void stack_init(void);
442 extern kern_return_t
thread_state_initialize(
445 extern kern_return_t
thread_setstatus(
448 thread_state_t tstate
,
449 mach_msg_type_number_t count
);
451 extern kern_return_t
thread_getstatus(
454 thread_state_t tstate
,
455 mach_msg_type_number_t
*count
);
457 extern kern_return_t
thread_info_internal(
459 thread_flavor_t flavor
,
460 thread_info_t thread_info_out
,
461 mach_msg_type_number_t
*thread_info_count
);
463 extern void thread_task_priority(
466 integer_t max_priority
);
468 extern void thread_policy_reset(
471 extern kern_return_t
kernel_thread_create(
472 thread_continue_t continuation
,
475 thread_t
*new_thread
);
477 extern kern_return_t
kernel_thread_start_priority(
478 thread_continue_t continuation
,
481 thread_t
*new_thread
);
483 extern void machine_stack_attach(
487 extern vm_offset_t
machine_stack_detach(
490 extern void machine_stack_handoff(
494 extern thread_t
machine_switch_context(
496 thread_continue_t continuation
,
497 thread_t new_thread
);
499 extern void machine_load_context(
502 extern kern_return_t
machine_thread_state_initialize(
505 extern kern_return_t
machine_thread_set_state(
507 thread_flavor_t flavor
,
508 thread_state_t state
,
509 mach_msg_type_number_t count
);
511 extern kern_return_t
machine_thread_get_state(
513 thread_flavor_t flavor
,
514 thread_state_t state
,
515 mach_msg_type_number_t
*count
);
517 extern kern_return_t
machine_thread_dup(
521 extern void machine_thread_init(void);
523 extern kern_return_t
machine_thread_create(
527 extern void machine_thread_destroy(
530 extern void machine_set_current_thread(
533 extern void machine_thread_terminate_self(void);
535 extern kern_return_t
machine_thread_get_kern_state(
537 thread_flavor_t flavor
,
538 thread_state_t tstate
,
539 mach_msg_type_number_t
*count
);
543 * XXX Funnel locks XXX
547 int fnl_type
; /* funnel type */
548 lck_mtx_t
*fnl_mutex
; /* underlying mutex for the funnel */
549 void * fnl_mtxholder
; /* thread (last)holdng mutex */
550 void * fnl_mtxrelease
; /* thread (last)releasing mutex */
551 lck_mtx_t
*fnl_oldmutex
; /* Mutex before collapsing split funnel */
554 typedef struct ReturnHandler ReturnHandler
;
556 #define thread_mtx_lock(thread) mutex_lock(&(thread)->mutex)
557 #define thread_mtx_try(thread) mutex_try(&(thread)->mutex)
558 #define thread_mtx_unlock(thread) mutex_unlock(&(thread)->mutex)
560 extern void act_execute_returnhandlers(void);
562 extern void install_special_handler(
565 extern void special_handler(
569 #else /* MACH_KERNEL_PRIVATE */
573 extern thread_t
current_thread(void);
575 extern void thread_reference(
578 extern void thread_deallocate(
583 #endif /* MACH_KERNEL_PRIVATE */
585 #ifdef KERNEL_PRIVATE
587 typedef struct funnel_lock funnel_t
;
589 #ifdef MACH_KERNEL_PRIVATE
591 extern void funnel_lock(
594 extern void funnel_unlock(
597 vm_offset_t
min_valid_stack_address(void);
598 vm_offset_t
max_valid_stack_address(void);
600 #endif /* MACH_KERNEL_PRIVATE */
604 extern funnel_t
*thread_funnel_get(void);
606 extern boolean_t
thread_funnel_set(
610 extern thread_t
kernel_thread(
612 void (*start
)(void));
616 #endif /* KERNEL_PRIVATE */
620 #ifdef XNU_KERNEL_PRIVATE
623 * XXX Funnel locks XXX
626 #define THR_FUNNEL_NULL (funnel_t *)0
628 extern funnel_t
*funnel_alloc(
631 extern void funnel_free(
634 extern void thread_read_times(
636 time_value_t
*user_time
,
637 time_value_t
*system_time
);
639 extern void thread_setuserstack(
641 mach_vm_offset_t user_stack
);
643 extern uint64_t thread_adjuserstack(
647 extern void thread_setentrypoint(
649 mach_vm_offset_t entry
);
651 extern kern_return_t
thread_wire_internal(
652 host_priv_t host_priv
,
655 boolean_t
*prev_state
);
657 /* JMM - These are only temporary */
658 extern boolean_t
is_thread_running(thread_t
); /* True is TH_RUN */
659 extern boolean_t
is_thread_idle(thread_t
); /* True is TH_IDLE */
661 extern kern_return_t
thread_dup(thread_t
);
663 extern task_t
get_threadtask(thread_t
);
665 extern void *get_bsdthread_info(thread_t
);
666 extern void set_bsdthread_info(thread_t
, void *);
667 extern void *uthread_alloc(task_t
, thread_t
);
668 extern void uthread_free(task_t
, void *, void *);
670 extern boolean_t
thread_should_halt(
673 #endif /* XNU_KERNEL_PRIVATE */
675 extern kern_return_t
kernel_thread_start(
676 thread_continue_t continuation
,
678 thread_t
*new_thread
);
682 #endif /* _KERN_THREAD_H_ */