-/*
- * thread_policy_common:
- *
- * Set scheduling policy & priority for thread.
- */
-static kern_return_t
-thread_policy_common(
- thread_t thread,
- integer_t policy,
- integer_t priority)
-{
- spl_t s;
-
- if ( thread == THREAD_NULL ||
- invalid_policy(policy) )
- return(KERN_INVALID_ARGUMENT);
-
- s = splsched();
- thread_lock(thread);
-
- if ( !(thread->sched_mode & TH_MODE_REALTIME) &&
- !(thread->safe_mode & TH_MODE_REALTIME) ) {
- if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
- integer_t oldmode = (thread->sched_mode & TH_MODE_TIMESHARE);
-
- if (policy == POLICY_TIMESHARE && !oldmode) {
- thread->sched_mode |= TH_MODE_TIMESHARE;
-
- if (thread->state & TH_RUN)
- pset_share_incr(thread->processor_set);
- }
- else
- if (policy != POLICY_TIMESHARE && oldmode) {
- thread->sched_mode &= ~TH_MODE_TIMESHARE;
-
- if (thread->state & TH_RUN)
- pset_share_decr(thread->processor_set);
- }
- }
- else {
- if (policy == POLICY_TIMESHARE)
- thread->safe_mode |= TH_MODE_TIMESHARE;
- else
- thread->safe_mode &= ~TH_MODE_TIMESHARE;
- }
-
- if (priority >= thread->max_priority)
- priority = thread->max_priority - thread->task_priority;
- else
- if (priority >= MINPRI_KERNEL)
- priority -= MINPRI_KERNEL;
- else
- if (priority >= MINPRI_RESERVED)
- priority -= MINPRI_RESERVED;
- else
- priority -= BASEPRI_DEFAULT;
-
- priority += thread->task_priority;
-
- if (priority > thread->max_priority)
- priority = thread->max_priority;
- else
- if (priority < MINPRI)
- priority = MINPRI;
-
- thread->importance = priority - thread->task_priority;
-
- set_priority(thread, priority);
- }
-
- thread_unlock(thread);
- splx(s);
-
- return (KERN_SUCCESS);
-}