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 os_refgrp_decl(static, sema_refgrp
, "semaphore", NULL
);
71 /* Forward declarations */
75 semaphore_wait_trap_internal(
76 mach_port_name_t name
,
77 void (*caller_cont
)(kern_return_t
));
80 semaphore_wait_signal_trap_internal(
81 mach_port_name_t wait_name
,
82 mach_port_name_t signal_name
,
83 void (*caller_cont
)(kern_return_t
));
86 semaphore_timedwait_trap_internal(
87 mach_port_name_t name
,
90 void (*caller_cont
)(kern_return_t
));
93 semaphore_timedwait_signal_trap_internal(
94 mach_port_name_t wait_name
,
95 mach_port_name_t signal_name
,
98 void (*caller_cont
)(kern_return_t
));
101 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
104 semaphore_signal_internal(
105 semaphore_t semaphore
,
110 semaphore_convert_wait_result(
114 semaphore_wait_continue(void);
117 semaphore_wait_internal(
118 semaphore_t wait_semaphore
,
119 semaphore_t signal_semaphore
,
122 void (*caller_cont
)(kern_return_t
));
124 static __inline__
uint64_t
131 nanoseconds_to_absolutetime((uint64_t)sec
* NSEC_PER_SEC
+ nsec
, &abstime
);
132 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
138 * ROUTINE: semaphore_init [private]
140 * Initialize the semaphore mechanisms.
141 * Right now, we only need to initialize the semaphore zone.
146 semaphore_zone
= zinit(sizeof(struct semaphore
),
147 semaphore_max
* sizeof(struct semaphore
),
148 sizeof(struct semaphore
),
150 zone_change(semaphore_zone
, Z_NOENCRYPT
, TRUE
);
154 * Routine: semaphore_create
156 * Creates a semaphore.
157 * The port representing the semaphore is returned as a parameter.
162 semaphore_t
*new_semaphore
,
166 semaphore_t s
= SEMAPHORE_NULL
;
170 *new_semaphore
= SEMAPHORE_NULL
;
171 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
172 return KERN_INVALID_ARGUMENT
;
175 s
= (semaphore_t
) zalloc(semaphore_zone
);
177 if (s
== SEMAPHORE_NULL
) {
178 return KERN_RESOURCE_SHORTAGE
;
181 kret
= waitq_init(&s
->waitq
, policy
| SYNC_POLICY_DISABLE_IRQ
); /* also inits lock */
182 if (kret
!= KERN_SUCCESS
) {
183 zfree(semaphore_zone
, s
);
188 * Initialize the semaphore values.
191 os_ref_init(&s
->ref_count
, &sema_refgrp
);
197 * Associate the new semaphore with the task by adding
198 * the new semaphore to the task's semaphore list.
201 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
202 task
->semaphores_owned
++;
211 * Routine: semaphore_destroy_internal
213 * Disassociate a semaphore from its owning task, mark it inactive,
214 * and set any waiting threads running with THREAD_RESTART.
218 * semaphore is locked
219 * semaphore is owned by the specified task
221 * with semaphore unlocked
224 semaphore_destroy_internal(
226 semaphore_t semaphore
)
230 /* unlink semaphore from owning task */
231 assert(semaphore
->owner
== task
);
232 remqueue((queue_entry_t
) semaphore
);
233 semaphore
->owner
= TASK_NULL
;
234 task
->semaphores_owned
--;
237 * Deactivate semaphore
239 assert(semaphore
->active
);
240 semaphore
->active
= FALSE
;
243 * Wakeup blocked threads
245 old_count
= semaphore
->count
;
246 semaphore
->count
= 0;
249 waitq_wakeup64_all_locked(&semaphore
->waitq
,
251 THREAD_RESTART
, NULL
,
252 WAITQ_ALL_PRIORITIES
,
254 /* waitq/semaphore is unlocked */
256 semaphore_unlock(semaphore
);
261 * Routine: semaphore_destroy
263 * Destroys a semaphore and consume the caller's reference on the
269 semaphore_t semaphore
)
273 if (semaphore
== SEMAPHORE_NULL
) {
274 return KERN_INVALID_ARGUMENT
;
277 if (task
== TASK_NULL
) {
278 semaphore_dereference(semaphore
);
279 return KERN_INVALID_ARGUMENT
;
283 spl_level
= splsched();
284 semaphore_lock(semaphore
);
286 if (semaphore
->owner
!= task
) {
287 semaphore_unlock(semaphore
);
288 semaphore_dereference(semaphore
);
291 return KERN_INVALID_ARGUMENT
;
294 semaphore_destroy_internal(task
, semaphore
);
295 /* semaphore unlocked */
300 semaphore_dereference(semaphore
);
305 * Routine: semaphore_destroy_all
307 * Destroy all the semaphores associated with a given task.
309 #define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
312 semaphore_destroy_all(
320 while (!queue_empty(&task
->semaphore_list
)) {
321 semaphore_t semaphore
;
323 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
326 spl_level
= splsched();
328 semaphore_lock(semaphore
);
330 semaphore_destroy_internal(task
, semaphore
);
331 /* semaphore unlocked */
333 /* throttle number of semaphores per interrupt disablement */
334 if (++count
== SEMASPERSPL
) {
347 * Routine: semaphore_signal_internal
349 * Signals the semaphore as direct.
351 * Semaphore is locked.
354 semaphore_signal_internal(
355 semaphore_t semaphore
,
362 spl_level
= splsched();
363 semaphore_lock(semaphore
);
365 if (!semaphore
->active
) {
366 semaphore_unlock(semaphore
);
368 return KERN_TERMINATED
;
371 if (thread
!= THREAD_NULL
) {
372 if (semaphore
->count
< 0) {
373 kr
= waitq_wakeup64_thread_locked(
379 /* waitq/semaphore is unlocked */
381 kr
= KERN_NOT_WAITING
;
382 semaphore_unlock(semaphore
);
388 if (options
& SEMAPHORE_SIGNAL_ALL
) {
389 int old_count
= semaphore
->count
;
391 kr
= KERN_NOT_WAITING
;
393 semaphore
->count
= 0; /* always reset */
394 kr
= waitq_wakeup64_all_locked(
397 THREAD_AWAKENED
, NULL
,
398 WAITQ_ALL_PRIORITIES
,
400 /* waitq / semaphore is unlocked */
402 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
406 semaphore_unlock(semaphore
);
412 if (semaphore
->count
< 0) {
413 kr
= waitq_wakeup64_one_locked(
416 THREAD_AWAKENED
, NULL
,
417 WAITQ_ALL_PRIORITIES
,
419 if (kr
== KERN_SUCCESS
) {
420 semaphore_unlock(semaphore
);
424 semaphore
->count
= 0; /* all waiters gone */
428 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
432 semaphore_unlock(semaphore
);
434 return KERN_NOT_WAITING
;
438 * Routine: semaphore_signal_thread
440 * If the specified thread is blocked on the semaphore, it is
441 * woken up. If a NULL thread was supplied, then any one
442 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
443 * and the semaphore is unchanged.
446 semaphore_signal_thread(
447 semaphore_t semaphore
,
452 if (semaphore
== SEMAPHORE_NULL
) {
453 return KERN_INVALID_ARGUMENT
;
456 ret
= semaphore_signal_internal(semaphore
,
458 SEMAPHORE_OPTION_NONE
);
463 * Routine: semaphore_signal_thread_trap
465 * Trap interface to the semaphore_signal_thread function.
468 semaphore_signal_thread_trap(
469 struct semaphore_signal_thread_trap_args
*args
)
471 mach_port_name_t sema_name
= args
->signal_name
;
472 mach_port_name_t thread_name
= args
->thread_name
;
473 semaphore_t semaphore
;
478 * MACH_PORT_NULL is not an error. It means that we want to
479 * select any one thread that is already waiting, but not to
480 * pre-post the semaphore.
482 if (thread_name
!= MACH_PORT_NULL
) {
483 thread
= port_name_to_thread(thread_name
, PORT_TO_THREAD_NONE
);
484 if (thread
== THREAD_NULL
) {
485 return KERN_INVALID_ARGUMENT
;
488 thread
= THREAD_NULL
;
491 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
492 if (kr
== KERN_SUCCESS
) {
493 kr
= semaphore_signal_internal(semaphore
,
495 SEMAPHORE_OPTION_NONE
);
496 semaphore_dereference(semaphore
);
498 if (thread
!= THREAD_NULL
) {
499 thread_deallocate(thread
);
507 * Routine: semaphore_signal
509 * Traditional (in-kernel client and MIG interface) semaphore
510 * signal routine. Most users will access the trap version.
512 * This interface in not defined to return info about whether
513 * this call found a thread waiting or not. The internal
514 * routines (and future external routines) do. We have to
515 * convert those into plain KERN_SUCCESS returns.
519 semaphore_t semaphore
)
523 if (semaphore
== SEMAPHORE_NULL
) {
524 return KERN_INVALID_ARGUMENT
;
527 kr
= semaphore_signal_internal(semaphore
,
529 SEMAPHORE_SIGNAL_PREPOST
);
530 if (kr
== KERN_NOT_WAITING
) {
537 * Routine: semaphore_signal_trap
539 * Trap interface to the semaphore_signal function.
542 semaphore_signal_trap(
543 struct semaphore_signal_trap_args
*args
)
545 mach_port_name_t sema_name
= args
->signal_name
;
547 return semaphore_signal_internal_trap(sema_name
);
551 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
553 semaphore_t semaphore
;
556 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
557 if (kr
== KERN_SUCCESS
) {
558 kr
= semaphore_signal_internal(semaphore
,
560 SEMAPHORE_SIGNAL_PREPOST
);
561 semaphore_dereference(semaphore
);
562 if (kr
== KERN_NOT_WAITING
) {
570 * Routine: semaphore_signal_all
572 * Awakens ALL threads currently blocked on the semaphore.
573 * The semaphore count returns to zero.
576 semaphore_signal_all(
577 semaphore_t semaphore
)
581 if (semaphore
== SEMAPHORE_NULL
) {
582 return KERN_INVALID_ARGUMENT
;
585 kr
= semaphore_signal_internal(semaphore
,
587 SEMAPHORE_SIGNAL_ALL
);
588 if (kr
== KERN_NOT_WAITING
) {
595 * Routine: semaphore_signal_all_trap
597 * Trap interface to the semaphore_signal_all function.
600 semaphore_signal_all_trap(
601 struct semaphore_signal_all_trap_args
*args
)
603 mach_port_name_t sema_name
= args
->signal_name
;
604 semaphore_t semaphore
;
607 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
608 if (kr
== KERN_SUCCESS
) {
609 kr
= semaphore_signal_internal(semaphore
,
611 SEMAPHORE_SIGNAL_ALL
);
612 semaphore_dereference(semaphore
);
613 if (kr
== KERN_NOT_WAITING
) {
621 * Routine: semaphore_convert_wait_result
623 * Generate the return code after a semaphore wait/block. It
624 * takes the wait result as an input and coverts that to an
625 * appropriate result.
628 semaphore_convert_wait_result(int wait_result
)
630 switch (wait_result
) {
631 case THREAD_AWAKENED
:
634 case THREAD_TIMED_OUT
:
635 return KERN_OPERATION_TIMED_OUT
;
637 case THREAD_INTERRUPTED
:
641 return KERN_TERMINATED
;
644 panic("semaphore_block\n");
650 * Routine: semaphore_wait_continue
652 * Common continuation routine after waiting on a semphore.
653 * It returns directly to user space.
656 semaphore_wait_continue(void)
658 thread_t self
= current_thread();
659 int wait_result
= self
->wait_result
;
660 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
662 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
663 semaphore_dereference(self
->sth_waitsemaphore
);
664 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
) {
665 semaphore_dereference(self
->sth_signalsemaphore
);
668 assert(caller_cont
!= (void (*)(kern_return_t
))0);
669 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
673 * Routine: semaphore_wait_internal
675 * Decrements the semaphore count by one. If the count is
676 * negative after the decrement, the calling thread blocks
677 * (possibly at a continuation and/or with a timeout).
681 * A reference is held on the signal semaphore.
684 semaphore_wait_internal(
685 semaphore_t wait_semaphore
,
686 semaphore_t signal_semaphore
,
689 void (*caller_cont
)(kern_return_t
))
693 kern_return_t kr
= KERN_ALREADY_WAITING
;
695 spl_level
= splsched();
696 semaphore_lock(wait_semaphore
);
698 if (!wait_semaphore
->active
) {
699 kr
= KERN_TERMINATED
;
700 } else if (wait_semaphore
->count
> 0) {
701 wait_semaphore
->count
--;
703 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
704 kr
= KERN_OPERATION_TIMED_OUT
;
706 thread_t self
= current_thread();
708 wait_semaphore
->count
= -1; /* we don't keep an actual count */
710 thread_set_pending_block_hint(self
, kThreadWaitSemaphore
);
711 (void)waitq_assert_wait64_locked(
712 &wait_semaphore
->waitq
,
715 TIMEOUT_URGENCY_USER_NORMAL
,
716 deadline
, TIMEOUT_NO_LEEWAY
,
719 semaphore_unlock(wait_semaphore
);
723 * wait_semaphore is unlocked so we are free to go ahead and
724 * signal the signal_semaphore (if one was provided).
726 if (signal_semaphore
!= SEMAPHORE_NULL
) {
727 kern_return_t signal_kr
;
730 * lock the signal semaphore reference we got and signal it.
731 * This will NOT block (we cannot block after having asserted
732 * our intention to wait above).
734 signal_kr
= semaphore_signal_internal(signal_semaphore
,
736 SEMAPHORE_SIGNAL_PREPOST
);
738 if (signal_kr
== KERN_NOT_WAITING
) {
739 signal_kr
= KERN_SUCCESS
;
740 } else if (signal_kr
== KERN_TERMINATED
) {
742 * Uh!Oh! The semaphore we were to signal died.
743 * We have to get ourselves out of the wait in
744 * case we get stuck here forever (it is assumed
745 * that the semaphore we were posting is gating
746 * the decision by someone else to post the
747 * semaphore we are waiting on). People will
748 * discover the other dead semaphore soon enough.
749 * If we got out of the wait cleanly (someone
750 * already posted a wakeup to us) then return that
751 * (most important) result. Otherwise,
752 * return the KERN_TERMINATED status.
754 thread_t self
= current_thread();
756 clear_wait(self
, THREAD_INTERRUPTED
);
757 kr
= semaphore_convert_wait_result(self
->wait_result
);
758 if (kr
== KERN_ABORTED
) {
759 kr
= KERN_TERMINATED
;
765 * If we had an error, or we didn't really need to wait we can
766 * return now that we have signalled the signal semaphore.
768 if (kr
!= KERN_ALREADY_WAITING
) {
773 * Now, we can block. If the caller supplied a continuation
774 * pointer of his own for after the block, block with the
775 * appropriate semaphore continuation. Thiswill gather the
776 * semaphore results, release references on the semaphore(s),
777 * and then call the caller's continuation.
780 thread_t self
= current_thread();
782 self
->sth_continuation
= caller_cont
;
783 self
->sth_waitsemaphore
= wait_semaphore
;
784 self
->sth_signalsemaphore
= signal_semaphore
;
785 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
787 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
790 return semaphore_convert_wait_result(wait_result
);
795 * Routine: semaphore_wait
797 * Traditional (non-continuation) interface presented to
798 * in-kernel clients to wait on a semaphore.
802 semaphore_t semaphore
)
804 if (semaphore
== SEMAPHORE_NULL
) {
805 return KERN_INVALID_ARGUMENT
;
808 return semaphore_wait_internal(semaphore
,
810 0ULL, SEMAPHORE_OPTION_NONE
,
811 (void (*)(kern_return_t
))0);
815 semaphore_wait_noblock(
816 semaphore_t semaphore
)
818 if (semaphore
== SEMAPHORE_NULL
) {
819 return KERN_INVALID_ARGUMENT
;
822 return semaphore_wait_internal(semaphore
,
824 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
825 (void (*)(kern_return_t
))0);
829 semaphore_wait_deadline(
830 semaphore_t semaphore
,
833 if (semaphore
== SEMAPHORE_NULL
) {
834 return KERN_INVALID_ARGUMENT
;
837 return semaphore_wait_internal(semaphore
,
839 deadline
, SEMAPHORE_OPTION_NONE
,
840 (void (*)(kern_return_t
))0);
844 * Trap: semaphore_wait_trap
846 * Trap version of semaphore wait. Called on behalf of user-level
852 struct semaphore_wait_trap_args
*args
)
854 return semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
);
860 semaphore_wait_trap_internal(
861 mach_port_name_t name
,
862 void (*caller_cont
)(kern_return_t
))
864 semaphore_t semaphore
;
867 kr
= port_name_to_semaphore(name
, &semaphore
);
868 if (kr
== KERN_SUCCESS
) {
869 kr
= semaphore_wait_internal(semaphore
,
871 0ULL, SEMAPHORE_OPTION_NONE
,
873 semaphore_dereference(semaphore
);
879 * Routine: semaphore_timedwait
881 * Traditional (non-continuation) interface presented to
882 * in-kernel clients to wait on a semaphore with a timeout.
884 * A timeout of {0,0} is considered non-blocking.
888 semaphore_t semaphore
,
889 mach_timespec_t wait_time
)
891 int option
= SEMAPHORE_OPTION_NONE
;
892 uint64_t deadline
= 0;
894 if (semaphore
== SEMAPHORE_NULL
) {
895 return KERN_INVALID_ARGUMENT
;
898 if (BAD_MACH_TIMESPEC(&wait_time
)) {
899 return KERN_INVALID_VALUE
;
902 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
903 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
905 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
908 return semaphore_wait_internal(semaphore
,
911 (void (*)(kern_return_t
))0);
915 * Trap: semaphore_timedwait_trap
917 * Trap version of a semaphore_timedwait. The timeout parameter
918 * is passed in two distinct parts and re-assembled on this side
919 * of the trap interface (to accomodate calling conventions that
920 * pass structures as pointers instead of inline in registers without
921 * having to add a copyin).
923 * A timeout of {0,0} is considered non-blocking.
926 semaphore_timedwait_trap(
927 struct semaphore_timedwait_trap_args
*args
)
929 return semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
934 semaphore_timedwait_trap_internal(
935 mach_port_name_t name
,
938 void (*caller_cont
)(kern_return_t
))
940 semaphore_t semaphore
;
941 mach_timespec_t wait_time
;
944 wait_time
.tv_sec
= sec
;
945 wait_time
.tv_nsec
= nsec
;
946 if (BAD_MACH_TIMESPEC(&wait_time
)) {
947 return KERN_INVALID_VALUE
;
950 kr
= port_name_to_semaphore(name
, &semaphore
);
951 if (kr
== KERN_SUCCESS
) {
952 int option
= SEMAPHORE_OPTION_NONE
;
953 uint64_t deadline
= 0;
955 if (sec
== 0 && nsec
== 0) {
956 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
958 deadline
= semaphore_deadline(sec
, nsec
);
961 kr
= semaphore_wait_internal(semaphore
,
965 semaphore_dereference(semaphore
);
971 * Routine: semaphore_wait_signal
973 * Atomically register a wait on a semaphore and THEN signal
974 * another. This is the in-kernel entry point that does not
975 * block at a continuation and does not free a signal_semaphore
979 semaphore_wait_signal(
980 semaphore_t wait_semaphore
,
981 semaphore_t signal_semaphore
)
983 if (wait_semaphore
== SEMAPHORE_NULL
) {
984 return KERN_INVALID_ARGUMENT
;
987 return semaphore_wait_internal(wait_semaphore
,
989 0ULL, SEMAPHORE_OPTION_NONE
,
990 (void (*)(kern_return_t
))0);
994 * Trap: semaphore_wait_signal_trap
996 * Atomically register a wait on a semaphore and THEN signal
997 * another. This is the trap version from user space.
1000 semaphore_wait_signal_trap(
1001 struct semaphore_wait_signal_trap_args
*args
)
1003 return semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
);
1007 semaphore_wait_signal_trap_internal(
1008 mach_port_name_t wait_name
,
1009 mach_port_name_t signal_name
,
1010 void (*caller_cont
)(kern_return_t
))
1012 semaphore_t wait_semaphore
;
1013 semaphore_t signal_semaphore
;
1016 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1017 if (kr
== KERN_SUCCESS
) {
1018 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1019 if (kr
== KERN_SUCCESS
) {
1020 kr
= semaphore_wait_internal(wait_semaphore
,
1022 0ULL, SEMAPHORE_OPTION_NONE
,
1024 semaphore_dereference(wait_semaphore
);
1026 semaphore_dereference(signal_semaphore
);
1033 * Routine: semaphore_timedwait_signal
1035 * Atomically register a wait on a semaphore and THEN signal
1036 * another. This is the in-kernel entry point that does not
1037 * block at a continuation.
1039 * A timeout of {0,0} is considered non-blocking.
1042 semaphore_timedwait_signal(
1043 semaphore_t wait_semaphore
,
1044 semaphore_t signal_semaphore
,
1045 mach_timespec_t wait_time
)
1047 int option
= SEMAPHORE_OPTION_NONE
;
1048 uint64_t deadline
= 0;
1050 if (wait_semaphore
== SEMAPHORE_NULL
) {
1051 return KERN_INVALID_ARGUMENT
;
1054 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1055 return KERN_INVALID_VALUE
;
1058 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
1059 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1061 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1064 return semaphore_wait_internal(wait_semaphore
,
1067 (void (*)(kern_return_t
))0);
1071 * Trap: semaphore_timedwait_signal_trap
1073 * Atomically register a timed wait on a semaphore and THEN signal
1074 * another. This is the trap version from user space.
1077 semaphore_timedwait_signal_trap(
1078 struct semaphore_timedwait_signal_trap_args
*args
)
1080 return semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
1084 semaphore_timedwait_signal_trap_internal(
1085 mach_port_name_t wait_name
,
1086 mach_port_name_t signal_name
,
1089 void (*caller_cont
)(kern_return_t
))
1091 semaphore_t wait_semaphore
;
1092 semaphore_t signal_semaphore
;
1093 mach_timespec_t wait_time
;
1096 wait_time
.tv_sec
= sec
;
1097 wait_time
.tv_nsec
= nsec
;
1098 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1099 return KERN_INVALID_VALUE
;
1102 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1103 if (kr
== KERN_SUCCESS
) {
1104 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1105 if (kr
== KERN_SUCCESS
) {
1106 int option
= SEMAPHORE_OPTION_NONE
;
1107 uint64_t deadline
= 0;
1109 if (sec
== 0 && nsec
== 0) {
1110 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1112 deadline
= semaphore_deadline(sec
, nsec
);
1115 kr
= semaphore_wait_internal(wait_semaphore
,
1119 semaphore_dereference(wait_semaphore
);
1121 semaphore_dereference(signal_semaphore
);
1128 * Routine: semaphore_reference
1130 * Take out a reference on a semaphore. This keeps the data structure
1131 * in existence (but the semaphore may be deactivated).
1134 semaphore_reference(
1135 semaphore_t semaphore
)
1137 os_ref_retain(&semaphore
->ref_count
);
1141 * Routine: semaphore_dereference
1143 * Release a reference on a semaphore. If this is the last reference,
1144 * the semaphore data structure is deallocated.
1147 semaphore_dereference(
1148 semaphore_t semaphore
)
1150 uint32_t collisions
;
1153 if (semaphore
== NULL
) {
1157 if (os_ref_release(&semaphore
->ref_count
) > 0) {
1162 * Last ref, clean up the port [if any]
1163 * associated with the semaphore, destroy
1164 * it (if still active) and then free
1167 ipc_port_t port
= semaphore
->port
;
1169 if (IP_VALID(port
)) {
1170 assert(!port
->ip_srights
);
1171 ipc_port_dealloc_kernel(port
);
1175 * Lock the semaphore to lock in the owner task reference.
1176 * Then continue to try to lock the task (inverse order).
1178 spl_level
= splsched();
1179 semaphore_lock(semaphore
);
1180 for (collisions
= 0; semaphore
->active
; collisions
++) {
1181 task_t task
= semaphore
->owner
;
1183 assert(task
!= TASK_NULL
);
1185 if (task_lock_try(task
)) {
1186 semaphore_destroy_internal(task
, semaphore
);
1187 /* semaphore unlocked */
1193 /* failed to get out-of-order locks */
1194 semaphore_unlock(semaphore
);
1196 mutex_pause(collisions
);
1197 spl_level
= splsched();
1198 semaphore_lock(semaphore
);
1200 semaphore_unlock(semaphore
);
1204 zfree(semaphore_zone
, semaphore
);
1207 #define WAITQ_TO_SEMA(wq) ((semaphore_t) ((uintptr_t)(wq) - offsetof(struct semaphore, waitq)))
1209 kdp_sema_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1211 semaphore_t sem
= WAITQ_TO_SEMA(waitq
);
1212 assert(event
== SEMAPHORE_EVENT
);
1213 assert(kdp_is_in_zone(sem
, "semaphores"));
1215 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(sem
->port
);
1217 waitinfo
->owner
= pid_from_task(sem
->owner
);