2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
63 * Thread management primitives implementation.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
84 #include <mach_host.h>
85 #include <mach_prof.h>
87 #include <mach/mach_types.h>
88 #include <mach/boolean.h>
89 #include <mach/policy.h>
90 #include <mach/thread_info.h>
91 #include <mach/thread_special_ports.h>
92 #include <mach/thread_status.h>
93 #include <mach/time_value.h>
94 #include <mach/vm_param.h>
96 #include <machine/thread.h>
98 #include <kern/kern_types.h>
99 #include <kern/kalloc.h>
100 #include <kern/cpu_data.h>
101 #include <kern/counters.h>
102 #include <kern/ipc_mig.h>
103 #include <kern/ipc_tt.h>
104 #include <kern/mach_param.h>
105 #include <kern/machine.h>
106 #include <kern/misc_protos.h>
107 #include <kern/processor.h>
108 #include <kern/queue.h>
109 #include <kern/sched.h>
110 #include <kern/sched_prim.h>
111 #include <kern/sync_lock.h>
112 #include <kern/syscall_subr.h>
113 #include <kern/task.h>
114 #include <kern/thread.h>
115 #include <kern/host.h>
116 #include <kern/zalloc.h>
117 #include <kern/profile.h>
118 #include <kern/assert.h>
120 #include <ipc/ipc_kmsg.h>
121 #include <ipc/ipc_port.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_pageout.h>
126 #include <sys/kdebug.h>
129 * Exported interfaces
131 #include <mach/task_server.h>
132 #include <mach/thread_act_server.h>
133 #include <mach/mach_host_server.h>
134 #include <mach/host_priv_server.h>
136 static struct zone
*thread_zone
;
138 decl_simple_lock_data(static,thread_stack_lock
)
139 static queue_head_t thread_stack_queue
;
141 decl_simple_lock_data(static,thread_terminate_lock
)
142 static queue_head_t thread_terminate_queue
;
144 static struct thread thread_template
, init_thread
;
147 extern void proc_exit(void *);
148 #endif /* MACH_BSD */
151 thread_bootstrap(void)
154 * Fill in a template thread for fast initialization.
157 thread_template
.runq
= RUN_QUEUE_NULL
;
159 thread_template
.ref_count
= 2;
161 thread_template
.reason
= AST_NONE
;
162 thread_template
.at_safe_point
= FALSE
;
163 thread_template
.wait_event
= NO_EVENT64
;
164 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
165 thread_template
.wait_result
= THREAD_WAITING
;
166 thread_template
.options
= THREAD_ABORTSAFE
;
167 thread_template
.state
= TH_WAIT
| TH_UNINT
;
168 thread_template
.wake_active
= FALSE
;
169 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
170 thread_template
.parameter
= NULL
;
172 thread_template
.importance
= 0;
173 thread_template
.sched_mode
= 0;
174 thread_template
.safe_mode
= 0;
175 thread_template
.safe_release
= 0;
177 thread_template
.priority
= 0;
178 thread_template
.sched_pri
= 0;
179 thread_template
.max_priority
= 0;
180 thread_template
.task_priority
= 0;
181 thread_template
.promotions
= 0;
182 thread_template
.pending_promoter_index
= 0;
183 thread_template
.pending_promoter
[0] =
184 thread_template
.pending_promoter
[1] = NULL
;
186 thread_template
.realtime
.deadline
= UINT64_MAX
;
188 thread_template
.current_quantum
= 0;
190 thread_template
.computation_metered
= 0;
191 thread_template
.computation_epoch
= 0;
193 thread_template
.sched_stamp
= 0;
194 thread_template
.sched_usage
= 0;
195 thread_template
.pri_shift
= INT8_MAX
;
196 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
198 thread_template
.bound_processor
= PROCESSOR_NULL
;
199 thread_template
.last_processor
= PROCESSOR_NULL
;
200 thread_template
.last_switch
= 0;
202 timer_init(&thread_template
.user_timer
);
203 timer_init(&thread_template
.system_timer
);
204 thread_template
.user_timer_save
= 0;
205 thread_template
.system_timer_save
= 0;
207 thread_template
.wait_timer_is_set
= FALSE
;
208 thread_template
.wait_timer_active
= 0;
210 thread_template
.depress_timer_active
= 0;
212 thread_template
.processor_set
= PROCESSOR_SET_NULL
;
214 thread_template
.special_handler
.handler
= special_handler
;
215 thread_template
.special_handler
.next
= 0;
218 thread_template
.may_assign
= TRUE
;
219 thread_template
.assign_active
= FALSE
;
220 #endif /* MACH_HOST */
221 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
222 thread_template
.funnel_state
= 0;
223 thread_template
.recover
= (vm_offset_t
)NULL
;
225 init_thread
= thread_template
;
226 machine_set_current_thread(&init_thread
);
233 sizeof(struct thread
),
234 THREAD_MAX
* sizeof(struct thread
),
235 THREAD_CHUNK
* sizeof(struct thread
),
241 * Initialize any machine-dependent
242 * per-thread structures necessary.
244 machine_thread_init();
248 thread_terminate_continue(void)
250 panic("thread_terminate_continue");
255 * thread_terminate_self:
258 thread_terminate_self(void)
260 thread_t thread
= current_thread();
268 * Cancel priority depression, reset scheduling parameters,
269 * and wait for concurrent expirations on other processors.
271 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
272 thread
->sched_mode
&= ~TH_MODE_ISDEPRESSED
;
274 if (timer_call_cancel(&thread
->depress_timer
))
275 thread
->depress_timer_active
--;
278 thread_policy_reset(thread
);
280 while (thread
->depress_timer_active
> 0) {
281 thread_unlock(thread
);
290 thread_unlock(thread
);
293 thread_mtx_lock(thread
);
295 ulock_release_all(thread
);
297 ipc_thread_disable(thread
);
299 thread_mtx_unlock(thread
);
302 * If we are the last thread to terminate and the task is
303 * associated with a BSD process, perform BSD process exit.
306 if ( hw_atomic_sub(&task
->active_thread_count
, 1) == 0 &&
307 task
->bsd_info
!= NULL
)
308 proc_exit(task
->bsd_info
);
314 * Cancel wait timer, and wait for
315 * concurrent expirations.
317 if (thread
->wait_timer_is_set
) {
318 thread
->wait_timer_is_set
= FALSE
;
320 if (timer_call_cancel(&thread
->wait_timer
))
321 thread
->wait_timer_active
--;
324 while (thread
->wait_timer_active
> 0) {
325 thread_unlock(thread
);
335 * If there is a reserved stack, release it.
337 if (thread
->reserved_stack
!= 0) {
338 if (thread
->reserved_stack
!= thread
->kernel_stack
)
339 stack_free_stack(thread
->reserved_stack
);
340 thread
->reserved_stack
= 0;
344 * Mark thread as terminating, and block.
346 thread
->state
|= TH_TERMINATE
;
347 thread_mark_wait_locked(thread
, THREAD_UNINT
);
348 assert(thread
->promotions
== 0);
349 thread_unlock(thread
);
352 thread_block((thread_continue_t
)thread_terminate_continue
);
360 processor_set_t pset
;
363 if (thread
== THREAD_NULL
)
366 if (thread_deallocate_internal(thread
) > 0)
369 ipc_thread_terminate(thread
);
375 void *ut
= thread
->uthread
;
377 thread
->uthread
= NULL
;
378 uthread_free(task
, ut
, task
->bsd_info
);
380 #endif /* MACH_BSD */
382 task_deallocate(task
);
384 pset
= thread
->processor_set
;
385 pset_deallocate(pset
);
387 if (thread
->kernel_stack
!= 0)
390 machine_thread_destroy(thread
);
392 zfree(thread_zone
, thread
);
396 * thread_terminate_daemon:
398 * Perform final clean up for terminating threads.
401 thread_terminate_daemon(void)
405 processor_set_t pset
;
408 simple_lock(&thread_terminate_lock
);
410 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
411 simple_unlock(&thread_terminate_lock
);
417 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
418 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
420 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
421 task
->thread_count
--;
424 pset
= thread
->processor_set
;
427 pset_remove_thread(pset
, thread
);
430 thread_deallocate(thread
);
433 simple_lock(&thread_terminate_lock
);
436 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
437 simple_unlock(&thread_terminate_lock
);
440 thread_block((thread_continue_t
)thread_terminate_daemon
);
445 * thread_terminate_enqueue:
447 * Enqueue a terminating thread for final disposition.
449 * Called at splsched.
452 thread_terminate_enqueue(
455 simple_lock(&thread_terminate_lock
);
456 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
457 simple_unlock(&thread_terminate_lock
);
459 thread_wakeup((event_t
)&thread_terminate_queue
);
463 * thread_stack_daemon:
465 * Perform stack allocation as required due to
469 thread_stack_daemon(void)
474 simple_lock(&thread_stack_lock
);
476 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
477 simple_unlock(&thread_stack_lock
);
483 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
484 thread_unlock(thread
);
488 simple_lock(&thread_stack_lock
);
491 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
492 simple_unlock(&thread_stack_lock
);
495 thread_block((thread_continue_t
)thread_stack_daemon
);
500 * thread_stack_enqueue:
502 * Enqueue a thread for stack allocation.
504 * Called at splsched.
507 thread_stack_enqueue(
510 simple_lock(&thread_stack_lock
);
511 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
512 simple_unlock(&thread_stack_lock
);
514 thread_wakeup((event_t
)&thread_stack_queue
);
518 thread_daemon_init(void)
520 kern_return_t result
;
523 simple_lock_init(&thread_terminate_lock
, 0);
524 queue_init(&thread_terminate_queue
);
526 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
527 if (result
!= KERN_SUCCESS
)
528 panic("thread_daemon_init: thread_terminate_daemon");
530 thread_deallocate(thread
);
532 simple_lock_init(&thread_stack_lock
, 0);
533 queue_init(&thread_stack_queue
);
535 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
536 if (result
!= KERN_SUCCESS
)
537 panic("thread_daemon_init: thread_stack_daemon");
539 thread_deallocate(thread
);
543 * Create a new thread.
544 * Doesn't start the thread running.
547 thread_create_internal(
550 thread_continue_t continuation
,
551 thread_t
*out_thread
)
554 processor_set_t pset
;
555 static thread_t first_thread
;
558 * Allocate a thread and initialize static fields
560 if (first_thread
== NULL
)
561 new_thread
= first_thread
= current_thread();
563 new_thread
= (thread_t
)zalloc(thread_zone
);
564 if (new_thread
== NULL
)
565 return (KERN_RESOURCE_SHORTAGE
);
567 if (new_thread
!= first_thread
)
568 *new_thread
= thread_template
;
572 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
);
573 if (new_thread
->uthread
== NULL
) {
574 zfree(thread_zone
, new_thread
);
575 return (KERN_RESOURCE_SHORTAGE
);
578 #endif /* MACH_BSD */
580 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
583 void *ut
= new_thread
->uthread
;
585 new_thread
->uthread
= NULL
;
586 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
588 #endif /* MACH_BSD */
589 zfree(thread_zone
, new_thread
);
590 return (KERN_FAILURE
);
593 new_thread
->task
= parent_task
;
595 thread_lock_init(new_thread
);
596 wake_lock_init(new_thread
);
598 mutex_init(&new_thread
->mutex
, 0);
600 ipc_thread_init(new_thread
);
601 queue_init(&new_thread
->held_ulocks
);
602 thread_prof_init(new_thread
, parent_task
);
604 new_thread
->continuation
= continuation
;
606 pset
= parent_task
->processor_set
;
607 assert(pset
== &default_pset
);
610 task_lock(parent_task
);
611 assert(parent_task
->processor_set
== pset
);
613 if ( !parent_task
->active
||
614 (parent_task
->thread_count
>= THREAD_MAX
&&
615 parent_task
!= kernel_task
)) {
616 task_unlock(parent_task
);
621 void *ut
= new_thread
->uthread
;
623 new_thread
->uthread
= NULL
;
624 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
626 #endif /* MACH_BSD */
627 ipc_thread_disable(new_thread
);
628 ipc_thread_terminate(new_thread
);
629 machine_thread_destroy(new_thread
);
630 zfree(thread_zone
, new_thread
);
631 return (KERN_FAILURE
);
634 task_reference_internal(parent_task
);
636 /* Cache the task's map */
637 new_thread
->map
= parent_task
->map
;
639 /* Chain the thread onto the task's list */
640 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
641 parent_task
->thread_count
++;
643 /* So terminating threads don't need to take the task lock to decrement */
644 hw_atomic_add(&parent_task
->active_thread_count
, 1);
646 /* Associate the thread with the processor set */
647 pset_add_thread(pset
, new_thread
);
649 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
650 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
652 /* Set the thread's scheduling parameters */
653 if (parent_task
!= kernel_task
)
654 new_thread
->sched_mode
|= TH_MODE_TIMESHARE
;
655 new_thread
->max_priority
= parent_task
->max_priority
;
656 new_thread
->task_priority
= parent_task
->priority
;
657 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
658 if (new_thread
->priority
> new_thread
->max_priority
)
659 new_thread
->priority
= new_thread
->max_priority
;
660 new_thread
->importance
=
661 new_thread
->priority
- new_thread
->task_priority
;
662 new_thread
->sched_stamp
= sched_tick
;
663 new_thread
->pri_shift
= new_thread
->processor_set
->pri_shift
;
664 compute_priority(new_thread
, FALSE
);
666 new_thread
->active
= TRUE
;
668 *out_thread
= new_thread
;
671 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
673 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
675 KERNEL_DEBUG_CONSTANT(
676 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
677 (vm_address_t
)new_thread
, dbg_arg2
, 0, 0, 0);
679 kdbg_trace_string(parent_task
->bsd_info
,
680 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
682 KERNEL_DEBUG_CONSTANT(
683 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
684 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
687 return (KERN_SUCCESS
);
693 thread_t
*new_thread
)
695 kern_return_t result
;
698 if (task
== TASK_NULL
|| task
== kernel_task
)
699 return (KERN_INVALID_ARGUMENT
);
701 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
702 if (result
!= KERN_SUCCESS
)
705 thread
->user_stop_count
= 1;
707 if (task
->suspend_count
> 0)
710 pset_unlock(task
->processor_set
);
713 *new_thread
= thread
;
715 return (KERN_SUCCESS
);
719 thread_create_running(
720 register task_t task
,
722 thread_state_t new_state
,
723 mach_msg_type_number_t new_state_count
,
724 thread_t
*new_thread
)
726 register kern_return_t result
;
729 if (task
== TASK_NULL
|| task
== kernel_task
)
730 return (KERN_INVALID_ARGUMENT
);
732 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
733 if (result
!= KERN_SUCCESS
)
736 result
= machine_thread_set_state(
737 thread
, flavor
, new_state
, new_state_count
);
738 if (result
!= KERN_SUCCESS
) {
739 pset_unlock(task
->processor_set
);
742 thread_terminate(thread
);
743 thread_deallocate(thread
);
747 thread_mtx_lock(thread
);
748 clear_wait(thread
, THREAD_AWAKENED
);
749 thread
->started
= TRUE
;
750 thread_mtx_unlock(thread
);
751 pset_unlock(task
->processor_set
);
754 *new_thread
= thread
;
760 * kernel_thread_create:
762 * Create a thread in the kernel task
763 * to execute in kernel context.
766 kernel_thread_create(
767 thread_continue_t continuation
,
770 thread_t
*new_thread
)
772 kern_return_t result
;
774 task_t task
= kernel_task
;
776 result
= thread_create_internal(task
, priority
, continuation
, &thread
);
777 if (result
!= KERN_SUCCESS
)
780 pset_unlock(task
->processor_set
);
784 assert(thread
->kernel_stack
!= 0);
785 thread
->reserved_stack
= thread
->kernel_stack
;
787 thread
->parameter
= parameter
;
789 *new_thread
= thread
;
795 kernel_thread_start_priority(
796 thread_continue_t continuation
,
799 thread_t
*new_thread
)
801 kern_return_t result
;
804 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
805 if (result
!= KERN_SUCCESS
)
808 thread_mtx_lock(thread
);
809 clear_wait(thread
, THREAD_AWAKENED
);
810 thread
->started
= TRUE
;
811 thread_mtx_unlock(thread
);
813 *new_thread
= thread
;
820 thread_continue_t continuation
,
822 thread_t
*new_thread
)
824 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
832 kern_return_t result
;
835 if (task
!= kernel_task
)
836 panic("kernel_thread");
838 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
839 if (result
!= KERN_SUCCESS
)
840 return (THREAD_NULL
);
842 thread_deallocate(thread
);
848 thread_info_internal(
849 register thread_t thread
,
850 thread_flavor_t flavor
,
851 thread_info_t thread_info_out
, /* ptr to OUT array */
852 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
857 if (thread
== THREAD_NULL
)
858 return (KERN_INVALID_ARGUMENT
);
860 if (flavor
== THREAD_BASIC_INFO
) {
861 register thread_basic_info_t basic_info
;
863 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
864 return (KERN_INVALID_ARGUMENT
);
866 basic_info
= (thread_basic_info_t
) thread_info_out
;
873 thread_read_times(thread
, &basic_info
->user_time
,
874 &basic_info
->system_time
);
877 * Update lazy-evaluated scheduler info because someone wants it.
879 if (thread
->sched_stamp
!= sched_tick
)
880 update_priority(thread
);
882 basic_info
->sleep_time
= 0;
885 * To calculate cpu_usage, first correct for timer rate,
886 * then for 5/8 ageing. The correction factor [3/5] is
889 basic_info
->cpu_usage
= ((uint64_t)thread
->cpu_usage
890 * TH_USAGE_SCALE
) / sched_tick_interval
;
891 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
893 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
894 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
896 basic_info
->policy
= ((thread
->sched_mode
& TH_MODE_TIMESHARE
)?
897 POLICY_TIMESHARE
: POLICY_RR
);
900 if (thread
->state
& TH_IDLE
)
901 flags
|= TH_FLAGS_IDLE
;
903 if (!thread
->kernel_stack
)
904 flags
|= TH_FLAGS_SWAPPED
;
907 if (thread
->state
& TH_TERMINATE
)
908 state
= TH_STATE_HALTED
;
910 if (thread
->state
& TH_RUN
)
911 state
= TH_STATE_RUNNING
;
913 if (thread
->state
& TH_UNINT
)
914 state
= TH_STATE_UNINTERRUPTIBLE
;
916 if (thread
->state
& TH_SUSP
)
917 state
= TH_STATE_STOPPED
;
919 if (thread
->state
& TH_WAIT
)
920 state
= TH_STATE_WAITING
;
922 basic_info
->run_state
= state
;
923 basic_info
->flags
= flags
;
925 basic_info
->suspend_count
= thread
->user_stop_count
;
927 thread_unlock(thread
);
930 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
932 return (KERN_SUCCESS
);
935 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
936 policy_timeshare_info_t ts_info
;
938 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
939 return (KERN_INVALID_ARGUMENT
);
941 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
946 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
947 thread_unlock(thread
);
950 return (KERN_INVALID_POLICY
);
953 ts_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
954 if (ts_info
->depressed
) {
955 ts_info
->base_priority
= DEPRESSPRI
;
956 ts_info
->depress_priority
= thread
->priority
;
959 ts_info
->base_priority
= thread
->priority
;
960 ts_info
->depress_priority
= -1;
963 ts_info
->cur_priority
= thread
->sched_pri
;
964 ts_info
->max_priority
= thread
->max_priority
;
966 thread_unlock(thread
);
969 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
971 return (KERN_SUCCESS
);
974 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
975 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
976 return (KERN_INVALID_ARGUMENT
);
978 return (KERN_INVALID_POLICY
);
981 if (flavor
== THREAD_SCHED_RR_INFO
) {
982 policy_rr_info_t rr_info
;
984 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
985 return (KERN_INVALID_ARGUMENT
);
987 rr_info
= (policy_rr_info_t
) thread_info_out
;
992 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
993 thread_unlock(thread
);
996 return (KERN_INVALID_POLICY
);
999 rr_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
1000 if (rr_info
->depressed
) {
1001 rr_info
->base_priority
= DEPRESSPRI
;
1002 rr_info
->depress_priority
= thread
->priority
;
1005 rr_info
->base_priority
= thread
->priority
;
1006 rr_info
->depress_priority
= -1;
1009 rr_info
->max_priority
= thread
->max_priority
;
1010 rr_info
->quantum
= std_quantum_us
/ 1000;
1012 thread_unlock(thread
);
1015 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1017 return (KERN_SUCCESS
);
1020 return (KERN_INVALID_ARGUMENT
);
1026 time_value_t
*user_time
,
1027 time_value_t
*system_time
)
1029 absolutetime_to_microtime(
1030 timer_grab(&thread
->user_timer
),
1031 &user_time
->seconds
, &user_time
->microseconds
);
1033 absolutetime_to_microtime(
1034 timer_grab(&thread
->system_timer
),
1035 &system_time
->seconds
, &system_time
->microseconds
);
1040 __unused thread_t thread
,
1041 __unused processor_set_t new_pset
)
1043 return (KERN_FAILURE
);
1047 * thread_assign_default:
1049 * Special version of thread_assign for assigning threads to default
1053 thread_assign_default(
1056 return (thread_assign(thread
, &default_pset
));
1060 * thread_get_assignment
1062 * Return current assignment for this thread.
1065 thread_get_assignment(
1067 processor_set_t
*pset
)
1070 return (KERN_INVALID_ARGUMENT
);
1072 *pset
= thread
->processor_set
;
1073 pset_reference(*pset
);
1074 return (KERN_SUCCESS
);
1078 * thread_wire_internal:
1080 * Specify that the target thread must always be able
1081 * to run and to allocate memory.
1084 thread_wire_internal(
1085 host_priv_t host_priv
,
1088 boolean_t
*prev_state
)
1090 if (host_priv
== NULL
|| thread
!= current_thread())
1091 return (KERN_INVALID_ARGUMENT
);
1093 assert(host_priv
== &realhost
);
1096 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1099 if (!(thread
->options
& TH_OPT_VMPRIV
))
1100 vm_page_free_reserve(1); /* XXX */
1101 thread
->options
|= TH_OPT_VMPRIV
;
1104 if (thread
->options
& TH_OPT_VMPRIV
)
1105 vm_page_free_reserve(-1); /* XXX */
1106 thread
->options
&= ~TH_OPT_VMPRIV
;
1109 return (KERN_SUCCESS
);
1116 * User-api wrapper for thread_wire_internal()
1120 host_priv_t host_priv
,
1124 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1127 int split_funnel_off
= 0;
1128 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1129 lck_grp_attr_t
*funnel_lck_grp_attr
;
1130 lck_attr_t
*funnel_lck_attr
;
1139 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1140 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1142 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1144 funnel_lck_attr
= lck_attr_alloc_init();
1146 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1147 bzero((void *)fnl
, sizeof(funnel_t
));
1148 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1149 kfree(fnl
, sizeof(funnel_t
));
1150 return(THR_FUNNEL_NULL
);
1153 fnl
->fnl_type
= type
;
1162 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1163 if (fnl
->fnl_oldmutex
)
1164 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1165 kfree(fnl
, sizeof(funnel_t
));
1172 lck_mtx_lock(fnl
->fnl_mutex
);
1173 fnl
->fnl_mtxholder
= current_thread();
1180 lck_mtx_unlock(fnl
->fnl_mutex
);
1181 fnl
->fnl_mtxrelease
= current_thread();
1188 thread_t th
= current_thread();
1190 if (th
->funnel_state
& TH_FN_OWNED
) {
1191 return(th
->funnel_lock
);
1193 return(THR_FUNNEL_NULL
);
1201 thread_t cur_thread
;
1202 boolean_t funnel_state_prev
;
1205 cur_thread
= current_thread();
1206 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1208 if (funnel_state_prev
!= funneled
) {
1209 intr
= ml_set_interrupts_enabled(FALSE
);
1211 if (funneled
== TRUE
) {
1212 if (cur_thread
->funnel_lock
)
1213 panic("Funnel lock called when holding one %x", cur_thread
->funnel_lock
);
1214 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1217 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1219 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1220 cur_thread
->funnel_lock
= fnl
;
1222 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1223 panic("Funnel unlock when not holding funnel");
1224 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1225 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1228 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1231 (void)ml_set_interrupts_enabled(intr
);
1233 /* if we are trying to acquire funnel recursively
1234 * check for funnel to be held already
1236 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1237 panic("thread_funnel_set: already holding a different funnel");
1240 return(funnel_state_prev
);
1245 * Export routines to other components for things that are done as macros
1246 * within the osfmk component.
1249 #undef thread_reference
1250 void thread_reference(thread_t thread
);
1255 if (thread
!= THREAD_NULL
)
1256 thread_reference_internal(thread
);
1259 #undef thread_should_halt
1265 return (thread_should_halt_fast(th
));