2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * File: kern/sync_sema.c
34 * Author: Joseph CaraDonna
36 * Contains RT distributed semaphore synchronization services.
39 #include <mach/mach_types.h>
40 #include <mach/mach_traps.h>
41 #include <mach/kern_return.h>
42 #include <mach/semaphore.h>
43 #include <mach/sync_policy.h>
44 #include <mach/task.h>
46 #include <kern/misc_protos.h>
47 #include <kern/sync_sema.h>
49 #include <kern/ipc_kobject.h>
50 #include <kern/ipc_sync.h>
51 #include <kern/ipc_tt.h>
52 #include <kern/thread.h>
53 #include <kern/clock.h>
54 #include <ipc/ipc_port.h>
55 #include <ipc/ipc_space.h>
56 #include <kern/host.h>
57 #include <kern/wait_queue.h>
58 #include <kern/zalloc.h>
59 #include <kern/mach_param.h>
61 static unsigned int semaphore_event
;
62 #define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event)
64 zone_t semaphore_zone
;
65 unsigned int semaphore_max
;
67 /* Forward declarations */
71 semaphore_wait_trap_internal(
72 mach_port_name_t name
,
73 void (*caller_cont
)(kern_return_t
));
76 semaphore_wait_signal_trap_internal(
77 mach_port_name_t wait_name
,
78 mach_port_name_t signal_name
,
79 void (*caller_cont
)(kern_return_t
));
82 semaphore_timedwait_trap_internal(
83 mach_port_name_t name
,
86 void (*caller_cont
)(kern_return_t
));
89 semaphore_timedwait_signal_trap_internal(
90 mach_port_name_t wait_name
,
91 mach_port_name_t signal_name
,
94 void (*caller_cont
)(kern_return_t
));
97 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
100 semaphore_signal_internal(
101 semaphore_t semaphore
,
106 semaphore_convert_wait_result(
110 semaphore_wait_continue(void);
113 semaphore_wait_internal(
114 semaphore_t wait_semaphore
,
115 semaphore_t signal_semaphore
,
118 void (*caller_cont
)(kern_return_t
));
120 static __inline__
uint64_t
127 nanoseconds_to_absolutetime((uint64_t)sec
* NSEC_PER_SEC
+ nsec
, &abstime
);
128 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
134 * ROUTINE: semaphore_init [private]
136 * Initialize the semaphore mechanisms.
137 * Right now, we only need to initialize the semaphore zone.
142 semaphore_zone
= zinit(sizeof(struct semaphore
),
143 semaphore_max
* sizeof(struct semaphore
),
144 sizeof(struct semaphore
),
146 zone_change(semaphore_zone
, Z_NOENCRYPT
, TRUE
);
150 * Routine: semaphore_create
152 * Creates a semaphore.
153 * The port representing the semaphore is returned as a parameter.
158 semaphore_t
*new_semaphore
,
162 semaphore_t s
= SEMAPHORE_NULL
;
166 *new_semaphore
= SEMAPHORE_NULL
;
167 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
)
168 return KERN_INVALID_ARGUMENT
;
170 s
= (semaphore_t
) zalloc (semaphore_zone
);
172 if (s
== SEMAPHORE_NULL
)
173 return KERN_RESOURCE_SHORTAGE
;
175 kret
= wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
176 if (kret
!= KERN_SUCCESS
) {
177 zfree(semaphore_zone
, s
);
182 s
->ref_count
= (task
== kernel_task
) ? 1 : 2;
185 * Create and initialize the semaphore port
187 s
->port
= ipc_port_alloc_kernel();
188 if (s
->port
== IP_NULL
) {
189 zfree(semaphore_zone
, s
);
190 return KERN_RESOURCE_SHORTAGE
;
193 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
196 * Associate the new semaphore with the task by adding
197 * the new semaphore to the task's semaphore list.
199 * Associate the task with the new semaphore by having the
200 * semaphores task pointer point to the owning task's structure.
203 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
204 task
->semaphores_owned
++;
215 * Routine: semaphore_destroy
217 * Destroys a semaphore. This call will only succeed if the
218 * specified task is the SAME task name specified at the semaphore's
221 * All threads currently blocked on the semaphore are awoken. These
222 * threads will return with the KERN_TERMINATED error.
227 semaphore_t semaphore
)
233 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
234 return KERN_INVALID_ARGUMENT
;
240 if (semaphore
->owner
!= task
) {
242 return KERN_INVALID_ARGUMENT
;
244 remqueue((queue_entry_t
) semaphore
);
245 semaphore
->owner
= TASK_NULL
;
246 task
->semaphores_owned
--;
249 spl_level
= splsched();
250 semaphore_lock(semaphore
);
253 * Deactivate semaphore
255 assert(semaphore
->active
);
256 semaphore
->active
= FALSE
;
259 * Wakeup blocked threads
261 old_count
= semaphore
->count
;
262 semaphore
->count
= 0;
265 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
270 semaphore_unlock(semaphore
);
277 * Drop the task's semaphore reference, which in turn deallocates
278 * the semaphore structure if the reference count goes to zero.
280 semaphore_dereference(semaphore
);
285 * Routine: semaphore_signal_internal
287 * Signals the semaphore as direct.
289 * Semaphore is locked.
292 semaphore_signal_internal(
293 semaphore_t semaphore
,
300 spl_level
= splsched();
301 semaphore_lock(semaphore
);
303 if (!semaphore
->active
) {
304 semaphore_unlock(semaphore
);
306 return KERN_TERMINATED
;
309 if (thread
!= THREAD_NULL
) {
310 if (semaphore
->count
< 0) {
311 kr
= wait_queue_wakeup64_thread_locked(
312 &semaphore
->wait_queue
,
318 semaphore_unlock(semaphore
);
319 kr
= KERN_NOT_WAITING
;
325 if (options
& SEMAPHORE_SIGNAL_ALL
) {
326 int old_count
= semaphore
->count
;
329 semaphore
->count
= 0; /* always reset */
330 kr
= wait_queue_wakeup64_all_locked(
331 &semaphore
->wait_queue
,
336 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
338 semaphore_unlock(semaphore
);
345 if (semaphore
->count
< 0) {
346 if (wait_queue_wakeup64_one_locked(
347 &semaphore
->wait_queue
,
350 FALSE
) == KERN_SUCCESS
) {
351 semaphore_unlock(semaphore
);
355 semaphore
->count
= 0; /* all waiters gone */
358 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
362 semaphore_unlock(semaphore
);
364 return KERN_NOT_WAITING
;
368 * Routine: semaphore_signal_thread
370 * If the specified thread is blocked on the semaphore, it is
371 * woken up. If a NULL thread was supplied, then any one
372 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
373 * and the semaphore is unchanged.
376 semaphore_signal_thread(
377 semaphore_t semaphore
,
382 if (semaphore
== SEMAPHORE_NULL
)
383 return KERN_INVALID_ARGUMENT
;
385 ret
= semaphore_signal_internal(semaphore
,
387 SEMAPHORE_OPTION_NONE
);
392 * Routine: semaphore_signal_thread_trap
394 * Trap interface to the semaphore_signal_thread function.
397 semaphore_signal_thread_trap(
398 struct semaphore_signal_thread_trap_args
*args
)
400 mach_port_name_t sema_name
= args
->signal_name
;
401 mach_port_name_t thread_name
= args
->thread_name
;
402 semaphore_t semaphore
;
407 * MACH_PORT_NULL is not an error. It means that we want to
408 * select any one thread that is already waiting, but not to
409 * pre-post the semaphore.
411 if (thread_name
!= MACH_PORT_NULL
) {
412 thread
= port_name_to_thread(thread_name
);
413 if (thread
== THREAD_NULL
)
414 return KERN_INVALID_ARGUMENT
;
416 thread
= THREAD_NULL
;
418 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
419 if (kr
== KERN_SUCCESS
) {
420 kr
= semaphore_signal_internal(semaphore
,
422 SEMAPHORE_OPTION_NONE
);
423 semaphore_dereference(semaphore
);
425 if (thread
!= THREAD_NULL
) {
426 thread_deallocate(thread
);
434 * Routine: semaphore_signal
436 * Traditional (in-kernel client and MIG interface) semaphore
437 * signal routine. Most users will access the trap version.
439 * This interface in not defined to return info about whether
440 * this call found a thread waiting or not. The internal
441 * routines (and future external routines) do. We have to
442 * convert those into plain KERN_SUCCESS returns.
446 semaphore_t semaphore
)
450 if (semaphore
== SEMAPHORE_NULL
)
451 return KERN_INVALID_ARGUMENT
;
453 kr
= semaphore_signal_internal(semaphore
,
455 SEMAPHORE_SIGNAL_PREPOST
);
456 if (kr
== KERN_NOT_WAITING
)
462 * Routine: semaphore_signal_trap
464 * Trap interface to the semaphore_signal function.
467 semaphore_signal_trap(
468 struct semaphore_signal_trap_args
*args
)
470 mach_port_name_t sema_name
= args
->signal_name
;
472 return (semaphore_signal_internal_trap(sema_name
));
476 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
478 semaphore_t semaphore
;
481 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
482 if (kr
== KERN_SUCCESS
) {
483 kr
= semaphore_signal_internal(semaphore
,
485 SEMAPHORE_SIGNAL_PREPOST
);
486 semaphore_dereference(semaphore
);
487 if (kr
== KERN_NOT_WAITING
)
494 * Routine: semaphore_signal_all
496 * Awakens ALL threads currently blocked on the semaphore.
497 * The semaphore count returns to zero.
500 semaphore_signal_all(
501 semaphore_t semaphore
)
505 if (semaphore
== SEMAPHORE_NULL
)
506 return KERN_INVALID_ARGUMENT
;
508 kr
= semaphore_signal_internal(semaphore
,
510 SEMAPHORE_SIGNAL_ALL
);
511 if (kr
== KERN_NOT_WAITING
)
517 * Routine: semaphore_signal_all_trap
519 * Trap interface to the semaphore_signal_all function.
522 semaphore_signal_all_trap(
523 struct semaphore_signal_all_trap_args
*args
)
525 mach_port_name_t sema_name
= args
->signal_name
;
526 semaphore_t semaphore
;
529 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
530 if (kr
== KERN_SUCCESS
) {
531 kr
= semaphore_signal_internal(semaphore
,
533 SEMAPHORE_SIGNAL_ALL
);
534 semaphore_dereference(semaphore
);
535 if (kr
== KERN_NOT_WAITING
)
542 * Routine: semaphore_convert_wait_result
544 * Generate the return code after a semaphore wait/block. It
545 * takes the wait result as an input and coverts that to an
546 * appropriate result.
549 semaphore_convert_wait_result(int wait_result
)
551 switch (wait_result
) {
552 case THREAD_AWAKENED
:
555 case THREAD_TIMED_OUT
:
556 return KERN_OPERATION_TIMED_OUT
;
558 case THREAD_INTERRUPTED
:
562 return KERN_TERMINATED
;
565 panic("semaphore_block\n");
571 * Routine: semaphore_wait_continue
573 * Common continuation routine after waiting on a semphore.
574 * It returns directly to user space.
577 semaphore_wait_continue(void)
579 thread_t self
= current_thread();
580 int wait_result
= self
->wait_result
;
581 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
583 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
584 semaphore_dereference(self
->sth_waitsemaphore
);
585 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
586 semaphore_dereference(self
->sth_signalsemaphore
);
588 assert(caller_cont
!= (void (*)(kern_return_t
))0);
589 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
593 * Routine: semaphore_wait_internal
595 * Decrements the semaphore count by one. If the count is
596 * negative after the decrement, the calling thread blocks
597 * (possibly at a continuation and/or with a timeout).
601 * A reference is held on the signal semaphore.
604 semaphore_wait_internal(
605 semaphore_t wait_semaphore
,
606 semaphore_t signal_semaphore
,
609 void (*caller_cont
)(kern_return_t
))
613 kern_return_t kr
= KERN_ALREADY_WAITING
;
615 spl_level
= splsched();
616 semaphore_lock(wait_semaphore
);
618 if (!wait_semaphore
->active
) {
619 kr
= KERN_TERMINATED
;
620 } else if (wait_semaphore
->count
> 0) {
621 wait_semaphore
->count
--;
623 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
624 kr
= KERN_OPERATION_TIMED_OUT
;
626 thread_t self
= current_thread();
628 wait_semaphore
->count
= -1; /* we don't keep an actual count */
630 (void)wait_queue_assert_wait64_locked(
631 &wait_semaphore
->wait_queue
,
633 THREAD_ABORTSAFE
, deadline
,
637 semaphore_unlock(wait_semaphore
);
641 * wait_semaphore is unlocked so we are free to go ahead and
642 * signal the signal_semaphore (if one was provided).
644 if (signal_semaphore
!= SEMAPHORE_NULL
) {
645 kern_return_t signal_kr
;
648 * lock the signal semaphore reference we got and signal it.
649 * This will NOT block (we cannot block after having asserted
650 * our intention to wait above).
652 signal_kr
= semaphore_signal_internal(signal_semaphore
,
654 SEMAPHORE_SIGNAL_PREPOST
);
656 if (signal_kr
== KERN_NOT_WAITING
)
657 signal_kr
= KERN_SUCCESS
;
658 else if (signal_kr
== KERN_TERMINATED
) {
660 * Uh!Oh! The semaphore we were to signal died.
661 * We have to get ourselves out of the wait in
662 * case we get stuck here forever (it is assumed
663 * that the semaphore we were posting is gating
664 * the decision by someone else to post the
665 * semaphore we are waiting on). People will
666 * discover the other dead semaphore soon enough.
667 * If we got out of the wait cleanly (someone
668 * already posted a wakeup to us) then return that
669 * (most important) result. Otherwise,
670 * return the KERN_TERMINATED status.
672 thread_t self
= current_thread();
674 clear_wait(self
, THREAD_INTERRUPTED
);
675 kr
= semaphore_convert_wait_result(self
->wait_result
);
676 if (kr
== KERN_ABORTED
)
677 kr
= KERN_TERMINATED
;
682 * If we had an error, or we didn't really need to wait we can
683 * return now that we have signalled the signal semaphore.
685 if (kr
!= KERN_ALREADY_WAITING
)
689 * Now, we can block. If the caller supplied a continuation
690 * pointer of his own for after the block, block with the
691 * appropriate semaphore continuation. Thiswill gather the
692 * semaphore results, release references on the semaphore(s),
693 * and then call the caller's continuation.
696 thread_t self
= current_thread();
698 self
->sth_continuation
= caller_cont
;
699 self
->sth_waitsemaphore
= wait_semaphore
;
700 self
->sth_signalsemaphore
= signal_semaphore
;
701 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
704 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
707 return (semaphore_convert_wait_result(wait_result
));
712 * Routine: semaphore_wait
714 * Traditional (non-continuation) interface presented to
715 * in-kernel clients to wait on a semaphore.
719 semaphore_t semaphore
)
722 if (semaphore
== SEMAPHORE_NULL
)
723 return KERN_INVALID_ARGUMENT
;
725 return(semaphore_wait_internal(semaphore
,
727 0ULL, SEMAPHORE_OPTION_NONE
,
728 (void (*)(kern_return_t
))0));
732 semaphore_wait_noblock(
733 semaphore_t semaphore
)
736 if (semaphore
== SEMAPHORE_NULL
)
737 return KERN_INVALID_ARGUMENT
;
739 return(semaphore_wait_internal(semaphore
,
741 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
742 (void (*)(kern_return_t
))0));
746 semaphore_wait_deadline(
747 semaphore_t semaphore
,
751 if (semaphore
== SEMAPHORE_NULL
)
752 return KERN_INVALID_ARGUMENT
;
754 return(semaphore_wait_internal(semaphore
,
756 deadline
, SEMAPHORE_OPTION_NONE
,
757 (void (*)(kern_return_t
))0));
761 * Trap: semaphore_wait_trap
763 * Trap version of semaphore wait. Called on behalf of user-level
769 struct semaphore_wait_trap_args
*args
)
771 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
777 semaphore_wait_trap_internal(
778 mach_port_name_t name
,
779 void (*caller_cont
)(kern_return_t
))
781 semaphore_t semaphore
;
784 kr
= port_name_to_semaphore(name
, &semaphore
);
785 if (kr
== KERN_SUCCESS
) {
786 kr
= semaphore_wait_internal(semaphore
,
788 0ULL, SEMAPHORE_OPTION_NONE
,
790 semaphore_dereference(semaphore
);
796 * Routine: semaphore_timedwait
798 * Traditional (non-continuation) interface presented to
799 * in-kernel clients to wait on a semaphore with a timeout.
801 * A timeout of {0,0} is considered non-blocking.
805 semaphore_t semaphore
,
806 mach_timespec_t wait_time
)
808 int option
= SEMAPHORE_OPTION_NONE
;
809 uint64_t deadline
= 0;
811 if (semaphore
== SEMAPHORE_NULL
)
812 return KERN_INVALID_ARGUMENT
;
814 if(BAD_MACH_TIMESPEC(&wait_time
))
815 return KERN_INVALID_VALUE
;
817 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
818 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
820 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
822 return (semaphore_wait_internal(semaphore
,
825 (void(*)(kern_return_t
))0));
830 * Trap: semaphore_timedwait_trap
832 * Trap version of a semaphore_timedwait. The timeout parameter
833 * is passed in two distinct parts and re-assembled on this side
834 * of the trap interface (to accomodate calling conventions that
835 * pass structures as pointers instead of inline in registers without
836 * having to add a copyin).
838 * A timeout of {0,0} is considered non-blocking.
841 semaphore_timedwait_trap(
842 struct semaphore_timedwait_trap_args
*args
)
845 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
850 semaphore_timedwait_trap_internal(
851 mach_port_name_t name
,
854 void (*caller_cont
)(kern_return_t
))
856 semaphore_t semaphore
;
857 mach_timespec_t wait_time
;
860 wait_time
.tv_sec
= sec
;
861 wait_time
.tv_nsec
= nsec
;
862 if(BAD_MACH_TIMESPEC(&wait_time
))
863 return KERN_INVALID_VALUE
;
865 kr
= port_name_to_semaphore(name
, &semaphore
);
866 if (kr
== KERN_SUCCESS
) {
867 int option
= SEMAPHORE_OPTION_NONE
;
868 uint64_t deadline
= 0;
870 if (sec
== 0 && nsec
== 0)
871 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
873 deadline
= semaphore_deadline(sec
, nsec
);
875 kr
= semaphore_wait_internal(semaphore
,
879 semaphore_dereference(semaphore
);
885 * Routine: semaphore_wait_signal
887 * Atomically register a wait on a semaphore and THEN signal
888 * another. This is the in-kernel entry point that does not
889 * block at a continuation and does not free a signal_semaphore
893 semaphore_wait_signal(
894 semaphore_t wait_semaphore
,
895 semaphore_t signal_semaphore
)
897 if (wait_semaphore
== SEMAPHORE_NULL
)
898 return KERN_INVALID_ARGUMENT
;
900 return(semaphore_wait_internal(wait_semaphore
,
902 0ULL, SEMAPHORE_OPTION_NONE
,
903 (void(*)(kern_return_t
))0));
907 * Trap: semaphore_wait_signal_trap
909 * Atomically register a wait on a semaphore and THEN signal
910 * another. This is the trap version from user space.
913 semaphore_wait_signal_trap(
914 struct semaphore_wait_signal_trap_args
*args
)
916 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
920 semaphore_wait_signal_trap_internal(
921 mach_port_name_t wait_name
,
922 mach_port_name_t signal_name
,
923 void (*caller_cont
)(kern_return_t
))
925 semaphore_t wait_semaphore
;
926 semaphore_t signal_semaphore
;
929 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
930 if (kr
== KERN_SUCCESS
) {
931 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
932 if (kr
== KERN_SUCCESS
) {
933 kr
= semaphore_wait_internal(wait_semaphore
,
935 0ULL, SEMAPHORE_OPTION_NONE
,
937 semaphore_dereference(wait_semaphore
);
939 semaphore_dereference(signal_semaphore
);
946 * Routine: semaphore_timedwait_signal
948 * Atomically register a wait on a semaphore and THEN signal
949 * another. This is the in-kernel entry point that does not
950 * block at a continuation.
952 * A timeout of {0,0} is considered non-blocking.
955 semaphore_timedwait_signal(
956 semaphore_t wait_semaphore
,
957 semaphore_t signal_semaphore
,
958 mach_timespec_t wait_time
)
960 int option
= SEMAPHORE_OPTION_NONE
;
961 uint64_t deadline
= 0;
963 if (wait_semaphore
== SEMAPHORE_NULL
)
964 return KERN_INVALID_ARGUMENT
;
966 if(BAD_MACH_TIMESPEC(&wait_time
))
967 return KERN_INVALID_VALUE
;
969 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
970 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
972 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
974 return(semaphore_wait_internal(wait_semaphore
,
977 (void(*)(kern_return_t
))0));
981 * Trap: semaphore_timedwait_signal_trap
983 * Atomically register a timed wait on a semaphore and THEN signal
984 * another. This is the trap version from user space.
987 semaphore_timedwait_signal_trap(
988 struct semaphore_timedwait_signal_trap_args
*args
)
990 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
994 semaphore_timedwait_signal_trap_internal(
995 mach_port_name_t wait_name
,
996 mach_port_name_t signal_name
,
999 void (*caller_cont
)(kern_return_t
))
1001 semaphore_t wait_semaphore
;
1002 semaphore_t signal_semaphore
;
1003 mach_timespec_t wait_time
;
1006 wait_time
.tv_sec
= sec
;
1007 wait_time
.tv_nsec
= nsec
;
1008 if(BAD_MACH_TIMESPEC(&wait_time
))
1009 return KERN_INVALID_VALUE
;
1011 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1012 if (kr
== KERN_SUCCESS
) {
1013 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1014 if (kr
== KERN_SUCCESS
) {
1015 int option
= SEMAPHORE_OPTION_NONE
;
1016 uint64_t deadline
= 0;
1018 if (sec
== 0 && nsec
== 0)
1019 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1021 deadline
= semaphore_deadline(sec
, nsec
);
1023 kr
= semaphore_wait_internal(wait_semaphore
,
1027 semaphore_dereference(wait_semaphore
);
1029 semaphore_dereference(signal_semaphore
);
1036 * Routine: semaphore_reference
1038 * Take out a reference on a semaphore. This keeps the data structure
1039 * in existence (but the semaphore may be deactivated).
1042 semaphore_reference(
1043 semaphore_t semaphore
)
1045 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1049 * Routine: semaphore_dereference
1051 * Release a reference on a semaphore. If this is the last reference,
1052 * the semaphore data structure is deallocated.
1055 semaphore_dereference(
1056 semaphore_t semaphore
)
1060 if (semaphore
!= NULL
) {
1061 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1063 if (ref_count
== 0) {
1064 assert(wait_queue_empty(&semaphore
->wait_queue
));
1065 ipc_port_dealloc_kernel(semaphore
->port
);
1066 zfree(semaphore_zone
, semaphore
);