2 * Copyright (c) 2000-2020 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_DECLARE(semaphore_zone
, "semaphores", sizeof(struct semaphore
), ZC_NONE
);
68 os_refgrp_decl(static, sema_refgrp
, "semaphore", NULL
);
70 /* Forward declarations */
74 semaphore_wait_trap_internal(
75 mach_port_name_t name
,
76 void (*caller_cont
)(kern_return_t
));
79 semaphore_wait_signal_trap_internal(
80 mach_port_name_t wait_name
,
81 mach_port_name_t signal_name
,
82 void (*caller_cont
)(kern_return_t
));
85 semaphore_timedwait_trap_internal(
86 mach_port_name_t name
,
89 void (*caller_cont
)(kern_return_t
));
92 semaphore_timedwait_signal_trap_internal(
93 mach_port_name_t wait_name
,
94 mach_port_name_t signal_name
,
97 void (*caller_cont
)(kern_return_t
));
100 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
103 semaphore_signal_internal(
104 semaphore_t semaphore
,
109 semaphore_convert_wait_result(
113 semaphore_wait_continue(void *arg __unused
, wait_result_t wr
);
116 semaphore_wait_internal(
117 semaphore_t wait_semaphore
,
118 semaphore_t signal_semaphore
,
121 void (*caller_cont
)(kern_return_t
));
123 static __inline__
uint64_t
130 nanoseconds_to_absolutetime((uint64_t)sec
* NSEC_PER_SEC
+ nsec
, &abstime
);
131 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
137 * Routine: semaphore_create
139 * Creates a semaphore.
140 * The port representing the semaphore is returned as a parameter.
145 semaphore_t
*new_semaphore
,
149 semaphore_t s
= SEMAPHORE_NULL
;
152 *new_semaphore
= SEMAPHORE_NULL
;
153 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
|| policy
< 0) {
154 return KERN_INVALID_ARGUMENT
;
157 s
= (semaphore_t
) zalloc(semaphore_zone
);
159 if (s
== SEMAPHORE_NULL
) {
160 return KERN_RESOURCE_SHORTAGE
;
163 kret
= waitq_init(&s
->waitq
, policy
| SYNC_POLICY_DISABLE_IRQ
); /* also inits lock */
164 if (kret
!= KERN_SUCCESS
) {
165 zfree(semaphore_zone
, s
);
170 * Initialize the semaphore values.
173 os_ref_init(&s
->ref_count
, &sema_refgrp
);
179 * Associate the new semaphore with the task by adding
180 * the new semaphore to the task's semaphore list.
183 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
184 task
->semaphores_owned
++;
193 * Routine: semaphore_destroy_internal
195 * Disassociate a semaphore from its owning task, mark it inactive,
196 * and set any waiting threads running with THREAD_RESTART.
200 * semaphore is locked
201 * semaphore is owned by the specified task
203 * with semaphore unlocked
206 semaphore_destroy_internal(
208 semaphore_t semaphore
)
212 /* unlink semaphore from owning task */
213 assert(semaphore
->owner
== task
);
214 remqueue((queue_entry_t
) semaphore
);
215 semaphore
->owner
= TASK_NULL
;
216 task
->semaphores_owned
--;
219 * Deactivate semaphore
221 assert(semaphore
->active
);
222 semaphore
->active
= FALSE
;
225 * Wakeup blocked threads
227 old_count
= semaphore
->count
;
228 semaphore
->count
= 0;
231 waitq_wakeup64_all_locked(&semaphore
->waitq
,
233 THREAD_RESTART
, NULL
,
234 WAITQ_ALL_PRIORITIES
,
236 /* waitq/semaphore is unlocked */
238 semaphore_unlock(semaphore
);
243 * Routine: semaphore_destroy
245 * Destroys a semaphore and consume the caller's reference on the
251 semaphore_t semaphore
)
255 if (semaphore
== SEMAPHORE_NULL
) {
256 return KERN_INVALID_ARGUMENT
;
259 if (task
== TASK_NULL
) {
260 semaphore_dereference(semaphore
);
261 return KERN_INVALID_ARGUMENT
;
265 spl_level
= splsched();
266 semaphore_lock(semaphore
);
268 if (semaphore
->owner
!= task
) {
269 semaphore_unlock(semaphore
);
270 semaphore_dereference(semaphore
);
273 return KERN_INVALID_ARGUMENT
;
276 semaphore_destroy_internal(task
, semaphore
);
277 /* semaphore unlocked */
282 semaphore_dereference(semaphore
);
287 * Routine: semaphore_destroy_all
289 * Destroy all the semaphores associated with a given task.
291 #define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
294 semaphore_destroy_all(
302 while (!queue_empty(&task
->semaphore_list
)) {
303 semaphore_t semaphore
;
305 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
308 spl_level
= splsched();
310 semaphore_lock(semaphore
);
312 semaphore_destroy_internal(task
, semaphore
);
313 /* semaphore unlocked */
315 /* throttle number of semaphores per interrupt disablement */
316 if (++count
== SEMASPERSPL
) {
329 * Routine: semaphore_signal_internal
331 * Signals the semaphore as direct.
333 * Semaphore is locked.
336 semaphore_signal_internal(
337 semaphore_t semaphore
,
344 spl_level
= splsched();
345 semaphore_lock(semaphore
);
347 if (!semaphore
->active
) {
348 semaphore_unlock(semaphore
);
350 return KERN_TERMINATED
;
353 if (thread
!= THREAD_NULL
) {
354 if (semaphore
->count
< 0) {
355 kr
= waitq_wakeup64_thread_locked(
361 /* waitq/semaphore is unlocked */
363 kr
= KERN_NOT_WAITING
;
364 semaphore_unlock(semaphore
);
370 if (options
& SEMAPHORE_SIGNAL_ALL
) {
371 int old_count
= semaphore
->count
;
373 kr
= KERN_NOT_WAITING
;
375 semaphore
->count
= 0; /* always reset */
376 kr
= waitq_wakeup64_all_locked(
379 THREAD_AWAKENED
, NULL
,
380 WAITQ_ALL_PRIORITIES
,
382 /* waitq / semaphore is unlocked */
384 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
388 semaphore_unlock(semaphore
);
394 if (semaphore
->count
< 0) {
395 waitq_options_t wq_option
= (options
& SEMAPHORE_THREAD_HANDOFF
) ?
396 WQ_OPTION_HANDOFF
: WQ_OPTION_NONE
;
397 kr
= waitq_wakeup64_one_locked(
400 THREAD_AWAKENED
, NULL
,
401 WAITQ_ALL_PRIORITIES
,
404 if (kr
== KERN_SUCCESS
) {
405 semaphore_unlock(semaphore
);
409 semaphore
->count
= 0; /* all waiters gone */
413 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
417 semaphore_unlock(semaphore
);
419 return KERN_NOT_WAITING
;
423 * Routine: semaphore_signal_thread
425 * If the specified thread is blocked on the semaphore, it is
426 * woken up. If a NULL thread was supplied, then any one
427 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
428 * and the semaphore is unchanged.
431 semaphore_signal_thread(
432 semaphore_t semaphore
,
437 if (semaphore
== SEMAPHORE_NULL
) {
438 return KERN_INVALID_ARGUMENT
;
441 ret
= semaphore_signal_internal(semaphore
,
443 SEMAPHORE_OPTION_NONE
);
448 * Routine: semaphore_signal_thread_trap
450 * Trap interface to the semaphore_signal_thread function.
453 semaphore_signal_thread_trap(
454 struct semaphore_signal_thread_trap_args
*args
)
456 mach_port_name_t sema_name
= args
->signal_name
;
457 mach_port_name_t thread_name
= args
->thread_name
;
458 semaphore_t semaphore
;
463 * MACH_PORT_NULL is not an error. It means that we want to
464 * select any one thread that is already waiting, but not to
465 * pre-post the semaphore.
467 if (thread_name
!= MACH_PORT_NULL
) {
468 thread
= port_name_to_thread(thread_name
, PORT_TO_THREAD_NONE
);
469 if (thread
== THREAD_NULL
) {
470 return KERN_INVALID_ARGUMENT
;
473 thread
= THREAD_NULL
;
476 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
477 if (kr
== KERN_SUCCESS
) {
478 kr
= semaphore_signal_internal(semaphore
,
480 SEMAPHORE_OPTION_NONE
);
481 semaphore_dereference(semaphore
);
483 if (thread
!= THREAD_NULL
) {
484 thread_deallocate(thread
);
492 * Routine: semaphore_signal
494 * Traditional (in-kernel client and MIG interface) semaphore
495 * signal routine. Most users will access the trap version.
497 * This interface in not defined to return info about whether
498 * this call found a thread waiting or not. The internal
499 * routines (and future external routines) do. We have to
500 * convert those into plain KERN_SUCCESS returns.
504 semaphore_t semaphore
)
508 if (semaphore
== SEMAPHORE_NULL
) {
509 return KERN_INVALID_ARGUMENT
;
512 kr
= semaphore_signal_internal(semaphore
,
514 SEMAPHORE_SIGNAL_PREPOST
);
515 if (kr
== KERN_NOT_WAITING
) {
522 * Routine: semaphore_signal_trap
524 * Trap interface to the semaphore_signal function.
527 semaphore_signal_trap(
528 struct semaphore_signal_trap_args
*args
)
530 mach_port_name_t sema_name
= args
->signal_name
;
532 return semaphore_signal_internal_trap(sema_name
);
536 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
538 semaphore_t semaphore
;
541 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
542 if (kr
== KERN_SUCCESS
) {
543 kr
= semaphore_signal_internal(semaphore
,
545 SEMAPHORE_SIGNAL_PREPOST
);
546 semaphore_dereference(semaphore
);
547 if (kr
== KERN_NOT_WAITING
) {
555 * Routine: semaphore_signal_all
557 * Awakens ALL threads currently blocked on the semaphore.
558 * The semaphore count returns to zero.
561 semaphore_signal_all(
562 semaphore_t semaphore
)
566 if (semaphore
== SEMAPHORE_NULL
) {
567 return KERN_INVALID_ARGUMENT
;
570 kr
= semaphore_signal_internal(semaphore
,
572 SEMAPHORE_SIGNAL_ALL
);
573 if (kr
== KERN_NOT_WAITING
) {
580 * Routine: semaphore_signal_all_trap
582 * Trap interface to the semaphore_signal_all function.
585 semaphore_signal_all_trap(
586 struct semaphore_signal_all_trap_args
*args
)
588 mach_port_name_t sema_name
= args
->signal_name
;
589 semaphore_t semaphore
;
592 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
593 if (kr
== KERN_SUCCESS
) {
594 kr
= semaphore_signal_internal(semaphore
,
596 SEMAPHORE_SIGNAL_ALL
);
597 semaphore_dereference(semaphore
);
598 if (kr
== KERN_NOT_WAITING
) {
606 * Routine: semaphore_convert_wait_result
608 * Generate the return code after a semaphore wait/block. It
609 * takes the wait result as an input and coverts that to an
610 * appropriate result.
613 semaphore_convert_wait_result(int wait_result
)
615 switch (wait_result
) {
616 case THREAD_AWAKENED
:
619 case THREAD_TIMED_OUT
:
620 return KERN_OPERATION_TIMED_OUT
;
622 case THREAD_INTERRUPTED
:
626 return KERN_TERMINATED
;
629 panic("semaphore_block\n");
635 * Routine: semaphore_wait_continue
637 * Common continuation routine after waiting on a semphore.
638 * It returns directly to user space.
641 semaphore_wait_continue(void *arg __unused
, wait_result_t wr
)
643 thread_t self
= current_thread();
644 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
646 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
647 semaphore_dereference(self
->sth_waitsemaphore
);
648 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
) {
649 semaphore_dereference(self
->sth_signalsemaphore
);
652 assert(self
->handoff_thread
== THREAD_NULL
);
653 assert(caller_cont
!= (void (*)(kern_return_t
))0);
654 (*caller_cont
)(semaphore_convert_wait_result(wr
));
658 * Routine: semaphore_wait_internal
660 * Decrements the semaphore count by one. If the count is
661 * negative after the decrement, the calling thread blocks
662 * (possibly at a continuation and/or with a timeout).
666 * A reference is held on the signal semaphore.
669 semaphore_wait_internal(
670 semaphore_t wait_semaphore
,
671 semaphore_t signal_semaphore
,
674 void (*caller_cont
)(kern_return_t
))
678 kern_return_t kr
= KERN_ALREADY_WAITING
;
680 spl_level
= splsched();
681 semaphore_lock(wait_semaphore
);
682 thread_t self
= current_thread();
683 thread_t handoff_thread
= THREAD_NULL
;
684 thread_handoff_option_t handoff_option
= THREAD_HANDOFF_NONE
;
685 int semaphore_signal_options
= SEMAPHORE_SIGNAL_PREPOST
;
687 if (!wait_semaphore
->active
) {
688 kr
= KERN_TERMINATED
;
689 } else if (wait_semaphore
->count
> 0) {
690 wait_semaphore
->count
--;
692 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
693 kr
= KERN_OPERATION_TIMED_OUT
;
695 wait_semaphore
->count
= -1; /* we don't keep an actual count */
697 thread_set_pending_block_hint(self
, kThreadWaitSemaphore
);
698 (void)waitq_assert_wait64_locked(
699 &wait_semaphore
->waitq
,
702 TIMEOUT_URGENCY_USER_NORMAL
,
703 deadline
, TIMEOUT_NO_LEEWAY
,
706 semaphore_signal_options
|= SEMAPHORE_THREAD_HANDOFF
;
708 semaphore_unlock(wait_semaphore
);
712 * wait_semaphore is unlocked so we are free to go ahead and
713 * signal the signal_semaphore (if one was provided).
715 if (signal_semaphore
!= SEMAPHORE_NULL
) {
716 kern_return_t signal_kr
;
719 * lock the signal semaphore reference we got and signal it.
720 * This will NOT block (we cannot block after having asserted
721 * our intention to wait above).
723 signal_kr
= semaphore_signal_internal(signal_semaphore
,
724 THREAD_NULL
, semaphore_signal_options
);
726 if (signal_kr
== KERN_NOT_WAITING
) {
727 assert(self
->handoff_thread
== THREAD_NULL
);
728 signal_kr
= KERN_SUCCESS
;
729 } else if (signal_kr
== KERN_TERMINATED
) {
731 * Uh!Oh! The semaphore we were to signal died.
732 * We have to get ourselves out of the wait in
733 * case we get stuck here forever (it is assumed
734 * that the semaphore we were posting is gating
735 * the decision by someone else to post the
736 * semaphore we are waiting on). People will
737 * discover the other dead semaphore soon enough.
738 * If we got out of the wait cleanly (someone
739 * already posted a wakeup to us) then return that
740 * (most important) result. Otherwise,
741 * return the KERN_TERMINATED status.
743 assert(self
->handoff_thread
== THREAD_NULL
);
744 clear_wait(self
, THREAD_INTERRUPTED
);
745 kr
= semaphore_convert_wait_result(self
->wait_result
);
746 if (kr
== KERN_ABORTED
) {
747 kr
= KERN_TERMINATED
;
753 * If we had an error, or we didn't really need to wait we can
754 * return now that we have signalled the signal semaphore.
756 if (kr
!= KERN_ALREADY_WAITING
) {
757 assert(self
->handoff_thread
== THREAD_NULL
);
761 if (self
->handoff_thread
) {
762 handoff_thread
= self
->handoff_thread
;
763 self
->handoff_thread
= THREAD_NULL
;
764 handoff_option
= THREAD_HANDOFF_SETRUN_NEEDED
;
767 * Now, we can block. If the caller supplied a continuation
768 * pointer of his own for after the block, block with the
769 * appropriate semaphore continuation. This will gather the
770 * semaphore results, release references on the semaphore(s),
771 * and then call the caller's continuation.
774 self
->sth_continuation
= caller_cont
;
775 self
->sth_waitsemaphore
= wait_semaphore
;
776 self
->sth_signalsemaphore
= signal_semaphore
;
778 thread_handoff_parameter(handoff_thread
, semaphore_wait_continue
,
779 NULL
, handoff_option
);
781 wait_result
= thread_handoff_deallocate(handoff_thread
, handoff_option
);
784 assert(self
->handoff_thread
== THREAD_NULL
);
785 return semaphore_convert_wait_result(wait_result
);
790 * Routine: semaphore_wait
792 * Traditional (non-continuation) interface presented to
793 * in-kernel clients to wait on a semaphore.
797 semaphore_t semaphore
)
799 if (semaphore
== SEMAPHORE_NULL
) {
800 return KERN_INVALID_ARGUMENT
;
803 return semaphore_wait_internal(semaphore
,
805 0ULL, SEMAPHORE_OPTION_NONE
,
806 (void (*)(kern_return_t
))0);
810 semaphore_wait_noblock(
811 semaphore_t semaphore
)
813 if (semaphore
== SEMAPHORE_NULL
) {
814 return KERN_INVALID_ARGUMENT
;
817 return semaphore_wait_internal(semaphore
,
819 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
820 (void (*)(kern_return_t
))0);
824 semaphore_wait_deadline(
825 semaphore_t semaphore
,
828 if (semaphore
== SEMAPHORE_NULL
) {
829 return KERN_INVALID_ARGUMENT
;
832 return semaphore_wait_internal(semaphore
,
834 deadline
, SEMAPHORE_OPTION_NONE
,
835 (void (*)(kern_return_t
))0);
839 * Trap: semaphore_wait_trap
841 * Trap version of semaphore wait. Called on behalf of user-level
847 struct semaphore_wait_trap_args
*args
)
849 return semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
);
855 semaphore_wait_trap_internal(
856 mach_port_name_t name
,
857 void (*caller_cont
)(kern_return_t
))
859 semaphore_t semaphore
;
862 kr
= port_name_to_semaphore(name
, &semaphore
);
863 if (kr
== KERN_SUCCESS
) {
864 kr
= semaphore_wait_internal(semaphore
,
866 0ULL, SEMAPHORE_OPTION_NONE
,
868 semaphore_dereference(semaphore
);
874 * Routine: semaphore_timedwait
876 * Traditional (non-continuation) interface presented to
877 * in-kernel clients to wait on a semaphore with a timeout.
879 * A timeout of {0,0} is considered non-blocking.
883 semaphore_t semaphore
,
884 mach_timespec_t wait_time
)
886 int option
= SEMAPHORE_OPTION_NONE
;
887 uint64_t deadline
= 0;
889 if (semaphore
== SEMAPHORE_NULL
) {
890 return KERN_INVALID_ARGUMENT
;
893 if (BAD_MACH_TIMESPEC(&wait_time
)) {
894 return KERN_INVALID_VALUE
;
897 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
898 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
900 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
903 return semaphore_wait_internal(semaphore
,
906 (void (*)(kern_return_t
))0);
910 * Trap: semaphore_timedwait_trap
912 * Trap version of a semaphore_timedwait. The timeout parameter
913 * is passed in two distinct parts and re-assembled on this side
914 * of the trap interface (to accomodate calling conventions that
915 * pass structures as pointers instead of inline in registers without
916 * having to add a copyin).
918 * A timeout of {0,0} is considered non-blocking.
921 semaphore_timedwait_trap(
922 struct semaphore_timedwait_trap_args
*args
)
924 return semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
929 semaphore_timedwait_trap_internal(
930 mach_port_name_t name
,
933 void (*caller_cont
)(kern_return_t
))
935 semaphore_t semaphore
;
936 mach_timespec_t wait_time
;
939 wait_time
.tv_sec
= sec
;
940 wait_time
.tv_nsec
= nsec
;
941 if (BAD_MACH_TIMESPEC(&wait_time
)) {
942 return KERN_INVALID_VALUE
;
945 kr
= port_name_to_semaphore(name
, &semaphore
);
946 if (kr
== KERN_SUCCESS
) {
947 int option
= SEMAPHORE_OPTION_NONE
;
948 uint64_t deadline
= 0;
950 if (sec
== 0 && nsec
== 0) {
951 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
953 deadline
= semaphore_deadline(sec
, nsec
);
956 kr
= semaphore_wait_internal(semaphore
,
960 semaphore_dereference(semaphore
);
966 * Routine: semaphore_wait_signal
968 * Atomically register a wait on a semaphore and THEN signal
969 * another. This is the in-kernel entry point that does not
970 * block at a continuation and does not free a signal_semaphore
974 semaphore_wait_signal(
975 semaphore_t wait_semaphore
,
976 semaphore_t signal_semaphore
)
978 if (wait_semaphore
== SEMAPHORE_NULL
) {
979 return KERN_INVALID_ARGUMENT
;
982 return semaphore_wait_internal(wait_semaphore
,
984 0ULL, SEMAPHORE_OPTION_NONE
,
985 (void (*)(kern_return_t
))0);
989 * Trap: semaphore_wait_signal_trap
991 * Atomically register a wait on a semaphore and THEN signal
992 * another. This is the trap version from user space.
995 semaphore_wait_signal_trap(
996 struct semaphore_wait_signal_trap_args
*args
)
998 return semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
);
1002 semaphore_wait_signal_trap_internal(
1003 mach_port_name_t wait_name
,
1004 mach_port_name_t signal_name
,
1005 void (*caller_cont
)(kern_return_t
))
1007 semaphore_t wait_semaphore
;
1008 semaphore_t signal_semaphore
;
1011 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1012 if (kr
== KERN_SUCCESS
) {
1013 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1014 if (kr
== KERN_SUCCESS
) {
1015 kr
= semaphore_wait_internal(wait_semaphore
,
1017 0ULL, SEMAPHORE_OPTION_NONE
,
1019 semaphore_dereference(wait_semaphore
);
1021 semaphore_dereference(signal_semaphore
);
1028 * Routine: semaphore_timedwait_signal
1030 * Atomically register a wait on a semaphore and THEN signal
1031 * another. This is the in-kernel entry point that does not
1032 * block at a continuation.
1034 * A timeout of {0,0} is considered non-blocking.
1037 semaphore_timedwait_signal(
1038 semaphore_t wait_semaphore
,
1039 semaphore_t signal_semaphore
,
1040 mach_timespec_t wait_time
)
1042 int option
= SEMAPHORE_OPTION_NONE
;
1043 uint64_t deadline
= 0;
1045 if (wait_semaphore
== SEMAPHORE_NULL
) {
1046 return KERN_INVALID_ARGUMENT
;
1049 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1050 return KERN_INVALID_VALUE
;
1053 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
1054 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1056 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1059 return semaphore_wait_internal(wait_semaphore
,
1062 (void (*)(kern_return_t
))0);
1066 * Trap: semaphore_timedwait_signal_trap
1068 * Atomically register a timed wait on a semaphore and THEN signal
1069 * another. This is the trap version from user space.
1072 semaphore_timedwait_signal_trap(
1073 struct semaphore_timedwait_signal_trap_args
*args
)
1075 return semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
1079 semaphore_timedwait_signal_trap_internal(
1080 mach_port_name_t wait_name
,
1081 mach_port_name_t signal_name
,
1084 void (*caller_cont
)(kern_return_t
))
1086 semaphore_t wait_semaphore
;
1087 semaphore_t signal_semaphore
;
1088 mach_timespec_t wait_time
;
1091 wait_time
.tv_sec
= sec
;
1092 wait_time
.tv_nsec
= nsec
;
1093 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1094 return KERN_INVALID_VALUE
;
1097 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1098 if (kr
== KERN_SUCCESS
) {
1099 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1100 if (kr
== KERN_SUCCESS
) {
1101 int option
= SEMAPHORE_OPTION_NONE
;
1102 uint64_t deadline
= 0;
1104 if (sec
== 0 && nsec
== 0) {
1105 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1107 deadline
= semaphore_deadline(sec
, nsec
);
1110 kr
= semaphore_wait_internal(wait_semaphore
,
1114 semaphore_dereference(wait_semaphore
);
1116 semaphore_dereference(signal_semaphore
);
1123 * Routine: semaphore_reference
1125 * Take out a reference on a semaphore. This keeps the data structure
1126 * in existence (but the semaphore may be deactivated).
1129 semaphore_reference(
1130 semaphore_t semaphore
)
1132 os_ref_retain(&semaphore
->ref_count
);
1136 * Routine: semaphore_dereference
1138 * Release a reference on a semaphore. If this is the last reference,
1139 * the semaphore data structure is deallocated.
1142 semaphore_dereference(
1143 semaphore_t semaphore
)
1145 uint32_t collisions
;
1148 if (semaphore
== NULL
) {
1152 if (os_ref_release(&semaphore
->ref_count
) > 0) {
1157 * Last ref, clean up the port [if any]
1158 * associated with the semaphore, destroy
1159 * it (if still active) and then free
1162 ipc_port_t port
= semaphore
->port
;
1164 if (IP_VALID(port
)) {
1165 assert(!port
->ip_srights
);
1166 ipc_port_dealloc_kernel(port
);
1170 * Lock the semaphore to lock in the owner task reference.
1171 * Then continue to try to lock the task (inverse order).
1173 spl_level
= splsched();
1174 semaphore_lock(semaphore
);
1175 for (collisions
= 0; semaphore
->active
; collisions
++) {
1176 task_t task
= semaphore
->owner
;
1178 assert(task
!= TASK_NULL
);
1180 if (task_lock_try(task
)) {
1181 semaphore_destroy_internal(task
, semaphore
);
1182 /* semaphore unlocked */
1188 /* failed to get out-of-order locks */
1189 semaphore_unlock(semaphore
);
1191 mutex_pause(collisions
);
1192 spl_level
= splsched();
1193 semaphore_lock(semaphore
);
1195 semaphore_unlock(semaphore
);
1199 zfree(semaphore_zone
, semaphore
);
1202 #define WAITQ_TO_SEMA(wq) ((semaphore_t) ((uintptr_t)(wq) - offsetof(struct semaphore, waitq)))
1204 kdp_sema_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1206 semaphore_t sem
= WAITQ_TO_SEMA(waitq
);
1207 assert(event
== SEMAPHORE_EVENT
);
1209 zone_require(semaphore_zone
, sem
);
1211 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(sem
->port
);
1213 waitinfo
->owner
= pid_from_task(sem
->owner
);