2 * Copyright (c) 2000-2004 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@
27 /* The routines in this module are all obsolete */
29 #include <mach/boolean.h>
30 #include <mach/thread_switch.h>
31 #include <ipc/ipc_port.h>
32 #include <ipc/ipc_space.h>
33 #include <kern/ipc_kobject.h>
34 #include <kern/processor.h>
35 #include <kern/sched.h>
36 #include <kern/sched_prim.h>
38 #include <kern/task.h>
39 #include <kern/thread.h>
40 #include <mach/policy.h>
42 #include <kern/syscall_subr.h>
43 #include <mach/mach_host_server.h>
44 #include <mach/mach_syscalls.h>
46 #include <kern/misc_protos.h>
48 #include <kern/sched.h>
49 #include <kern/sched_prim.h>
50 #include <kern/assert.h>
51 #include <kern/thread.h>
52 #include <mach/mach_host_server.h>
53 #include <mach/thread_act_server.h>
54 #include <mach/host_priv_server.h>
57 * thread_policy_common:
59 * Set scheduling policy & priority for thread.
69 if ( thread
== THREAD_NULL
||
70 invalid_policy(policy
) )
71 return(KERN_INVALID_ARGUMENT
);
76 if ( !(thread
->sched_mode
& TH_MODE_REALTIME
) &&
77 !(thread
->safe_mode
& TH_MODE_REALTIME
) ) {
78 if (!(thread
->sched_mode
& TH_MODE_FAILSAFE
)) {
79 integer_t oldmode
= (thread
->sched_mode
& TH_MODE_TIMESHARE
);
81 if (policy
== POLICY_TIMESHARE
&& !oldmode
) {
82 thread
->sched_mode
|= TH_MODE_TIMESHARE
;
84 if (thread
->state
& TH_RUN
)
85 pset_share_incr(thread
->processor_set
);
88 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
89 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
91 if (thread
->state
& TH_RUN
)
92 pset_share_decr(thread
->processor_set
);
96 if (policy
== POLICY_TIMESHARE
)
97 thread
->safe_mode
|= TH_MODE_TIMESHARE
;
99 thread
->safe_mode
&= ~TH_MODE_TIMESHARE
;
102 if (priority
>= thread
->max_priority
)
103 priority
= thread
->max_priority
- thread
->task_priority
;
105 if (priority
>= MINPRI_KERNEL
)
106 priority
-= MINPRI_KERNEL
;
108 if (priority
>= MINPRI_RESERVED
)
109 priority
-= MINPRI_RESERVED
;
111 priority
-= BASEPRI_DEFAULT
;
113 priority
+= thread
->task_priority
;
115 if (priority
> thread
->max_priority
)
116 priority
= thread
->max_priority
;
118 if (priority
< MINPRI
)
121 thread
->importance
= priority
- thread
->task_priority
;
123 set_priority(thread
, priority
);
126 thread_unlock(thread
);
129 return (KERN_SUCCESS
);
135 * Set scheduling policy and parameters, both base and limit, for
136 * the given thread. Policy can be any policy implemented by the
137 * processor set, whether enabled or not.
142 processor_set_t pset
,
145 mach_msg_type_number_t base_count
,
146 policy_limit_t limit
,
147 mach_msg_type_number_t limit_count
)
150 kern_return_t result
= KERN_SUCCESS
;
152 if ( thread
== THREAD_NULL
||
153 pset
== PROCESSOR_SET_NULL
)
154 return (KERN_INVALID_ARGUMENT
);
156 thread_mtx_lock(thread
);
158 if (pset
!= thread
->processor_set
) {
159 thread_mtx_unlock(thread
);
161 return (KERN_FAILURE
);
168 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
169 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
171 if ( base_count
!= POLICY_RR_BASE_COUNT
||
172 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
173 result
= KERN_INVALID_ARGUMENT
;
177 bas
= rr_base
->base_priority
;
178 max
= rr_limit
->max_priority
;
179 if (invalid_pri(bas
) || invalid_pri(max
)) {
180 result
= KERN_INVALID_ARGUMENT
;
189 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
190 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
192 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
193 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
194 result
= KERN_INVALID_ARGUMENT
;
198 bas
= fifo_base
->base_priority
;
199 max
= fifo_limit
->max_priority
;
200 if (invalid_pri(bas
) || invalid_pri(max
)) {
201 result
= KERN_INVALID_ARGUMENT
;
208 case POLICY_TIMESHARE
:
210 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
211 policy_timeshare_limit_t ts_limit
=
212 (policy_timeshare_limit_t
) limit
;
214 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
215 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
216 result
= KERN_INVALID_ARGUMENT
;
220 bas
= ts_base
->base_priority
;
221 max
= ts_limit
->max_priority
;
222 if (invalid_pri(bas
) || invalid_pri(max
)) {
223 result
= KERN_INVALID_ARGUMENT
;
231 result
= KERN_INVALID_POLICY
;
234 if (result
!= KERN_SUCCESS
) {
235 thread_mtx_unlock(thread
);
240 result
= thread_policy_common(thread
, policy
, bas
);
242 thread_mtx_unlock(thread
);
251 * Set scheduling policy and parameters, both base and limit, for
252 * the given thread. Policy must be a policy which is enabled for the
253 * processor set. Change contained threads if requested.
260 mach_msg_type_number_t count
,
263 kern_return_t result
= KERN_SUCCESS
;
264 processor_set_t pset
;
265 policy_limit_t limit
;
267 policy_rr_limit_data_t rr_limit
;
268 policy_fifo_limit_data_t fifo_limit
;
269 policy_timeshare_limit_data_t ts_limit
;
271 if (thread
== THREAD_NULL
)
272 return (KERN_INVALID_ARGUMENT
);
274 thread_mtx_lock(thread
);
276 pset
= thread
->processor_set
;
277 if (pset
== PROCESSOR_SET_NULL
) {
278 thread_mtx_unlock(thread
);
280 return (KERN_INVALID_ARGUMENT
);
283 if ( invalid_policy(policy
) ||
284 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
285 thread_mtx_unlock(thread
);
287 return (KERN_INVALID_POLICY
);
292 * Set scheduling limits to base priority.
298 policy_rr_base_t rr_base
;
300 if (count
!= POLICY_RR_BASE_COUNT
) {
301 result
= KERN_INVALID_ARGUMENT
;
305 limcount
= POLICY_RR_LIMIT_COUNT
;
306 rr_base
= (policy_rr_base_t
) base
;
307 rr_limit
.max_priority
= rr_base
->base_priority
;
308 limit
= (policy_limit_t
) &rr_limit
;
315 policy_fifo_base_t fifo_base
;
317 if (count
!= POLICY_FIFO_BASE_COUNT
) {
318 result
= KERN_INVALID_ARGUMENT
;
322 limcount
= POLICY_FIFO_LIMIT_COUNT
;
323 fifo_base
= (policy_fifo_base_t
) base
;
324 fifo_limit
.max_priority
= fifo_base
->base_priority
;
325 limit
= (policy_limit_t
) &fifo_limit
;
330 case POLICY_TIMESHARE
:
332 policy_timeshare_base_t ts_base
;
334 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
335 result
= KERN_INVALID_ARGUMENT
;
339 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
340 ts_base
= (policy_timeshare_base_t
) base
;
341 ts_limit
.max_priority
= ts_base
->base_priority
;
342 limit
= (policy_limit_t
) &ts_limit
;
348 result
= KERN_INVALID_POLICY
;
355 * Use current scheduling limits. Ensure that the
356 * new base priority will not exceed current limits.
362 policy_rr_base_t rr_base
;
364 if (count
!= POLICY_RR_BASE_COUNT
) {
365 result
= KERN_INVALID_ARGUMENT
;
369 limcount
= POLICY_RR_LIMIT_COUNT
;
370 rr_base
= (policy_rr_base_t
) base
;
371 if (rr_base
->base_priority
> thread
->max_priority
) {
372 result
= KERN_POLICY_LIMIT
;
376 rr_limit
.max_priority
= thread
->max_priority
;
377 limit
= (policy_limit_t
) &rr_limit
;
384 policy_fifo_base_t fifo_base
;
386 if (count
!= POLICY_FIFO_BASE_COUNT
) {
387 result
= KERN_INVALID_ARGUMENT
;
391 limcount
= POLICY_FIFO_LIMIT_COUNT
;
392 fifo_base
= (policy_fifo_base_t
) base
;
393 if (fifo_base
->base_priority
> thread
->max_priority
) {
394 result
= KERN_POLICY_LIMIT
;
398 fifo_limit
.max_priority
= thread
->max_priority
;
399 limit
= (policy_limit_t
) &fifo_limit
;
404 case POLICY_TIMESHARE
:
406 policy_timeshare_base_t ts_base
;
408 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
409 result
= KERN_INVALID_ARGUMENT
;
413 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
414 ts_base
= (policy_timeshare_base_t
) base
;
415 if (ts_base
->base_priority
> thread
->max_priority
) {
416 result
= KERN_POLICY_LIMIT
;
420 ts_limit
.max_priority
= thread
->max_priority
;
421 limit
= (policy_limit_t
) &ts_limit
;
427 result
= KERN_INVALID_POLICY
;
433 thread_mtx_unlock(thread
);
435 if (result
== KERN_SUCCESS
)
436 result
= thread_set_policy(thread
, pset
,
437 policy
, base
, count
, limit
, limcount
);