]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Mach Operating System | |
30 | * Copyright (c) 1987 Carnegie-Mellon University | |
31 | * All rights reserved. The CMU software License Agreement specifies | |
32 | * the terms and conditions for use and redistribution. | |
33 | */ | |
34 | ||
35 | #include <sys/param.h> | |
36 | #include <sys/systm.h> | |
91447636 | 37 | #include <sys/proc_internal.h> |
1c79356b | 38 | #include <sys/user.h> |
91447636 | 39 | #include <sys/file_internal.h> |
1c79356b A |
40 | #include <sys/vnode.h> |
41 | #include <sys/kernel.h> | |
1c79356b A |
42 | |
43 | #include <machine/spl.h> | |
44 | ||
45 | #include <kern/queue.h> | |
46 | #include <sys/lock.h> | |
47 | #include <kern/thread.h> | |
9bccf70c | 48 | #include <kern/sched_prim.h> |
1c79356b A |
49 | #include <kern/ast.h> |
50 | ||
51 | #include <kern/cpu_number.h> | |
52 | #include <vm/vm_kern.h> | |
53 | ||
54 | #include <kern/task.h> | |
55 | #include <mach/time_value.h> | |
91447636 A |
56 | #include <kern/lock.h> |
57 | ||
2d21ac55 A |
58 | #include <sys/systm.h> /* for unix_syscall_return() */ |
59 | #include <libkern/OSAtomic.h> | |
60 | ||
61 | extern boolean_t thread_should_abort(thread_t); /* XXX */ | |
62 | extern void compute_averunnable(void *); /* XXX */ | |
63 | ||
1c79356b | 64 | |
9bccf70c A |
65 | |
66 | static void | |
2d21ac55 | 67 | _sleep_continue( __unused void *parameter, wait_result_t wresult) |
1c79356b | 68 | { |
2d21ac55 A |
69 | struct proc *p = current_proc(); |
70 | thread_t self = current_thread(); | |
1c79356b A |
71 | struct uthread * ut; |
72 | int sig, catch; | |
73 | int error = 0; | |
b0d623f7 | 74 | int dropmutex, spinmutex; |
1c79356b | 75 | |
55e303ae | 76 | ut = get_bsdthread_info(self); |
91447636 A |
77 | catch = ut->uu_pri & PCATCH; |
78 | dropmutex = ut->uu_pri & PDROP; | |
b0d623f7 | 79 | spinmutex = ut->uu_pri & PSPIN; |
1c79356b | 80 | |
91447636 | 81 | switch (wresult) { |
1c79356b A |
82 | case THREAD_TIMED_OUT: |
83 | error = EWOULDBLOCK; | |
84 | break; | |
85 | case THREAD_AWAKENED: | |
86 | /* | |
87 | * Posix implies any signal should be delivered | |
88 | * first, regardless of whether awakened due | |
89 | * to receiving event. | |
90 | */ | |
91 | if (!catch) | |
92 | break; | |
93 | /* else fall through */ | |
94 | case THREAD_INTERRUPTED: | |
95 | if (catch) { | |
55e303ae | 96 | if (thread_should_abort(self)) { |
1c79356b A |
97 | error = EINTR; |
98 | } else if (SHOULDissignal(p,ut)) { | |
2d21ac55 | 99 | if ((sig = CURSIG(p)) != 0) { |
1c79356b A |
100 | if (p->p_sigacts->ps_sigintr & sigmask(sig)) |
101 | error = EINTR; | |
102 | else | |
103 | error = ERESTART; | |
104 | } | |
55e303ae | 105 | if (thread_should_abort(self)) { |
1c79356b A |
106 | error = EINTR; |
107 | } | |
91447636 A |
108 | } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) { |
109 | /* due to thread cancel */ | |
110 | error = EINTR; | |
111 | } | |
1c79356b A |
112 | } else |
113 | error = EINTR; | |
114 | break; | |
115 | } | |
116 | ||
9bccf70c | 117 | if (error == EINTR || error == ERESTART) |
55e303ae | 118 | act_set_astbsd(self); |
9bccf70c | 119 | |
b0d623f7 A |
120 | if (ut->uu_mtx && !dropmutex) { |
121 | if (spinmutex) | |
122 | lck_mtx_lock_spin(ut->uu_mtx); | |
123 | else | |
124 | lck_mtx_lock(ut->uu_mtx); | |
125 | } | |
2d21ac55 A |
126 | ut->uu_wchan = NULL; |
127 | ut->uu_wmesg = NULL; | |
128 | ||
1c79356b A |
129 | unix_syscall_return((*ut->uu_continuation)(error)); |
130 | } | |
131 | ||
132 | /* | |
133 | * Give up the processor till a wakeup occurs | |
134 | * on chan, at which time the process | |
135 | * enters the scheduling queue at priority pri. | |
136 | * The most important effect of pri is that when | |
137 | * pri<=PZERO a signal cannot disturb the sleep; | |
138 | * if pri>PZERO signals will be processed. | |
139 | * If pri&PCATCH is set, signals will cause sleep | |
140 | * to return 1, rather than longjmp. | |
141 | * Callers of this routine must be prepared for | |
142 | * premature return, and check that the reason for | |
143 | * sleeping has gone away. | |
91447636 A |
144 | * |
145 | * if msleep was the entry point, than we have a mutex to deal with | |
146 | * | |
147 | * The mutex is unlocked before the caller is blocked, and | |
148 | * relocked before msleep returns unless the priority includes the PDROP | |
149 | * flag... if PDROP is specified, _sleep returns with the mutex unlocked | |
150 | * regardless of whether it actually blocked or not. | |
1c79356b A |
151 | */ |
152 | ||
9bccf70c A |
153 | static int |
154 | _sleep( | |
155 | caddr_t chan, | |
91447636 A |
156 | int pri, |
157 | const char *wmsg, | |
9bccf70c | 158 | u_int64_t abstime, |
91447636 A |
159 | int (*continuation)(int), |
160 | lck_mtx_t *mtx) | |
1c79356b | 161 | { |
2d21ac55 A |
162 | struct proc *p; |
163 | thread_t self = current_thread(); | |
1c79356b | 164 | struct uthread * ut; |
6d2010ae | 165 | int sig, catch; |
91447636 | 166 | int dropmutex = pri & PDROP; |
b0d623f7 | 167 | int spinmutex = pri & PSPIN; |
9bccf70c | 168 | int wait_result; |
1c79356b | 169 | int error = 0; |
1c79356b | 170 | |
55e303ae | 171 | ut = get_bsdthread_info(self); |
91447636 | 172 | |
1c79356b | 173 | p = current_proc(); |
1c79356b | 174 | p->p_priority = pri & PRIMASK; |
2d21ac55 A |
175 | /* It can still block in proc_exit() after the teardown. */ |
176 | if (p->p_stats != NULL) | |
b0d623f7 | 177 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nvcsw); |
6d2010ae A |
178 | |
179 | if (pri & PCATCH) | |
180 | catch = THREAD_ABORTSAFE; | |
181 | else | |
182 | catch = THREAD_UNINT; | |
2d21ac55 A |
183 | |
184 | /* set wait message & channel */ | |
185 | ut->uu_wchan = chan; | |
186 | ut->uu_wmesg = wmsg ? wmsg : "unknown"; | |
91447636 A |
187 | |
188 | if (mtx != NULL && chan != NULL && (thread_continue_t)continuation == THREAD_CONTINUE_NULL) { | |
6d2010ae A |
189 | int flags; |
190 | ||
191 | if (dropmutex) | |
192 | flags = LCK_SLEEP_UNLOCK; | |
193 | else | |
194 | flags = LCK_SLEEP_DEFAULT; | |
195 | ||
196 | if (spinmutex) | |
197 | flags |= LCK_SLEEP_SPIN; | |
91447636 A |
198 | |
199 | if (abstime) | |
6d2010ae | 200 | wait_result = lck_mtx_sleep_deadline(mtx, flags, chan, catch, abstime); |
91447636 | 201 | else |
6d2010ae | 202 | wait_result = lck_mtx_sleep(mtx, flags, chan, catch); |
91447636 A |
203 | } |
204 | else { | |
205 | if (chan != NULL) | |
6d2010ae | 206 | assert_wait_deadline(chan, catch, abstime); |
91447636 A |
207 | if (mtx) |
208 | lck_mtx_unlock(mtx); | |
6d2010ae A |
209 | |
210 | if (catch == THREAD_ABORTSAFE) { | |
91447636 | 211 | if (SHOULDissignal(p,ut)) { |
2d21ac55 | 212 | if ((sig = CURSIG(p)) != 0) { |
91447636 A |
213 | if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE) |
214 | goto block; | |
91447636 A |
215 | if (p->p_sigacts->ps_sigintr & sigmask(sig)) |
216 | error = EINTR; | |
217 | else | |
218 | error = ERESTART; | |
b0d623f7 A |
219 | if (mtx && !dropmutex) { |
220 | if (spinmutex) | |
221 | lck_mtx_lock_spin(mtx); | |
222 | else | |
223 | lck_mtx_lock(mtx); | |
224 | } | |
1c79356b A |
225 | goto out; |
226 | } | |
91447636 A |
227 | } |
228 | if (thread_should_abort(self)) { | |
229 | if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE) | |
230 | goto block; | |
231 | error = EINTR; | |
232 | ||
b0d623f7 A |
233 | if (mtx && !dropmutex) { |
234 | if (spinmutex) | |
235 | lck_mtx_lock_spin(mtx); | |
236 | else | |
237 | lck_mtx_lock(mtx); | |
238 | } | |
1c79356b A |
239 | goto out; |
240 | } | |
91447636 | 241 | } |
1c79356b | 242 | |
55e303ae | 243 | |
91447636 A |
244 | block: |
245 | if ((thread_continue_t)continuation != THREAD_CONTINUE_NULL) { | |
246 | ut->uu_continuation = continuation; | |
247 | ut->uu_pri = pri; | |
248 | ut->uu_timo = abstime? 1: 0; | |
249 | ut->uu_mtx = mtx; | |
250 | (void) thread_block(_sleep_continue); | |
251 | /* NOTREACHED */ | |
252 | } | |
253 | ||
254 | wait_result = thread_block(THREAD_CONTINUE_NULL); | |
1c79356b | 255 | |
b0d623f7 A |
256 | if (mtx && !dropmutex) { |
257 | if (spinmutex) | |
258 | lck_mtx_lock_spin(mtx); | |
259 | else | |
260 | lck_mtx_lock(mtx); | |
261 | } | |
1c79356b A |
262 | } |
263 | ||
9bccf70c | 264 | switch (wait_result) { |
1c79356b A |
265 | case THREAD_TIMED_OUT: |
266 | error = EWOULDBLOCK; | |
267 | break; | |
268 | case THREAD_AWAKENED: | |
269 | /* | |
270 | * Posix implies any signal should be delivered | |
271 | * first, regardless of whether awakened due | |
272 | * to receiving event. | |
273 | */ | |
6d2010ae | 274 | if (catch != THREAD_ABORTSAFE) |
1c79356b A |
275 | break; |
276 | /* else fall through */ | |
277 | case THREAD_INTERRUPTED: | |
6d2010ae | 278 | if (catch == THREAD_ABORTSAFE) { |
55e303ae | 279 | if (thread_should_abort(self)) { |
1c79356b | 280 | error = EINTR; |
91447636 | 281 | } else if (SHOULDissignal(p, ut)) { |
2d21ac55 | 282 | if ((sig = CURSIG(p)) != 0) { |
1c79356b A |
283 | if (p->p_sigacts->ps_sigintr & sigmask(sig)) |
284 | error = EINTR; | |
285 | else | |
286 | error = ERESTART; | |
287 | } | |
55e303ae | 288 | if (thread_should_abort(self)) { |
1c79356b A |
289 | error = EINTR; |
290 | } | |
b0d623f7 A |
291 | } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) { |
292 | /* due to thread cancel */ | |
293 | error = EINTR; | |
294 | } | |
1c79356b A |
295 | } else |
296 | error = EINTR; | |
297 | break; | |
298 | } | |
299 | out: | |
9bccf70c | 300 | if (error == EINTR || error == ERESTART) |
55e303ae | 301 | act_set_astbsd(self); |
2d21ac55 A |
302 | ut->uu_wchan = NULL; |
303 | ut->uu_wmesg = NULL; | |
91447636 | 304 | |
1c79356b A |
305 | return (error); |
306 | } | |
307 | ||
9bccf70c A |
308 | int |
309 | sleep( | |
310 | void *chan, | |
311 | int pri) | |
1c79356b | 312 | { |
91447636 A |
313 | return _sleep((caddr_t)chan, pri, (char *)NULL, 0, (int (*)(int))0, (lck_mtx_t *)0); |
314 | } | |
315 | ||
316 | int | |
317 | msleep0( | |
318 | void *chan, | |
319 | lck_mtx_t *mtx, | |
320 | int pri, | |
321 | const char *wmsg, | |
322 | int timo, | |
323 | int (*continuation)(int)) | |
324 | { | |
325 | u_int64_t abstime = 0; | |
326 | ||
327 | if (timo) | |
328 | clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime); | |
329 | ||
330 | return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, mtx); | |
331 | } | |
332 | ||
333 | int | |
334 | msleep( | |
335 | void *chan, | |
336 | lck_mtx_t *mtx, | |
337 | int pri, | |
338 | const char *wmsg, | |
339 | struct timespec *ts) | |
340 | { | |
341 | u_int64_t abstime = 0; | |
342 | ||
343 | if (ts && (ts->tv_sec || ts->tv_nsec)) { | |
344 | nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &abstime ); | |
345 | clock_absolutetime_interval_to_deadline( abstime, &abstime ); | |
346 | } | |
347 | ||
348 | return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx); | |
349 | } | |
350 | ||
351 | int | |
352 | msleep1( | |
353 | void *chan, | |
354 | lck_mtx_t *mtx, | |
355 | int pri, | |
356 | const char *wmsg, | |
357 | u_int64_t abstime) | |
358 | { | |
359 | return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx); | |
1c79356b A |
360 | } |
361 | ||
9bccf70c A |
362 | int |
363 | tsleep( | |
91447636 | 364 | void *chan, |
9bccf70c | 365 | int pri, |
91447636 | 366 | const char *wmsg, |
9bccf70c A |
367 | int timo) |
368 | { | |
369 | u_int64_t abstime = 0; | |
370 | ||
371 | if (timo) | |
372 | clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime); | |
91447636 | 373 | return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, (lck_mtx_t *)0); |
1c79356b A |
374 | } |
375 | ||
9bccf70c A |
376 | int |
377 | tsleep0( | |
91447636 | 378 | void *chan, |
9bccf70c | 379 | int pri, |
91447636 | 380 | const char *wmsg, |
9bccf70c A |
381 | int timo, |
382 | int (*continuation)(int)) | |
1c79356b | 383 | { |
9bccf70c A |
384 | u_int64_t abstime = 0; |
385 | ||
386 | if (timo) | |
387 | clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime); | |
91447636 | 388 | return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0); |
0b4e3aa0 A |
389 | } |
390 | ||
9bccf70c A |
391 | int |
392 | tsleep1( | |
393 | void *chan, | |
91447636 A |
394 | int pri, |
395 | const char *wmsg, | |
9bccf70c | 396 | u_int64_t abstime, |
91447636 | 397 | int (*continuation)(int)) |
0b4e3aa0 | 398 | { |
91447636 | 399 | return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0); |
1c79356b A |
400 | } |
401 | ||
402 | /* | |
403 | * Wake up all processes sleeping on chan. | |
404 | */ | |
405 | void | |
2d21ac55 | 406 | wakeup(void *chan) |
1c79356b | 407 | { |
6d2010ae | 408 | thread_wakeup((caddr_t)chan); |
1c79356b A |
409 | } |
410 | ||
411 | /* | |
412 | * Wake up the first process sleeping on chan. | |
413 | * | |
414 | * Be very sure that the first process is really | |
415 | * the right one to wakeup. | |
416 | */ | |
9bccf70c | 417 | void |
2d21ac55 | 418 | wakeup_one(caddr_t chan) |
1c79356b | 419 | { |
6d2010ae | 420 | thread_wakeup_one((caddr_t)chan); |
1c79356b A |
421 | } |
422 | ||
423 | /* | |
424 | * Compute the priority of a process when running in user mode. | |
425 | * Arrange to reschedule if the resulting priority is better | |
426 | * than that of the current process. | |
427 | */ | |
428 | void | |
2d21ac55 | 429 | resetpriority(struct proc *p) |
1c79356b | 430 | { |
0b4e3aa0 | 431 | (void)task_importance(p->task, -p->p_nice); |
1c79356b | 432 | } |
9bccf70c A |
433 | |
434 | struct loadavg averunnable = | |
435 | { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ | |
436 | /* | |
437 | * Constants for averages over 1, 5, and 15 minutes | |
438 | * when sampling at 5 second intervals. | |
439 | */ | |
440 | static fixpt_t cexp[3] = { | |
441 | (fixpt_t)(0.9200444146293232 * FSCALE), /* exp(-1/12) */ | |
442 | (fixpt_t)(0.9834714538216174 * FSCALE), /* exp(-1/60) */ | |
443 | (fixpt_t)(0.9944598480048967 * FSCALE), /* exp(-1/180) */ | |
444 | }; | |
445 | ||
446 | void | |
2d21ac55 | 447 | compute_averunnable(void *arg) |
9bccf70c | 448 | { |
91447636 | 449 | unsigned int nrun = *(unsigned int *)arg; |
9bccf70c | 450 | struct loadavg *avg = &averunnable; |
2d21ac55 | 451 | int i; |
9bccf70c A |
452 | |
453 | for (i = 0; i < 3; i++) | |
454 | avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + | |
455 | nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; | |
456 | } |