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/waitq.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
= waitq_init(&s
->waitq
, policy
| SYNC_POLICY_DISABLE_IRQ
); /* also inits lock */
178 if (kret
!= KERN_SUCCESS
) {
179 zfree(semaphore_zone
, s
);
184 * Initialize the semaphore values.
193 * Associate the new semaphore with the task by adding
194 * the new semaphore to the task's semaphore list.
197 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
198 task
->semaphores_owned
++;
207 * Routine: semaphore_destroy_internal
209 * This call will only succeed if the specified task is the SAME task
210 * specified at the semaphore's creation.
212 * All threads currently blocked on the semaphore are awoken. These
213 * threads will return with the KERN_TERMINATED error.
216 semaphore_destroy_internal(
218 semaphore_t semaphore
)
227 if (semaphore
->owner
!= task
) {
229 return KERN_INVALID_ARGUMENT
;
231 spl_level
= splsched();
232 semaphore_lock(semaphore
);
234 remqueue((queue_entry_t
) semaphore
);
235 semaphore
->owner
= TASK_NULL
;
236 task
->semaphores_owned
--;
241 * Deactivate semaphore
243 assert(semaphore
->active
);
244 semaphore
->active
= FALSE
;
247 * Wakeup blocked threads
249 old_count
= semaphore
->count
;
250 semaphore
->count
= 0;
253 waitq_wakeup64_all_locked(&semaphore
->waitq
,
255 THREAD_RESTART
, NULL
,
256 WAITQ_ALL_PRIORITIES
,
258 /* waitq/semaphore is unlocked */
260 semaphore_unlock(semaphore
);
268 * Routine: semaphore_destroy
270 * Destroys a semaphore and consume the caller's reference on the
276 semaphore_t semaphore
)
280 if (semaphore
== SEMAPHORE_NULL
)
281 return KERN_INVALID_ARGUMENT
;
283 if (task
== TASK_NULL
) {
284 kr
= KERN_INVALID_ARGUMENT
;
286 kr
= semaphore_destroy_internal(task
, semaphore
);
288 semaphore_dereference(semaphore
);
293 * Routine: semaphore_signal_internal
295 * Signals the semaphore as direct.
297 * Semaphore is locked.
300 semaphore_signal_internal(
301 semaphore_t semaphore
,
308 spl_level
= splsched();
309 semaphore_lock(semaphore
);
311 if (!semaphore
->active
) {
312 semaphore_unlock(semaphore
);
314 return KERN_TERMINATED
;
317 if (thread
!= THREAD_NULL
) {
318 if (semaphore
->count
< 0) {
319 kr
= waitq_wakeup64_thread_locked(
325 /* waitq/semaphore is unlocked */
327 kr
= KERN_NOT_WAITING
;
328 semaphore_unlock(semaphore
);
334 if (options
& SEMAPHORE_SIGNAL_ALL
) {
335 int old_count
= semaphore
->count
;
337 kr
= KERN_NOT_WAITING
;
339 semaphore
->count
= 0; /* always reset */
340 kr
= waitq_wakeup64_all_locked(
343 THREAD_AWAKENED
, NULL
,
344 WAITQ_ALL_PRIORITIES
,
346 /* waitq / semaphore is unlocked */
348 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
351 semaphore_unlock(semaphore
);
357 if (semaphore
->count
< 0) {
358 kr
= waitq_wakeup64_one_locked(
361 THREAD_AWAKENED
, NULL
,
362 WAITQ_ALL_PRIORITIES
,
364 if (kr
== KERN_SUCCESS
) {
365 semaphore_unlock(semaphore
);
369 semaphore
->count
= 0; /* all waiters gone */
373 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
377 semaphore_unlock(semaphore
);
379 return KERN_NOT_WAITING
;
383 * Routine: semaphore_signal_thread
385 * If the specified thread is blocked on the semaphore, it is
386 * woken up. If a NULL thread was supplied, then any one
387 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
388 * and the semaphore is unchanged.
391 semaphore_signal_thread(
392 semaphore_t semaphore
,
397 if (semaphore
== SEMAPHORE_NULL
)
398 return KERN_INVALID_ARGUMENT
;
400 ret
= semaphore_signal_internal(semaphore
,
402 SEMAPHORE_OPTION_NONE
);
407 * Routine: semaphore_signal_thread_trap
409 * Trap interface to the semaphore_signal_thread function.
412 semaphore_signal_thread_trap(
413 struct semaphore_signal_thread_trap_args
*args
)
415 mach_port_name_t sema_name
= args
->signal_name
;
416 mach_port_name_t thread_name
= args
->thread_name
;
417 semaphore_t semaphore
;
422 * MACH_PORT_NULL is not an error. It means that we want to
423 * select any one thread that is already waiting, but not to
424 * pre-post the semaphore.
426 if (thread_name
!= MACH_PORT_NULL
) {
427 thread
= port_name_to_thread(thread_name
);
428 if (thread
== THREAD_NULL
)
429 return KERN_INVALID_ARGUMENT
;
431 thread
= THREAD_NULL
;
433 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
434 if (kr
== KERN_SUCCESS
) {
435 kr
= semaphore_signal_internal(semaphore
,
437 SEMAPHORE_OPTION_NONE
);
438 semaphore_dereference(semaphore
);
440 if (thread
!= THREAD_NULL
) {
441 thread_deallocate(thread
);
449 * Routine: semaphore_signal
451 * Traditional (in-kernel client and MIG interface) semaphore
452 * signal routine. Most users will access the trap version.
454 * This interface in not defined to return info about whether
455 * this call found a thread waiting or not. The internal
456 * routines (and future external routines) do. We have to
457 * convert those into plain KERN_SUCCESS returns.
461 semaphore_t semaphore
)
465 if (semaphore
== SEMAPHORE_NULL
)
466 return KERN_INVALID_ARGUMENT
;
468 kr
= semaphore_signal_internal(semaphore
,
470 SEMAPHORE_SIGNAL_PREPOST
);
471 if (kr
== KERN_NOT_WAITING
)
477 * Routine: semaphore_signal_trap
479 * Trap interface to the semaphore_signal function.
482 semaphore_signal_trap(
483 struct semaphore_signal_trap_args
*args
)
485 mach_port_name_t sema_name
= args
->signal_name
;
487 return (semaphore_signal_internal_trap(sema_name
));
491 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
493 semaphore_t semaphore
;
496 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
497 if (kr
== KERN_SUCCESS
) {
498 kr
= semaphore_signal_internal(semaphore
,
500 SEMAPHORE_SIGNAL_PREPOST
);
501 semaphore_dereference(semaphore
);
502 if (kr
== KERN_NOT_WAITING
)
509 * Routine: semaphore_signal_all
511 * Awakens ALL threads currently blocked on the semaphore.
512 * The semaphore count returns to zero.
515 semaphore_signal_all(
516 semaphore_t semaphore
)
520 if (semaphore
== SEMAPHORE_NULL
)
521 return KERN_INVALID_ARGUMENT
;
523 kr
= semaphore_signal_internal(semaphore
,
525 SEMAPHORE_SIGNAL_ALL
);
526 if (kr
== KERN_NOT_WAITING
)
532 * Routine: semaphore_signal_all_trap
534 * Trap interface to the semaphore_signal_all function.
537 semaphore_signal_all_trap(
538 struct semaphore_signal_all_trap_args
*args
)
540 mach_port_name_t sema_name
= args
->signal_name
;
541 semaphore_t semaphore
;
544 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
545 if (kr
== KERN_SUCCESS
) {
546 kr
= semaphore_signal_internal(semaphore
,
548 SEMAPHORE_SIGNAL_ALL
);
549 semaphore_dereference(semaphore
);
550 if (kr
== KERN_NOT_WAITING
)
557 * Routine: semaphore_convert_wait_result
559 * Generate the return code after a semaphore wait/block. It
560 * takes the wait result as an input and coverts that to an
561 * appropriate result.
564 semaphore_convert_wait_result(int wait_result
)
566 switch (wait_result
) {
567 case THREAD_AWAKENED
:
570 case THREAD_TIMED_OUT
:
571 return KERN_OPERATION_TIMED_OUT
;
573 case THREAD_INTERRUPTED
:
577 return KERN_TERMINATED
;
580 panic("semaphore_block\n");
586 * Routine: semaphore_wait_continue
588 * Common continuation routine after waiting on a semphore.
589 * It returns directly to user space.
592 semaphore_wait_continue(void)
594 thread_t self
= current_thread();
595 int wait_result
= self
->wait_result
;
596 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
598 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
599 semaphore_dereference(self
->sth_waitsemaphore
);
600 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
601 semaphore_dereference(self
->sth_signalsemaphore
);
603 assert(caller_cont
!= (void (*)(kern_return_t
))0);
604 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
608 * Routine: semaphore_wait_internal
610 * Decrements the semaphore count by one. If the count is
611 * negative after the decrement, the calling thread blocks
612 * (possibly at a continuation and/or with a timeout).
616 * A reference is held on the signal semaphore.
619 semaphore_wait_internal(
620 semaphore_t wait_semaphore
,
621 semaphore_t signal_semaphore
,
624 void (*caller_cont
)(kern_return_t
))
628 kern_return_t kr
= KERN_ALREADY_WAITING
;
630 spl_level
= splsched();
631 semaphore_lock(wait_semaphore
);
633 if (!wait_semaphore
->active
) {
634 kr
= KERN_TERMINATED
;
635 } else if (wait_semaphore
->count
> 0) {
636 wait_semaphore
->count
--;
638 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
639 kr
= KERN_OPERATION_TIMED_OUT
;
641 thread_t self
= current_thread();
643 wait_semaphore
->count
= -1; /* we don't keep an actual count */
645 (void)waitq_assert_wait64_locked(
646 &wait_semaphore
->waitq
,
649 TIMEOUT_URGENCY_USER_NORMAL
,
650 deadline
, TIMEOUT_NO_LEEWAY
,
654 semaphore_unlock(wait_semaphore
);
658 * wait_semaphore is unlocked so we are free to go ahead and
659 * signal the signal_semaphore (if one was provided).
661 if (signal_semaphore
!= SEMAPHORE_NULL
) {
662 kern_return_t signal_kr
;
665 * lock the signal semaphore reference we got and signal it.
666 * This will NOT block (we cannot block after having asserted
667 * our intention to wait above).
669 signal_kr
= semaphore_signal_internal(signal_semaphore
,
671 SEMAPHORE_SIGNAL_PREPOST
);
673 if (signal_kr
== KERN_NOT_WAITING
)
674 signal_kr
= KERN_SUCCESS
;
675 else if (signal_kr
== KERN_TERMINATED
) {
677 * Uh!Oh! The semaphore we were to signal died.
678 * We have to get ourselves out of the wait in
679 * case we get stuck here forever (it is assumed
680 * that the semaphore we were posting is gating
681 * the decision by someone else to post the
682 * semaphore we are waiting on). People will
683 * discover the other dead semaphore soon enough.
684 * If we got out of the wait cleanly (someone
685 * already posted a wakeup to us) then return that
686 * (most important) result. Otherwise,
687 * return the KERN_TERMINATED status.
689 thread_t self
= current_thread();
691 clear_wait(self
, THREAD_INTERRUPTED
);
692 kr
= semaphore_convert_wait_result(self
->wait_result
);
693 if (kr
== KERN_ABORTED
)
694 kr
= KERN_TERMINATED
;
699 * If we had an error, or we didn't really need to wait we can
700 * return now that we have signalled the signal semaphore.
702 if (kr
!= KERN_ALREADY_WAITING
)
706 * Now, we can block. If the caller supplied a continuation
707 * pointer of his own for after the block, block with the
708 * appropriate semaphore continuation. Thiswill gather the
709 * semaphore results, release references on the semaphore(s),
710 * and then call the caller's continuation.
713 thread_t self
= current_thread();
715 self
->sth_continuation
= caller_cont
;
716 self
->sth_waitsemaphore
= wait_semaphore
;
717 self
->sth_signalsemaphore
= signal_semaphore
;
718 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
721 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
724 return (semaphore_convert_wait_result(wait_result
));
729 * Routine: semaphore_wait
731 * Traditional (non-continuation) interface presented to
732 * in-kernel clients to wait on a semaphore.
736 semaphore_t semaphore
)
739 if (semaphore
== SEMAPHORE_NULL
)
740 return KERN_INVALID_ARGUMENT
;
742 return(semaphore_wait_internal(semaphore
,
744 0ULL, SEMAPHORE_OPTION_NONE
,
745 (void (*)(kern_return_t
))0));
749 semaphore_wait_noblock(
750 semaphore_t semaphore
)
753 if (semaphore
== SEMAPHORE_NULL
)
754 return KERN_INVALID_ARGUMENT
;
756 return(semaphore_wait_internal(semaphore
,
758 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
759 (void (*)(kern_return_t
))0));
763 semaphore_wait_deadline(
764 semaphore_t semaphore
,
768 if (semaphore
== SEMAPHORE_NULL
)
769 return KERN_INVALID_ARGUMENT
;
771 return(semaphore_wait_internal(semaphore
,
773 deadline
, SEMAPHORE_OPTION_NONE
,
774 (void (*)(kern_return_t
))0));
778 * Trap: semaphore_wait_trap
780 * Trap version of semaphore wait. Called on behalf of user-level
786 struct semaphore_wait_trap_args
*args
)
788 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
794 semaphore_wait_trap_internal(
795 mach_port_name_t name
,
796 void (*caller_cont
)(kern_return_t
))
798 semaphore_t semaphore
;
801 kr
= port_name_to_semaphore(name
, &semaphore
);
802 if (kr
== KERN_SUCCESS
) {
803 kr
= semaphore_wait_internal(semaphore
,
805 0ULL, SEMAPHORE_OPTION_NONE
,
807 semaphore_dereference(semaphore
);
813 * Routine: semaphore_timedwait
815 * Traditional (non-continuation) interface presented to
816 * in-kernel clients to wait on a semaphore with a timeout.
818 * A timeout of {0,0} is considered non-blocking.
822 semaphore_t semaphore
,
823 mach_timespec_t wait_time
)
825 int option
= SEMAPHORE_OPTION_NONE
;
826 uint64_t deadline
= 0;
828 if (semaphore
== SEMAPHORE_NULL
)
829 return KERN_INVALID_ARGUMENT
;
831 if(BAD_MACH_TIMESPEC(&wait_time
))
832 return KERN_INVALID_VALUE
;
834 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
835 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
837 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
839 return (semaphore_wait_internal(semaphore
,
842 (void(*)(kern_return_t
))0));
847 * Trap: semaphore_timedwait_trap
849 * Trap version of a semaphore_timedwait. The timeout parameter
850 * is passed in two distinct parts and re-assembled on this side
851 * of the trap interface (to accomodate calling conventions that
852 * pass structures as pointers instead of inline in registers without
853 * having to add a copyin).
855 * A timeout of {0,0} is considered non-blocking.
858 semaphore_timedwait_trap(
859 struct semaphore_timedwait_trap_args
*args
)
862 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
867 semaphore_timedwait_trap_internal(
868 mach_port_name_t name
,
871 void (*caller_cont
)(kern_return_t
))
873 semaphore_t semaphore
;
874 mach_timespec_t wait_time
;
877 wait_time
.tv_sec
= sec
;
878 wait_time
.tv_nsec
= nsec
;
879 if(BAD_MACH_TIMESPEC(&wait_time
))
880 return KERN_INVALID_VALUE
;
882 kr
= port_name_to_semaphore(name
, &semaphore
);
883 if (kr
== KERN_SUCCESS
) {
884 int option
= SEMAPHORE_OPTION_NONE
;
885 uint64_t deadline
= 0;
887 if (sec
== 0 && nsec
== 0)
888 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
890 deadline
= semaphore_deadline(sec
, nsec
);
892 kr
= semaphore_wait_internal(semaphore
,
896 semaphore_dereference(semaphore
);
902 * Routine: semaphore_wait_signal
904 * Atomically register a wait on a semaphore and THEN signal
905 * another. This is the in-kernel entry point that does not
906 * block at a continuation and does not free a signal_semaphore
910 semaphore_wait_signal(
911 semaphore_t wait_semaphore
,
912 semaphore_t signal_semaphore
)
914 if (wait_semaphore
== SEMAPHORE_NULL
)
915 return KERN_INVALID_ARGUMENT
;
917 return(semaphore_wait_internal(wait_semaphore
,
919 0ULL, SEMAPHORE_OPTION_NONE
,
920 (void(*)(kern_return_t
))0));
924 * Trap: semaphore_wait_signal_trap
926 * Atomically register a wait on a semaphore and THEN signal
927 * another. This is the trap version from user space.
930 semaphore_wait_signal_trap(
931 struct semaphore_wait_signal_trap_args
*args
)
933 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
937 semaphore_wait_signal_trap_internal(
938 mach_port_name_t wait_name
,
939 mach_port_name_t signal_name
,
940 void (*caller_cont
)(kern_return_t
))
942 semaphore_t wait_semaphore
;
943 semaphore_t signal_semaphore
;
946 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
947 if (kr
== KERN_SUCCESS
) {
948 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
949 if (kr
== KERN_SUCCESS
) {
950 kr
= semaphore_wait_internal(wait_semaphore
,
952 0ULL, SEMAPHORE_OPTION_NONE
,
954 semaphore_dereference(wait_semaphore
);
956 semaphore_dereference(signal_semaphore
);
963 * Routine: semaphore_timedwait_signal
965 * Atomically register a wait on a semaphore and THEN signal
966 * another. This is the in-kernel entry point that does not
967 * block at a continuation.
969 * A timeout of {0,0} is considered non-blocking.
972 semaphore_timedwait_signal(
973 semaphore_t wait_semaphore
,
974 semaphore_t signal_semaphore
,
975 mach_timespec_t wait_time
)
977 int option
= SEMAPHORE_OPTION_NONE
;
978 uint64_t deadline
= 0;
980 if (wait_semaphore
== SEMAPHORE_NULL
)
981 return KERN_INVALID_ARGUMENT
;
983 if(BAD_MACH_TIMESPEC(&wait_time
))
984 return KERN_INVALID_VALUE
;
986 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
987 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
989 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
991 return(semaphore_wait_internal(wait_semaphore
,
994 (void(*)(kern_return_t
))0));
998 * Trap: semaphore_timedwait_signal_trap
1000 * Atomically register a timed wait on a semaphore and THEN signal
1001 * another. This is the trap version from user space.
1004 semaphore_timedwait_signal_trap(
1005 struct semaphore_timedwait_signal_trap_args
*args
)
1007 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1011 semaphore_timedwait_signal_trap_internal(
1012 mach_port_name_t wait_name
,
1013 mach_port_name_t signal_name
,
1016 void (*caller_cont
)(kern_return_t
))
1018 semaphore_t wait_semaphore
;
1019 semaphore_t signal_semaphore
;
1020 mach_timespec_t wait_time
;
1023 wait_time
.tv_sec
= sec
;
1024 wait_time
.tv_nsec
= nsec
;
1025 if(BAD_MACH_TIMESPEC(&wait_time
))
1026 return KERN_INVALID_VALUE
;
1028 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1029 if (kr
== KERN_SUCCESS
) {
1030 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1031 if (kr
== KERN_SUCCESS
) {
1032 int option
= SEMAPHORE_OPTION_NONE
;
1033 uint64_t deadline
= 0;
1035 if (sec
== 0 && nsec
== 0)
1036 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1038 deadline
= semaphore_deadline(sec
, nsec
);
1040 kr
= semaphore_wait_internal(wait_semaphore
,
1044 semaphore_dereference(wait_semaphore
);
1046 semaphore_dereference(signal_semaphore
);
1053 * Routine: semaphore_reference
1055 * Take out a reference on a semaphore. This keeps the data structure
1056 * in existence (but the semaphore may be deactivated).
1059 semaphore_reference(
1060 semaphore_t semaphore
)
1062 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1066 * Routine: semaphore_dereference
1068 * Release a reference on a semaphore. If this is the last reference,
1069 * the semaphore data structure is deallocated.
1072 semaphore_dereference(
1073 semaphore_t semaphore
)
1075 if (semaphore
== NULL
)
1078 if (hw_atomic_sub(&semaphore
->ref_count
, 1) != 0)
1082 * Last ref, clean up the port [if any]
1083 * associated with the semaphore, destroy
1084 * it (if still active) and then free
1087 ipc_port_t port
= semaphore
->port
;
1089 if (IP_VALID(port
)) {
1090 assert(!port
->ip_srights
);
1091 ipc_port_dealloc_kernel(port
);
1093 if (semaphore
->active
) {
1094 assert(semaphore
->owner
!= TASK_NULL
);
1095 semaphore_destroy_internal(semaphore
->owner
, semaphore
);
1097 zfree(semaphore_zone
, semaphore
);