2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
57 * Thread management primitives implementation.
60 * Copyright (c) 1993 The University of Utah and
61 * the Computer Systems Laboratory (CSL). All rights reserved.
63 * Permission to use, copy, modify and distribute this software and its
64 * documentation is hereby granted, provided that both the copyright
65 * notice and this permission notice appear in all copies of the
66 * software, derivative works or modified versions, and any portions
67 * thereof, and that both notices appear in supporting documentation.
69 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
70 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
71 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
73 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
74 * improvements that they make and grant CSL redistribution rights.
78 #include <mach_host.h>
79 #include <mach_prof.h>
81 #include <mach/mach_types.h>
82 #include <mach/boolean.h>
83 #include <mach/policy.h>
84 #include <mach/thread_info.h>
85 #include <mach/thread_special_ports.h>
86 #include <mach/thread_status.h>
87 #include <mach/time_value.h>
88 #include <mach/vm_param.h>
90 #include <machine/thread.h>
92 #include <kern/kern_types.h>
93 #include <kern/kalloc.h>
94 #include <kern/cpu_data.h>
95 #include <kern/counters.h>
96 #include <kern/ipc_mig.h>
97 #include <kern/ipc_tt.h>
98 #include <kern/mach_param.h>
99 #include <kern/machine.h>
100 #include <kern/misc_protos.h>
101 #include <kern/processor.h>
102 #include <kern/queue.h>
103 #include <kern/sched.h>
104 #include <kern/sched_prim.h>
105 #include <kern/sync_lock.h>
106 #include <kern/syscall_subr.h>
107 #include <kern/task.h>
108 #include <kern/thread.h>
109 #include <kern/host.h>
110 #include <kern/zalloc.h>
111 #include <kern/profile.h>
112 #include <kern/assert.h>
114 #include <ipc/ipc_kmsg.h>
115 #include <ipc/ipc_port.h>
117 #include <vm/vm_kern.h>
118 #include <vm/vm_pageout.h>
120 #include <sys/kdebug.h>
123 * Exported interfaces
125 #include <mach/task_server.h>
126 #include <mach/thread_act_server.h>
127 #include <mach/mach_host_server.h>
128 #include <mach/host_priv_server.h>
130 static struct zone
*thread_zone
;
132 decl_simple_lock_data(static,thread_stack_lock
)
133 static queue_head_t thread_stack_queue
;
135 decl_simple_lock_data(static,thread_terminate_lock
)
136 static queue_head_t thread_terminate_queue
;
138 static struct thread thread_template
, init_thread
;
141 extern void proc_exit(void *);
142 #endif /* MACH_BSD */
145 thread_bootstrap(void)
148 * Fill in a template thread for fast initialization.
151 thread_template
.runq
= RUN_QUEUE_NULL
;
153 thread_template
.ref_count
= 2;
155 thread_template
.reason
= AST_NONE
;
156 thread_template
.at_safe_point
= FALSE
;
157 thread_template
.wait_event
= NO_EVENT64
;
158 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
159 thread_template
.wait_result
= THREAD_WAITING
;
160 thread_template
.options
= THREAD_ABORTSAFE
;
161 thread_template
.state
= TH_WAIT
| TH_UNINT
;
162 thread_template
.wake_active
= FALSE
;
163 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
164 thread_template
.parameter
= NULL
;
166 thread_template
.importance
= 0;
167 thread_template
.sched_mode
= 0;
168 thread_template
.safe_mode
= 0;
169 thread_template
.safe_release
= 0;
171 thread_template
.priority
= 0;
172 thread_template
.sched_pri
= 0;
173 thread_template
.max_priority
= 0;
174 thread_template
.task_priority
= 0;
175 thread_template
.promotions
= 0;
176 thread_template
.pending_promoter_index
= 0;
177 thread_template
.pending_promoter
[0] =
178 thread_template
.pending_promoter
[1] = NULL
;
180 thread_template
.realtime
.deadline
= UINT64_MAX
;
182 thread_template
.current_quantum
= 0;
184 thread_template
.computation_metered
= 0;
185 thread_template
.computation_epoch
= 0;
187 thread_template
.sched_stamp
= 0;
188 thread_template
.sched_usage
= 0;
189 thread_template
.pri_shift
= INT8_MAX
;
190 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
192 thread_template
.bound_processor
= PROCESSOR_NULL
;
193 thread_template
.last_processor
= PROCESSOR_NULL
;
194 thread_template
.last_switch
= 0;
196 timer_init(&thread_template
.user_timer
);
197 timer_init(&thread_template
.system_timer
);
198 thread_template
.user_timer_save
= 0;
199 thread_template
.system_timer_save
= 0;
201 thread_template
.wait_timer_is_set
= FALSE
;
202 thread_template
.wait_timer_active
= 0;
204 thread_template
.depress_timer_active
= 0;
206 thread_template
.processor_set
= PROCESSOR_SET_NULL
;
208 thread_template
.special_handler
.handler
= special_handler
;
209 thread_template
.special_handler
.next
= 0;
212 thread_template
.may_assign
= TRUE
;
213 thread_template
.assign_active
= FALSE
;
214 #endif /* MACH_HOST */
215 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
216 thread_template
.funnel_state
= 0;
217 thread_template
.recover
= (vm_offset_t
)NULL
;
219 init_thread
= thread_template
;
220 machine_set_current_thread(&init_thread
);
227 sizeof(struct thread
),
228 THREAD_MAX
* sizeof(struct thread
),
229 THREAD_CHUNK
* sizeof(struct thread
),
235 * Initialize any machine-dependent
236 * per-thread structures necessary.
238 machine_thread_init();
242 thread_terminate_continue(void)
244 panic("thread_terminate_continue");
249 * thread_terminate_self:
252 thread_terminate_self(void)
254 thread_t thread
= current_thread();
262 * Cancel priority depression, reset scheduling parameters,
263 * and wait for concurrent expirations on other processors.
265 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
266 thread
->sched_mode
&= ~TH_MODE_ISDEPRESSED
;
268 if (timer_call_cancel(&thread
->depress_timer
))
269 thread
->depress_timer_active
--;
272 thread_policy_reset(thread
);
274 while (thread
->depress_timer_active
> 0) {
275 thread_unlock(thread
);
284 thread_unlock(thread
);
287 thread_mtx_lock(thread
);
289 ulock_release_all(thread
);
291 ipc_thread_disable(thread
);
293 thread_mtx_unlock(thread
);
296 * If we are the last thread to terminate and the task is
297 * associated with a BSD process, perform BSD process exit.
300 if ( hw_atomic_sub(&task
->active_thread_count
, 1) == 0 &&
301 task
->bsd_info
!= NULL
)
302 proc_exit(task
->bsd_info
);
308 * Cancel wait timer, and wait for
309 * concurrent expirations.
311 if (thread
->wait_timer_is_set
) {
312 thread
->wait_timer_is_set
= FALSE
;
314 if (timer_call_cancel(&thread
->wait_timer
))
315 thread
->wait_timer_active
--;
318 while (thread
->wait_timer_active
> 0) {
319 thread_unlock(thread
);
329 * If there is a reserved stack, release it.
331 if (thread
->reserved_stack
!= 0) {
332 if (thread
->reserved_stack
!= thread
->kernel_stack
)
333 stack_free_stack(thread
->reserved_stack
);
334 thread
->reserved_stack
= 0;
338 * Mark thread as terminating, and block.
340 thread
->state
|= TH_TERMINATE
;
341 thread_mark_wait_locked(thread
, THREAD_UNINT
);
342 assert(thread
->promotions
== 0);
343 thread_unlock(thread
);
346 thread_block((thread_continue_t
)thread_terminate_continue
);
354 processor_set_t pset
;
357 if (thread
== THREAD_NULL
)
360 if (thread_deallocate_internal(thread
) > 0)
363 ipc_thread_terminate(thread
);
369 void *ut
= thread
->uthread
;
371 thread
->uthread
= NULL
;
372 uthread_free(task
, ut
, task
->bsd_info
);
374 #endif /* MACH_BSD */
376 task_deallocate(task
);
378 pset
= thread
->processor_set
;
379 pset_deallocate(pset
);
381 if (thread
->kernel_stack
!= 0)
384 machine_thread_destroy(thread
);
386 zfree(thread_zone
, thread
);
390 * thread_terminate_daemon:
392 * Perform final clean up for terminating threads.
395 thread_terminate_daemon(void)
399 processor_set_t pset
;
402 simple_lock(&thread_terminate_lock
);
404 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
405 simple_unlock(&thread_terminate_lock
);
411 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
412 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
414 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
415 task
->thread_count
--;
418 pset
= thread
->processor_set
;
421 pset_remove_thread(pset
, thread
);
424 thread_deallocate(thread
);
427 simple_lock(&thread_terminate_lock
);
430 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
431 simple_unlock(&thread_terminate_lock
);
434 thread_block((thread_continue_t
)thread_terminate_daemon
);
439 * thread_terminate_enqueue:
441 * Enqueue a terminating thread for final disposition.
443 * Called at splsched.
446 thread_terminate_enqueue(
449 simple_lock(&thread_terminate_lock
);
450 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
451 simple_unlock(&thread_terminate_lock
);
453 thread_wakeup((event_t
)&thread_terminate_queue
);
457 * thread_stack_daemon:
459 * Perform stack allocation as required due to
463 thread_stack_daemon(void)
468 simple_lock(&thread_stack_lock
);
470 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
471 simple_unlock(&thread_stack_lock
);
477 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
478 thread_unlock(thread
);
482 simple_lock(&thread_stack_lock
);
485 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
486 simple_unlock(&thread_stack_lock
);
489 thread_block((thread_continue_t
)thread_stack_daemon
);
494 * thread_stack_enqueue:
496 * Enqueue a thread for stack allocation.
498 * Called at splsched.
501 thread_stack_enqueue(
504 simple_lock(&thread_stack_lock
);
505 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
506 simple_unlock(&thread_stack_lock
);
508 thread_wakeup((event_t
)&thread_stack_queue
);
512 thread_daemon_init(void)
514 kern_return_t result
;
517 simple_lock_init(&thread_terminate_lock
, 0);
518 queue_init(&thread_terminate_queue
);
520 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
521 if (result
!= KERN_SUCCESS
)
522 panic("thread_daemon_init: thread_terminate_daemon");
524 thread_deallocate(thread
);
526 simple_lock_init(&thread_stack_lock
, 0);
527 queue_init(&thread_stack_queue
);
529 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
530 if (result
!= KERN_SUCCESS
)
531 panic("thread_daemon_init: thread_stack_daemon");
533 thread_deallocate(thread
);
537 * Create a new thread.
538 * Doesn't start the thread running.
541 thread_create_internal(
544 thread_continue_t continuation
,
545 thread_t
*out_thread
)
548 processor_set_t pset
;
549 static thread_t first_thread
;
552 * Allocate a thread and initialize static fields
554 if (first_thread
== NULL
)
555 new_thread
= first_thread
= current_thread();
557 new_thread
= (thread_t
)zalloc(thread_zone
);
558 if (new_thread
== NULL
)
559 return (KERN_RESOURCE_SHORTAGE
);
561 if (new_thread
!= first_thread
)
562 *new_thread
= thread_template
;
566 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
);
567 if (new_thread
->uthread
== NULL
) {
568 zfree(thread_zone
, new_thread
);
569 return (KERN_RESOURCE_SHORTAGE
);
572 #endif /* MACH_BSD */
574 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
577 void *ut
= new_thread
->uthread
;
579 new_thread
->uthread
= NULL
;
580 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
582 #endif /* MACH_BSD */
583 zfree(thread_zone
, new_thread
);
584 return (KERN_FAILURE
);
587 new_thread
->task
= parent_task
;
589 thread_lock_init(new_thread
);
590 wake_lock_init(new_thread
);
592 mutex_init(&new_thread
->mutex
, 0);
594 ipc_thread_init(new_thread
);
595 queue_init(&new_thread
->held_ulocks
);
596 thread_prof_init(new_thread
, parent_task
);
598 new_thread
->continuation
= continuation
;
600 pset
= parent_task
->processor_set
;
601 assert(pset
== &default_pset
);
604 task_lock(parent_task
);
605 assert(parent_task
->processor_set
== pset
);
607 if ( !parent_task
->active
||
608 (parent_task
->thread_count
>= THREAD_MAX
&&
609 parent_task
!= kernel_task
)) {
610 task_unlock(parent_task
);
615 void *ut
= new_thread
->uthread
;
617 new_thread
->uthread
= NULL
;
618 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
620 #endif /* MACH_BSD */
621 ipc_thread_disable(new_thread
);
622 ipc_thread_terminate(new_thread
);
623 machine_thread_destroy(new_thread
);
624 zfree(thread_zone
, new_thread
);
625 return (KERN_FAILURE
);
628 task_reference_internal(parent_task
);
630 /* Cache the task's map */
631 new_thread
->map
= parent_task
->map
;
633 /* Chain the thread onto the task's list */
634 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
635 parent_task
->thread_count
++;
637 /* So terminating threads don't need to take the task lock to decrement */
638 hw_atomic_add(&parent_task
->active_thread_count
, 1);
640 /* Associate the thread with the processor set */
641 pset_add_thread(pset
, new_thread
);
643 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
644 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
646 /* Set the thread's scheduling parameters */
647 if (parent_task
!= kernel_task
)
648 new_thread
->sched_mode
|= TH_MODE_TIMESHARE
;
649 new_thread
->max_priority
= parent_task
->max_priority
;
650 new_thread
->task_priority
= parent_task
->priority
;
651 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
652 if (new_thread
->priority
> new_thread
->max_priority
)
653 new_thread
->priority
= new_thread
->max_priority
;
654 new_thread
->importance
=
655 new_thread
->priority
- new_thread
->task_priority
;
656 new_thread
->sched_stamp
= sched_tick
;
657 new_thread
->pri_shift
= new_thread
->processor_set
->pri_shift
;
658 compute_priority(new_thread
, FALSE
);
660 new_thread
->active
= TRUE
;
662 *out_thread
= new_thread
;
665 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
667 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
669 KERNEL_DEBUG_CONSTANT(
670 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
671 (vm_address_t
)new_thread
, dbg_arg2
, 0, 0, 0);
673 kdbg_trace_string(parent_task
->bsd_info
,
674 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
676 KERNEL_DEBUG_CONSTANT(
677 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
678 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
681 return (KERN_SUCCESS
);
687 thread_t
*new_thread
)
689 kern_return_t result
;
692 if (task
== TASK_NULL
|| task
== kernel_task
)
693 return (KERN_INVALID_ARGUMENT
);
695 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
696 if (result
!= KERN_SUCCESS
)
699 thread
->user_stop_count
= 1;
701 if (task
->suspend_count
> 0)
704 pset_unlock(task
->processor_set
);
707 *new_thread
= thread
;
709 return (KERN_SUCCESS
);
713 thread_create_running(
714 register task_t task
,
716 thread_state_t new_state
,
717 mach_msg_type_number_t new_state_count
,
718 thread_t
*new_thread
)
720 register kern_return_t result
;
723 if (task
== TASK_NULL
|| task
== kernel_task
)
724 return (KERN_INVALID_ARGUMENT
);
726 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
727 if (result
!= KERN_SUCCESS
)
730 result
= machine_thread_set_state(
731 thread
, flavor
, new_state
, new_state_count
);
732 if (result
!= KERN_SUCCESS
) {
733 pset_unlock(task
->processor_set
);
736 thread_terminate(thread
);
737 thread_deallocate(thread
);
741 thread_mtx_lock(thread
);
742 clear_wait(thread
, THREAD_AWAKENED
);
743 thread
->started
= TRUE
;
744 thread_mtx_unlock(thread
);
745 pset_unlock(task
->processor_set
);
748 *new_thread
= thread
;
754 * kernel_thread_create:
756 * Create a thread in the kernel task
757 * to execute in kernel context.
760 kernel_thread_create(
761 thread_continue_t continuation
,
764 thread_t
*new_thread
)
766 kern_return_t result
;
768 task_t task
= kernel_task
;
770 result
= thread_create_internal(task
, priority
, continuation
, &thread
);
771 if (result
!= KERN_SUCCESS
)
774 pset_unlock(task
->processor_set
);
778 assert(thread
->kernel_stack
!= 0);
779 thread
->reserved_stack
= thread
->kernel_stack
;
781 thread
->parameter
= parameter
;
783 *new_thread
= thread
;
789 kernel_thread_start_priority(
790 thread_continue_t continuation
,
793 thread_t
*new_thread
)
795 kern_return_t result
;
798 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
799 if (result
!= KERN_SUCCESS
)
802 thread_mtx_lock(thread
);
803 clear_wait(thread
, THREAD_AWAKENED
);
804 thread
->started
= TRUE
;
805 thread_mtx_unlock(thread
);
807 *new_thread
= thread
;
814 thread_continue_t continuation
,
816 thread_t
*new_thread
)
818 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
826 kern_return_t result
;
829 if (task
!= kernel_task
)
830 panic("kernel_thread");
832 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
833 if (result
!= KERN_SUCCESS
)
834 return (THREAD_NULL
);
836 thread_deallocate(thread
);
842 thread_info_internal(
843 register thread_t thread
,
844 thread_flavor_t flavor
,
845 thread_info_t thread_info_out
, /* ptr to OUT array */
846 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
851 if (thread
== THREAD_NULL
)
852 return (KERN_INVALID_ARGUMENT
);
854 if (flavor
== THREAD_BASIC_INFO
) {
855 register thread_basic_info_t basic_info
;
857 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
858 return (KERN_INVALID_ARGUMENT
);
860 basic_info
= (thread_basic_info_t
) thread_info_out
;
867 thread_read_times(thread
, &basic_info
->user_time
,
868 &basic_info
->system_time
);
871 * Update lazy-evaluated scheduler info because someone wants it.
873 if (thread
->sched_stamp
!= sched_tick
)
874 update_priority(thread
);
876 basic_info
->sleep_time
= 0;
879 * To calculate cpu_usage, first correct for timer rate,
880 * then for 5/8 ageing. The correction factor [3/5] is
883 basic_info
->cpu_usage
= ((uint64_t)thread
->cpu_usage
884 * TH_USAGE_SCALE
) / sched_tick_interval
;
885 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
887 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
888 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
890 basic_info
->policy
= ((thread
->sched_mode
& TH_MODE_TIMESHARE
)?
891 POLICY_TIMESHARE
: POLICY_RR
);
894 if (thread
->state
& TH_IDLE
)
895 flags
|= TH_FLAGS_IDLE
;
897 if (!thread
->kernel_stack
)
898 flags
|= TH_FLAGS_SWAPPED
;
901 if (thread
->state
& TH_TERMINATE
)
902 state
= TH_STATE_HALTED
;
904 if (thread
->state
& TH_RUN
)
905 state
= TH_STATE_RUNNING
;
907 if (thread
->state
& TH_UNINT
)
908 state
= TH_STATE_UNINTERRUPTIBLE
;
910 if (thread
->state
& TH_SUSP
)
911 state
= TH_STATE_STOPPED
;
913 if (thread
->state
& TH_WAIT
)
914 state
= TH_STATE_WAITING
;
916 basic_info
->run_state
= state
;
917 basic_info
->flags
= flags
;
919 basic_info
->suspend_count
= thread
->user_stop_count
;
921 thread_unlock(thread
);
924 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
926 return (KERN_SUCCESS
);
929 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
930 policy_timeshare_info_t ts_info
;
932 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
933 return (KERN_INVALID_ARGUMENT
);
935 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
940 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
941 thread_unlock(thread
);
944 return (KERN_INVALID_POLICY
);
947 ts_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
948 if (ts_info
->depressed
) {
949 ts_info
->base_priority
= DEPRESSPRI
;
950 ts_info
->depress_priority
= thread
->priority
;
953 ts_info
->base_priority
= thread
->priority
;
954 ts_info
->depress_priority
= -1;
957 ts_info
->cur_priority
= thread
->sched_pri
;
958 ts_info
->max_priority
= thread
->max_priority
;
960 thread_unlock(thread
);
963 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
965 return (KERN_SUCCESS
);
968 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
969 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
970 return (KERN_INVALID_ARGUMENT
);
972 return (KERN_INVALID_POLICY
);
975 if (flavor
== THREAD_SCHED_RR_INFO
) {
976 policy_rr_info_t rr_info
;
978 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
979 return (KERN_INVALID_ARGUMENT
);
981 rr_info
= (policy_rr_info_t
) thread_info_out
;
986 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
987 thread_unlock(thread
);
990 return (KERN_INVALID_POLICY
);
993 rr_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
994 if (rr_info
->depressed
) {
995 rr_info
->base_priority
= DEPRESSPRI
;
996 rr_info
->depress_priority
= thread
->priority
;
999 rr_info
->base_priority
= thread
->priority
;
1000 rr_info
->depress_priority
= -1;
1003 rr_info
->max_priority
= thread
->max_priority
;
1004 rr_info
->quantum
= std_quantum_us
/ 1000;
1006 thread_unlock(thread
);
1009 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1011 return (KERN_SUCCESS
);
1014 return (KERN_INVALID_ARGUMENT
);
1020 time_value_t
*user_time
,
1021 time_value_t
*system_time
)
1023 absolutetime_to_microtime(
1024 timer_grab(&thread
->user_timer
),
1025 &user_time
->seconds
, &user_time
->microseconds
);
1027 absolutetime_to_microtime(
1028 timer_grab(&thread
->system_timer
),
1029 &system_time
->seconds
, &system_time
->microseconds
);
1034 __unused thread_t thread
,
1035 __unused processor_set_t new_pset
)
1037 return (KERN_FAILURE
);
1041 * thread_assign_default:
1043 * Special version of thread_assign for assigning threads to default
1047 thread_assign_default(
1050 return (thread_assign(thread
, &default_pset
));
1054 * thread_get_assignment
1056 * Return current assignment for this thread.
1059 thread_get_assignment(
1061 processor_set_t
*pset
)
1064 return (KERN_INVALID_ARGUMENT
);
1066 *pset
= thread
->processor_set
;
1067 pset_reference(*pset
);
1068 return (KERN_SUCCESS
);
1072 * thread_wire_internal:
1074 * Specify that the target thread must always be able
1075 * to run and to allocate memory.
1078 thread_wire_internal(
1079 host_priv_t host_priv
,
1082 boolean_t
*prev_state
)
1084 if (host_priv
== NULL
|| thread
!= current_thread())
1085 return (KERN_INVALID_ARGUMENT
);
1087 assert(host_priv
== &realhost
);
1090 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1093 if (!(thread
->options
& TH_OPT_VMPRIV
))
1094 vm_page_free_reserve(1); /* XXX */
1095 thread
->options
|= TH_OPT_VMPRIV
;
1098 if (thread
->options
& TH_OPT_VMPRIV
)
1099 vm_page_free_reserve(-1); /* XXX */
1100 thread
->options
&= ~TH_OPT_VMPRIV
;
1103 return (KERN_SUCCESS
);
1110 * User-api wrapper for thread_wire_internal()
1114 host_priv_t host_priv
,
1118 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1121 int split_funnel_off
= 0;
1122 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1123 lck_grp_attr_t
*funnel_lck_grp_attr
;
1124 lck_attr_t
*funnel_lck_attr
;
1133 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1134 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1136 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1138 funnel_lck_attr
= lck_attr_alloc_init();
1140 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1141 bzero((void *)fnl
, sizeof(funnel_t
));
1142 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1143 kfree(fnl
, sizeof(funnel_t
));
1144 return(THR_FUNNEL_NULL
);
1147 fnl
->fnl_type
= type
;
1156 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1157 if (fnl
->fnl_oldmutex
)
1158 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1159 kfree(fnl
, sizeof(funnel_t
));
1166 lck_mtx_lock(fnl
->fnl_mutex
);
1167 fnl
->fnl_mtxholder
= current_thread();
1174 lck_mtx_unlock(fnl
->fnl_mutex
);
1175 fnl
->fnl_mtxrelease
= current_thread();
1182 thread_t th
= current_thread();
1184 if (th
->funnel_state
& TH_FN_OWNED
) {
1185 return(th
->funnel_lock
);
1187 return(THR_FUNNEL_NULL
);
1195 thread_t cur_thread
;
1196 boolean_t funnel_state_prev
;
1199 cur_thread
= current_thread();
1200 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1202 if (funnel_state_prev
!= funneled
) {
1203 intr
= ml_set_interrupts_enabled(FALSE
);
1205 if (funneled
== TRUE
) {
1206 if (cur_thread
->funnel_lock
)
1207 panic("Funnel lock called when holding one %x", cur_thread
->funnel_lock
);
1208 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1211 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1213 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1214 cur_thread
->funnel_lock
= fnl
;
1216 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1217 panic("Funnel unlock when not holding funnel");
1218 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1219 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1222 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1225 (void)ml_set_interrupts_enabled(intr
);
1227 /* if we are trying to acquire funnel recursively
1228 * check for funnel to be held already
1230 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1231 panic("thread_funnel_set: already holding a different funnel");
1234 return(funnel_state_prev
);
1239 * Export routines to other components for things that are done as macros
1240 * within the osfmk component.
1243 #undef thread_reference
1244 void thread_reference(thread_t thread
);
1249 if (thread
!= THREAD_NULL
)
1250 thread_reference_internal(thread
);
1253 #undef thread_should_halt
1259 return (thread_should_halt_fast(th
));