2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
27 * 15 October 2000 (debo)
31 #include <kern/thread.h>
36 thread_policy_flavor_t flavor
,
37 thread_policy_t policy_info
,
38 mach_msg_type_number_t count
)
40 kern_return_t result
= KERN_SUCCESS
;
45 if (act
== THR_ACT_NULL
)
46 return (KERN_INVALID_ARGUMENT
);
54 thread
= act_lock_thread(act
);
56 act_unlock_thread(act
);
59 return (KERN_TERMINATED
);
62 if (thread
== THREAD_NULL
) {
63 act_unlock_thread(act
);
66 return (KERN_NOT_SUPPORTED
);
69 #define thread_priority_set(thread, pri) \
71 if ((thread)->depress_priority >= 0) \
72 (thread)->depress_priority = (pri); \
74 (thread)->priority = (pri); \
75 compute_priority((thread), TRUE); \
77 if ((thread) == current_thread()) \
84 case THREAD_STANDARD_POLICY
:
91 thread
->sched_mode
&=~ TH_MODE_REALTIME
;
93 thread
->policy
= POLICY_TIMESHARE
;
95 if (thread
->importance
> MAXPRI
)
98 if (thread
->importance
< -MAXPRI
)
101 priority
= thread
->importance
;
103 priority
+= task
->priority
;
105 if (priority
> thread
->max_priority
)
106 priority
= thread
->max_priority
;
108 if (priority
< MINPRI
)
111 thread_priority_set(thread
, priority
);
113 thread_unlock(thread
);
119 case THREAD_TIME_CONSTRAINT_POLICY
:
121 thread_time_constraint_policy_t info
;
123 if (count
< THREAD_TIME_CONSTRAINT_POLICY_COUNT
) {
124 result
= KERN_INVALID_ARGUMENT
;
128 info
= (thread_time_constraint_policy_t
)policy_info
;
133 thread
->sched_mode
|= TH_MODE_REALTIME
;
135 thread
->realtime
.period
= info
->period
;
136 thread
->realtime
.computation
= info
->computation
;
137 thread
->realtime
.constraint
= info
->constraint
;
138 thread
->realtime
.preemptible
= info
->preemptible
;
140 thread
->policy
= POLICY_RR
;
142 thread_priority_set(thread
, BASEPRI_REALTIME
);
144 thread_unlock(thread
);
150 case THREAD_PRECEDENCE_POLICY
:
152 thread_precedence_policy_t info
;
154 if (count
< THREAD_PRECEDENCE_POLICY_COUNT
) {
155 result
= KERN_INVALID_ARGUMENT
;
159 info
= (thread_precedence_policy_t
)policy_info
;
164 thread
->importance
= info
->importance
;
166 if (!(thread
->sched_mode
& TH_MODE_REALTIME
)) {
169 if (thread
->importance
> MAXPRI
)
172 if (thread
->importance
< -MAXPRI
)
175 priority
= thread
->importance
;
177 priority
+= task
->priority
;
179 if (priority
> thread
->max_priority
)
180 priority
= thread
->max_priority
;
182 if (priority
< MINPRI
)
185 thread_priority_set(thread
, priority
);
188 thread_unlock(thread
);
195 result
= KERN_INVALID_ARGUMENT
;
199 act_unlock_thread(act
);
209 thread_policy_flavor_t flavor
,
210 thread_policy_t policy_info
,
211 mach_msg_type_number_t
*count
,
212 boolean_t
*get_default
)
214 kern_return_t result
= KERN_SUCCESS
;
218 if (act
== THR_ACT_NULL
)
219 return (KERN_INVALID_ARGUMENT
);
221 thread
= act_lock_thread(act
);
223 act_unlock_thread(act
);
225 return (KERN_TERMINATED
);
228 if (thread
== THREAD_NULL
) {
229 act_unlock_thread(act
);
231 return (KERN_NOT_SUPPORTED
);
236 case THREAD_STANDARD_POLICY
:
240 if (thread
->sched_mode
& TH_MODE_REALTIME
)
243 thread_unlock(thread
);
247 case THREAD_TIME_CONSTRAINT_POLICY
:
249 thread_time_constraint_policy_t info
;
251 if (*count
< THREAD_TIME_CONSTRAINT_POLICY_COUNT
) {
252 result
= KERN_INVALID_ARGUMENT
;
256 info
= (thread_time_constraint_policy_t
)policy_info
;
261 if ((thread
->sched_mode
& TH_MODE_REALTIME
) && !(*get_default
)) {
262 info
->period
= thread
->realtime
.period
;
263 info
->computation
= thread
->realtime
.computation
;
264 info
->constraint
= thread
->realtime
.constraint
;
265 info
->preemptible
= thread
->realtime
.preemptible
;
268 extern natural_t min_quantum_abstime
;
273 info
->computation
= min_quantum_abstime
/ 2;
274 info
->constraint
= min_quantum_abstime
;
275 info
->preemptible
= TRUE
;
278 thread_unlock(thread
);
284 case THREAD_PRECEDENCE_POLICY
:
286 thread_precedence_policy_t info
;
288 if (*count
< THREAD_PRECEDENCE_POLICY_COUNT
) {
289 result
= KERN_INVALID_ARGUMENT
;
293 info
= (thread_precedence_policy_t
)policy_info
;
296 info
->importance
= 0;
301 info
->importance
= thread
->importance
;
303 thread_unlock(thread
);
311 result
= KERN_INVALID_ARGUMENT
;
315 act_unlock_thread(act
);