Lines Matching refs:thread

59  *	File:	kern/thread.c
93 #include <machine/thread.h>
114 #include <kern/thread.h>
148 static struct thread thread_template, init_thread;
152 thread_t thread);
173 * Fill in a template thread for fast initialization.
285 sizeof(struct thread),
286 thread_max * sizeof(struct thread),
287 THREAD_CHUNK * sizeof(struct thread),
291 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
298 * per-thread structures necessary.
318 thread_t thread = current_thread();
326 thread_mtx_lock(thread);
328 ulock_release_all(thread);
330 ipc_thread_disable(thread);
332 thread_mtx_unlock(thread);
335 thread_lock(thread);
341 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
342 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
344 if (timer_call_cancel(&thread->depress_timer))
345 thread->depress_timer_active--;
348 while (thread->depress_timer_active > 0) {
349 thread_unlock(thread);
355 thread_lock(thread);
358 thread_sched_call(thread, NULL);
360 thread_unlock(thread);
363 thread_policy_reset(thread);
366 thead_remove_taskwatch(thread);
369 task = thread->task;
370 uthread_cleanup(task, thread->uthread, task->bsd_info);
374 * If we are the last thread to terminate and the task is
380 uthread_cred_free(thread->uthread);
383 thread_lock(thread);
389 if (thread->wait_timer_is_set) {
390 thread->wait_timer_is_set = FALSE;
392 if (timer_call_cancel(&thread->wait_timer))
393 thread->wait_timer_active--;
396 while (thread->wait_timer_active > 0) {
397 thread_unlock(thread);
403 thread_lock(thread);
409 if (thread->reserved_stack != 0) {
410 stack_free_reserved(thread);
411 thread->reserved_stack = 0;
415 * Mark thread as terminating, and block.
417 thread->state |= TH_TERMINATE;
418 thread_mark_wait_locked(thread, THREAD_UNINT);
419 thread_unlock(thread);
428 thread_t thread)
432 if (thread == THREAD_NULL)
435 if (thread_deallocate_internal(thread) > 0)
439 ipc_thread_terminate(thread);
441 task = thread->task;
445 void *ut = thread->uthread;
447 thread->uthread = NULL;
452 if (thread->t_ledger)
453 ledger_dereference(thread->t_ledger);
454 if (thread->t_threadledger)
455 ledger_dereference(thread->t_threadledger);
457 if (thread->kernel_stack != 0)
458 stack_free(thread);
460 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
461 machine_thread_destroy(thread);
465 zfree(thread_zone, thread);
476 thread_t self, thread;
485 while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
489 task = thread->task;
492 task->total_user_time += timer_grab(&thread->user_timer);
493 if (thread->precise_user_kernel_time) {
494 task->total_system_time += timer_grab(&thread->system_timer);
496 task->total_user_time += timer_grab(&thread->system_timer);
499 task->c_switch += thread->c_switch;
500 task->p_switch += thread->p_switch;
501 task->ps_switch += thread->ps_switch;
503 task->syscalls_unix += thread->syscalls_unix;
504 task->syscalls_mach += thread->syscalls_mach;
506 task->task_timer_wakeups_bin_1 += thread->thread_timer_wakeups_bin_1;
507 task->task_timer_wakeups_bin_2 += thread->thread_timer_wakeups_bin_2;
508 queue_remove(&task->threads, thread, thread_t, task_threads);
512 * If the task is being halted, and there is only one thread
513 * left in the task after this one, then wakeup that thread.
521 queue_remove(&threads, thread, thread_t, threads);
525 thread_deallocate(thread);
543 * Enqueue a terminating thread for final disposition.
549 thread_t thread)
552 enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
567 thread_t thread;
571 while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
574 stack_alloc(thread);
577 thread_lock(thread);
578 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
579 thread_unlock(thread);
595 * Enqueue a thread for stack allocation.
601 thread_t thread)
604 enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
614 thread_t thread = NULL;
619 result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
623 thread_deallocate(thread);
628 result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
632 thread_deallocate(thread);
636 * Create a new thread.
637 * Doesn't start the thread running.
654 * Allocate a thread and initialize static fields
737 * This task has a per-thread CPU limit; make sure this new thread
742 new_thread->t_threadledger = LEDGER_NULL; /* per thread ledger is not inherited */
750 /* Chain the thread onto the task's list */
769 * thread.
775 /* Set the thread's scheduling parameters */
796 /* set thread self backgrounding */
844 thread_t thread;
849 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
853 thread->user_stop_count = 1;
854 thread_hold(thread);
856 thread_hold(thread);
864 *new_thread = thread;
901 thread_t thread;
906 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
911 thread, flavor, new_state, new_state_count);
916 thread_terminate(thread);
917 thread_deallocate(thread);
921 thread_mtx_lock(thread);
922 thread_start_internal(thread);
923 thread_mtx_unlock(thread);
931 *new_thread = thread;
978 thread_t thread;
983 result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
987 thread->user_stop_count = 1;
988 thread_hold(thread);
990 thread_hold(thread);
995 *new_thread = thread;
1003 * Create a thread in the kernel task
1014 thread_t thread;
1017 result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
1024 stack_alloc(thread);
1025 assert(thread->kernel_stack != 0);
1029 thread->reserved_stack = thread->kernel_stack;
1031 thread->parameter = parameter;
1034 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
1035 *new_thread = thread;
1048 thread_t thread;
1050 result = kernel_thread_create(continuation, parameter, priority, &thread);
1054 *new_thread = thread;
1056 thread_mtx_lock(thread);
1057 thread_start_internal(thread);
1058 thread_mtx_unlock(thread);
1080 thread_t thread;
1085 result = kernel_thread_start_priority((thread_continue_t)start, NULL, -1, &thread);
1089 thread_deallocate(thread);
1091 return (thread);
1098 register thread_t thread,
1106 if (thread == THREAD_NULL)
1118 thread_lock(thread);
1122 thread_read_times(thread, &basic_info->user_time,
1128 if (SCHED(can_update_priority)(thread))
1129 SCHED(update_priority)(thread);
1141 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1150 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
1154 if (thread->bound_processor != PROCESSOR_NULL && thread->bound_processor->idle_thread == thread)
1157 if (!thread->kernel_stack)
1161 if (thread->state & TH_TERMINATE)
1164 if (thread->state & TH_RUN)
1167 if (thread->state & TH_UNINT)
1170 if (thread->state & TH_SUSP)
1173 if (thread->state & TH_WAIT)
1179 basic_info->suspend_count = thread->user_stop_count;
1181 thread_unlock(thread);
1198 thread_lock(thread);
1200 identifier_info->thread_id = thread->thread_id;
1201 identifier_info->thread_handle = thread->machine.cthread_self;
1202 if(thread->task->bsd_info) {
1203 identifier_info->dispatch_qaddr = identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1205 thread_unlock(thread);
1210 thread_unlock(thread);
1224 thread_lock(thread);
1226 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1227 thread_unlock(thread);
1233 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1236 ts_info->depress_priority = thread->priority;
1239 ts_info->base_priority = thread->priority;
1243 ts_info->cur_priority = thread->sched_pri;
1244 ts_info->max_priority = thread->max_priority;
1246 thread_unlock(thread);
1272 thread_lock(thread);
1274 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1275 thread_unlock(thread);
1281 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1284 rr_info->depress_priority = thread->priority;
1287 rr_info->base_priority = thread->priority;
1294 rr_info->max_priority = thread->max_priority;
1297 thread_unlock(thread);
1310 thread_t thread,
1318 tval_user = timer_grab(&thread->user_timer);
1319 tval_system = timer_grab(&thread->system_timer);
1321 if (thread->precise_user_kernel_time) {
1343 __unused thread_t thread,
1357 thread_t thread)
1359 return (thread_assign(thread, &pset0));
1365 * Return current assignment for this thread.
1369 thread_t thread,
1372 if (thread == NULL)
1383 * Specify that the target thread must always be able
1389 thread_t thread,
1393 if (host_priv == NULL || thread != current_thread())
1399 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
1402 if (!(thread->options & TH_OPT_VMPRIV))
1404 thread->options |= TH_OPT_VMPRIV;
1407 if (thread->options & TH_OPT_VMPRIV)
1409 thread->options &= ~TH_OPT_VMPRIV;
1424 thread_t thread,
1427 return (thread_wire_internal(host_priv, thread, wired, NULL));
1433 thread_t thread = current_thread();
1436 assert(thread->t_threadledger != LEDGER_NULL);
1442 ledger_set_action(thread->t_threadledger, thread_ledgers.cpu_time,
1444 ledger_disable_callback(thread->t_threadledger, thread_ledgers.cpu_time);
1457 if ((t = ledger_template_create("Per-thread ledger")) == NULL)
1458 panic("couldn't create thread ledger template");
1461 panic("couldn't create cpu_time entry for thread ledger template");
1466 panic("couldn't set thread ledger callback for cpu_time entry");
1474 * Set CPU usage limit on a thread.
1476 * Calling with percentage of 0 will unset the limit for this thread.
1482 thread_t thread = current_thread();
1493 if (thread->t_threadledger != LEDGER_NULL) {
1495 * The only way to get a per-thread ledger is via CPU limits.
1497 assert(thread->options & (TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT));
1498 ledger_dereference(thread->t_threadledger);
1499 thread->t_threadledger = LEDGER_NULL;
1500 thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
1506 l = thread->t_threadledger;
1509 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1515 * We are the first to create this thread's ledger, so only activate our entry.
1518 thread->t_threadledger = l;
1523 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1529 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1537 thread->options |= TH_OPT_PROC_CPULIMIT;
1542 thread->options |= TH_OPT_PRVT_CPULIMIT;
1543 /* The per-thread ledger template by default has a callback for CPU time */
1548 thread->t_threadledger = l;
1672 __unused thread_t thread)
1679 thread_t thread,
1682 thread->sched_call = (call != NULL)? call: sched_call_null;
1687 thread_t thread,
1690 thread_mtx_lock(thread);
1691 thread->static_param = state;
1692 thread_mtx_unlock(thread);
1697 thread_t thread)
1699 return (thread != THREAD_NULL? thread->thread_id: 0);
1713 thread_t thread)
1718 if (thread != THREAD_NULL) {
1719 thread_handle = thread->machine.cthread_self;
1721 if (thread->task->bsd_info)
1722 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1734 void thread_reference(thread_t thread);
1737 thread_t thread)
1739 if (thread != THREAD_NULL)
1740 thread_reference_internal(thread);
1753 uint32_t dtrace_get_thread_predcache(thread_t thread)
1755 if (thread != THREAD_NULL)
1756 return thread->t_dtrace_predcache;
1761 int64_t dtrace_get_thread_vtime(thread_t thread)
1763 if (thread != THREAD_NULL)
1764 return thread->t_dtrace_vtime;
1769 int64_t dtrace_get_thread_tracing(thread_t thread)
1771 if (thread != THREAD_NULL)
1772 return thread->t_dtrace_tracing;
1777 boolean_t dtrace_get_thread_reentering(thread_t thread)
1779 if (thread != THREAD_NULL)
1780 return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
1785 vm_offset_t dtrace_get_kernel_stack(thread_t thread)
1787 if (thread != THREAD_NULL)
1788 return thread->kernel_stack;
1793 int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
1795 if (thread != THREAD_NULL) {
1802 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
1808 void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
1810 if (thread != THREAD_NULL)
1811 thread->t_dtrace_predcache = predcache;
1814 void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
1816 if (thread != THREAD_NULL)
1817 thread->t_dtrace_vtime = vtime;
1820 void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
1822 if (thread != THREAD_NULL)
1823 thread->t_dtrace_tracing = accum;
1826 void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
1828 if (thread != THREAD_NULL) {
1830 thread->options |= TH_OPT_DTRACE;
1832 thread->options &= (~TH_OPT_DTRACE);
1836 vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
1840 if (thread != THREAD_NULL) {
1841 prev = thread->recover;
1842 thread->recover = recover;