]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/thread_policy.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / osfmk / kern / thread_policy.c
index e9eacf91b5588b1315d7ff92ff3021768f2369fe..45cd6ef24d3ab6d6de876879958a1e9e6ab49e58 100644 (file)
@@ -1,34 +1,38 @@
 /*
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_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.
+ * 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. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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,
  * 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@
- */
-/*
- * Copyright (c) 2000 Apple Computer, Inc.  All rights reserved.
- *
- * HISTORY
- *
- * 15 October 2000 (debo)
- *  Created.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
  */
 
+#include <mach/mach_types.h>
+#include <mach/thread_act_server.h>
+
+#include <kern/kern_types.h>
+#include <kern/processor.h>
 #include <kern/thread.h>
 #include <kern/thread.h>
+#include <kern/affinity.h>
 
 static void
 thread_recompute_priority(
 
 static void
 thread_recompute_priority(
@@ -36,26 +40,29 @@ thread_recompute_priority(
 
 kern_return_t
 thread_policy_set(
 
 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_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;
 
        spl_t                                   s;
 
-       if (act == THR_ACT_NULL)
+       if (thread == THREAD_NULL)
                return (KERN_INVALID_ARGUMENT);
 
                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);
        }
 
 
                return (KERN_TERMINATED);
        }
 
-       assert(thread != THREAD_NULL);
+       if (thread->static_param) {
+               thread_mtx_unlock(thread);
+
+               return (KERN_SUCCESS);
+       }
 
        switch (flavor) {
 
 
        switch (flavor) {
 
@@ -74,13 +81,24 @@ thread_policy_set(
                thread_lock(thread);
 
                if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
                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;
 
                        thread->sched_mode &= ~TH_MODE_REALTIME;
 
-                       if (timeshare)
+                       if (timeshare && !oldmode) {
                                thread->sched_mode |= TH_MODE_TIMESHARE;
                                thread->sched_mode |= TH_MODE_TIMESHARE;
+
+                               if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN)
+                                       sched_share_incr();
+                       }
                        else
                        else
+                       if (!timeshare && oldmode) {
                                thread->sched_mode &= ~TH_MODE_TIMESHARE;
 
                                thread->sched_mode &= ~TH_MODE_TIMESHARE;
 
+                               if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN)
+                                       sched_share_decr();
+                       }
+
                        thread_recompute_priority(thread);
                }
                else {
                        thread_recompute_priority(thread);
                }
                else {
@@ -108,7 +126,8 @@ thread_policy_set(
                }
 
                info = (thread_time_constraint_policy_t)policy_info;
                }
 
                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;
                                info->computation < min_rt_quantum              ) {
                        result = KERN_INVALID_ARGUMENT;
                        break;
@@ -123,7 +142,12 @@ thread_policy_set(
                thread->realtime.preemptible = info->preemptible;
 
                if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
                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|TH_IDLE)) == TH_RUN)
+                                       sched_share_decr();
+                       }
                        thread->sched_mode |= TH_MODE_REALTIME;
                        thread_recompute_priority(thread);
                }
                        thread->sched_mode |= TH_MODE_REALTIME;
                        thread_recompute_priority(thread);
                }
@@ -162,12 +186,35 @@ thread_policy_set(
                break;
        }
 
                break;
        }
 
+       case THREAD_AFFINITY_POLICY:
+       {
+               thread_affinity_policy_t        info;
+
+               if (!thread_affinity_is_supported()) {
+                       result = KERN_NOT_SUPPORTED;
+                       break;
+               }
+               if (count < THREAD_AFFINITY_POLICY_COUNT) {
+                       result = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
+               info = (thread_affinity_policy_t) policy_info;
+               /*
+                * Unlock the thread mutex here and
+                * return directly after calling thread_affinity_set().
+                * This is necessary for correct lock ordering because
+                * thread_affinity_set() takes the task lock.
+                */
+               thread_mtx_unlock(thread);
+               return thread_affinity_set(thread, info->affinity_tag);
+       }
        default:
                result = KERN_INVALID_ARGUMENT;
                break;
        }
 
        default:
                result = KERN_INVALID_ARGUMENT;
                break;
        }
 
-       act_unlock_thread(act);
+       thread_mtx_unlock(thread);
 
        return (result);
 }
 
        return (result);
 }
@@ -179,7 +226,7 @@ thread_recompute_priority(
        integer_t               priority;
 
        if (thread->sched_mode & TH_MODE_REALTIME)
        integer_t               priority;
 
        if (thread->sched_mode & TH_MODE_REALTIME)
-               priority = BASEPRI_REALTIME;
+               priority = BASEPRI_RTQUEUES;
        else {
                if (thread->importance > MAXPRI)
                        priority = MAXPRI;
        else {
                if (thread->importance > MAXPRI)
                        priority = MAXPRI;
@@ -198,15 +245,7 @@ thread_recompute_priority(
                        priority = MINPRI;
        }
 
                        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
 }
 
 void
@@ -231,30 +270,59 @@ thread_task_priority(
        splx(s);
 }
 
        splx(s);
 }
 
+void
+thread_policy_reset(
+       thread_t                thread)
+{
+       spl_t           s;
+
+       s = splsched();
+       thread_lock(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|TH_IDLE)) == TH_RUN)
+                               sched_share_incr();
+               }
+       }
+       else {
+               thread->safe_mode = 0;
+               thread->sched_mode &= ~TH_MODE_FAILSAFE;
+       }
+
+       thread->importance = 0;
+
+       thread_recompute_priority(thread);
+
+       thread_unlock(thread);
+       splx(s);
+}
+
 kern_return_t
 thread_policy_get(
 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_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;
 
        spl_t                                   s;
 
-       if (act == THR_ACT_NULL)
+       if (thread == THREAD_NULL)
                return (KERN_INVALID_ARGUMENT);
 
                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);
        }
 
 
                return (KERN_TERMINATED);
        }
 
-       assert(thread != THREAD_NULL);
-
        switch (flavor) {
 
        case THREAD_EXTENDED_POLICY:
        switch (flavor) {
 
        case THREAD_EXTENDED_POLICY:
@@ -354,12 +422,35 @@ thread_policy_get(
                break;
        }
 
                break;
        }
 
+       case THREAD_AFFINITY_POLICY:
+       {
+               thread_affinity_policy_t                info;
+
+               if (!thread_affinity_is_supported()) {
+                       result = KERN_NOT_SUPPORTED;
+                       break;
+               }
+               if (*count < THREAD_AFFINITY_POLICY_COUNT) {
+                       result = KERN_INVALID_ARGUMENT;
+                       break;
+               }
+
+               info = (thread_affinity_policy_t)policy_info;
+
+               if (!(*get_default))
+                       info->affinity_tag = thread_affinity_get(thread);
+               else
+                       info->affinity_tag = THREAD_AFFINITY_TAG_NULL;
+
+               break;
+       }
+
        default:
                result = KERN_INVALID_ARGUMENT;
                break;
        }
 
        default:
                result = KERN_INVALID_ARGUMENT;
                break;
        }
 
-       act_unlock_thread(act);
+       thread_mtx_unlock(thread);
 
        return (result);
 }
 
        return (result);
 }