]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sync_sema.c
xnu-517.tar.gz
[apple/xnu.git] / osfmk / kern / sync_sema.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
56 static unsigned int semaphore_event;
57 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
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) {
194 wait_queue_wakeup64_all_locked(&semaphore->wait_queue,
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) {
241 kr = wait_queue_wakeup64_thread_locked(
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 */
260 kr = wait_queue_wakeup64_all_locked(
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) {
276 if (wait_queue_wakeup64_one_locked(
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);
563 uint64_t abstime;
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 thread_t self = current_thread();
588
589 wait_semaphore->count = -1; /* we don't keep an actual count */
590 thread_lock(self);
591 (void)wait_queue_assert_wait64_locked(
592 &wait_semaphore->wait_queue,
593 SEMAPHORE_EVENT,
594 THREAD_ABORTSAFE,
595 self);
596 thread_unlock(self);
597 }
598 semaphore_unlock(wait_semaphore);
599 splx(spl_level);
600
601 /*
602 * wait_semaphore is unlocked so we are free to go ahead and
603 * signal the signal_semaphore (if one was provided).
604 */
605 if (signal_semaphore != SEMAPHORE_NULL) {
606 kern_return_t signal_kr;
607
608 /*
609 * lock the signal semaphore reference we got and signal it.
610 * This will NOT block (we cannot block after having asserted
611 * our intention to wait above).
612 */
613 signal_kr = semaphore_signal_internal(signal_semaphore,
614 THR_ACT_NULL,
615 SEMAPHORE_SIGNAL_PREPOST);
616
617 if (signal_kr == KERN_NOT_WAITING)
618 signal_kr = KERN_SUCCESS;
619 else if (signal_kr == KERN_TERMINATED) {
620 /*
621 * Uh!Oh! The semaphore we were to signal died.
622 * We have to get ourselves out of the wait in
623 * case we get stuck here forever (it is assumed
624 * that the semaphore we were posting is gating
625 * the decision by someone else to post the
626 * semaphore we are waiting on). People will
627 * discover the other dead semaphore soon enough.
628 * If we got out of the wait cleanly (someone
629 * already posted a wakeup to us) then return that
630 * (most important) result. Otherwise,
631 * return the KERN_TERMINATED status.
632 */
633 thread_t self = current_thread();
634
635 clear_wait(self, THREAD_INTERRUPTED);
636 kr = semaphore_convert_wait_result(self->wait_result);
637 if (kr == KERN_ABORTED)
638 kr = KERN_TERMINATED;
639 }
640 }
641
642 /*
643 * If we had an error, or we didn't really need to wait we can
644 * return now that we have signalled the signal semaphore.
645 */
646 if (kr != KERN_ALREADY_WAITING)
647 return kr;
648
649 /*
650 * If it is a timed wait, go ahead and set up the timer.
651 */
652 if (wait_timep != (mach_timespec_t *)0) {
653 nanoseconds_to_absolutetime((uint64_t)wait_timep->tv_sec *
654 NSEC_PER_SEC + wait_timep->tv_nsec, &abstime);
655 clock_absolutetime_interval_to_deadline(abstime, &abstime);
656 thread_set_timer_deadline(abstime);
657 continuation = semaphore_timedwait_continue;
658 } else {
659 continuation = semaphore_wait_continue;
660 }
661
662 /*
663 * Now, we can block. If the caller supplied a continuation
664 * pointer of his own for after the block, block with the
665 * appropriate semaphore continuation. Thiswill gather the
666 * semaphore results, release references on the semaphore(s),
667 * and then call the caller's continuation.
668 */
669 if (caller_cont) {
670 thread_t self = current_thread();
671
672 self->sth_continuation = caller_cont;
673 self->sth_waitsemaphore = wait_semaphore;
674 self->sth_signalsemaphore = signal_semaphore;
675 wait_result = thread_block(continuation);
676 } else {
677 wait_result = thread_block(THREAD_CONTINUE_NULL);
678 }
679
680 /*
681 * If we came back here (not continuation case) cancel
682 * any pending timers, convert the wait result to an
683 * appropriate semaphore return value, and then return
684 * that.
685 */
686 if (wait_timep && (wait_result != THREAD_TIMED_OUT))
687 thread_cancel_timer();
688
689 return (semaphore_convert_wait_result(wait_result));
690 }
691
692
693 /*
694 * Routine: semaphore_wait
695 *
696 * Traditional (non-continuation) interface presented to
697 * in-kernel clients to wait on a semaphore.
698 */
699 kern_return_t
700 semaphore_wait(
701 semaphore_t semaphore)
702 {
703
704 if (semaphore == SEMAPHORE_NULL)
705 return KERN_INVALID_ARGUMENT;
706
707 return(semaphore_wait_internal(semaphore,
708 SEMAPHORE_NULL,
709 (mach_timespec_t *)0,
710 (void (*)(kern_return_t))0));
711 }
712
713 /*
714 * Trap: semaphore_wait_trap
715 *
716 * Trap version of semaphore wait. Called on behalf of user-level
717 * clients.
718 */
719 kern_return_t
720 semaphore_wait_trap(
721 mach_port_name_t name)
722 {
723 semaphore_t semaphore;
724 kern_return_t kr;
725
726 kr = port_name_to_semaphore(name, &semaphore);
727 if (kr != KERN_SUCCESS)
728 return kr;
729
730 kr = semaphore_wait_internal(semaphore,
731 SEMAPHORE_NULL,
732 (mach_timespec_t *)0,
733 thread_syscall_return);
734 semaphore_dereference(semaphore);
735 return kr;
736 }
737
738 /*
739 * Routine: semaphore_timedwait
740 *
741 * Traditional (non-continuation) interface presented to
742 * in-kernel clients to wait on a semaphore with a timeout.
743 *
744 * A timeout of {0,0} is considered non-blocking.
745 */
746 kern_return_t
747 semaphore_timedwait(
748 semaphore_t semaphore,
749 mach_timespec_t wait_time)
750 {
751 if (semaphore == SEMAPHORE_NULL)
752 return KERN_INVALID_ARGUMENT;
753
754 if(BAD_MACH_TIMESPEC(&wait_time))
755 return KERN_INVALID_VALUE;
756
757 return (semaphore_wait_internal(semaphore,
758 SEMAPHORE_NULL,
759 &wait_time,
760 (void(*)(kern_return_t))0));
761
762 }
763
764 /*
765 * Trap: semaphore_timedwait_trap
766 *
767 * Trap version of a semaphore_timedwait. The timeout parameter
768 * is passed in two distinct parts and re-assembled on this side
769 * of the trap interface (to accomodate calling conventions that
770 * pass structures as pointers instead of inline in registers without
771 * having to add a copyin).
772 *
773 * A timeout of {0,0} is considered non-blocking.
774 */
775 kern_return_t
776 semaphore_timedwait_trap(
777 mach_port_name_t name,
778 unsigned int sec,
779 clock_res_t nsec)
780 {
781 semaphore_t semaphore;
782 mach_timespec_t wait_time;
783 kern_return_t kr;
784
785 wait_time.tv_sec = sec;
786 wait_time.tv_nsec = nsec;
787 if(BAD_MACH_TIMESPEC(&wait_time))
788 return KERN_INVALID_VALUE;
789
790 kr = port_name_to_semaphore(name, &semaphore);
791 if (kr != KERN_SUCCESS)
792 return kr;
793
794 kr = semaphore_wait_internal(semaphore,
795 SEMAPHORE_NULL,
796 &wait_time,
797 thread_syscall_return);
798 semaphore_dereference(semaphore);
799 return kr;
800 }
801
802 /*
803 * Routine: semaphore_wait_signal
804 *
805 * Atomically register a wait on a semaphore and THEN signal
806 * another. This is the in-kernel entry point that does not
807 * block at a continuation and does not free a signal_semaphore
808 * reference.
809 */
810 kern_return_t
811 semaphore_wait_signal(
812 semaphore_t wait_semaphore,
813 semaphore_t signal_semaphore)
814 {
815 if (wait_semaphore == SEMAPHORE_NULL)
816 return KERN_INVALID_ARGUMENT;
817
818 return(semaphore_wait_internal(wait_semaphore,
819 signal_semaphore,
820 (mach_timespec_t *)0,
821 (void(*)(kern_return_t))0));
822 }
823
824 /*
825 * Trap: semaphore_wait_signal_trap
826 *
827 * Atomically register a wait on a semaphore and THEN signal
828 * another. This is the trap version from user space.
829 */
830 kern_return_t
831 semaphore_wait_signal_trap(
832 mach_port_name_t wait_name,
833 mach_port_name_t signal_name)
834 {
835 semaphore_t wait_semaphore;
836 semaphore_t signal_semaphore;
837 kern_return_t kr;
838
839 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
840 if (kr != KERN_SUCCESS)
841 return kr;
842
843 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
844 if (kr != KERN_SUCCESS) {
845 semaphore_dereference(signal_semaphore);
846 return kr;
847 }
848
849 kr = semaphore_wait_internal(wait_semaphore,
850 signal_semaphore,
851 (mach_timespec_t *)0,
852 thread_syscall_return);
853
854 semaphore_dereference(wait_semaphore);
855 semaphore_dereference(signal_semaphore);
856 return kr;
857 }
858
859
860 /*
861 * Routine: semaphore_timedwait_signal
862 *
863 * Atomically register a wait on a semaphore and THEN signal
864 * another. This is the in-kernel entry point that does not
865 * block at a continuation.
866 *
867 * A timeout of {0,0} is considered non-blocking.
868 */
869 kern_return_t
870 semaphore_timedwait_signal(
871 semaphore_t wait_semaphore,
872 semaphore_t signal_semaphore,
873 mach_timespec_t wait_time)
874 {
875 if (wait_semaphore == SEMAPHORE_NULL)
876 return KERN_INVALID_ARGUMENT;
877
878 if(BAD_MACH_TIMESPEC(&wait_time))
879 return KERN_INVALID_VALUE;
880
881 return(semaphore_wait_internal(wait_semaphore,
882 signal_semaphore,
883 &wait_time,
884 (void(*)(kern_return_t))0));
885 }
886
887 /*
888 * Trap: semaphore_timedwait_signal_trap
889 *
890 * Atomically register a timed wait on a semaphore and THEN signal
891 * another. This is the trap version from user space.
892 */
893 kern_return_t
894 semaphore_timedwait_signal_trap(
895 mach_port_name_t wait_name,
896 mach_port_name_t signal_name,
897 unsigned int sec,
898 clock_res_t nsec)
899 {
900 semaphore_t wait_semaphore;
901 semaphore_t signal_semaphore;
902 mach_timespec_t wait_time;
903 kern_return_t kr;
904
905 wait_time.tv_sec = sec;
906 wait_time.tv_nsec = nsec;
907 if(BAD_MACH_TIMESPEC(&wait_time))
908 return KERN_INVALID_VALUE;
909
910 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
911 if (kr != KERN_SUCCESS)
912 return kr;
913
914 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
915 if (kr != KERN_SUCCESS) {
916 semaphore_dereference(signal_semaphore);
917 return kr;
918 }
919
920 kr = semaphore_wait_internal(wait_semaphore,
921 signal_semaphore,
922 &wait_time,
923 thread_syscall_return);
924
925 semaphore_dereference(wait_semaphore);
926 semaphore_dereference(signal_semaphore);
927 return kr;
928 }
929
930
931 /*
932 * Routine: semaphore_reference
933 *
934 * Take out a reference on a semaphore. This keeps the data structure
935 * in existence (but the semaphore may be deactivated).
936 */
937 void
938 semaphore_reference(
939 semaphore_t semaphore)
940 {
941 spl_t spl_level;
942
943 spl_level = splsched();
944 semaphore_lock(semaphore);
945
946 semaphore->ref_count++;
947
948 semaphore_unlock(semaphore);
949 splx(spl_level);
950 }
951
952 /*
953 * Routine: semaphore_dereference
954 *
955 * Release a reference on a semaphore. If this is the last reference,
956 * the semaphore data structure is deallocated.
957 */
958 void
959 semaphore_dereference(
960 semaphore_t semaphore)
961 {
962 int ref_count;
963 spl_t spl_level;
964
965 if (semaphore != NULL) {
966 spl_level = splsched();
967 semaphore_lock(semaphore);
968
969 ref_count = --(semaphore->ref_count);
970
971 semaphore_unlock(semaphore);
972 splx(spl_level);
973
974 if (ref_count == 0) {
975 assert(wait_queue_empty(&semaphore->wait_queue));
976 zfree(semaphore_zone, (vm_offset_t)semaphore);
977 }
978 }
979 }