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