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