]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/thread_policy.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / kern / thread_policy.c
index b04cf8f01d8d806ab87135cc0d5a54c760929059..cf5cb1f2e0c9f323fe2c8e8ab5ee5184b068f5e0 100644 (file)
@@ -3,19 +3,22 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
 
 #include <kern/thread.h>
 
+static void
+thread_recompute_priority(
+       thread_t                thread);
+
 kern_return_t
 thread_policy_set(
        thread_act_t                    act,
@@ -39,76 +46,54 @@ thread_policy_set(
 {
        kern_return_t                   result = KERN_SUCCESS;
        thread_t                                thread;
-       task_t                                  task;
        spl_t                                   s;
 
        if (act == THR_ACT_NULL)
                return (KERN_INVALID_ARGUMENT);
 
-       act_lock(act);
-       task = act->task;
-       act_unlock(act);
-
-       task_lock(task);
-
        thread = act_lock_thread(act);
        if (!act->active) {
                act_unlock_thread(act);
-               task_unlock(task);
 
                return (KERN_TERMINATED);
        }
 
-       if (thread == THREAD_NULL) {
-               act_unlock_thread(act);
-               task_unlock(task);
-
-               return (KERN_NOT_SUPPORTED);
-       }
-
-#define        thread_priority_set(thread, pri)                                        \
-MACRO_BEGIN                                                                                                    \
-       if ((thread)->depress_priority >= 0)                                    \
-               (thread)->depress_priority = (pri);                                     \
-       else {                                                                                                  \
-               (thread)->priority = (pri);                                                     \
-               compute_priority((thread), TRUE);                                       \
-                                                                                                                       \
-               if ((thread) == current_thread())                                       \
-                       ast_on(AST_BLOCK);                                                              \
-       }                                                                                                               \
-MACRO_END
+       assert(thread != THREAD_NULL);
 
        switch (flavor) {
 
-       case THREAD_STANDARD_POLICY:
+       case THREAD_EXTENDED_POLICY:
        {
-               integer_t                               priority;
+               boolean_t                               timeshare = TRUE;
 
-               s = splsched();
-               thread_lock(thread);
+               if (count >= THREAD_EXTENDED_POLICY_COUNT) {
+                       thread_extended_policy_t        info;
 
-               thread->sched_mode &=~ TH_MODE_REALTIME;
+                       info = (thread_extended_policy_t)policy_info;
+                       timeshare = info->timeshare;
+               }
 
-               thread->policy = POLICY_TIMESHARE;
+               s = splsched();
+               thread_lock(thread);
 
-               if (thread->importance > MAXPRI)
-                       priority = MAXPRI;
-               else
-               if (thread->importance < -MAXPRI)
-                       priority = -MAXPRI;
-               else
-                       priority = thread->importance;
+               if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
+                       thread->sched_mode &= ~TH_MODE_REALTIME;
 
-               priority += task->priority;
+                       if (timeshare)
+                               thread->sched_mode |= TH_MODE_TIMESHARE;
+                       else
+                               thread->sched_mode &= ~TH_MODE_TIMESHARE;
 
-               if (priority > thread->max_priority)
-                       priority = thread->max_priority;
-               else
-               if (priority < MINPRI)
-                       priority = MINPRI;
+                       thread_recompute_priority(thread);
+               }
+               else {
+                       thread->safe_mode &= ~TH_MODE_REALTIME;
 
-               thread_priority_set(thread, priority);
+                       if (timeshare)
+                               thread->safe_mode |= TH_MODE_TIMESHARE;
+                       else
+                               thread->safe_mode &= ~TH_MODE_TIMESHARE;
+               }
 
                thread_unlock(thread);
                splx(s);
@@ -126,20 +111,29 @@ MACRO_END
                }
 
                info = (thread_time_constraint_policy_t)policy_info;
+               if (    info->computation > max_rt_quantum      ||
+                               info->computation < min_rt_quantum              ) {
+                       result = KERN_INVALID_ARGUMENT;
+                       break;
+               }
 
                s = splsched();
                thread_lock(thread);
 
-               thread->sched_mode |= TH_MODE_REALTIME;
-
                thread->realtime.period = info->period;
                thread->realtime.computation = info->computation;
                thread->realtime.constraint = info->constraint;
                thread->realtime.preemptible = info->preemptible;
 
-               thread->policy = POLICY_RR;
-
-               thread_priority_set(thread, BASEPRI_REALTIME);
+               if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
+                       thread->sched_mode &= ~TH_MODE_TIMESHARE;
+                       thread->sched_mode |= TH_MODE_REALTIME;
+                       thread_recompute_priority(thread);
+               }
+               else {
+                       thread->safe_mode &= ~TH_MODE_TIMESHARE;
+                       thread->safe_mode |= TH_MODE_REALTIME;
+               }
 
                thread_unlock(thread);
                splx(s);
@@ -163,27 +157,7 @@ MACRO_END
 
                thread->importance = info->importance;
 
-               if (!(thread->sched_mode & TH_MODE_REALTIME)) {
-                       integer_t                                       priority;
-
-                       if (thread->importance > MAXPRI)
-                               priority = MAXPRI;
-                       else
-                       if (thread->importance < -MAXPRI)
-                               priority = -MAXPRI;
-                       else
-                               priority = thread->importance;
-
-                       priority += task->priority;
-
-                       if (priority > thread->max_priority)
-                               priority = thread->max_priority;
-                       else
-                       if (priority < MINPRI)
-                               priority = MINPRI;
-
-                       thread_priority_set(thread, priority);
-               }
+               thread_recompute_priority(thread);
 
                thread_unlock(thread);
                splx(s);
@@ -198,11 +172,60 @@ MACRO_END
 
        act_unlock_thread(act);
 
-       task_unlock(task);
-
        return (result);
 }
 
+static void
+thread_recompute_priority(
+       thread_t                thread)
+{
+       integer_t               priority;
+
+       if (thread->sched_mode & TH_MODE_REALTIME)
+               priority = BASEPRI_REALTIME;
+       else {
+               if (thread->importance > MAXPRI)
+                       priority = MAXPRI;
+               else
+               if (thread->importance < -MAXPRI)
+                       priority = -MAXPRI;
+               else
+                       priority = thread->importance;
+
+               priority += thread->task_priority;
+
+               if (priority > thread->max_priority)
+                       priority = thread->max_priority;
+               else
+               if (priority < MINPRI)
+                       priority = MINPRI;
+       }
+
+       set_priority(thread, priority);
+}
+
+void
+thread_task_priority(
+       thread_t                thread,
+       integer_t               priority,
+       integer_t               max_priority)
+{
+       spl_t                           s;
+
+       assert(thread != THREAD_NULL);
+
+       s = splsched();
+       thread_lock(thread);
+
+       thread->task_priority = priority;
+       thread->max_priority = max_priority;
+
+       thread_recompute_priority(thread);
+
+       thread_unlock(thread);
+       splx(s);
+}
+
 kern_return_t
 thread_policy_get(
        thread_act_t                    act,
@@ -225,24 +248,41 @@ thread_policy_get(
                return (KERN_TERMINATED);
        }
 
-       if (thread == THREAD_NULL) {
-               act_unlock_thread(act);
-
-               return (KERN_NOT_SUPPORTED);
-       }
+       assert(thread != THREAD_NULL);
 
        switch (flavor) {
 
-       case THREAD_STANDARD_POLICY:
-               s = splsched();
-               thread_lock(thread);
+       case THREAD_EXTENDED_POLICY:
+       {
+               boolean_t               timeshare = TRUE;
 
-               if (thread->sched_mode & TH_MODE_REALTIME)
-                       *get_default = TRUE;
+               if (!(*get_default)) {
+                       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))
+                                       timeshare = (thread->sched_mode & TH_MODE_TIMESHARE) != 0;
+                               else
+                                       timeshare = (thread->safe_mode & TH_MODE_TIMESHARE) != 0;
+                       }
+                       else
+                               *get_default = TRUE;
+
+                       thread_unlock(thread);
+                       splx(s);
+               }
+
+               if (*count >= THREAD_EXTENDED_POLICY_COUNT) {
+                       thread_extended_policy_t        info;
+
+                       info = (thread_extended_policy_t)policy_info;
+                       info->timeshare = timeshare;
+               }
 
-               thread_unlock(thread);
-               splx(s);
                break;
+       }
 
        case THREAD_TIME_CONSTRAINT_POLICY:
        {
@@ -255,29 +295,31 @@ thread_policy_get(
 
                info = (thread_time_constraint_policy_t)policy_info;
 
-               s = splsched();
-               thread_lock(thread);
+               if (!(*get_default)) {
+                       s = splsched();
+                       thread_lock(thread);
 
-               if ((thread->sched_mode & TH_MODE_REALTIME) && !(*get_default)) {
-                       info->period = thread->realtime.period;
-                       info->computation = thread->realtime.computation;
-                       info->constraint = thread->realtime.constraint;
-                       info->preemptible = thread->realtime.preemptible;
-               }
-               else {
-                       extern natural_t                min_quantum_abstime;
+                       if (    (thread->sched_mode & TH_MODE_REALTIME) ||
+                                       (thread->safe_mode & TH_MODE_REALTIME)          ) {
+                               info->period = thread->realtime.period;
+                               info->computation = thread->realtime.computation;
+                               info->constraint = thread->realtime.constraint;
+                               info->preemptible = thread->realtime.preemptible;
+                       }
+                       else
+                               *get_default = TRUE;
 
-                       *get_default = TRUE;
+                       thread_unlock(thread);
+                       splx(s);
+               }
 
+               if (*get_default) {
                        info->period = 0;
-                       info->computation = min_quantum_abstime / 2;
-                       info->constraint = min_quantum_abstime;
+                       info->computation = std_quantum / 2;
+                       info->constraint = std_quantum;
                        info->preemptible = TRUE;
                }
 
-               thread_unlock(thread);
-               splx(s);
-
                break;
        }
 
@@ -292,9 +334,7 @@ thread_policy_get(
 
                info = (thread_precedence_policy_t)policy_info;
 
-               if (*get_default)
-                       info->importance = 0;
-               else {
+               if (!(*get_default)) {
                        s = splsched();
                        thread_lock(thread);
 
@@ -303,6 +343,8 @@ thread_policy_get(
                        thread_unlock(thread);
                        splx(s);
                }
+               else
+                       info->importance = 0;
 
                break;
        }