]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/task_policy.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / kern / task_policy.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
1c79356b 22
91447636
A
23#include <mach/mach_types.h>
24#include <mach/task_server.h>
25
26#include <kern/sched.h>
0b4e3aa0
A
27#include <kern/task.h>
28
29static void
30task_priority(
31 task_t task,
32 integer_t priority,
33 integer_t max_priority);
1c79356b
A
34
35kern_return_t
36task_policy_set(
37 task_t task,
38 task_policy_flavor_t flavor,
39 task_policy_t policy_info,
40 mach_msg_type_number_t count)
41{
0b4e3aa0
A
42 kern_return_t result = KERN_SUCCESS;
43
44 if (task == TASK_NULL || task == kernel_task)
1c79356b
A
45 return (KERN_INVALID_ARGUMENT);
46
47 switch (flavor) {
48
49 case TASK_CATEGORY_POLICY:
50 {
51 task_category_policy_t info = (task_category_policy_t)policy_info;
52
53 if (count < TASK_CATEGORY_POLICY_COUNT)
54 return (KERN_INVALID_ARGUMENT);
55
0b4e3aa0
A
56 task_lock(task);
57
58 if ( info->role == TASK_FOREGROUND_APPLICATION ||
59 info->role == TASK_BACKGROUND_APPLICATION ) {
60 switch (task->role) {
61
62 case TASK_FOREGROUND_APPLICATION:
63 case TASK_BACKGROUND_APPLICATION:
64 case TASK_UNSPECIFIED:
9bccf70c
A
65 task_priority(task,
66 ((info->role == TASK_FOREGROUND_APPLICATION)?
67 BASEPRI_FOREGROUND: BASEPRI_BACKGROUND),
0b4e3aa0
A
68 task->max_priority);
69 task->role = info->role;
70 break;
71
72 case TASK_CONTROL_APPLICATION:
73 case TASK_RENICED:
74 /* fail silently */
75 break;
76
77 default:
78 result = KERN_INVALID_ARGUMENT;
79 break;
80 }
1c79356b 81 }
0b4e3aa0
A
82 else
83 if (info->role == TASK_CONTROL_APPLICATION) {
84 if ( task != current_task() ||
85 task->sec_token.val[0] != 0 )
86 result = KERN_INVALID_ARGUMENT;
87 else {
9bccf70c 88 task_priority(task, BASEPRI_CONTROL, task->max_priority);
0b4e3aa0
A
89 task->role = info->role;
90 }
91 }
92 else
93 if (info->role == TASK_GRAPHICS_SERVER) {
94 if ( task != current_task() ||
95 task->sec_token.val[0] != 0 )
96 result = KERN_INVALID_ARGUMENT;
97 else {
91447636 98 task_priority(task, MAXPRI_RESERVED - 3, MAXPRI_RESERVED);
0b4e3aa0
A
99 task->role = info->role;
100 }
101 }
102 else
103 result = KERN_INVALID_ARGUMENT;
104
105 task_unlock(task);
1c79356b
A
106
107 break;
108 }
109
110 default:
0b4e3aa0
A
111 result = KERN_INVALID_ARGUMENT;
112 break;
113 }
114
115 return (result);
116}
117
118static void
119task_priority(
120 task_t task,
121 integer_t priority,
122 integer_t max_priority)
123{
91447636 124 thread_t thread;
0b4e3aa0
A
125
126 task->max_priority = max_priority;
127
128 if (priority > task->max_priority)
129 priority = task->max_priority;
130 else
131 if (priority < MINPRI)
132 priority = MINPRI;
133
134 task->priority = priority;
135
91447636
A
136 queue_iterate(&task->threads, thread, thread_t, task_threads) {
137 thread_mtx_lock(thread);
0b4e3aa0 138
91447636 139 if (thread->active)
0b4e3aa0
A
140 thread_task_priority(thread, priority, max_priority);
141
91447636 142 thread_mtx_unlock(thread);
0b4e3aa0
A
143 }
144}
145
146kern_return_t
147task_importance(
148 task_t task,
149 integer_t importance)
150{
151 if (task == TASK_NULL || task == kernel_task)
152 return (KERN_INVALID_ARGUMENT);
153
154 task_lock(task);
155
156 if (!task->active) {
157 task_unlock(task);
158
159 return (KERN_TERMINATED);
160 }
161
162 if (task->role >= TASK_CONTROL_APPLICATION) {
163 task_unlock(task);
164
1c79356b
A
165 return (KERN_INVALID_ARGUMENT);
166 }
167
0b4e3aa0
A
168 task_priority(task, importance + BASEPRI_DEFAULT, task->max_priority);
169 task->role = TASK_RENICED;
170
171 task_unlock(task);
172
1c79356b
A
173 return (KERN_SUCCESS);
174}
0b4e3aa0 175
1c79356b
A
176kern_return_t
177task_policy_get(
178 task_t task,
179 task_policy_flavor_t flavor,
180 task_policy_t policy_info,
181 mach_msg_type_number_t *count,
182 boolean_t *get_default)
183{
0b4e3aa0 184 if (task == TASK_NULL || task == kernel_task)
1c79356b
A
185 return (KERN_INVALID_ARGUMENT);
186
187 switch (flavor) {
188
189 case TASK_CATEGORY_POLICY:
190 {
191 task_category_policy_t info = (task_category_policy_t)policy_info;
192
193 if (*count < TASK_CATEGORY_POLICY_COUNT)
194 return (KERN_INVALID_ARGUMENT);
195
0b4e3aa0
A
196 if (*get_default)
197 info->role = TASK_UNSPECIFIED;
198 else {
199 task_lock(task);
200 info->role = task->role;
201 task_unlock(task);
202 }
1c79356b
A
203 break;
204 }
205
206 default:
207 return (KERN_INVALID_ARGUMENT);
208 }
209
210 return (KERN_SUCCESS);
211}