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