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