]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/thread_policy.c
xnu-792.tar.gz
[apple/xnu.git] / osfmk / kern / thread_policy.c
index e9eacf91b5588b1315d7ff92ff3021768f2369fe..c9e75fc1ebfa1b2f535aef41f937f6513066d4a4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
-/*
- * Copyright (c) 2000 Apple Computer, Inc.  All rights reserved.
- *
- * HISTORY
- *
- * 15 October 2000 (debo)
- *  Created.
- */
 
+#include <mach/mach_types.h>
+#include <mach/thread_act_server.h>
+
+#include <kern/kern_types.h>
+#include <kern/processor.h>
 #include <kern/thread.h>
 
 static void
@@ -36,27 +33,24 @@ thread_recompute_priority(
 
 kern_return_t
 thread_policy_set(
-       thread_act_t                    act,
+       thread_t                                thread,
        thread_policy_flavor_t  flavor,
        thread_policy_t                 policy_info,
        mach_msg_type_number_t  count)
 {
        kern_return_t                   result = KERN_SUCCESS;
-       thread_t                                thread;
        spl_t                                   s;
 
-       if (act == THR_ACT_NULL)
+       if (thread == THREAD_NULL)
                return (KERN_INVALID_ARGUMENT);
 
-       thread = act_lock_thread(act);
-       if (!act->active) {
-               act_unlock_thread(act);
+       thread_mtx_lock(thread);
+       if (!thread->active) {
+               thread_mtx_unlock(thread);
 
                return (KERN_TERMINATED);
        }
 
-       assert(thread != THREAD_NULL);
-
        switch (flavor) {
 
        case THREAD_EXTENDED_POLICY:
@@ -74,13 +68,24 @@ thread_policy_set(
                thread_lock(thread);
 
                if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
+                       integer_t       oldmode = (thread->sched_mode & TH_MODE_TIMESHARE);
+
                        thread->sched_mode &= ~TH_MODE_REALTIME;
 
-                       if (timeshare)
+                       if (timeshare && !oldmode) {
                                thread->sched_mode |= TH_MODE_TIMESHARE;
+
+                               if (thread->state & TH_RUN)
+                                       pset_share_incr(thread->processor_set);
+                       }
                        else
+                       if (!timeshare && oldmode) {
                                thread->sched_mode &= ~TH_MODE_TIMESHARE;
 
+                               if (thread->state & TH_RUN)
+                                       pset_share_decr(thread->processor_set);
+                       }
+
                        thread_recompute_priority(thread);
                }
                else {
@@ -108,7 +113,8 @@ thread_policy_set(
                }
 
                info = (thread_time_constraint_policy_t)policy_info;
-               if (    info->computation > max_rt_quantum      ||
+               if (    info->constraint < info->computation    ||
+                               info->computation > max_rt_quantum              ||
                                info->computation < min_rt_quantum              ) {
                        result = KERN_INVALID_ARGUMENT;
                        break;
@@ -123,7 +129,12 @@ thread_policy_set(
                thread->realtime.preemptible = info->preemptible;
 
                if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
-                       thread->sched_mode &= ~TH_MODE_TIMESHARE;
+                       if (thread->sched_mode & TH_MODE_TIMESHARE) {
+                               thread->sched_mode &= ~TH_MODE_TIMESHARE;
+
+                               if (thread->state & TH_RUN)
+                                       pset_share_decr(thread->processor_set);
+                       }
                        thread->sched_mode |= TH_MODE_REALTIME;
                        thread_recompute_priority(thread);
                }
@@ -167,7 +178,7 @@ thread_policy_set(
                break;
        }
 
-       act_unlock_thread(act);
+       thread_mtx_unlock(thread);
 
        return (result);
 }
@@ -179,7 +190,7 @@ thread_recompute_priority(
        integer_t               priority;
 
        if (thread->sched_mode & TH_MODE_REALTIME)
-               priority = BASEPRI_REALTIME;
+               priority = BASEPRI_RTQUEUES;
        else {
                if (thread->importance > MAXPRI)
                        priority = MAXPRI;
@@ -198,15 +209,7 @@ thread_recompute_priority(
                        priority = MINPRI;
        }
 
-       if (thread->depress_priority >= 0)
-               thread->depress_priority = priority;
-       else {
-               thread->priority = priority;
-               compute_priority(thread, TRUE);
-
-               if (thread == current_thread())
-                       ast_on(AST_BLOCK);
-       }
+       set_priority(thread, priority);
 }
 
 void
@@ -231,30 +234,51 @@ thread_task_priority(
        splx(s);
 }
 
+void
+thread_policy_reset(
+       thread_t                thread)
+{
+       if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
+               thread->sched_mode &= ~TH_MODE_REALTIME;
+
+               if (!(thread->sched_mode & TH_MODE_TIMESHARE)) {
+                       thread->sched_mode |= TH_MODE_TIMESHARE;
+
+                       if (thread->state & TH_RUN)
+                               pset_share_incr(thread->processor_set);
+               }
+       }
+       else {
+               thread->safe_mode = 0;
+               thread->sched_mode &= ~TH_MODE_FAILSAFE;
+       }
+
+       thread->importance = 0;
+
+       thread_recompute_priority(thread);
+}
+
 kern_return_t
 thread_policy_get(
-       thread_act_t                    act,
+       thread_t                                thread,
        thread_policy_flavor_t  flavor,
        thread_policy_t                 policy_info,
        mach_msg_type_number_t  *count,
        boolean_t                               *get_default)
 {
        kern_return_t                   result = KERN_SUCCESS;
-       thread_t                                thread;
        spl_t                                   s;
 
-       if (act == THR_ACT_NULL)
+       if (thread == THREAD_NULL)
                return (KERN_INVALID_ARGUMENT);
 
-       thread = act_lock_thread(act);
-       if (!act->active) {
-               act_unlock_thread(act);
+       thread_mtx_lock(thread);
+       if (!thread->active) {
+               thread_mtx_unlock(thread);
 
                return (KERN_TERMINATED);
        }
 
-       assert(thread != THREAD_NULL);
-
        switch (flavor) {
 
        case THREAD_EXTENDED_POLICY:
@@ -359,7 +383,7 @@ thread_policy_get(
                break;
        }
 
-       act_unlock_thread(act);
+       thread_mtx_unlock(thread);
 
        return (result);
 }