2 * Copyright (c) 2000-2008 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
),
149 * Routine: semaphore_create
151 * Creates a semaphore.
152 * The port representing the semaphore is returned as a parameter.
157 semaphore_t
*new_semaphore
,
161 semaphore_t s
= SEMAPHORE_NULL
;
165 *new_semaphore
= SEMAPHORE_NULL
;
166 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
)
167 return KERN_INVALID_ARGUMENT
;
169 s
= (semaphore_t
) zalloc (semaphore_zone
);
171 if (s
== SEMAPHORE_NULL
)
172 return KERN_RESOURCE_SHORTAGE
;
174 kret
= wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
175 if (kret
!= KERN_SUCCESS
) {
176 zfree(semaphore_zone
, s
);
181 s
->ref_count
= (task
== kernel_task
) ? 1 : 2;
184 * Create and initialize the semaphore port
186 s
->port
= ipc_port_alloc_kernel();
187 if (s
->port
== IP_NULL
) {
188 zfree(semaphore_zone
, s
);
189 return KERN_RESOURCE_SHORTAGE
;
192 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
195 * Associate the new semaphore with the task by adding
196 * the new semaphore to the task's semaphore list.
198 * Associate the task with the new semaphore by having the
199 * semaphores task pointer point to the owning task's structure.
202 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
203 task
->semaphores_owned
++;
214 * Routine: semaphore_destroy
216 * Destroys a semaphore. This call will only succeed if the
217 * specified task is the SAME task name specified at the semaphore's
220 * All threads currently blocked on the semaphore are awoken. These
221 * threads will return with the KERN_TERMINATED error.
226 semaphore_t semaphore
)
232 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
233 return KERN_INVALID_ARGUMENT
;
239 if (semaphore
->owner
!= task
) {
241 return KERN_INVALID_ARGUMENT
;
243 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
244 semaphore
->owner
= TASK_NULL
;
245 task
->semaphores_owned
--;
248 spl_level
= splsched();
249 semaphore_lock(semaphore
);
252 * Deactivate semaphore
254 assert(semaphore
->active
);
255 semaphore
->active
= FALSE
;
258 * Wakeup blocked threads
260 old_count
= semaphore
->count
;
261 semaphore
->count
= 0;
264 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
269 semaphore_unlock(semaphore
);
276 * Drop the task's semaphore reference, which in turn deallocates
277 * the semaphore structure if the reference count goes to zero.
279 semaphore_dereference(semaphore
);
284 * Routine: semaphore_signal_internal
286 * Signals the semaphore as direct.
288 * Semaphore is locked.
291 semaphore_signal_internal(
292 semaphore_t semaphore
,
299 spl_level
= splsched();
300 semaphore_lock(semaphore
);
302 if (!semaphore
->active
) {
303 semaphore_unlock(semaphore
);
305 return KERN_TERMINATED
;
308 if (thread
!= THREAD_NULL
) {
309 if (semaphore
->count
< 0) {
310 kr
= wait_queue_wakeup64_thread_locked(
311 &semaphore
->wait_queue
,
317 semaphore_unlock(semaphore
);
318 kr
= KERN_NOT_WAITING
;
324 if (options
& SEMAPHORE_SIGNAL_ALL
) {
325 int old_count
= semaphore
->count
;
328 semaphore
->count
= 0; /* always reset */
329 kr
= wait_queue_wakeup64_all_locked(
330 &semaphore
->wait_queue
,
335 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
337 semaphore_unlock(semaphore
);
344 if (semaphore
->count
< 0) {
345 if (wait_queue_wakeup64_one_locked(
346 &semaphore
->wait_queue
,
349 FALSE
) == KERN_SUCCESS
) {
350 semaphore_unlock(semaphore
);
354 semaphore
->count
= 0; /* all waiters gone */
357 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
361 semaphore_unlock(semaphore
);
363 return KERN_NOT_WAITING
;
367 * Routine: semaphore_signal_thread
369 * If the specified thread is blocked on the semaphore, it is
370 * woken up. If a NULL thread was supplied, then any one
371 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
372 * and the semaphore is unchanged.
375 semaphore_signal_thread(
376 semaphore_t semaphore
,
381 if (semaphore
== SEMAPHORE_NULL
)
382 return KERN_INVALID_ARGUMENT
;
384 ret
= semaphore_signal_internal(semaphore
,
386 SEMAPHORE_OPTION_NONE
);
391 * Routine: semaphore_signal_thread_trap
393 * Trap interface to the semaphore_signal_thread function.
396 semaphore_signal_thread_trap(
397 struct semaphore_signal_thread_trap_args
*args
)
399 mach_port_name_t sema_name
= args
->signal_name
;
400 mach_port_name_t thread_name
= args
->thread_name
;
401 semaphore_t semaphore
;
406 * MACH_PORT_NULL is not an error. It means that we want to
407 * select any one thread that is already waiting, but not to
408 * pre-post the semaphore.
410 if (thread_name
!= MACH_PORT_NULL
) {
411 thread
= port_name_to_thread(thread_name
);
412 if (thread
== THREAD_NULL
)
413 return KERN_INVALID_ARGUMENT
;
415 thread
= THREAD_NULL
;
417 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
418 if (kr
== KERN_SUCCESS
) {
419 kr
= semaphore_signal_internal(semaphore
,
421 SEMAPHORE_OPTION_NONE
);
422 semaphore_dereference(semaphore
);
424 if (thread
!= THREAD_NULL
) {
425 thread_deallocate(thread
);
433 * Routine: semaphore_signal
435 * Traditional (in-kernel client and MIG interface) semaphore
436 * signal routine. Most users will access the trap version.
438 * This interface in not defined to return info about whether
439 * this call found a thread waiting or not. The internal
440 * routines (and future external routines) do. We have to
441 * convert those into plain KERN_SUCCESS returns.
445 semaphore_t semaphore
)
449 if (semaphore
== SEMAPHORE_NULL
)
450 return KERN_INVALID_ARGUMENT
;
452 kr
= semaphore_signal_internal(semaphore
,
454 SEMAPHORE_SIGNAL_PREPOST
);
455 if (kr
== KERN_NOT_WAITING
)
461 * Routine: semaphore_signal_trap
463 * Trap interface to the semaphore_signal function.
466 semaphore_signal_trap(
467 struct semaphore_signal_trap_args
*args
)
469 mach_port_name_t sema_name
= args
->signal_name
;
471 return (semaphore_signal_internal_trap(sema_name
));
475 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
477 semaphore_t semaphore
;
480 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
481 if (kr
== KERN_SUCCESS
) {
482 kr
= semaphore_signal_internal(semaphore
,
484 SEMAPHORE_SIGNAL_PREPOST
);
485 semaphore_dereference(semaphore
);
486 if (kr
== KERN_NOT_WAITING
)
493 * Routine: semaphore_signal_all
495 * Awakens ALL threads currently blocked on the semaphore.
496 * The semaphore count returns to zero.
499 semaphore_signal_all(
500 semaphore_t semaphore
)
504 if (semaphore
== SEMAPHORE_NULL
)
505 return KERN_INVALID_ARGUMENT
;
507 kr
= semaphore_signal_internal(semaphore
,
509 SEMAPHORE_SIGNAL_ALL
);
510 if (kr
== KERN_NOT_WAITING
)
516 * Routine: semaphore_signal_all_trap
518 * Trap interface to the semaphore_signal_all function.
521 semaphore_signal_all_trap(
522 struct semaphore_signal_all_trap_args
*args
)
524 mach_port_name_t sema_name
= args
->signal_name
;
525 semaphore_t semaphore
;
528 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
529 if (kr
== KERN_SUCCESS
) {
530 kr
= semaphore_signal_internal(semaphore
,
532 SEMAPHORE_SIGNAL_ALL
);
533 semaphore_dereference(semaphore
);
534 if (kr
== KERN_NOT_WAITING
)
541 * Routine: semaphore_convert_wait_result
543 * Generate the return code after a semaphore wait/block. It
544 * takes the wait result as an input and coverts that to an
545 * appropriate result.
548 semaphore_convert_wait_result(int wait_result
)
550 switch (wait_result
) {
551 case THREAD_AWAKENED
:
554 case THREAD_TIMED_OUT
:
555 return KERN_OPERATION_TIMED_OUT
;
557 case THREAD_INTERRUPTED
:
561 return KERN_TERMINATED
;
564 panic("semaphore_block\n");
570 * Routine: semaphore_wait_continue
572 * Common continuation routine after waiting on a semphore.
573 * It returns directly to user space.
576 semaphore_wait_continue(void)
578 thread_t self
= current_thread();
579 int wait_result
= self
->wait_result
;
580 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
582 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
583 semaphore_dereference(self
->sth_waitsemaphore
);
584 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
585 semaphore_dereference(self
->sth_signalsemaphore
);
587 assert(caller_cont
!= (void (*)(kern_return_t
))0);
588 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
592 * Routine: semaphore_wait_internal
594 * Decrements the semaphore count by one. If the count is
595 * negative after the decrement, the calling thread blocks
596 * (possibly at a continuation and/or with a timeout).
600 * A reference is held on the signal semaphore.
603 semaphore_wait_internal(
604 semaphore_t wait_semaphore
,
605 semaphore_t signal_semaphore
,
608 void (*caller_cont
)(kern_return_t
))
612 kern_return_t kr
= KERN_ALREADY_WAITING
;
614 spl_level
= splsched();
615 semaphore_lock(wait_semaphore
);
617 if (!wait_semaphore
->active
) {
618 kr
= KERN_TERMINATED
;
619 } else if (wait_semaphore
->count
> 0) {
620 wait_semaphore
->count
--;
622 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
623 kr
= KERN_OPERATION_TIMED_OUT
;
625 thread_t self
= current_thread();
627 wait_semaphore
->count
= -1; /* we don't keep an actual count */
629 (void)wait_queue_assert_wait64_locked(
630 &wait_semaphore
->wait_queue
,
632 THREAD_ABORTSAFE
, deadline
,
636 semaphore_unlock(wait_semaphore
);
640 * wait_semaphore is unlocked so we are free to go ahead and
641 * signal the signal_semaphore (if one was provided).
643 if (signal_semaphore
!= SEMAPHORE_NULL
) {
644 kern_return_t signal_kr
;
647 * lock the signal semaphore reference we got and signal it.
648 * This will NOT block (we cannot block after having asserted
649 * our intention to wait above).
651 signal_kr
= semaphore_signal_internal(signal_semaphore
,
653 SEMAPHORE_SIGNAL_PREPOST
);
655 if (signal_kr
== KERN_NOT_WAITING
)
656 signal_kr
= KERN_SUCCESS
;
657 else if (signal_kr
== KERN_TERMINATED
) {
659 * Uh!Oh! The semaphore we were to signal died.
660 * We have to get ourselves out of the wait in
661 * case we get stuck here forever (it is assumed
662 * that the semaphore we were posting is gating
663 * the decision by someone else to post the
664 * semaphore we are waiting on). People will
665 * discover the other dead semaphore soon enough.
666 * If we got out of the wait cleanly (someone
667 * already posted a wakeup to us) then return that
668 * (most important) result. Otherwise,
669 * return the KERN_TERMINATED status.
671 thread_t self
= current_thread();
673 clear_wait(self
, THREAD_INTERRUPTED
);
674 kr
= semaphore_convert_wait_result(self
->wait_result
);
675 if (kr
== KERN_ABORTED
)
676 kr
= KERN_TERMINATED
;
681 * If we had an error, or we didn't really need to wait we can
682 * return now that we have signalled the signal semaphore.
684 if (kr
!= KERN_ALREADY_WAITING
)
688 * Now, we can block. If the caller supplied a continuation
689 * pointer of his own for after the block, block with the
690 * appropriate semaphore continuation. Thiswill gather the
691 * semaphore results, release references on the semaphore(s),
692 * and then call the caller's continuation.
695 thread_t self
= current_thread();
697 self
->sth_continuation
= caller_cont
;
698 self
->sth_waitsemaphore
= wait_semaphore
;
699 self
->sth_signalsemaphore
= signal_semaphore
;
700 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
703 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
706 return (semaphore_convert_wait_result(wait_result
));
711 * Routine: semaphore_wait
713 * Traditional (non-continuation) interface presented to
714 * in-kernel clients to wait on a semaphore.
718 semaphore_t semaphore
)
721 if (semaphore
== SEMAPHORE_NULL
)
722 return KERN_INVALID_ARGUMENT
;
724 return(semaphore_wait_internal(semaphore
,
726 0ULL, SEMAPHORE_OPTION_NONE
,
727 (void (*)(kern_return_t
))0));
731 semaphore_wait_noblock(
732 semaphore_t semaphore
)
735 if (semaphore
== SEMAPHORE_NULL
)
736 return KERN_INVALID_ARGUMENT
;
738 return(semaphore_wait_internal(semaphore
,
740 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
741 (void (*)(kern_return_t
))0));
745 semaphore_wait_deadline(
746 semaphore_t semaphore
,
750 if (semaphore
== SEMAPHORE_NULL
)
751 return KERN_INVALID_ARGUMENT
;
753 return(semaphore_wait_internal(semaphore
,
755 deadline
, SEMAPHORE_OPTION_NONE
,
756 (void (*)(kern_return_t
))0));
760 * Trap: semaphore_wait_trap
762 * Trap version of semaphore wait. Called on behalf of user-level
768 struct semaphore_wait_trap_args
*args
)
770 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
776 semaphore_wait_trap_internal(
777 mach_port_name_t name
,
778 void (*caller_cont
)(kern_return_t
))
780 semaphore_t semaphore
;
783 kr
= port_name_to_semaphore(name
, &semaphore
);
784 if (kr
== KERN_SUCCESS
) {
785 kr
= semaphore_wait_internal(semaphore
,
787 0ULL, SEMAPHORE_OPTION_NONE
,
789 semaphore_dereference(semaphore
);
795 * Routine: semaphore_timedwait
797 * Traditional (non-continuation) interface presented to
798 * in-kernel clients to wait on a semaphore with a timeout.
800 * A timeout of {0,0} is considered non-blocking.
804 semaphore_t semaphore
,
805 mach_timespec_t wait_time
)
807 int option
= SEMAPHORE_OPTION_NONE
;
808 uint64_t deadline
= 0;
810 if (semaphore
== SEMAPHORE_NULL
)
811 return KERN_INVALID_ARGUMENT
;
813 if(BAD_MACH_TIMESPEC(&wait_time
))
814 return KERN_INVALID_VALUE
;
816 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
817 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
819 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
821 return (semaphore_wait_internal(semaphore
,
824 (void(*)(kern_return_t
))0));
829 * Trap: semaphore_timedwait_trap
831 * Trap version of a semaphore_timedwait. The timeout parameter
832 * is passed in two distinct parts and re-assembled on this side
833 * of the trap interface (to accomodate calling conventions that
834 * pass structures as pointers instead of inline in registers without
835 * having to add a copyin).
837 * A timeout of {0,0} is considered non-blocking.
840 semaphore_timedwait_trap(
841 struct semaphore_timedwait_trap_args
*args
)
844 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
849 semaphore_timedwait_trap_internal(
850 mach_port_name_t name
,
853 void (*caller_cont
)(kern_return_t
))
855 semaphore_t semaphore
;
856 mach_timespec_t wait_time
;
859 wait_time
.tv_sec
= sec
;
860 wait_time
.tv_nsec
= nsec
;
861 if(BAD_MACH_TIMESPEC(&wait_time
))
862 return KERN_INVALID_VALUE
;
864 kr
= port_name_to_semaphore(name
, &semaphore
);
865 if (kr
== KERN_SUCCESS
) {
866 int option
= SEMAPHORE_OPTION_NONE
;
867 uint64_t deadline
= 0;
869 if (sec
== 0 && nsec
== 0)
870 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
872 deadline
= semaphore_deadline(sec
, nsec
);
874 kr
= semaphore_wait_internal(semaphore
,
878 semaphore_dereference(semaphore
);
884 * Routine: semaphore_wait_signal
886 * Atomically register a wait on a semaphore and THEN signal
887 * another. This is the in-kernel entry point that does not
888 * block at a continuation and does not free a signal_semaphore
892 semaphore_wait_signal(
893 semaphore_t wait_semaphore
,
894 semaphore_t signal_semaphore
)
896 if (wait_semaphore
== SEMAPHORE_NULL
)
897 return KERN_INVALID_ARGUMENT
;
899 return(semaphore_wait_internal(wait_semaphore
,
901 0ULL, SEMAPHORE_OPTION_NONE
,
902 (void(*)(kern_return_t
))0));
906 * Trap: semaphore_wait_signal_trap
908 * Atomically register a wait on a semaphore and THEN signal
909 * another. This is the trap version from user space.
912 semaphore_wait_signal_trap(
913 struct semaphore_wait_signal_trap_args
*args
)
915 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
919 semaphore_wait_signal_trap_internal(
920 mach_port_name_t wait_name
,
921 mach_port_name_t signal_name
,
922 void (*caller_cont
)(kern_return_t
))
924 semaphore_t wait_semaphore
;
925 semaphore_t signal_semaphore
;
928 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
929 if (kr
== KERN_SUCCESS
) {
930 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
931 if (kr
== KERN_SUCCESS
) {
932 kr
= semaphore_wait_internal(wait_semaphore
,
934 0ULL, SEMAPHORE_OPTION_NONE
,
936 semaphore_dereference(wait_semaphore
);
938 semaphore_dereference(signal_semaphore
);
945 * Routine: semaphore_timedwait_signal
947 * Atomically register a wait on a semaphore and THEN signal
948 * another. This is the in-kernel entry point that does not
949 * block at a continuation.
951 * A timeout of {0,0} is considered non-blocking.
954 semaphore_timedwait_signal(
955 semaphore_t wait_semaphore
,
956 semaphore_t signal_semaphore
,
957 mach_timespec_t wait_time
)
959 int option
= SEMAPHORE_OPTION_NONE
;
960 uint64_t deadline
= 0;
962 if (wait_semaphore
== SEMAPHORE_NULL
)
963 return KERN_INVALID_ARGUMENT
;
965 if(BAD_MACH_TIMESPEC(&wait_time
))
966 return KERN_INVALID_VALUE
;
968 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
969 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
971 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
973 return(semaphore_wait_internal(wait_semaphore
,
976 (void(*)(kern_return_t
))0));
980 * Trap: semaphore_timedwait_signal_trap
982 * Atomically register a timed wait on a semaphore and THEN signal
983 * another. This is the trap version from user space.
986 semaphore_timedwait_signal_trap(
987 struct semaphore_timedwait_signal_trap_args
*args
)
989 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
993 semaphore_timedwait_signal_trap_internal(
994 mach_port_name_t wait_name
,
995 mach_port_name_t signal_name
,
998 void (*caller_cont
)(kern_return_t
))
1000 semaphore_t wait_semaphore
;
1001 semaphore_t signal_semaphore
;
1002 mach_timespec_t wait_time
;
1005 wait_time
.tv_sec
= sec
;
1006 wait_time
.tv_nsec
= nsec
;
1007 if(BAD_MACH_TIMESPEC(&wait_time
))
1008 return KERN_INVALID_VALUE
;
1010 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1011 if (kr
== KERN_SUCCESS
) {
1012 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1013 if (kr
== KERN_SUCCESS
) {
1014 int option
= SEMAPHORE_OPTION_NONE
;
1015 uint64_t deadline
= 0;
1017 if (sec
== 0 && nsec
== 0)
1018 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1020 deadline
= semaphore_deadline(sec
, nsec
);
1022 kr
= semaphore_wait_internal(wait_semaphore
,
1026 semaphore_dereference(wait_semaphore
);
1028 semaphore_dereference(signal_semaphore
);
1035 * Routine: semaphore_reference
1037 * Take out a reference on a semaphore. This keeps the data structure
1038 * in existence (but the semaphore may be deactivated).
1041 semaphore_reference(
1042 semaphore_t semaphore
)
1044 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1048 * Routine: semaphore_dereference
1050 * Release a reference on a semaphore. If this is the last reference,
1051 * the semaphore data structure is deallocated.
1054 semaphore_dereference(
1055 semaphore_t semaphore
)
1059 if (semaphore
!= NULL
) {
1060 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1062 if (ref_count
== 0) {
1063 assert(wait_queue_empty(&semaphore
->wait_queue
));
1064 ipc_port_dealloc_kernel(semaphore
->port
);
1065 zfree(semaphore_zone
, semaphore
);