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
,
640 THREAD_ABORTSAFE
, deadline
,
644 semaphore_unlock(wait_semaphore
);
648 * wait_semaphore is unlocked so we are free to go ahead and
649 * signal the signal_semaphore (if one was provided).
651 if (signal_semaphore
!= SEMAPHORE_NULL
) {
652 kern_return_t signal_kr
;
655 * lock the signal semaphore reference we got and signal it.
656 * This will NOT block (we cannot block after having asserted
657 * our intention to wait above).
659 signal_kr
= semaphore_signal_internal(signal_semaphore
,
661 SEMAPHORE_SIGNAL_PREPOST
);
663 if (signal_kr
== KERN_NOT_WAITING
)
664 signal_kr
= KERN_SUCCESS
;
665 else if (signal_kr
== KERN_TERMINATED
) {
667 * Uh!Oh! The semaphore we were to signal died.
668 * We have to get ourselves out of the wait in
669 * case we get stuck here forever (it is assumed
670 * that the semaphore we were posting is gating
671 * the decision by someone else to post the
672 * semaphore we are waiting on). People will
673 * discover the other dead semaphore soon enough.
674 * If we got out of the wait cleanly (someone
675 * already posted a wakeup to us) then return that
676 * (most important) result. Otherwise,
677 * return the KERN_TERMINATED status.
679 thread_t self
= current_thread();
681 clear_wait(self
, THREAD_INTERRUPTED
);
682 kr
= semaphore_convert_wait_result(self
->wait_result
);
683 if (kr
== KERN_ABORTED
)
684 kr
= KERN_TERMINATED
;
689 * If we had an error, or we didn't really need to wait we can
690 * return now that we have signalled the signal semaphore.
692 if (kr
!= KERN_ALREADY_WAITING
)
696 * Now, we can block. If the caller supplied a continuation
697 * pointer of his own for after the block, block with the
698 * appropriate semaphore continuation. Thiswill gather the
699 * semaphore results, release references on the semaphore(s),
700 * and then call the caller's continuation.
703 thread_t self
= current_thread();
705 self
->sth_continuation
= caller_cont
;
706 self
->sth_waitsemaphore
= wait_semaphore
;
707 self
->sth_signalsemaphore
= signal_semaphore
;
708 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
711 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
714 return (semaphore_convert_wait_result(wait_result
));
719 * Routine: semaphore_wait
721 * Traditional (non-continuation) interface presented to
722 * in-kernel clients to wait on a semaphore.
726 semaphore_t semaphore
)
729 if (semaphore
== SEMAPHORE_NULL
)
730 return KERN_INVALID_ARGUMENT
;
732 return(semaphore_wait_internal(semaphore
,
734 0ULL, SEMAPHORE_OPTION_NONE
,
735 (void (*)(kern_return_t
))0));
739 semaphore_wait_noblock(
740 semaphore_t semaphore
)
743 if (semaphore
== SEMAPHORE_NULL
)
744 return KERN_INVALID_ARGUMENT
;
746 return(semaphore_wait_internal(semaphore
,
748 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
749 (void (*)(kern_return_t
))0));
753 semaphore_wait_deadline(
754 semaphore_t semaphore
,
758 if (semaphore
== SEMAPHORE_NULL
)
759 return KERN_INVALID_ARGUMENT
;
761 return(semaphore_wait_internal(semaphore
,
763 deadline
, SEMAPHORE_OPTION_NONE
,
764 (void (*)(kern_return_t
))0));
768 * Trap: semaphore_wait_trap
770 * Trap version of semaphore wait. Called on behalf of user-level
776 struct semaphore_wait_trap_args
*args
)
778 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
784 semaphore_wait_trap_internal(
785 mach_port_name_t name
,
786 void (*caller_cont
)(kern_return_t
))
788 semaphore_t semaphore
;
791 kr
= port_name_to_semaphore(name
, &semaphore
);
792 if (kr
== KERN_SUCCESS
) {
793 kr
= semaphore_wait_internal(semaphore
,
795 0ULL, SEMAPHORE_OPTION_NONE
,
797 semaphore_dereference(semaphore
);
803 * Routine: semaphore_timedwait
805 * Traditional (non-continuation) interface presented to
806 * in-kernel clients to wait on a semaphore with a timeout.
808 * A timeout of {0,0} is considered non-blocking.
812 semaphore_t semaphore
,
813 mach_timespec_t wait_time
)
815 int option
= SEMAPHORE_OPTION_NONE
;
816 uint64_t deadline
= 0;
818 if (semaphore
== SEMAPHORE_NULL
)
819 return KERN_INVALID_ARGUMENT
;
821 if(BAD_MACH_TIMESPEC(&wait_time
))
822 return KERN_INVALID_VALUE
;
824 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
825 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
827 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
829 return (semaphore_wait_internal(semaphore
,
832 (void(*)(kern_return_t
))0));
837 * Trap: semaphore_timedwait_trap
839 * Trap version of a semaphore_timedwait. The timeout parameter
840 * is passed in two distinct parts and re-assembled on this side
841 * of the trap interface (to accomodate calling conventions that
842 * pass structures as pointers instead of inline in registers without
843 * having to add a copyin).
845 * A timeout of {0,0} is considered non-blocking.
848 semaphore_timedwait_trap(
849 struct semaphore_timedwait_trap_args
*args
)
852 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
857 semaphore_timedwait_trap_internal(
858 mach_port_name_t name
,
861 void (*caller_cont
)(kern_return_t
))
863 semaphore_t semaphore
;
864 mach_timespec_t wait_time
;
867 wait_time
.tv_sec
= sec
;
868 wait_time
.tv_nsec
= nsec
;
869 if(BAD_MACH_TIMESPEC(&wait_time
))
870 return KERN_INVALID_VALUE
;
872 kr
= port_name_to_semaphore(name
, &semaphore
);
873 if (kr
== KERN_SUCCESS
) {
874 int option
= SEMAPHORE_OPTION_NONE
;
875 uint64_t deadline
= 0;
877 if (sec
== 0 && nsec
== 0)
878 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
880 deadline
= semaphore_deadline(sec
, nsec
);
882 kr
= semaphore_wait_internal(semaphore
,
886 semaphore_dereference(semaphore
);
892 * Routine: semaphore_wait_signal
894 * Atomically register a wait on a semaphore and THEN signal
895 * another. This is the in-kernel entry point that does not
896 * block at a continuation and does not free a signal_semaphore
900 semaphore_wait_signal(
901 semaphore_t wait_semaphore
,
902 semaphore_t signal_semaphore
)
904 if (wait_semaphore
== SEMAPHORE_NULL
)
905 return KERN_INVALID_ARGUMENT
;
907 return(semaphore_wait_internal(wait_semaphore
,
909 0ULL, SEMAPHORE_OPTION_NONE
,
910 (void(*)(kern_return_t
))0));
914 * Trap: semaphore_wait_signal_trap
916 * Atomically register a wait on a semaphore and THEN signal
917 * another. This is the trap version from user space.
920 semaphore_wait_signal_trap(
921 struct semaphore_wait_signal_trap_args
*args
)
923 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
927 semaphore_wait_signal_trap_internal(
928 mach_port_name_t wait_name
,
929 mach_port_name_t signal_name
,
930 void (*caller_cont
)(kern_return_t
))
932 semaphore_t wait_semaphore
;
933 semaphore_t signal_semaphore
;
936 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
937 if (kr
== KERN_SUCCESS
) {
938 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
939 if (kr
== KERN_SUCCESS
) {
940 kr
= semaphore_wait_internal(wait_semaphore
,
942 0ULL, SEMAPHORE_OPTION_NONE
,
944 semaphore_dereference(wait_semaphore
);
946 semaphore_dereference(signal_semaphore
);
953 * Routine: semaphore_timedwait_signal
955 * Atomically register a wait on a semaphore and THEN signal
956 * another. This is the in-kernel entry point that does not
957 * block at a continuation.
959 * A timeout of {0,0} is considered non-blocking.
962 semaphore_timedwait_signal(
963 semaphore_t wait_semaphore
,
964 semaphore_t signal_semaphore
,
965 mach_timespec_t wait_time
)
967 int option
= SEMAPHORE_OPTION_NONE
;
968 uint64_t deadline
= 0;
970 if (wait_semaphore
== SEMAPHORE_NULL
)
971 return KERN_INVALID_ARGUMENT
;
973 if(BAD_MACH_TIMESPEC(&wait_time
))
974 return KERN_INVALID_VALUE
;
976 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
977 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
979 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
981 return(semaphore_wait_internal(wait_semaphore
,
984 (void(*)(kern_return_t
))0));
988 * Trap: semaphore_timedwait_signal_trap
990 * Atomically register a timed wait on a semaphore and THEN signal
991 * another. This is the trap version from user space.
994 semaphore_timedwait_signal_trap(
995 struct semaphore_timedwait_signal_trap_args
*args
)
997 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1001 semaphore_timedwait_signal_trap_internal(
1002 mach_port_name_t wait_name
,
1003 mach_port_name_t signal_name
,
1006 void (*caller_cont
)(kern_return_t
))
1008 semaphore_t wait_semaphore
;
1009 semaphore_t signal_semaphore
;
1010 mach_timespec_t wait_time
;
1013 wait_time
.tv_sec
= sec
;
1014 wait_time
.tv_nsec
= nsec
;
1015 if(BAD_MACH_TIMESPEC(&wait_time
))
1016 return KERN_INVALID_VALUE
;
1018 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1019 if (kr
== KERN_SUCCESS
) {
1020 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1021 if (kr
== KERN_SUCCESS
) {
1022 int option
= SEMAPHORE_OPTION_NONE
;
1023 uint64_t deadline
= 0;
1025 if (sec
== 0 && nsec
== 0)
1026 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1028 deadline
= semaphore_deadline(sec
, nsec
);
1030 kr
= semaphore_wait_internal(wait_semaphore
,
1034 semaphore_dereference(wait_semaphore
);
1036 semaphore_dereference(signal_semaphore
);
1043 * Routine: semaphore_reference
1045 * Take out a reference on a semaphore. This keeps the data structure
1046 * in existence (but the semaphore may be deactivated).
1049 semaphore_reference(
1050 semaphore_t semaphore
)
1052 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1056 * Routine: semaphore_dereference
1058 * Release a reference on a semaphore. If this is the last reference,
1059 * the semaphore data structure is deallocated.
1062 semaphore_dereference(
1063 semaphore_t semaphore
)
1067 if (semaphore
!= NULL
) {
1068 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1070 if (ref_count
== 1) {
1071 ipc_port_t port
= semaphore
->port
;
1073 if (IP_VALID(port
) &&
1074 OSCompareAndSwapPtr(port
, IP_NULL
, &semaphore
->port
)) {
1076 * We get to disassociate the port from the sema and
1077 * drop the port's reference on the sema.
1079 ipc_port_dealloc_kernel(port
);
1080 ref_count
= hw_atomic_sub(&semaphore
->ref_count
, 1);
1083 if (ref_count
== 0) {
1084 assert(wait_queue_empty(&semaphore
->wait_queue
));
1085 zfree(semaphore_zone
, semaphore
);