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 /* Check for race with task_terminate */
186 zfree(semaphore_zone
, s
);
187 return KERN_INVALID_TASK
;
189 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
190 task
->semaphores_owned
++;
199 * Routine: semaphore_destroy_internal
201 * Disassociate a semaphore from its owning task, mark it inactive,
202 * and set any waiting threads running with THREAD_RESTART.
206 * semaphore is locked
207 * semaphore is owned by the specified task
209 * with semaphore unlocked
212 semaphore_destroy_internal(
214 semaphore_t semaphore
)
218 /* unlink semaphore from owning task */
219 assert(semaphore
->owner
== task
);
220 remqueue((queue_entry_t
) semaphore
);
221 semaphore
->owner
= TASK_NULL
;
222 task
->semaphores_owned
--;
225 * Deactivate semaphore
227 assert(semaphore
->active
);
228 semaphore
->active
= FALSE
;
231 * Wakeup blocked threads
233 old_count
= semaphore
->count
;
234 semaphore
->count
= 0;
237 waitq_wakeup64_all_locked(&semaphore
->waitq
,
239 THREAD_RESTART
, NULL
,
240 WAITQ_ALL_PRIORITIES
,
242 /* waitq/semaphore is unlocked */
244 semaphore_unlock(semaphore
);
249 * Routine: semaphore_destroy
251 * Destroys a semaphore and consume the caller's reference on the
257 semaphore_t semaphore
)
261 if (semaphore
== SEMAPHORE_NULL
) {
262 return KERN_INVALID_ARGUMENT
;
265 if (task
== TASK_NULL
) {
266 semaphore_dereference(semaphore
);
267 return KERN_INVALID_ARGUMENT
;
271 spl_level
= splsched();
272 semaphore_lock(semaphore
);
274 if (semaphore
->owner
!= task
) {
275 semaphore_unlock(semaphore
);
276 semaphore_dereference(semaphore
);
279 return KERN_INVALID_ARGUMENT
;
282 semaphore_destroy_internal(task
, semaphore
);
283 /* semaphore unlocked */
288 semaphore_dereference(semaphore
);
293 * Routine: semaphore_destroy_all
295 * Destroy all the semaphores associated with a given task.
297 #define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
300 semaphore_destroy_all(
308 while (!queue_empty(&task
->semaphore_list
)) {
309 semaphore_t semaphore
;
311 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
314 spl_level
= splsched();
316 semaphore_lock(semaphore
);
318 semaphore_destroy_internal(task
, semaphore
);
319 /* semaphore unlocked */
321 /* throttle number of semaphores per interrupt disablement */
322 if (++count
== SEMASPERSPL
) {
335 * Routine: semaphore_signal_internal
337 * Signals the semaphore as direct.
339 * Semaphore is locked.
342 semaphore_signal_internal(
343 semaphore_t semaphore
,
350 spl_level
= splsched();
351 semaphore_lock(semaphore
);
353 if (!semaphore
->active
) {
354 semaphore_unlock(semaphore
);
356 return KERN_TERMINATED
;
359 if (thread
!= THREAD_NULL
) {
360 if (semaphore
->count
< 0) {
361 kr
= waitq_wakeup64_thread_locked(
367 /* waitq/semaphore is unlocked */
369 kr
= KERN_NOT_WAITING
;
370 semaphore_unlock(semaphore
);
376 if (options
& SEMAPHORE_SIGNAL_ALL
) {
377 int old_count
= semaphore
->count
;
379 kr
= KERN_NOT_WAITING
;
381 semaphore
->count
= 0; /* always reset */
382 kr
= waitq_wakeup64_all_locked(
385 THREAD_AWAKENED
, NULL
,
386 WAITQ_ALL_PRIORITIES
,
388 /* waitq / semaphore is unlocked */
390 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
394 semaphore_unlock(semaphore
);
400 if (semaphore
->count
< 0) {
401 waitq_options_t wq_option
= (options
& SEMAPHORE_THREAD_HANDOFF
) ?
402 WQ_OPTION_HANDOFF
: WQ_OPTION_NONE
;
403 kr
= waitq_wakeup64_one_locked(
406 THREAD_AWAKENED
, NULL
,
407 WAITQ_ALL_PRIORITIES
,
410 if (kr
== KERN_SUCCESS
) {
411 semaphore_unlock(semaphore
);
415 semaphore
->count
= 0; /* all waiters gone */
419 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
423 semaphore_unlock(semaphore
);
425 return KERN_NOT_WAITING
;
429 * Routine: semaphore_signal_thread
431 * If the specified thread is blocked on the semaphore, it is
432 * woken up. If a NULL thread was supplied, then any one
433 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
434 * and the semaphore is unchanged.
437 semaphore_signal_thread(
438 semaphore_t semaphore
,
443 if (semaphore
== SEMAPHORE_NULL
) {
444 return KERN_INVALID_ARGUMENT
;
447 ret
= semaphore_signal_internal(semaphore
,
449 SEMAPHORE_OPTION_NONE
);
454 * Routine: semaphore_signal_thread_trap
456 * Trap interface to the semaphore_signal_thread function.
459 semaphore_signal_thread_trap(
460 struct semaphore_signal_thread_trap_args
*args
)
462 mach_port_name_t sema_name
= args
->signal_name
;
463 mach_port_name_t thread_name
= args
->thread_name
;
464 semaphore_t semaphore
;
469 * MACH_PORT_NULL is not an error. It means that we want to
470 * select any one thread that is already waiting, but not to
471 * pre-post the semaphore.
473 if (thread_name
!= MACH_PORT_NULL
) {
474 thread
= port_name_to_thread(thread_name
, PORT_TO_THREAD_NONE
);
475 if (thread
== THREAD_NULL
) {
476 return KERN_INVALID_ARGUMENT
;
479 thread
= THREAD_NULL
;
482 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
483 if (kr
== KERN_SUCCESS
) {
484 kr
= semaphore_signal_internal(semaphore
,
486 SEMAPHORE_OPTION_NONE
);
487 semaphore_dereference(semaphore
);
489 if (thread
!= THREAD_NULL
) {
490 thread_deallocate(thread
);
498 * Routine: semaphore_signal
500 * Traditional (in-kernel client and MIG interface) semaphore
501 * signal routine. Most users will access the trap version.
503 * This interface in not defined to return info about whether
504 * this call found a thread waiting or not. The internal
505 * routines (and future external routines) do. We have to
506 * convert those into plain KERN_SUCCESS returns.
510 semaphore_t semaphore
)
514 if (semaphore
== SEMAPHORE_NULL
) {
515 return KERN_INVALID_ARGUMENT
;
518 kr
= semaphore_signal_internal(semaphore
,
520 SEMAPHORE_SIGNAL_PREPOST
);
521 if (kr
== KERN_NOT_WAITING
) {
528 * Routine: semaphore_signal_trap
530 * Trap interface to the semaphore_signal function.
533 semaphore_signal_trap(
534 struct semaphore_signal_trap_args
*args
)
536 mach_port_name_t sema_name
= args
->signal_name
;
538 return semaphore_signal_internal_trap(sema_name
);
542 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
544 semaphore_t semaphore
;
547 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
548 if (kr
== KERN_SUCCESS
) {
549 kr
= semaphore_signal_internal(semaphore
,
551 SEMAPHORE_SIGNAL_PREPOST
);
552 semaphore_dereference(semaphore
);
553 if (kr
== KERN_NOT_WAITING
) {
561 * Routine: semaphore_signal_all
563 * Awakens ALL threads currently blocked on the semaphore.
564 * The semaphore count returns to zero.
567 semaphore_signal_all(
568 semaphore_t semaphore
)
572 if (semaphore
== SEMAPHORE_NULL
) {
573 return KERN_INVALID_ARGUMENT
;
576 kr
= semaphore_signal_internal(semaphore
,
578 SEMAPHORE_SIGNAL_ALL
);
579 if (kr
== KERN_NOT_WAITING
) {
586 * Routine: semaphore_signal_all_trap
588 * Trap interface to the semaphore_signal_all function.
591 semaphore_signal_all_trap(
592 struct semaphore_signal_all_trap_args
*args
)
594 mach_port_name_t sema_name
= args
->signal_name
;
595 semaphore_t semaphore
;
598 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
599 if (kr
== KERN_SUCCESS
) {
600 kr
= semaphore_signal_internal(semaphore
,
602 SEMAPHORE_SIGNAL_ALL
);
603 semaphore_dereference(semaphore
);
604 if (kr
== KERN_NOT_WAITING
) {
612 * Routine: semaphore_convert_wait_result
614 * Generate the return code after a semaphore wait/block. It
615 * takes the wait result as an input and coverts that to an
616 * appropriate result.
619 semaphore_convert_wait_result(int wait_result
)
621 switch (wait_result
) {
622 case THREAD_AWAKENED
:
625 case THREAD_TIMED_OUT
:
626 return KERN_OPERATION_TIMED_OUT
;
628 case THREAD_INTERRUPTED
:
632 return KERN_TERMINATED
;
635 panic("semaphore_block\n");
641 * Routine: semaphore_wait_continue
643 * Common continuation routine after waiting on a semphore.
644 * It returns directly to user space.
647 semaphore_wait_continue(void *arg __unused
, wait_result_t wr
)
649 thread_t self
= current_thread();
650 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
652 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
653 semaphore_dereference(self
->sth_waitsemaphore
);
654 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
) {
655 semaphore_dereference(self
->sth_signalsemaphore
);
658 assert(self
->handoff_thread
== THREAD_NULL
);
659 assert(caller_cont
!= (void (*)(kern_return_t
))0);
660 (*caller_cont
)(semaphore_convert_wait_result(wr
));
664 * Routine: semaphore_wait_internal
666 * Decrements the semaphore count by one. If the count is
667 * negative after the decrement, the calling thread blocks
668 * (possibly at a continuation and/or with a timeout).
672 * A reference is held on the signal semaphore.
675 semaphore_wait_internal(
676 semaphore_t wait_semaphore
,
677 semaphore_t signal_semaphore
,
680 void (*caller_cont
)(kern_return_t
))
684 kern_return_t kr
= KERN_ALREADY_WAITING
;
686 spl_level
= splsched();
687 semaphore_lock(wait_semaphore
);
688 thread_t self
= current_thread();
689 thread_t handoff_thread
= THREAD_NULL
;
690 thread_handoff_option_t handoff_option
= THREAD_HANDOFF_NONE
;
691 int semaphore_signal_options
= SEMAPHORE_SIGNAL_PREPOST
;
693 if (!wait_semaphore
->active
) {
694 kr
= KERN_TERMINATED
;
695 } else if (wait_semaphore
->count
> 0) {
696 wait_semaphore
->count
--;
698 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
699 kr
= KERN_OPERATION_TIMED_OUT
;
701 wait_semaphore
->count
= -1; /* we don't keep an actual count */
703 thread_set_pending_block_hint(self
, kThreadWaitSemaphore
);
704 (void)waitq_assert_wait64_locked(
705 &wait_semaphore
->waitq
,
708 TIMEOUT_URGENCY_USER_NORMAL
,
709 deadline
, TIMEOUT_NO_LEEWAY
,
712 semaphore_signal_options
|= SEMAPHORE_THREAD_HANDOFF
;
714 semaphore_unlock(wait_semaphore
);
718 * wait_semaphore is unlocked so we are free to go ahead and
719 * signal the signal_semaphore (if one was provided).
721 if (signal_semaphore
!= SEMAPHORE_NULL
) {
722 kern_return_t signal_kr
;
725 * lock the signal semaphore reference we got and signal it.
726 * This will NOT block (we cannot block after having asserted
727 * our intention to wait above).
729 signal_kr
= semaphore_signal_internal(signal_semaphore
,
730 THREAD_NULL
, semaphore_signal_options
);
732 if (signal_kr
== KERN_NOT_WAITING
) {
733 assert(self
->handoff_thread
== THREAD_NULL
);
734 signal_kr
= KERN_SUCCESS
;
735 } else if (signal_kr
== KERN_TERMINATED
) {
737 * Uh!Oh! The semaphore we were to signal died.
738 * We have to get ourselves out of the wait in
739 * case we get stuck here forever (it is assumed
740 * that the semaphore we were posting is gating
741 * the decision by someone else to post the
742 * semaphore we are waiting on). People will
743 * discover the other dead semaphore soon enough.
744 * If we got out of the wait cleanly (someone
745 * already posted a wakeup to us) then return that
746 * (most important) result. Otherwise,
747 * return the KERN_TERMINATED status.
749 assert(self
->handoff_thread
== THREAD_NULL
);
750 clear_wait(self
, THREAD_INTERRUPTED
);
751 kr
= semaphore_convert_wait_result(self
->wait_result
);
752 if (kr
== KERN_ABORTED
) {
753 kr
= KERN_TERMINATED
;
759 * If we had an error, or we didn't really need to wait we can
760 * return now that we have signalled the signal semaphore.
762 if (kr
!= KERN_ALREADY_WAITING
) {
763 assert(self
->handoff_thread
== THREAD_NULL
);
767 if (self
->handoff_thread
) {
768 handoff_thread
= self
->handoff_thread
;
769 self
->handoff_thread
= THREAD_NULL
;
770 handoff_option
= THREAD_HANDOFF_SETRUN_NEEDED
;
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. This will gather the
776 * semaphore results, release references on the semaphore(s),
777 * and then call the caller's continuation.
780 self
->sth_continuation
= caller_cont
;
781 self
->sth_waitsemaphore
= wait_semaphore
;
782 self
->sth_signalsemaphore
= signal_semaphore
;
784 thread_handoff_parameter(handoff_thread
, semaphore_wait_continue
,
785 NULL
, handoff_option
);
787 wait_result
= thread_handoff_deallocate(handoff_thread
, handoff_option
);
790 assert(self
->handoff_thread
== THREAD_NULL
);
791 return semaphore_convert_wait_result(wait_result
);
796 * Routine: semaphore_wait
798 * Traditional (non-continuation) interface presented to
799 * in-kernel clients to wait on a semaphore.
803 semaphore_t semaphore
)
805 if (semaphore
== SEMAPHORE_NULL
) {
806 return KERN_INVALID_ARGUMENT
;
809 return semaphore_wait_internal(semaphore
,
811 0ULL, SEMAPHORE_OPTION_NONE
,
812 (void (*)(kern_return_t
))0);
816 semaphore_wait_noblock(
817 semaphore_t semaphore
)
819 if (semaphore
== SEMAPHORE_NULL
) {
820 return KERN_INVALID_ARGUMENT
;
823 return semaphore_wait_internal(semaphore
,
825 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
826 (void (*)(kern_return_t
))0);
830 semaphore_wait_deadline(
831 semaphore_t semaphore
,
834 if (semaphore
== SEMAPHORE_NULL
) {
835 return KERN_INVALID_ARGUMENT
;
838 return semaphore_wait_internal(semaphore
,
840 deadline
, SEMAPHORE_OPTION_NONE
,
841 (void (*)(kern_return_t
))0);
845 * Trap: semaphore_wait_trap
847 * Trap version of semaphore wait. Called on behalf of user-level
853 struct semaphore_wait_trap_args
*args
)
855 return semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
);
861 semaphore_wait_trap_internal(
862 mach_port_name_t name
,
863 void (*caller_cont
)(kern_return_t
))
865 semaphore_t semaphore
;
868 kr
= port_name_to_semaphore(name
, &semaphore
);
869 if (kr
== KERN_SUCCESS
) {
870 kr
= semaphore_wait_internal(semaphore
,
872 0ULL, SEMAPHORE_OPTION_NONE
,
874 semaphore_dereference(semaphore
);
880 * Routine: semaphore_timedwait
882 * Traditional (non-continuation) interface presented to
883 * in-kernel clients to wait on a semaphore with a timeout.
885 * A timeout of {0,0} is considered non-blocking.
889 semaphore_t semaphore
,
890 mach_timespec_t wait_time
)
892 int option
= SEMAPHORE_OPTION_NONE
;
893 uint64_t deadline
= 0;
895 if (semaphore
== SEMAPHORE_NULL
) {
896 return KERN_INVALID_ARGUMENT
;
899 if (BAD_MACH_TIMESPEC(&wait_time
)) {
900 return KERN_INVALID_VALUE
;
903 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
904 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
906 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
909 return semaphore_wait_internal(semaphore
,
912 (void (*)(kern_return_t
))0);
916 * Trap: semaphore_timedwait_trap
918 * Trap version of a semaphore_timedwait. The timeout parameter
919 * is passed in two distinct parts and re-assembled on this side
920 * of the trap interface (to accomodate calling conventions that
921 * pass structures as pointers instead of inline in registers without
922 * having to add a copyin).
924 * A timeout of {0,0} is considered non-blocking.
927 semaphore_timedwait_trap(
928 struct semaphore_timedwait_trap_args
*args
)
930 return semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
935 semaphore_timedwait_trap_internal(
936 mach_port_name_t name
,
939 void (*caller_cont
)(kern_return_t
))
941 semaphore_t semaphore
;
942 mach_timespec_t wait_time
;
945 wait_time
.tv_sec
= sec
;
946 wait_time
.tv_nsec
= nsec
;
947 if (BAD_MACH_TIMESPEC(&wait_time
)) {
948 return KERN_INVALID_VALUE
;
951 kr
= port_name_to_semaphore(name
, &semaphore
);
952 if (kr
== KERN_SUCCESS
) {
953 int option
= SEMAPHORE_OPTION_NONE
;
954 uint64_t deadline
= 0;
956 if (sec
== 0 && nsec
== 0) {
957 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
959 deadline
= semaphore_deadline(sec
, nsec
);
962 kr
= semaphore_wait_internal(semaphore
,
966 semaphore_dereference(semaphore
);
972 * Routine: semaphore_wait_signal
974 * Atomically register a wait on a semaphore and THEN signal
975 * another. This is the in-kernel entry point that does not
976 * block at a continuation and does not free a signal_semaphore
980 semaphore_wait_signal(
981 semaphore_t wait_semaphore
,
982 semaphore_t signal_semaphore
)
984 if (wait_semaphore
== SEMAPHORE_NULL
) {
985 return KERN_INVALID_ARGUMENT
;
988 return semaphore_wait_internal(wait_semaphore
,
990 0ULL, SEMAPHORE_OPTION_NONE
,
991 (void (*)(kern_return_t
))0);
995 * Trap: semaphore_wait_signal_trap
997 * Atomically register a wait on a semaphore and THEN signal
998 * another. This is the trap version from user space.
1001 semaphore_wait_signal_trap(
1002 struct semaphore_wait_signal_trap_args
*args
)
1004 return semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
);
1008 semaphore_wait_signal_trap_internal(
1009 mach_port_name_t wait_name
,
1010 mach_port_name_t signal_name
,
1011 void (*caller_cont
)(kern_return_t
))
1013 semaphore_t wait_semaphore
;
1014 semaphore_t signal_semaphore
;
1017 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1018 if (kr
== KERN_SUCCESS
) {
1019 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1020 if (kr
== KERN_SUCCESS
) {
1021 kr
= semaphore_wait_internal(wait_semaphore
,
1023 0ULL, SEMAPHORE_OPTION_NONE
,
1025 semaphore_dereference(wait_semaphore
);
1027 semaphore_dereference(signal_semaphore
);
1034 * Routine: semaphore_timedwait_signal
1036 * Atomically register a wait on a semaphore and THEN signal
1037 * another. This is the in-kernel entry point that does not
1038 * block at a continuation.
1040 * A timeout of {0,0} is considered non-blocking.
1043 semaphore_timedwait_signal(
1044 semaphore_t wait_semaphore
,
1045 semaphore_t signal_semaphore
,
1046 mach_timespec_t wait_time
)
1048 int option
= SEMAPHORE_OPTION_NONE
;
1049 uint64_t deadline
= 0;
1051 if (wait_semaphore
== SEMAPHORE_NULL
) {
1052 return KERN_INVALID_ARGUMENT
;
1055 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1056 return KERN_INVALID_VALUE
;
1059 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0) {
1060 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1062 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1065 return semaphore_wait_internal(wait_semaphore
,
1068 (void (*)(kern_return_t
))0);
1072 * Trap: semaphore_timedwait_signal_trap
1074 * Atomically register a timed wait on a semaphore and THEN signal
1075 * another. This is the trap version from user space.
1078 semaphore_timedwait_signal_trap(
1079 struct semaphore_timedwait_signal_trap_args
*args
)
1081 return semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
);
1085 semaphore_timedwait_signal_trap_internal(
1086 mach_port_name_t wait_name
,
1087 mach_port_name_t signal_name
,
1090 void (*caller_cont
)(kern_return_t
))
1092 semaphore_t wait_semaphore
;
1093 semaphore_t signal_semaphore
;
1094 mach_timespec_t wait_time
;
1097 wait_time
.tv_sec
= sec
;
1098 wait_time
.tv_nsec
= nsec
;
1099 if (BAD_MACH_TIMESPEC(&wait_time
)) {
1100 return KERN_INVALID_VALUE
;
1103 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1104 if (kr
== KERN_SUCCESS
) {
1105 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1106 if (kr
== KERN_SUCCESS
) {
1107 int option
= SEMAPHORE_OPTION_NONE
;
1108 uint64_t deadline
= 0;
1110 if (sec
== 0 && nsec
== 0) {
1111 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1113 deadline
= semaphore_deadline(sec
, nsec
);
1116 kr
= semaphore_wait_internal(wait_semaphore
,
1120 semaphore_dereference(wait_semaphore
);
1122 semaphore_dereference(signal_semaphore
);
1129 * Routine: semaphore_reference
1131 * Take out a reference on a semaphore. This keeps the data structure
1132 * in existence (but the semaphore may be deactivated).
1135 semaphore_reference(
1136 semaphore_t semaphore
)
1138 os_ref_retain(&semaphore
->ref_count
);
1142 * Routine: semaphore_dereference
1144 * Release a reference on a semaphore. If this is the last reference,
1145 * the semaphore data structure is deallocated.
1148 semaphore_dereference(
1149 semaphore_t semaphore
)
1151 uint32_t collisions
;
1154 if (semaphore
== NULL
) {
1158 if (os_ref_release(&semaphore
->ref_count
) > 0) {
1163 * Last ref, clean up the port [if any]
1164 * associated with the semaphore, destroy
1165 * it (if still active) and then free
1168 ipc_port_t port
= semaphore
->port
;
1170 if (IP_VALID(port
)) {
1171 assert(!port
->ip_srights
);
1172 ipc_port_dealloc_kernel(port
);
1176 * Lock the semaphore to lock in the owner task reference.
1177 * Then continue to try to lock the task (inverse order).
1179 spl_level
= splsched();
1180 semaphore_lock(semaphore
);
1181 for (collisions
= 0; semaphore
->active
; collisions
++) {
1182 task_t task
= semaphore
->owner
;
1184 assert(task
!= TASK_NULL
);
1186 if (task_lock_try(task
)) {
1187 semaphore_destroy_internal(task
, semaphore
);
1188 /* semaphore unlocked */
1194 /* failed to get out-of-order locks */
1195 semaphore_unlock(semaphore
);
1197 mutex_pause(collisions
);
1198 spl_level
= splsched();
1199 semaphore_lock(semaphore
);
1201 semaphore_unlock(semaphore
);
1205 zfree(semaphore_zone
, semaphore
);
1208 #define WAITQ_TO_SEMA(wq) ((semaphore_t) ((uintptr_t)(wq) - offsetof(struct semaphore, waitq)))
1210 kdp_sema_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1212 semaphore_t sem
= WAITQ_TO_SEMA(waitq
);
1213 assert(event
== SEMAPHORE_EVENT
);
1215 zone_require(semaphore_zone
, sem
);
1217 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(sem
->port
);
1219 waitinfo
->owner
= pid_from_task(sem
->owner
);