]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * @OSF_COPYRIGHT@ | |
27 | * | |
28 | */ | |
29 | /* | |
30 | * File: kern/sync_sema.c | |
31 | * Author: Joseph CaraDonna | |
32 | * | |
33 | * Contains RT distributed semaphore synchronization services. | |
34 | */ | |
35 | ||
36 | #include <mach/mach_types.h> | |
37 | #include <mach/kern_return.h> | |
38 | #include <mach/semaphore.h> | |
39 | #include <mach/sync_policy.h> | |
40 | ||
41 | #include <kern/misc_protos.h> | |
42 | #include <kern/sync_sema.h> | |
43 | #include <kern/spl.h> | |
44 | #include <kern/ipc_kobject.h> | |
45 | #include <kern/ipc_sync.h> | |
46 | #include <kern/ipc_tt.h> | |
47 | #include <kern/thread.h> | |
48 | #include <kern/clock.h> | |
49 | #include <ipc/ipc_port.h> | |
50 | #include <ipc/ipc_space.h> | |
51 | #include <kern/host.h> | |
52 | #include <kern/wait_queue.h> | |
53 | #include <kern/zalloc.h> | |
54 | #include <kern/mach_param.h> | |
55 | ||
9bccf70c A |
56 | static unsigned int semaphore_event; |
57 | #define SEMAPHORE_EVENT ((event64_t)&semaphore_event) | |
1c79356b A |
58 | |
59 | zone_t semaphore_zone; | |
60 | unsigned int semaphore_max = SEMAPHORE_MAX; | |
61 | ||
62 | /* | |
63 | * ROUTINE: semaphore_init [private] | |
64 | * | |
65 | * Initialize the semaphore mechanisms. | |
66 | * Right now, we only need to initialize the semaphore zone. | |
67 | */ | |
68 | void | |
69 | semaphore_init(void) | |
70 | { | |
71 | semaphore_zone = zinit(sizeof(struct semaphore), | |
72 | semaphore_max * sizeof(struct semaphore), | |
73 | sizeof(struct semaphore), | |
74 | "semaphores"); | |
75 | } | |
76 | ||
77 | /* | |
78 | * Routine: semaphore_create | |
79 | * | |
80 | * Creates a semaphore. | |
81 | * The port representing the semaphore is returned as a parameter. | |
82 | */ | |
83 | kern_return_t | |
84 | semaphore_create( | |
85 | task_t task, | |
86 | semaphore_t *new_semaphore, | |
87 | int policy, | |
88 | int value) | |
89 | { | |
90 | semaphore_t s = SEMAPHORE_NULL; | |
91 | ||
92 | ||
93 | ||
94 | if (task == TASK_NULL || value < 0 || policy > SYNC_POLICY_MAX) { | |
95 | *new_semaphore = SEMAPHORE_NULL; | |
96 | return KERN_INVALID_ARGUMENT; | |
97 | } | |
98 | ||
99 | s = (semaphore_t) zalloc (semaphore_zone); | |
100 | ||
101 | if (s == SEMAPHORE_NULL) { | |
102 | *new_semaphore = SEMAPHORE_NULL; | |
103 | return KERN_RESOURCE_SHORTAGE; | |
104 | } | |
105 | ||
106 | wait_queue_init(&s->wait_queue, policy); /* also inits lock */ | |
107 | s->count = value; | |
108 | s->ref_count = 1; | |
109 | ||
110 | /* | |
111 | * Create and initialize the semaphore port | |
112 | */ | |
113 | s->port = ipc_port_alloc_kernel(); | |
114 | if (s->port == IP_NULL) { | |
115 | /* This will deallocate the semaphore */ | |
116 | semaphore_dereference(s); | |
117 | *new_semaphore = SEMAPHORE_NULL; | |
118 | return KERN_RESOURCE_SHORTAGE; | |
119 | } | |
120 | ||
121 | ipc_kobject_set (s->port, (ipc_kobject_t) s, IKOT_SEMAPHORE); | |
122 | ||
123 | /* | |
124 | * Associate the new semaphore with the task by adding | |
125 | * the new semaphore to the task's semaphore list. | |
126 | * | |
127 | * Associate the task with the new semaphore by having the | |
128 | * semaphores task pointer point to the owning task's structure. | |
129 | */ | |
130 | task_lock(task); | |
131 | enqueue_head(&task->semaphore_list, (queue_entry_t) s); | |
132 | task->semaphores_owned++; | |
133 | s->owner = task; | |
134 | s->active = TRUE; | |
135 | task_unlock(task); | |
136 | ||
137 | *new_semaphore = s; | |
138 | ||
139 | return KERN_SUCCESS; | |
140 | } | |
141 | ||
142 | /* | |
143 | * Routine: semaphore_destroy | |
144 | * | |
145 | * Destroys a semaphore. This call will only succeed if the | |
146 | * specified task is the SAME task name specified at the semaphore's | |
147 | * creation. | |
148 | * | |
149 | * All threads currently blocked on the semaphore are awoken. These | |
150 | * threads will return with the KERN_TERMINATED error. | |
151 | */ | |
152 | kern_return_t | |
153 | semaphore_destroy( | |
154 | task_t task, | |
155 | semaphore_t semaphore) | |
156 | { | |
157 | int old_count; | |
158 | thread_t thread; | |
159 | spl_t spl_level; | |
160 | ||
161 | ||
162 | if (task == TASK_NULL || semaphore == SEMAPHORE_NULL) | |
163 | return KERN_INVALID_ARGUMENT; | |
164 | ||
165 | /* | |
166 | * Disown semaphore | |
167 | */ | |
168 | task_lock(task); | |
169 | if (semaphore->owner != task) { | |
170 | task_unlock(task); | |
171 | return KERN_INVALID_ARGUMENT; | |
172 | } | |
173 | remqueue(&task->semaphore_list, (queue_entry_t) semaphore); | |
174 | semaphore->owner = TASK_NULL; | |
175 | task->semaphores_owned--; | |
176 | task_unlock(task); | |
177 | ||
178 | spl_level = splsched(); | |
179 | semaphore_lock(semaphore); | |
180 | ||
181 | /* | |
182 | * Deactivate semaphore | |
183 | */ | |
184 | assert(semaphore->active); | |
185 | semaphore->active = FALSE; | |
186 | ||
187 | /* | |
188 | * Wakeup blocked threads | |
189 | */ | |
190 | old_count = semaphore->count; | |
191 | semaphore->count = 0; | |
192 | ||
193 | if (old_count < 0) { | |
9bccf70c | 194 | wait_queue_wakeup64_all_locked(&semaphore->wait_queue, |
1c79356b A |
195 | SEMAPHORE_EVENT, |
196 | THREAD_RESTART, | |
197 | TRUE); /* unlock? */ | |
198 | } else { | |
199 | semaphore_unlock(semaphore); | |
200 | } | |
201 | splx(spl_level); | |
202 | ||
203 | /* | |
204 | * Deallocate | |
205 | * | |
206 | * Drop the semaphore reference, which in turn deallocates the | |
207 | * semaphore structure if the reference count goes to zero. | |
208 | */ | |
209 | ipc_port_dealloc_kernel(semaphore->port); | |
210 | semaphore_dereference(semaphore); | |
211 | return KERN_SUCCESS; | |
212 | } | |
213 | ||
214 | /* | |
215 | * Routine: semaphore_signal_internal | |
216 | * | |
217 | * Signals the semaphore as direct. | |
218 | * Assumptions: | |
219 | * Semaphore is locked. | |
220 | */ | |
221 | kern_return_t | |
222 | semaphore_signal_internal( | |
223 | semaphore_t semaphore, | |
224 | thread_act_t thread_act, | |
225 | int options) | |
226 | { | |
227 | kern_return_t kr; | |
228 | spl_t spl_level; | |
229 | ||
230 | spl_level = splsched(); | |
231 | semaphore_lock(semaphore); | |
232 | ||
233 | if (!semaphore->active) { | |
234 | semaphore_unlock(semaphore); | |
235 | splx(spl_level); | |
236 | return KERN_TERMINATED; | |
237 | } | |
238 | ||
239 | if (thread_act != THR_ACT_NULL) { | |
240 | if (semaphore->count < 0) { | |
9bccf70c | 241 | kr = wait_queue_wakeup64_thread_locked( |
1c79356b A |
242 | &semaphore->wait_queue, |
243 | SEMAPHORE_EVENT, | |
244 | thread_act->thread, | |
245 | THREAD_AWAKENED, | |
246 | TRUE); /* unlock? */ | |
247 | } else { | |
248 | semaphore_unlock(semaphore); | |
249 | kr = KERN_NOT_WAITING; | |
250 | } | |
251 | splx(spl_level); | |
252 | return kr; | |
253 | } | |
254 | ||
255 | if (options & SEMAPHORE_SIGNAL_ALL) { | |
256 | int old_count = semaphore->count; | |
257 | ||
258 | if (old_count < 0) { | |
259 | semaphore->count = 0; /* always reset */ | |
9bccf70c | 260 | kr = wait_queue_wakeup64_all_locked( |
1c79356b A |
261 | &semaphore->wait_queue, |
262 | SEMAPHORE_EVENT, | |
263 | THREAD_AWAKENED, | |
264 | TRUE); /* unlock? */ | |
265 | } else { | |
266 | if (options & SEMAPHORE_SIGNAL_PREPOST) | |
267 | semaphore->count++; | |
268 | semaphore_unlock(semaphore); | |
269 | kr = KERN_SUCCESS; | |
270 | } | |
271 | splx(spl_level); | |
272 | return kr; | |
273 | } | |
274 | ||
275 | if (semaphore->count < 0) { | |
9bccf70c | 276 | if (wait_queue_wakeup64_one_locked( |
1c79356b A |
277 | &semaphore->wait_queue, |
278 | SEMAPHORE_EVENT, | |
279 | THREAD_AWAKENED, | |
280 | FALSE) == KERN_SUCCESS) { | |
281 | semaphore_unlock(semaphore); | |
282 | splx(spl_level); | |
283 | return KERN_SUCCESS; | |
284 | } else | |
285 | semaphore->count = 0; /* all waiters gone */ | |
286 | } | |
287 | ||
288 | if (options & SEMAPHORE_SIGNAL_PREPOST) { | |
289 | semaphore->count++; | |
290 | } | |
291 | ||
292 | semaphore_unlock(semaphore); | |
293 | splx(spl_level); | |
294 | return KERN_NOT_WAITING; | |
295 | } | |
296 | ||
297 | /* | |
298 | * Routine: semaphore_signal_thread | |
299 | * | |
300 | * If the specified thread_act is blocked on the semaphore, it is | |
301 | * woken up. If a NULL thread_act was supplied, then any one | |
302 | * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING | |
303 | * and the semaphore is unchanged. | |
304 | */ | |
305 | kern_return_t | |
306 | semaphore_signal_thread( | |
307 | semaphore_t semaphore, | |
308 | thread_act_t thread_act) | |
309 | { | |
310 | kern_return_t ret; | |
311 | ||
312 | if (semaphore == SEMAPHORE_NULL) | |
313 | return KERN_INVALID_ARGUMENT; | |
314 | ||
315 | ret = semaphore_signal_internal(semaphore, | |
316 | thread_act, | |
317 | SEMAPHORE_OPTION_NONE); | |
318 | return ret; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Routine: semaphore_signal_thread_trap | |
323 | * | |
324 | * Trap interface to the semaphore_signal_thread function. | |
325 | */ | |
326 | kern_return_t | |
327 | semaphore_signal_thread_trap( | |
328 | mach_port_name_t sema_name, | |
329 | mach_port_name_t thread_name) | |
330 | { | |
331 | ||
332 | semaphore_t semaphore; | |
333 | thread_act_t thread_act; | |
334 | kern_return_t kr; | |
335 | ||
336 | /* | |
337 | * MACH_PORT_NULL is not an error. It means that we want to | |
338 | * select any one thread that is already waiting, but not to | |
339 | * pre-post the semaphore. | |
340 | */ | |
341 | if (thread_name != MACH_PORT_NULL) { | |
342 | thread_act = port_name_to_act(thread_name); | |
343 | if (thread_act == THR_ACT_NULL) | |
344 | return KERN_INVALID_ARGUMENT; | |
345 | } else | |
346 | thread_act = THR_ACT_NULL; | |
347 | ||
348 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
349 | if (kr != KERN_SUCCESS) { | |
350 | act_deallocate(thread_act); | |
351 | return kr; | |
352 | } | |
353 | kr = semaphore_signal_internal(semaphore, | |
354 | thread_act, | |
355 | SEMAPHORE_OPTION_NONE); | |
356 | semaphore_dereference(semaphore); | |
357 | act_deallocate(thread_act); | |
358 | return kr; | |
359 | } | |
360 | ||
361 | ||
362 | ||
363 | /* | |
364 | * Routine: semaphore_signal | |
365 | * | |
366 | * Traditional (in-kernel client and MIG interface) semaphore | |
367 | * signal routine. Most users will access the trap version. | |
368 | * | |
369 | * This interface in not defined to return info about whether | |
370 | * this call found a thread waiting or not. The internal | |
371 | * routines (and future external routines) do. We have to | |
372 | * convert those into plain KERN_SUCCESS returns. | |
373 | */ | |
374 | kern_return_t | |
375 | semaphore_signal( | |
376 | semaphore_t semaphore) | |
377 | { | |
378 | kern_return_t kr; | |
379 | ||
380 | if (semaphore == SEMAPHORE_NULL) | |
381 | return KERN_INVALID_ARGUMENT; | |
382 | ||
383 | kr = semaphore_signal_internal(semaphore, | |
384 | THR_ACT_NULL, | |
385 | SEMAPHORE_SIGNAL_PREPOST); | |
386 | if (kr == KERN_NOT_WAITING) | |
387 | return KERN_SUCCESS; | |
388 | return kr; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Routine: semaphore_signal_trap | |
393 | * | |
394 | * Trap interface to the semaphore_signal function. | |
395 | */ | |
396 | kern_return_t | |
397 | semaphore_signal_trap( | |
398 | mach_port_name_t sema_name) | |
399 | { | |
400 | ||
401 | semaphore_t semaphore; | |
402 | kern_return_t kr; | |
403 | ||
404 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
405 | if (kr != KERN_SUCCESS) { | |
406 | return kr; | |
407 | } | |
408 | kr = semaphore_signal_internal(semaphore, | |
409 | THR_ACT_NULL, | |
410 | SEMAPHORE_SIGNAL_PREPOST); | |
411 | semaphore_dereference(semaphore); | |
412 | if (kr == KERN_NOT_WAITING) | |
413 | return KERN_SUCCESS; | |
414 | return kr; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Routine: semaphore_signal_all | |
419 | * | |
420 | * Awakens ALL threads currently blocked on the semaphore. | |
421 | * The semaphore count returns to zero. | |
422 | */ | |
423 | kern_return_t | |
424 | semaphore_signal_all( | |
425 | semaphore_t semaphore) | |
426 | { | |
427 | kern_return_t kr; | |
428 | ||
429 | if (semaphore == SEMAPHORE_NULL) | |
430 | return KERN_INVALID_ARGUMENT; | |
431 | ||
432 | kr = semaphore_signal_internal(semaphore, | |
433 | THR_ACT_NULL, | |
434 | SEMAPHORE_SIGNAL_ALL); | |
435 | if (kr == KERN_NOT_WAITING) | |
436 | return KERN_SUCCESS; | |
437 | return kr; | |
438 | } | |
439 | ||
440 | /* | |
441 | * Routine: semaphore_signal_all_trap | |
442 | * | |
443 | * Trap interface to the semaphore_signal_all function. | |
444 | */ | |
445 | kern_return_t | |
446 | semaphore_signal_all_trap( | |
447 | mach_port_name_t sema_name) | |
448 | { | |
449 | ||
450 | semaphore_t semaphore; | |
451 | kern_return_t kr; | |
452 | ||
453 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
454 | if (kr != KERN_SUCCESS) { | |
455 | return kr; | |
456 | } | |
457 | kr = semaphore_signal_internal(semaphore, | |
458 | THR_ACT_NULL, | |
459 | SEMAPHORE_SIGNAL_ALL); | |
460 | semaphore_dereference(semaphore); | |
461 | if (kr == KERN_NOT_WAITING) | |
462 | return KERN_SUCCESS; | |
463 | return kr; | |
464 | } | |
465 | ||
466 | /* | |
467 | * Routine: semaphore_convert_wait_result | |
468 | * | |
469 | * Generate the return code after a semaphore wait/block. It | |
470 | * takes the wait result as an input and coverts that to an | |
471 | * appropriate result. | |
472 | */ | |
473 | kern_return_t | |
474 | semaphore_convert_wait_result(int wait_result) | |
475 | { | |
476 | switch (wait_result) { | |
477 | case THREAD_AWAKENED: | |
478 | return KERN_SUCCESS; | |
479 | ||
480 | case THREAD_TIMED_OUT: | |
481 | return KERN_OPERATION_TIMED_OUT; | |
482 | ||
483 | case THREAD_INTERRUPTED: | |
484 | return KERN_ABORTED; | |
485 | ||
486 | case THREAD_RESTART: | |
487 | return KERN_TERMINATED; | |
488 | ||
489 | default: | |
490 | panic("semaphore_block\n"); | |
491 | return KERN_FAILURE; | |
492 | } | |
493 | } | |
494 | ||
495 | /* | |
496 | * Routine: semaphore_wait_continue | |
497 | * | |
498 | * Common continuation routine after waiting on a semphore. | |
499 | * It returns directly to user space. | |
500 | */ | |
501 | void | |
502 | semaphore_wait_continue(void) | |
503 | { | |
504 | thread_t self = current_thread(); | |
505 | int wait_result = self->wait_result; | |
506 | void (*caller_cont)(kern_return_t) = self->sth_continuation; | |
507 | ||
508 | assert(self->sth_waitsemaphore != SEMAPHORE_NULL); | |
509 | semaphore_dereference(self->sth_waitsemaphore); | |
510 | if (self->sth_signalsemaphore != SEMAPHORE_NULL) | |
511 | semaphore_dereference(self->sth_signalsemaphore); | |
512 | ||
513 | assert(caller_cont != (void (*)(kern_return_t))0); | |
514 | (*caller_cont)(semaphore_convert_wait_result(wait_result)); | |
515 | } | |
516 | ||
517 | /* | |
518 | * Routine: semaphore_timedwait_continue | |
519 | * | |
520 | * Common continuation routine after doing a timed wait on a | |
521 | * semaphore. It clears the timer before calling the semaphore | |
522 | * routine saved in the thread struct. | |
523 | */ | |
524 | void | |
525 | semaphore_timedwait_continue(void) | |
526 | { | |
527 | thread_t self = current_thread(); | |
528 | int wait_result = self->wait_result; | |
529 | void (*caller_cont)(kern_return_t) = self->sth_continuation; | |
530 | ||
531 | if (wait_result != THREAD_TIMED_OUT) | |
532 | thread_cancel_timer(); | |
533 | ||
534 | assert(self->sth_waitsemaphore != SEMAPHORE_NULL); | |
535 | semaphore_dereference(self->sth_waitsemaphore); | |
536 | if (self->sth_signalsemaphore != SEMAPHORE_NULL) | |
537 | semaphore_dereference(self->sth_signalsemaphore); | |
538 | ||
539 | assert(caller_cont != (void (*)(kern_return_t))0); | |
540 | (*caller_cont)(semaphore_convert_wait_result(wait_result)); | |
541 | } | |
542 | ||
543 | ||
544 | /* | |
545 | * Routine: semaphore_wait_internal | |
546 | * | |
547 | * Decrements the semaphore count by one. If the count is | |
548 | * negative after the decrement, the calling thread blocks | |
549 | * (possibly at a continuation and/or with a timeout). | |
550 | * | |
551 | * Assumptions: | |
552 | * The reference | |
553 | * A reference is held on the signal semaphore. | |
554 | */ | |
555 | kern_return_t | |
556 | semaphore_wait_internal( | |
557 | semaphore_t wait_semaphore, | |
558 | semaphore_t signal_semaphore, | |
559 | mach_timespec_t *wait_timep, | |
560 | void (*caller_cont)(kern_return_t)) | |
561 | { | |
562 | void (*continuation)(void); | |
0b4e3aa0 | 563 | uint64_t abstime, nsinterval; |
1c79356b A |
564 | boolean_t nonblocking; |
565 | int wait_result; | |
566 | spl_t spl_level; | |
567 | kern_return_t kr = KERN_ALREADY_WAITING; | |
568 | ||
569 | spl_level = splsched(); | |
570 | semaphore_lock(wait_semaphore); | |
571 | ||
572 | /* | |
573 | * Decide if we really have to wait. | |
574 | */ | |
575 | nonblocking = (wait_timep != (mach_timespec_t *)0) ? | |
576 | (wait_timep->tv_sec == 0 && wait_timep->tv_nsec == 0) : | |
577 | FALSE; | |
578 | ||
579 | if (!wait_semaphore->active) { | |
580 | kr = KERN_TERMINATED; | |
581 | } else if (wait_semaphore->count > 0) { | |
582 | wait_semaphore->count--; | |
583 | kr = KERN_SUCCESS; | |
584 | } else if (nonblocking) { | |
585 | kr = KERN_OPERATION_TIMED_OUT; | |
586 | } else { | |
587 | wait_semaphore->count = -1; /* we don't keep an actual count */ | |
9bccf70c A |
588 | (void)wait_queue_assert_wait64_locked( |
589 | &wait_semaphore->wait_queue, | |
590 | SEMAPHORE_EVENT, | |
591 | THREAD_ABORTSAFE, | |
592 | FALSE); /* unlock? */ | |
1c79356b A |
593 | } |
594 | semaphore_unlock(wait_semaphore); | |
595 | splx(spl_level); | |
596 | ||
597 | /* | |
598 | * wait_semaphore is unlocked so we are free to go ahead and | |
599 | * signal the signal_semaphore (if one was provided). | |
600 | */ | |
601 | if (signal_semaphore != SEMAPHORE_NULL) { | |
602 | kern_return_t signal_kr; | |
603 | ||
604 | /* | |
605 | * lock the signal semaphore reference we got and signal it. | |
606 | * This will NOT block (we cannot block after having asserted | |
607 | * our intention to wait above). | |
608 | */ | |
609 | signal_kr = semaphore_signal_internal(signal_semaphore, | |
610 | THR_ACT_NULL, | |
611 | SEMAPHORE_SIGNAL_PREPOST); | |
612 | ||
613 | if (signal_kr == KERN_NOT_WAITING) | |
614 | signal_kr = KERN_SUCCESS; | |
615 | else if (signal_kr == KERN_TERMINATED) { | |
616 | /* | |
617 | * Uh!Oh! The semaphore we were to signal died. | |
618 | * We have to get ourselves out of the wait in | |
619 | * case we get stuck here forever (it is assumed | |
620 | * that the semaphore we were posting is gating | |
621 | * the decision by someone else to post the | |
622 | * semaphore we are waiting on). People will | |
623 | * discover the other dead semaphore soon enough. | |
624 | * If we got out of the wait cleanly (someone | |
625 | * already posted a wakeup to us) then return that | |
626 | * (most important) result. Otherwise, | |
627 | * return the KERN_TERMINATED status. | |
628 | */ | |
629 | thread_t self = current_thread(); | |
630 | ||
631 | clear_wait(self, THREAD_INTERRUPTED); | |
632 | kr = semaphore_convert_wait_result(self->wait_result); | |
633 | if (kr == KERN_ABORTED) | |
634 | kr = KERN_TERMINATED; | |
635 | } | |
636 | } | |
637 | ||
638 | /* | |
639 | * If we had an error, or we didn't really need to wait we can | |
640 | * return now that we have signalled the signal semaphore. | |
641 | */ | |
642 | if (kr != KERN_ALREADY_WAITING) | |
643 | return kr; | |
644 | ||
645 | /* | |
646 | * If it is a timed wait, go ahead and set up the timer. | |
647 | */ | |
648 | if (wait_timep != (mach_timespec_t *)0) { | |
649 | clock_interval_to_absolutetime_interval(wait_timep->tv_sec, | |
650 | NSEC_PER_SEC, | |
651 | &abstime); | |
652 | clock_interval_to_absolutetime_interval(wait_timep->tv_nsec, | |
653 | 1, | |
654 | &nsinterval); | |
0b4e3aa0 | 655 | abstime += nsinterval; |
1c79356b A |
656 | clock_absolutetime_interval_to_deadline(abstime, &abstime); |
657 | thread_set_timer_deadline(abstime); | |
658 | continuation = semaphore_timedwait_continue; | |
659 | } else { | |
660 | continuation = semaphore_wait_continue; | |
661 | } | |
662 | ||
663 | /* | |
664 | * Now, we can block. If the caller supplied a continuation | |
665 | * pointer of his own for after the block, block with the | |
666 | * appropriate semaphore continuation. Thiswill gather the | |
667 | * semaphore results, release references on the semaphore(s), | |
668 | * and then call the caller's continuation. | |
669 | */ | |
670 | if (caller_cont) { | |
671 | thread_t self = current_thread(); | |
672 | ||
673 | self->sth_continuation = caller_cont; | |
674 | self->sth_waitsemaphore = wait_semaphore; | |
675 | self->sth_signalsemaphore = signal_semaphore; | |
676 | wait_result = thread_block(continuation); | |
677 | } else { | |
9bccf70c | 678 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
1c79356b A |
679 | } |
680 | ||
681 | /* | |
682 | * If we came back here (not continuation case) cancel | |
683 | * any pending timers, convert the wait result to an | |
684 | * appropriate semaphore return value, and then return | |
685 | * that. | |
686 | */ | |
687 | if (wait_timep && (wait_result != THREAD_TIMED_OUT)) | |
688 | thread_cancel_timer(); | |
689 | ||
690 | return (semaphore_convert_wait_result(wait_result)); | |
691 | } | |
692 | ||
693 | ||
694 | /* | |
695 | * Routine: semaphore_wait | |
696 | * | |
697 | * Traditional (non-continuation) interface presented to | |
698 | * in-kernel clients to wait on a semaphore. | |
699 | */ | |
700 | kern_return_t | |
701 | semaphore_wait( | |
702 | semaphore_t semaphore) | |
703 | { | |
704 | ||
705 | if (semaphore == SEMAPHORE_NULL) | |
706 | return KERN_INVALID_ARGUMENT; | |
707 | ||
708 | return(semaphore_wait_internal(semaphore, | |
709 | SEMAPHORE_NULL, | |
710 | (mach_timespec_t *)0, | |
711 | (void (*)(kern_return_t))0)); | |
712 | } | |
713 | ||
714 | /* | |
715 | * Trap: semaphore_wait_trap | |
716 | * | |
717 | * Trap version of semaphore wait. Called on behalf of user-level | |
718 | * clients. | |
719 | */ | |
720 | kern_return_t | |
721 | semaphore_wait_trap( | |
722 | mach_port_name_t name) | |
723 | { | |
724 | semaphore_t semaphore; | |
725 | kern_return_t kr; | |
726 | ||
727 | kr = port_name_to_semaphore(name, &semaphore); | |
728 | if (kr != KERN_SUCCESS) | |
729 | return kr; | |
730 | ||
731 | kr = semaphore_wait_internal(semaphore, | |
732 | SEMAPHORE_NULL, | |
733 | (mach_timespec_t *)0, | |
734 | thread_syscall_return); | |
735 | semaphore_dereference(semaphore); | |
736 | return kr; | |
737 | } | |
738 | ||
739 | /* | |
740 | * Routine: semaphore_timedwait | |
741 | * | |
742 | * Traditional (non-continuation) interface presented to | |
743 | * in-kernel clients to wait on a semaphore with a timeout. | |
744 | * | |
745 | * A timeout of {0,0} is considered non-blocking. | |
746 | */ | |
747 | kern_return_t | |
748 | semaphore_timedwait( | |
749 | semaphore_t semaphore, | |
750 | mach_timespec_t wait_time) | |
751 | { | |
752 | if (semaphore == SEMAPHORE_NULL) | |
753 | return KERN_INVALID_ARGUMENT; | |
754 | ||
755 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
756 | return KERN_INVALID_VALUE; | |
757 | ||
758 | return (semaphore_wait_internal(semaphore, | |
759 | SEMAPHORE_NULL, | |
760 | &wait_time, | |
761 | (void(*)(kern_return_t))0)); | |
762 | ||
763 | } | |
764 | ||
765 | /* | |
766 | * Trap: semaphore_timedwait_trap | |
767 | * | |
768 | * Trap version of a semaphore_timedwait. The timeout parameter | |
769 | * is passed in two distinct parts and re-assembled on this side | |
770 | * of the trap interface (to accomodate calling conventions that | |
771 | * pass structures as pointers instead of inline in registers without | |
772 | * having to add a copyin). | |
773 | * | |
774 | * A timeout of {0,0} is considered non-blocking. | |
775 | */ | |
776 | kern_return_t | |
777 | semaphore_timedwait_trap( | |
778 | mach_port_name_t name, | |
779 | unsigned int sec, | |
780 | clock_res_t nsec) | |
781 | { | |
782 | semaphore_t semaphore; | |
783 | mach_timespec_t wait_time; | |
784 | kern_return_t kr; | |
785 | ||
786 | wait_time.tv_sec = sec; | |
787 | wait_time.tv_nsec = nsec; | |
788 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
789 | return KERN_INVALID_VALUE; | |
790 | ||
791 | kr = port_name_to_semaphore(name, &semaphore); | |
792 | if (kr != KERN_SUCCESS) | |
793 | return kr; | |
794 | ||
795 | kr = semaphore_wait_internal(semaphore, | |
796 | SEMAPHORE_NULL, | |
797 | &wait_time, | |
798 | thread_syscall_return); | |
799 | semaphore_dereference(semaphore); | |
800 | return kr; | |
801 | } | |
802 | ||
803 | /* | |
804 | * Routine: semaphore_wait_signal | |
805 | * | |
806 | * Atomically register a wait on a semaphore and THEN signal | |
807 | * another. This is the in-kernel entry point that does not | |
808 | * block at a continuation and does not free a signal_semaphore | |
809 | * reference. | |
810 | */ | |
811 | kern_return_t | |
812 | semaphore_wait_signal( | |
813 | semaphore_t wait_semaphore, | |
814 | semaphore_t signal_semaphore) | |
815 | { | |
816 | if (wait_semaphore == SEMAPHORE_NULL) | |
817 | return KERN_INVALID_ARGUMENT; | |
818 | ||
819 | return(semaphore_wait_internal(wait_semaphore, | |
820 | signal_semaphore, | |
821 | (mach_timespec_t *)0, | |
822 | (void(*)(kern_return_t))0)); | |
823 | } | |
824 | ||
825 | /* | |
826 | * Trap: semaphore_wait_signal_trap | |
827 | * | |
828 | * Atomically register a wait on a semaphore and THEN signal | |
829 | * another. This is the trap version from user space. | |
830 | */ | |
831 | kern_return_t | |
832 | semaphore_wait_signal_trap( | |
833 | mach_port_name_t wait_name, | |
834 | mach_port_name_t signal_name) | |
835 | { | |
836 | semaphore_t wait_semaphore; | |
837 | semaphore_t signal_semaphore; | |
838 | kern_return_t kr; | |
839 | ||
840 | kr = port_name_to_semaphore(signal_name, &signal_semaphore); | |
841 | if (kr != KERN_SUCCESS) | |
842 | return kr; | |
843 | ||
844 | kr = port_name_to_semaphore(wait_name, &wait_semaphore); | |
845 | if (kr != KERN_SUCCESS) { | |
846 | semaphore_dereference(signal_semaphore); | |
847 | return kr; | |
848 | } | |
849 | ||
850 | kr = semaphore_wait_internal(wait_semaphore, | |
851 | signal_semaphore, | |
852 | (mach_timespec_t *)0, | |
853 | thread_syscall_return); | |
854 | ||
855 | semaphore_dereference(wait_semaphore); | |
856 | semaphore_dereference(signal_semaphore); | |
857 | return kr; | |
858 | } | |
859 | ||
860 | ||
861 | /* | |
862 | * Routine: semaphore_timedwait_signal | |
863 | * | |
864 | * Atomically register a wait on a semaphore and THEN signal | |
865 | * another. This is the in-kernel entry point that does not | |
866 | * block at a continuation. | |
867 | * | |
868 | * A timeout of {0,0} is considered non-blocking. | |
869 | */ | |
870 | kern_return_t | |
871 | semaphore_timedwait_signal( | |
872 | semaphore_t wait_semaphore, | |
873 | semaphore_t signal_semaphore, | |
874 | mach_timespec_t wait_time) | |
875 | { | |
876 | if (wait_semaphore == SEMAPHORE_NULL) | |
877 | return KERN_INVALID_ARGUMENT; | |
878 | ||
879 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
880 | return KERN_INVALID_VALUE; | |
881 | ||
882 | return(semaphore_wait_internal(wait_semaphore, | |
883 | signal_semaphore, | |
884 | &wait_time, | |
885 | (void(*)(kern_return_t))0)); | |
886 | } | |
887 | ||
888 | /* | |
889 | * Trap: semaphore_timedwait_signal_trap | |
890 | * | |
891 | * Atomically register a timed wait on a semaphore and THEN signal | |
892 | * another. This is the trap version from user space. | |
893 | */ | |
894 | kern_return_t | |
895 | semaphore_timedwait_signal_trap( | |
896 | mach_port_name_t wait_name, | |
897 | mach_port_name_t signal_name, | |
898 | unsigned int sec, | |
899 | clock_res_t nsec) | |
900 | { | |
901 | semaphore_t wait_semaphore; | |
902 | semaphore_t signal_semaphore; | |
903 | mach_timespec_t wait_time; | |
904 | kern_return_t kr; | |
905 | ||
906 | wait_time.tv_sec = sec; | |
907 | wait_time.tv_nsec = nsec; | |
908 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
909 | return KERN_INVALID_VALUE; | |
910 | ||
911 | kr = port_name_to_semaphore(signal_name, &signal_semaphore); | |
912 | if (kr != KERN_SUCCESS) | |
913 | return kr; | |
914 | ||
915 | kr = port_name_to_semaphore(wait_name, &wait_semaphore); | |
916 | if (kr != KERN_SUCCESS) { | |
917 | semaphore_dereference(signal_semaphore); | |
918 | return kr; | |
919 | } | |
920 | ||
921 | kr = semaphore_wait_internal(wait_semaphore, | |
922 | signal_semaphore, | |
923 | &wait_time, | |
924 | thread_syscall_return); | |
925 | ||
926 | semaphore_dereference(wait_semaphore); | |
927 | semaphore_dereference(signal_semaphore); | |
928 | return kr; | |
929 | } | |
930 | ||
931 | ||
932 | /* | |
933 | * Routine: semaphore_reference | |
934 | * | |
935 | * Take out a reference on a semaphore. This keeps the data structure | |
936 | * in existence (but the semaphore may be deactivated). | |
937 | */ | |
938 | void | |
939 | semaphore_reference( | |
940 | semaphore_t semaphore) | |
941 | { | |
942 | spl_t spl_level; | |
943 | ||
944 | spl_level = splsched(); | |
945 | semaphore_lock(semaphore); | |
946 | ||
947 | semaphore->ref_count++; | |
948 | ||
949 | semaphore_unlock(semaphore); | |
950 | splx(spl_level); | |
951 | } | |
952 | ||
953 | /* | |
954 | * Routine: semaphore_dereference | |
955 | * | |
956 | * Release a reference on a semaphore. If this is the last reference, | |
957 | * the semaphore data structure is deallocated. | |
958 | */ | |
959 | void | |
960 | semaphore_dereference( | |
961 | semaphore_t semaphore) | |
962 | { | |
963 | int ref_count; | |
964 | spl_t spl_level; | |
965 | ||
966 | if (semaphore != NULL) { | |
967 | spl_level = splsched(); | |
968 | semaphore_lock(semaphore); | |
969 | ||
970 | ref_count = --(semaphore->ref_count); | |
971 | ||
972 | semaphore_unlock(semaphore); | |
973 | splx(spl_level); | |
974 | ||
975 | if (ref_count == 0) { | |
976 | assert(wait_queue_empty(&semaphore->wait_queue)); | |
977 | zfree(semaphore_zone, (vm_offset_t)semaphore); | |
978 | } | |
979 | } | |
980 | } |