]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_synch.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / kern_synch.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple 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 90 }
f427ee49 91 OS_FALLTHROUGH;
0a7de745
A
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;
f427ee49 260 ut->uu_pri = (uint16_t)pri;
91447636
A
261 ut->uu_mtx = mtx;
262 (void) thread_block(_sleep_continue);
263 /* NOTREACHED */
264 }
0a7de745 265
91447636 266 wait_result = thread_block(THREAD_CONTINUE_NULL);
1c79356b 267
b0d623f7 268 if (mtx && !dropmutex) {
0a7de745 269 if (spinmutex) {
b0d623f7 270 lck_mtx_lock_spin(mtx);
0a7de745 271 } else {
b0d623f7 272 lck_mtx_lock(mtx);
0a7de745 273 }
b0d623f7 274 }
1c79356b
A
275 }
276
9bccf70c 277 switch (wait_result) {
0a7de745
A
278 case THREAD_TIMED_OUT:
279 error = EWOULDBLOCK;
280 break;
281 case THREAD_AWAKENED:
282 case THREAD_RESTART:
283 /*
284 * Posix implies any signal should be delivered
285 * first, regardless of whether awakened due
286 * to receiving event.
287 */
288 if (catch != THREAD_ABORTSAFE) {
1c79356b 289 break;
0a7de745 290 }
f427ee49 291 OS_FALLTHROUGH;
0a7de745
A
292 case THREAD_INTERRUPTED:
293 if (catch == THREAD_ABORTSAFE) {
294 if (thread_should_abort(self)) {
295 error = EINTR;
296 } else if (SHOULDissignal(p, ut)) {
297 if ((sig = CURSIG(p)) != 0) {
298 if (p->p_sigacts->ps_sigintr & sigmask(sig)) {
1c79356b 299 error = EINTR;
0a7de745
A
300 } else {
301 error = ERESTART;
1c79356b 302 }
0a7de745
A
303 }
304 if (thread_should_abort(self)) {
305 error = EINTR;
306 }
307 } else if ((ut->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
308 /* due to thread cancel */
1c79356b 309 error = EINTR;
0a7de745
A
310 }
311 } else {
312 error = EINTR;
313 }
314 break;
1c79356b
A
315 }
316out:
0a7de745 317 if (error == EINTR || error == ERESTART) {
55e303ae 318 act_set_astbsd(self);
0a7de745 319 }
2d21ac55
A
320 ut->uu_wchan = NULL;
321 ut->uu_wmesg = NULL;
91447636 322
0a7de745 323 return error;
1c79356b
A
324}
325
9bccf70c
A
326int
327sleep(
0a7de745
A
328 void *chan,
329 int pri)
1c79356b 330{
91447636
A
331 return _sleep((caddr_t)chan, pri, (char *)NULL, 0, (int (*)(int))0, (lck_mtx_t *)0);
332}
333
334int
335msleep0(
0a7de745
A
336 void *chan,
337 lck_mtx_t *mtx,
338 int pri,
339 const char *wmsg,
340 int timo,
341 int (*continuation)(int))
91447636 342{
0a7de745 343 u_int64_t abstime = 0;
91447636 344
0a7de745 345 if (timo) {
91447636 346 clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
0a7de745 347 }
91447636
A
348
349 return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, mtx);
350}
351
352int
353msleep(
0a7de745
A
354 void *chan,
355 lck_mtx_t *mtx,
356 int pri,
357 const char *wmsg,
358 struct timespec *ts)
91447636 359{
0a7de745 360 u_int64_t abstime = 0;
91447636
A
361
362 if (ts && (ts->tv_sec || ts->tv_nsec)) {
0a7de745 363 nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &abstime );
91447636
A
364 clock_absolutetime_interval_to_deadline( abstime, &abstime );
365 }
366
367 return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx);
368}
369
370int
371msleep1(
0a7de745
A
372 void *chan,
373 lck_mtx_t *mtx,
374 int pri,
375 const char *wmsg,
376 u_int64_t abstime)
91447636
A
377{
378 return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx);
1c79356b
A
379}
380
9bccf70c
A
381int
382tsleep(
0a7de745
A
383 void *chan,
384 int pri,
385 const char *wmsg,
386 int timo)
9bccf70c 387{
0a7de745 388 u_int64_t abstime = 0;
9bccf70c 389
0a7de745 390 if (timo) {
9bccf70c 391 clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
0a7de745 392 }
91447636 393 return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, (lck_mtx_t *)0);
1c79356b
A
394}
395
9bccf70c
A
396int
397tsleep0(
0a7de745
A
398 void *chan,
399 int pri,
400 const char *wmsg,
401 int timo,
402 int (*continuation)(int))
403{
404 u_int64_t abstime = 0;
405
406 if (timo) {
9bccf70c 407 clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
0a7de745 408 }
91447636 409 return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
0b4e3aa0
A
410}
411
9bccf70c
A
412int
413tsleep1(
0a7de745
A
414 void *chan,
415 int pri,
416 const char *wmsg,
417 u_int64_t abstime,
418 int (*continuation)(int))
419{
91447636 420 return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
1c79356b
A
421}
422
423/*
424 * Wake up all processes sleeping on chan.
425 */
426void
2d21ac55 427wakeup(void *chan)
1c79356b 428{
6d2010ae 429 thread_wakeup((caddr_t)chan);
1c79356b
A
430}
431
432/*
433 * Wake up the first process sleeping on chan.
434 *
435 * Be very sure that the first process is really
436 * the right one to wakeup.
437 */
9bccf70c 438void
2d21ac55 439wakeup_one(caddr_t chan)
1c79356b 440{
6d2010ae 441 thread_wakeup_one((caddr_t)chan);
1c79356b
A
442}
443
444/*
445 * Compute the priority of a process when running in user mode.
446 * Arrange to reschedule if the resulting priority is better
447 * than that of the current process.
448 */
449void
2d21ac55 450resetpriority(struct proc *p)
1c79356b 451{
0b4e3aa0 452 (void)task_importance(p->task, -p->p_nice);
1c79356b 453}
9bccf70c
A
454
455struct loadavg averunnable =
0a7de745 456{ {0, 0, 0}, FSCALE }; /* load average, of runnable procs */
9bccf70c
A
457/*
458 * Constants for averages over 1, 5, and 15 minutes
459 * when sampling at 5 second intervals.
460 */
461static fixpt_t cexp[3] = {
0a7de745
A
462 (fixpt_t)(0.9200444146293232 * FSCALE), /* exp(-1/12) */
463 (fixpt_t)(0.9834714538216174 * FSCALE), /* exp(-1/60) */
464 (fixpt_t)(0.9944598480048967 * FSCALE), /* exp(-1/180) */
9bccf70c
A
465};
466
467void
2d21ac55 468compute_averunnable(void *arg)
9bccf70c 469{
0a7de745
A
470 unsigned int nrun = *(unsigned int *)arg;
471 struct loadavg *avg = &averunnable;
472 int i;
9bccf70c 473
0a7de745
A
474 for (i = 0; i < 3; i++) {
475 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
476 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
477 }
9bccf70c 478}