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 #include <libkern/OSAtomic.h>
63 static unsigned int semaphore_event
;
64 #define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event)
66 zone_t semaphore_zone
;
67 unsigned int semaphore_max
;
69 /* Forward declarations */
73 semaphore_wait_trap_internal(
74 mach_port_name_t name
,
75 void (*caller_cont
)(kern_return_t
));
78 semaphore_wait_signal_trap_internal(
79 mach_port_name_t wait_name
,
80 mach_port_name_t signal_name
,
81 void (*caller_cont
)(kern_return_t
));
84 semaphore_timedwait_trap_internal(
85 mach_port_name_t name
,
88 void (*caller_cont
)(kern_return_t
));
91 semaphore_timedwait_signal_trap_internal(
92 mach_port_name_t wait_name
,
93 mach_port_name_t signal_name
,
96 void (*caller_cont
)(kern_return_t
));
99 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
102 semaphore_signal_internal(
103 semaphore_t semaphore
,
108 semaphore_convert_wait_result(
112 semaphore_wait_continue(void);
115 semaphore_wait_internal(
116 semaphore_t wait_semaphore
,
117 semaphore_t signal_semaphore
,
120 void (*caller_cont
)(kern_return_t
));
122 static __inline__
uint64_t
129 nanoseconds_to_absolutetime((uint64_t)sec
* NSEC_PER_SEC
+ nsec
, &abstime
);
130 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
136 * ROUTINE: semaphore_init [private]
138 * Initialize the semaphore mechanisms.
139 * Right now, we only need to initialize the semaphore zone.
144 semaphore_zone
= zinit(sizeof(struct semaphore
),
145 semaphore_max
* sizeof(struct semaphore
),
146 sizeof(struct semaphore
),
148 zone_change(semaphore_zone
, Z_NOENCRYPT
, TRUE
);
152 * Routine: semaphore_create
154 * Creates a semaphore.
155 * The port representing the semaphore is returned as a parameter.
160 semaphore_t
*new_semaphore
,
164 semaphore_t s
= SEMAPHORE_NULL
;
168 *new_semaphore
= SEMAPHORE_NULL
;
169 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
)
170 return KERN_INVALID_ARGUMENT
;
172 s
= (semaphore_t
) zalloc (semaphore_zone
);
174 if (s
== SEMAPHORE_NULL
)
175 return KERN_RESOURCE_SHORTAGE
;
177 kret
= wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
178 if (kret
!= KERN_SUCCESS
) {
179 zfree(semaphore_zone
, s
);
186 * One reference for caller, one for port, and one for owner
187 * task (if not the kernel itself).
189 s
->ref_count
= (task
== kernel_task
) ? 2 : 3;
192 * Create and initialize the semaphore port
194 s
->port
= ipc_port_alloc_kernel();
195 if (s
->port
== IP_NULL
) {
196 zfree(semaphore_zone
, s
);
197 return KERN_RESOURCE_SHORTAGE
;
200 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
203 * Associate the new semaphore with the task by adding
204 * the new semaphore to the task's semaphore list.
206 * Associate the task with the new semaphore by having the
207 * semaphores task pointer point to the owning task's structure.
210 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
211 task
->semaphores_owned
++;
222 * Routine: semaphore_destroy
224 * Destroys a semaphore. This call will only succeed if the
225 * specified task is the SAME task name specified at the semaphore's
228 * All threads currently blocked on the semaphore are awoken. These
229 * threads will return with the KERN_TERMINATED error.
234 semaphore_t semaphore
)
240 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
241 return KERN_INVALID_ARGUMENT
;
247 if (semaphore
->owner
!= task
) {
249 return KERN_INVALID_ARGUMENT
;
251 remqueue((queue_entry_t
) semaphore
);
252 semaphore
->owner
= TASK_NULL
;
253 task
->semaphores_owned
--;
256 spl_level
= splsched();
257 semaphore_lock(semaphore
);
260 * Deactivate semaphore
262 assert(semaphore
->active
);
263 semaphore
->active
= FALSE
;
266 * Wakeup blocked threads
268 old_count
= semaphore
->count
;
269 semaphore
->count
= 0;
272 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
277 semaphore_unlock(semaphore
);
284 * Drop the task's semaphore reference, which in turn deallocates
285 * the semaphore structure if the reference count goes to zero.
287 semaphore_dereference(semaphore
);
292 * Routine: semaphore_signal_internal
294 * Signals the semaphore as direct.
296 * Semaphore is locked.
299 semaphore_signal_internal(
300 semaphore_t semaphore
,
307 spl_level
= splsched();
308 semaphore_lock(semaphore
);
310 if (!semaphore
->active
) {
311 semaphore_unlock(semaphore
);
313 return KERN_TERMINATED
;
316 if (thread
!= THREAD_NULL
) {
317 if (semaphore
->count
< 0) {
318 kr
= wait_queue_wakeup64_thread_locked(
319 &semaphore
->wait_queue
,
325 semaphore_unlock(semaphore
);
326 kr
= KERN_NOT_WAITING
;
332 if (options
& SEMAPHORE_SIGNAL_ALL
) {
333 int old_count
= semaphore
->count
;
336 semaphore
->count
= 0; /* always reset */
337 kr
= wait_queue_wakeup64_all_locked(
338 &semaphore
->wait_queue
,
343 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
345 semaphore_unlock(semaphore
);
352 if (semaphore
->count
< 0) {
353 if (wait_queue_wakeup64_one_locked(
354 &semaphore
->wait_queue
,
357 FALSE
) == KERN_SUCCESS
) {
358 semaphore_unlock(semaphore
);
362 semaphore
->count
= 0; /* all waiters gone */
365 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
369 semaphore_unlock(semaphore
);
371 return KERN_NOT_WAITING
;
375 * Routine: semaphore_signal_thread
377 * If the specified thread is blocked on the semaphore, it is
378 * woken up. If a NULL thread was supplied, then any one
379 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
380 * and the semaphore is unchanged.
383 semaphore_signal_thread(
384 semaphore_t semaphore
,
389 if (semaphore
== SEMAPHORE_NULL
)
390 return KERN_INVALID_ARGUMENT
;
392 ret
= semaphore_signal_internal(semaphore
,
394 SEMAPHORE_OPTION_NONE
);
399 * Routine: semaphore_signal_thread_trap
401 * Trap interface to the semaphore_signal_thread function.
404 semaphore_signal_thread_trap(
405 struct semaphore_signal_thread_trap_args
*args
)
407 mach_port_name_t sema_name
= args
->signal_name
;
408 mach_port_name_t thread_name
= args
->thread_name
;
409 semaphore_t semaphore
;
414 * MACH_PORT_NULL is not an error. It means that we want to
415 * select any one thread that is already waiting, but not to
416 * pre-post the semaphore.
418 if (thread_name
!= MACH_PORT_NULL
) {
419 thread
= port_name_to_thread(thread_name
);
420 if (thread
== THREAD_NULL
)
421 return KERN_INVALID_ARGUMENT
;
423 thread
= THREAD_NULL
;
425 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
426 if (kr
== KERN_SUCCESS
) {
427 kr
= semaphore_signal_internal(semaphore
,
429 SEMAPHORE_OPTION_NONE
);
430 semaphore_dereference(semaphore
);
432 if (thread
!= THREAD_NULL
) {
433 thread_deallocate(thread
);
441 * Routine: semaphore_signal
443 * Traditional (in-kernel client and MIG interface) semaphore
444 * signal routine. Most users will access the trap version.
446 * This interface in not defined to return info about whether
447 * this call found a thread waiting or not. The internal
448 * routines (and future external routines) do. We have to
449 * convert those into plain KERN_SUCCESS returns.
453 semaphore_t semaphore
)
457 if (semaphore
== SEMAPHORE_NULL
)
458 return KERN_INVALID_ARGUMENT
;
460 kr
= semaphore_signal_internal(semaphore
,
462 SEMAPHORE_SIGNAL_PREPOST
);
463 if (kr
== KERN_NOT_WAITING
)
469 * Routine: semaphore_signal_trap
471 * Trap interface to the semaphore_signal function.
474 semaphore_signal_trap(
475 struct semaphore_signal_trap_args
*args
)
477 mach_port_name_t sema_name
= args
->signal_name
;
479 return (semaphore_signal_internal_trap(sema_name
));
483 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
485 semaphore_t semaphore
;
488 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
489 if (kr
== KERN_SUCCESS
) {
490 kr
= semaphore_signal_internal(semaphore
,
492 SEMAPHORE_SIGNAL_PREPOST
);
493 semaphore_dereference(semaphore
);
494 if (kr
== KERN_NOT_WAITING
)
501 * Routine: semaphore_signal_all
503 * Awakens ALL threads currently blocked on the semaphore.
504 * The semaphore count returns to zero.
507 semaphore_signal_all(
508 semaphore_t semaphore
)
512 if (semaphore
== SEMAPHORE_NULL
)
513 return KERN_INVALID_ARGUMENT
;
515 kr
= semaphore_signal_internal(semaphore
,
517 SEMAPHORE_SIGNAL_ALL
);
518 if (kr
== KERN_NOT_WAITING
)
524 * Routine: semaphore_signal_all_trap
526 * Trap interface to the semaphore_signal_all function.
529 semaphore_signal_all_trap(
530 struct semaphore_signal_all_trap_args
*args
)
532 mach_port_name_t sema_name
= args
->signal_name
;
533 semaphore_t semaphore
;
536 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
537 if (kr
== KERN_SUCCESS
) {
538 kr
= semaphore_signal_internal(semaphore
,
540 SEMAPHORE_SIGNAL_ALL
);
541 semaphore_dereference(semaphore
);
542 if (kr
== KERN_NOT_WAITING
)
549 * Routine: semaphore_convert_wait_result
551 * Generate the return code after a semaphore wait/block. It
552 * takes the wait result as an input and coverts that to an
553 * appropriate result.
556 semaphore_convert_wait_result(int wait_result
)
558 switch (wait_result
) {
559 case THREAD_AWAKENED
:
562 case THREAD_TIMED_OUT
:
563 return KERN_OPERATION_TIMED_OUT
;
565 case THREAD_INTERRUPTED
:
569 return KERN_TERMINATED
;
572 panic("semaphore_block\n");
578 * Routine: semaphore_wait_continue
580 * Common continuation routine after waiting on a semphore.
581 * It returns directly to user space.
584 semaphore_wait_continue(void)
586 thread_t self
= current_thread();
587 int wait_result
= self
->wait_result
;
588 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
590 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
591 semaphore_dereference(self
->sth_waitsemaphore
);
592 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
593 semaphore_dereference(self
->sth_signalsemaphore
);
595 assert(caller_cont
!= (void (*)(kern_return_t
))0);
596 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
600 * Routine: semaphore_wait_internal
602 * Decrements the semaphore count by one. If the count is
603 * negative after the decrement, the calling thread blocks
604 * (possibly at a continuation and/or with a timeout).
608 * A reference is held on the signal semaphore.
611 semaphore_wait_internal(
612 semaphore_t wait_semaphore
,
613 semaphore_t signal_semaphore
,
616 void (*caller_cont
)(kern_return_t
))
620 kern_return_t kr
= KERN_ALREADY_WAITING
;
622 spl_level
= splsched();
623 semaphore_lock(wait_semaphore
);
625 if (!wait_semaphore
->active
) {
626 kr
= KERN_TERMINATED
;
627 } else if (wait_semaphore
->count
> 0) {
628 wait_semaphore
->count
--;
630 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
631 kr
= KERN_OPERATION_TIMED_OUT
;
633 thread_t self
= current_thread();
635 wait_semaphore
->count
= -1; /* we don't keep an actual count */
637 (void)wait_queue_assert_wait64_locked(
638 &wait_semaphore
->wait_queue
,
641 TIMEOUT_URGENCY_USER_NORMAL
,
646 semaphore_unlock(wait_semaphore
);
650 * wait_semaphore is unlocked so we are free to go ahead and
651 * signal the signal_semaphore (if one was provided).
653 if (signal_semaphore
!= SEMAPHORE_NULL
) {
654 kern_return_t signal_kr
;
657 * lock the signal semaphore reference we got and signal it.
658 * This will NOT block (we cannot block after having asserted
659 * our intention to wait above).
661 signal_kr
= semaphore_signal_internal(signal_semaphore
,
663 SEMAPHORE_SIGNAL_PREPOST
);
665 if (signal_kr
== KERN_NOT_WAITING
)
666 signal_kr
= KERN_SUCCESS
;
667 else if (signal_kr
== KERN_TERMINATED
) {
669 * Uh!Oh! The semaphore we were to signal died.
670 * We have to get ourselves out of the wait in
671 * case we get stuck here forever (it is assumed
672 * that the semaphore we were posting is gating
673 * the decision by someone else to post the
674 * semaphore we are waiting on). People will
675 * discover the other dead semaphore soon enough.
676 * If we got out of the wait cleanly (someone
677 * already posted a wakeup to us) then return that
678 * (most important) result. Otherwise,
679 * return the KERN_TERMINATED status.
681 thread_t self
= current_thread();
683 clear_wait(self
, THREAD_INTERRUPTED
);
684 kr
= semaphore_convert_wait_result(self
->wait_result
);
685 if (kr
== KERN_ABORTED
)
686 kr
= KERN_TERMINATED
;
691 * If we had an error, or we didn't really need to wait we can
692 * return now that we have signalled the signal semaphore.
694 if (kr
!= KERN_ALREADY_WAITING
)
698 * Now, we can block. If the caller supplied a continuation
699 * pointer of his own for after the block, block with the
700 * appropriate semaphore continuation. Thiswill gather the
701 * semaphore results, release references on the semaphore(s),
702 * and then call the caller's continuation.
705 thread_t self
= current_thread();
707 self
->sth_continuation
= caller_cont
;
708 self
->sth_waitsemaphore
= wait_semaphore
;
709 self
->sth_signalsemaphore
= signal_semaphore
;
710 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
713 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
716 return (semaphore_convert_wait_result(wait_result
));
721 * Routine: semaphore_wait
723 * Traditional (non-continuation) interface presented to
724 * in-kernel clients to wait on a semaphore.
728 semaphore_t semaphore
)
731 if (semaphore
== SEMAPHORE_NULL
)
732 return KERN_INVALID_ARGUMENT
;
734 return(semaphore_wait_internal(semaphore
,
736 0ULL, SEMAPHORE_OPTION_NONE
,
737 (void (*)(kern_return_t
))0));
741 semaphore_wait_noblock(
742 semaphore_t semaphore
)
745 if (semaphore
== SEMAPHORE_NULL
)
746 return KERN_INVALID_ARGUMENT
;
748 return(semaphore_wait_internal(semaphore
,
750 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
751 (void (*)(kern_return_t
))0));
755 semaphore_wait_deadline(
756 semaphore_t semaphore
,
760 if (semaphore
== SEMAPHORE_NULL
)
761 return KERN_INVALID_ARGUMENT
;
763 return(semaphore_wait_internal(semaphore
,
765 deadline
, SEMAPHORE_OPTION_NONE
,
766 (void (*)(kern_return_t
))0));
770 * Trap: semaphore_wait_trap
772 * Trap version of semaphore wait. Called on behalf of user-level
778 struct semaphore_wait_trap_args
*args
)
780 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
786 semaphore_wait_trap_internal(
787 mach_port_name_t name
,
788 void (*caller_cont
)(kern_return_t
))
790 semaphore_t semaphore
;
793 kr
= port_name_to_semaphore(name
, &semaphore
);
794 if (kr
== KERN_SUCCESS
) {
795 kr
= semaphore_wait_internal(semaphore
,
797 0ULL, SEMAPHORE_OPTION_NONE
,
799 semaphore_dereference(semaphore
);
805 * Routine: semaphore_timedwait
807 * Traditional (non-continuation) interface presented to
808 * in-kernel clients to wait on a semaphore with a timeout.
810 * A timeout of {0,0} is considered non-blocking.
814 semaphore_t semaphore
,
815 mach_timespec_t wait_time
)
817 int option
= SEMAPHORE_OPTION_NONE
;
818 uint64_t deadline
= 0;
820 if (semaphore
== SEMAPHORE_NULL
)
821 return KERN_INVALID_ARGUMENT
;
823 if(BAD_MACH_TIMESPEC(&wait_time
))
824 return KERN_INVALID_VALUE
;
826 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
827 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
829 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
831 return (semaphore_wait_internal(semaphore
,
834 (void(*)(kern_return_t
))0));
839 * Trap: semaphore_timedwait_trap
841 * Trap version of a semaphore_timedwait. The timeout parameter
842 * is passed in two distinct parts and re-assembled on this side
843 * of the trap interface (to accomodate calling conventions that
844 * pass structures as pointers instead of inline in registers without
845 * having to add a copyin).
847 * A timeout of {0,0} is considered non-blocking.
850 semaphore_timedwait_trap(
851 struct semaphore_timedwait_trap_args
*args
)
854 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
859 semaphore_timedwait_trap_internal(
860 mach_port_name_t name
,
863 void (*caller_cont
)(kern_return_t
))
865 semaphore_t semaphore
;
866 mach_timespec_t wait_time
;
869 wait_time
.tv_sec
= sec
;
870 wait_time
.tv_nsec
= nsec
;
871 if(BAD_MACH_TIMESPEC(&wait_time
))
872 return KERN_INVALID_VALUE
;
874 kr
= port_name_to_semaphore(name
, &semaphore
);
875 if (kr
== KERN_SUCCESS
) {
876 int option
= SEMAPHORE_OPTION_NONE
;
877 uint64_t deadline
= 0;
879 if (sec
== 0 && nsec
== 0)
880 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
882 deadline
= semaphore_deadline(sec
, nsec
);
884 kr
= semaphore_wait_internal(semaphore
,
888 semaphore_dereference(semaphore
);
894 * Routine: semaphore_wait_signal
896 * Atomically register a wait on a semaphore and THEN signal
897 * another. This is the in-kernel entry point that does not
898 * block at a continuation and does not free a signal_semaphore
902 semaphore_wait_signal(
903 semaphore_t wait_semaphore
,
904 semaphore_t signal_semaphore
)
906 if (wait_semaphore
== SEMAPHORE_NULL
)
907 return KERN_INVALID_ARGUMENT
;
909 return(semaphore_wait_internal(wait_semaphore
,
911 0ULL, SEMAPHORE_OPTION_NONE
,
912 (void(*)(kern_return_t
))0));
916 * Trap: semaphore_wait_signal_trap
918 * Atomically register a wait on a semaphore and THEN signal
919 * another. This is the trap version from user space.
922 semaphore_wait_signal_trap(
923 struct semaphore_wait_signal_trap_args
*args
)
925 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
929 semaphore_wait_signal_trap_internal(
930 mach_port_name_t wait_name
,
931 mach_port_name_t signal_name
,
932 void (*caller_cont
)(kern_return_t
))
934 semaphore_t wait_semaphore
;
935 semaphore_t signal_semaphore
;
938 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
939 if (kr
== KERN_SUCCESS
) {
940 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
941 if (kr
== KERN_SUCCESS
) {
942 kr
= semaphore_wait_internal(wait_semaphore
,
944 0ULL, SEMAPHORE_OPTION_NONE
,
946 semaphore_dereference(wait_semaphore
);
948 semaphore_dereference(signal_semaphore
);
955 * Routine: semaphore_timedwait_signal
957 * Atomically register a wait on a semaphore and THEN signal
958 * another. This is the in-kernel entry point that does not
959 * block at a continuation.
961 * A timeout of {0,0} is considered non-blocking.
964 semaphore_timedwait_signal(
965 semaphore_t wait_semaphore
,
966 semaphore_t signal_semaphore
,
967 mach_timespec_t wait_time
)
969 int option
= SEMAPHORE_OPTION_NONE
;
970 uint64_t deadline
= 0;
972 if (wait_semaphore
== SEMAPHORE_NULL
)
973 return KERN_INVALID_ARGUMENT
;
975 if(BAD_MACH_TIMESPEC(&wait_time
))
976 return KERN_INVALID_VALUE
;
978 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
979 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
981 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
983 return(semaphore_wait_internal(wait_semaphore
,
986 (void(*)(kern_return_t
))0));
990 * Trap: semaphore_timedwait_signal_trap
992 * Atomically register a timed wait on a semaphore and THEN signal
993 * another. This is the trap version from user space.
996 semaphore_timedwait_signal_trap(
997 struct semaphore_timedwait_signal_trap_args
*args
)
999 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1003 semaphore_timedwait_signal_trap_internal(
1004 mach_port_name_t wait_name
,
1005 mach_port_name_t signal_name
,
1008 void (*caller_cont
)(kern_return_t
))
1010 semaphore_t wait_semaphore
;
1011 semaphore_t signal_semaphore
;
1012 mach_timespec_t wait_time
;
1015 wait_time
.tv_sec
= sec
;
1016 wait_time
.tv_nsec
= nsec
;
1017 if(BAD_MACH_TIMESPEC(&wait_time
))
1018 return KERN_INVALID_VALUE
;
1020 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1021 if (kr
== KERN_SUCCESS
) {
1022 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1023 if (kr
== KERN_SUCCESS
) {
1024 int option
= SEMAPHORE_OPTION_NONE
;
1025 uint64_t deadline
= 0;
1027 if (sec
== 0 && nsec
== 0)
1028 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1030 deadline
= semaphore_deadline(sec
, nsec
);
1032 kr
= semaphore_wait_internal(wait_semaphore
,
1036 semaphore_dereference(wait_semaphore
);
1038 semaphore_dereference(signal_semaphore
);
1045 * Routine: semaphore_reference
1047 * Take out a reference on a semaphore. This keeps the data structure
1048 * in existence (but the semaphore may be deactivated).
1051 semaphore_reference(
1052 semaphore_t semaphore
)
1054 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1058 * Routine: semaphore_dereference
1060 * Release a reference on a semaphore. If this is the last reference,
1061 * the semaphore data structure is deallocated.
1064 semaphore_dereference(
1065 semaphore_t semaphore
)
1069 if (semaphore
!= NULL
) {
1070 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1072 if (ref_count
== 1) {
1073 ipc_port_t port
= semaphore
->port
;
1075 if (IP_VALID(port
) &&
1076 OSCompareAndSwapPtr(port
, IP_NULL
, &semaphore
->port
)) {
1078 * We get to disassociate the port from the sema and
1079 * drop the port's reference on the sema.
1081 ipc_port_dealloc_kernel(port
);
1082 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1085 if (ref_count
== 0) {
1086 assert(wait_queue_empty(&semaphore
->wait_queue
));
1087 zfree(semaphore_zone
, semaphore
);