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
;
174 s
= (semaphore_t
) zalloc (semaphore_zone
);
176 if (s
== SEMAPHORE_NULL
)
177 return KERN_RESOURCE_SHORTAGE
;
179 kret
= waitq_init(&s
->waitq
, policy
| SYNC_POLICY_DISABLE_IRQ
); /* also inits lock */
180 if (kret
!= KERN_SUCCESS
) {
181 zfree(semaphore_zone
, s
);
186 * Initialize the semaphore values.
189 os_ref_init(&s
->ref_count
, &sema_refgrp
);
195 * Associate the new semaphore with the task by adding
196 * the new semaphore to the task's semaphore list.
199 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
200 task
->semaphores_owned
++;
209 * Routine: semaphore_destroy_internal
211 * Disassociate a semaphore from its owning task, mark it inactive,
212 * and set any waiting threads running with THREAD_RESTART.
216 * semaphore is locked
217 * semaphore is owned by the specified task
219 * with semaphore unlocked
222 semaphore_destroy_internal(
224 semaphore_t semaphore
)
228 /* unlink semaphore from owning task */
229 assert(semaphore
->owner
== task
);
230 remqueue((queue_entry_t
) semaphore
);
231 semaphore
->owner
= TASK_NULL
;
232 task
->semaphores_owned
--;
235 * Deactivate semaphore
237 assert(semaphore
->active
);
238 semaphore
->active
= FALSE
;
241 * Wakeup blocked threads
243 old_count
= semaphore
->count
;
244 semaphore
->count
= 0;
247 waitq_wakeup64_all_locked(&semaphore
->waitq
,
249 THREAD_RESTART
, NULL
,
250 WAITQ_ALL_PRIORITIES
,
252 /* waitq/semaphore is unlocked */
254 semaphore_unlock(semaphore
);
259 * Routine: semaphore_destroy
261 * Destroys a semaphore and consume the caller's reference on the
267 semaphore_t semaphore
)
271 if (semaphore
== SEMAPHORE_NULL
)
272 return KERN_INVALID_ARGUMENT
;
274 if (task
== TASK_NULL
) {
275 semaphore_dereference(semaphore
);
276 return KERN_INVALID_ARGUMENT
;
280 spl_level
= splsched();
281 semaphore_lock(semaphore
);
283 if (semaphore
->owner
!= task
) {
284 semaphore_unlock(semaphore
);
285 semaphore_dereference(semaphore
);
288 return KERN_INVALID_ARGUMENT
;
291 semaphore_destroy_internal(task
, semaphore
);
292 /* semaphore unlocked */
297 semaphore_dereference(semaphore
);
302 * Routine: semaphore_destroy_all
304 * Destroy all the semaphores associated with a given task.
306 #define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
309 semaphore_destroy_all(
317 while (!queue_empty(&task
->semaphore_list
)) {
318 semaphore_t semaphore
;
320 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
323 spl_level
= splsched();
324 semaphore_lock(semaphore
);
326 semaphore_destroy_internal(task
, semaphore
);
327 /* semaphore unlocked */
329 /* throttle number of semaphores per interrupt disablement */
330 if (++count
== SEMASPERSPL
) {
342 * Routine: semaphore_signal_internal
344 * Signals the semaphore as direct.
346 * Semaphore is locked.
349 semaphore_signal_internal(
350 semaphore_t semaphore
,
357 spl_level
= splsched();
358 semaphore_lock(semaphore
);
360 if (!semaphore
->active
) {
361 semaphore_unlock(semaphore
);
363 return KERN_TERMINATED
;
366 if (thread
!= THREAD_NULL
) {
367 if (semaphore
->count
< 0) {
368 kr
= waitq_wakeup64_thread_locked(
374 /* waitq/semaphore is unlocked */
376 kr
= KERN_NOT_WAITING
;
377 semaphore_unlock(semaphore
);
383 if (options
& SEMAPHORE_SIGNAL_ALL
) {
384 int old_count
= semaphore
->count
;
386 kr
= KERN_NOT_WAITING
;
388 semaphore
->count
= 0; /* always reset */
389 kr
= waitq_wakeup64_all_locked(
392 THREAD_AWAKENED
, NULL
,
393 WAITQ_ALL_PRIORITIES
,
395 /* waitq / semaphore is unlocked */
397 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
400 semaphore_unlock(semaphore
);
406 if (semaphore
->count
< 0) {
407 kr
= waitq_wakeup64_one_locked(
410 THREAD_AWAKENED
, NULL
,
411 WAITQ_ALL_PRIORITIES
,
413 if (kr
== KERN_SUCCESS
) {
414 semaphore_unlock(semaphore
);
418 semaphore
->count
= 0; /* all waiters gone */
422 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
426 semaphore_unlock(semaphore
);
428 return KERN_NOT_WAITING
;
432 * Routine: semaphore_signal_thread
434 * If the specified thread is blocked on the semaphore, it is
435 * woken up. If a NULL thread was supplied, then any one
436 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
437 * and the semaphore is unchanged.
440 semaphore_signal_thread(
441 semaphore_t semaphore
,
446 if (semaphore
== SEMAPHORE_NULL
)
447 return KERN_INVALID_ARGUMENT
;
449 ret
= semaphore_signal_internal(semaphore
,
451 SEMAPHORE_OPTION_NONE
);
456 * Routine: semaphore_signal_thread_trap
458 * Trap interface to the semaphore_signal_thread function.
461 semaphore_signal_thread_trap(
462 struct semaphore_signal_thread_trap_args
*args
)
464 mach_port_name_t sema_name
= args
->signal_name
;
465 mach_port_name_t thread_name
= args
->thread_name
;
466 semaphore_t semaphore
;
471 * MACH_PORT_NULL is not an error. It means that we want to
472 * select any one thread that is already waiting, but not to
473 * pre-post the semaphore.
475 if (thread_name
!= MACH_PORT_NULL
) {
476 thread
= port_name_to_thread(thread_name
);
477 if (thread
== THREAD_NULL
)
478 return KERN_INVALID_ARGUMENT
;
480 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
;
517 kr
= semaphore_signal_internal(semaphore
,
519 SEMAPHORE_SIGNAL_PREPOST
);
520 if (kr
== KERN_NOT_WAITING
)
526 * Routine: semaphore_signal_trap
528 * Trap interface to the semaphore_signal function.
531 semaphore_signal_trap(
532 struct semaphore_signal_trap_args
*args
)
534 mach_port_name_t sema_name
= args
->signal_name
;
536 return (semaphore_signal_internal_trap(sema_name
));
540 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
542 semaphore_t semaphore
;
545 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
546 if (kr
== KERN_SUCCESS
) {
547 kr
= semaphore_signal_internal(semaphore
,
549 SEMAPHORE_SIGNAL_PREPOST
);
550 semaphore_dereference(semaphore
);
551 if (kr
== KERN_NOT_WAITING
)
558 * Routine: semaphore_signal_all
560 * Awakens ALL threads currently blocked on the semaphore.
561 * The semaphore count returns to zero.
564 semaphore_signal_all(
565 semaphore_t semaphore
)
569 if (semaphore
== SEMAPHORE_NULL
)
570 return KERN_INVALID_ARGUMENT
;
572 kr
= semaphore_signal_internal(semaphore
,
574 SEMAPHORE_SIGNAL_ALL
);
575 if (kr
== KERN_NOT_WAITING
)
581 * Routine: semaphore_signal_all_trap
583 * Trap interface to the semaphore_signal_all function.
586 semaphore_signal_all_trap(
587 struct semaphore_signal_all_trap_args
*args
)
589 mach_port_name_t sema_name
= args
->signal_name
;
590 semaphore_t semaphore
;
593 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
594 if (kr
== KERN_SUCCESS
) {
595 kr
= semaphore_signal_internal(semaphore
,
597 SEMAPHORE_SIGNAL_ALL
);
598 semaphore_dereference(semaphore
);
599 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)
643 thread_t self
= current_thread();
644 int wait_result
= self
->wait_result
;
645 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
647 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
648 semaphore_dereference(self
->sth_waitsemaphore
);
649 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
650 semaphore_dereference(self
->sth_signalsemaphore
);
652 assert(caller_cont
!= (void (*)(kern_return_t
))0);
653 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
657 * Routine: semaphore_wait_internal
659 * Decrements the semaphore count by one. If the count is
660 * negative after the decrement, the calling thread blocks
661 * (possibly at a continuation and/or with a timeout).
665 * A reference is held on the signal semaphore.
668 semaphore_wait_internal(
669 semaphore_t wait_semaphore
,
670 semaphore_t signal_semaphore
,
673 void (*caller_cont
)(kern_return_t
))
677 kern_return_t kr
= KERN_ALREADY_WAITING
;
679 spl_level
= splsched();
680 semaphore_lock(wait_semaphore
);
682 if (!wait_semaphore
->active
) {
683 kr
= KERN_TERMINATED
;
684 } else if (wait_semaphore
->count
> 0) {
685 wait_semaphore
->count
--;
687 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
688 kr
= KERN_OPERATION_TIMED_OUT
;
690 thread_t self
= current_thread();
692 wait_semaphore
->count
= -1; /* we don't keep an actual count */
694 thread_set_pending_block_hint(self
, kThreadWaitSemaphore
);
695 (void)waitq_assert_wait64_locked(
696 &wait_semaphore
->waitq
,
699 TIMEOUT_URGENCY_USER_NORMAL
,
700 deadline
, TIMEOUT_NO_LEEWAY
,
703 semaphore_unlock(wait_semaphore
);
707 * wait_semaphore is unlocked so we are free to go ahead and
708 * signal the signal_semaphore (if one was provided).
710 if (signal_semaphore
!= SEMAPHORE_NULL
) {
711 kern_return_t signal_kr
;
714 * lock the signal semaphore reference we got and signal it.
715 * This will NOT block (we cannot block after having asserted
716 * our intention to wait above).
718 signal_kr
= semaphore_signal_internal(signal_semaphore
,
720 SEMAPHORE_SIGNAL_PREPOST
);
722 if (signal_kr
== KERN_NOT_WAITING
)
723 signal_kr
= KERN_SUCCESS
;
724 else if (signal_kr
== KERN_TERMINATED
) {
726 * Uh!Oh! The semaphore we were to signal died.
727 * We have to get ourselves out of the wait in
728 * case we get stuck here forever (it is assumed
729 * that the semaphore we were posting is gating
730 * the decision by someone else to post the
731 * semaphore we are waiting on). People will
732 * discover the other dead semaphore soon enough.
733 * If we got out of the wait cleanly (someone
734 * already posted a wakeup to us) then return that
735 * (most important) result. Otherwise,
736 * return the KERN_TERMINATED status.
738 thread_t self
= current_thread();
740 clear_wait(self
, THREAD_INTERRUPTED
);
741 kr
= semaphore_convert_wait_result(self
->wait_result
);
742 if (kr
== KERN_ABORTED
)
743 kr
= KERN_TERMINATED
;
748 * If we had an error, or we didn't really need to wait we can
749 * return now that we have signalled the signal semaphore.
751 if (kr
!= KERN_ALREADY_WAITING
)
755 * Now, we can block. If the caller supplied a continuation
756 * pointer of his own for after the block, block with the
757 * appropriate semaphore continuation. Thiswill gather the
758 * semaphore results, release references on the semaphore(s),
759 * and then call the caller's continuation.
762 thread_t self
= current_thread();
764 self
->sth_continuation
= caller_cont
;
765 self
->sth_waitsemaphore
= wait_semaphore
;
766 self
->sth_signalsemaphore
= signal_semaphore
;
767 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
770 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
773 return (semaphore_convert_wait_result(wait_result
));
778 * Routine: semaphore_wait
780 * Traditional (non-continuation) interface presented to
781 * in-kernel clients to wait on a semaphore.
785 semaphore_t semaphore
)
788 if (semaphore
== SEMAPHORE_NULL
)
789 return KERN_INVALID_ARGUMENT
;
791 return(semaphore_wait_internal(semaphore
,
793 0ULL, SEMAPHORE_OPTION_NONE
,
794 (void (*)(kern_return_t
))0));
798 semaphore_wait_noblock(
799 semaphore_t semaphore
)
802 if (semaphore
== SEMAPHORE_NULL
)
803 return KERN_INVALID_ARGUMENT
;
805 return(semaphore_wait_internal(semaphore
,
807 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
808 (void (*)(kern_return_t
))0));
812 semaphore_wait_deadline(
813 semaphore_t semaphore
,
817 if (semaphore
== SEMAPHORE_NULL
)
818 return KERN_INVALID_ARGUMENT
;
820 return(semaphore_wait_internal(semaphore
,
822 deadline
, SEMAPHORE_OPTION_NONE
,
823 (void (*)(kern_return_t
))0));
827 * Trap: semaphore_wait_trap
829 * Trap version of semaphore wait. Called on behalf of user-level
835 struct semaphore_wait_trap_args
*args
)
837 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
843 semaphore_wait_trap_internal(
844 mach_port_name_t name
,
845 void (*caller_cont
)(kern_return_t
))
847 semaphore_t semaphore
;
850 kr
= port_name_to_semaphore(name
, &semaphore
);
851 if (kr
== KERN_SUCCESS
) {
852 kr
= semaphore_wait_internal(semaphore
,
854 0ULL, SEMAPHORE_OPTION_NONE
,
856 semaphore_dereference(semaphore
);
862 * Routine: semaphore_timedwait
864 * Traditional (non-continuation) interface presented to
865 * in-kernel clients to wait on a semaphore with a timeout.
867 * A timeout of {0,0} is considered non-blocking.
871 semaphore_t semaphore
,
872 mach_timespec_t wait_time
)
874 int option
= SEMAPHORE_OPTION_NONE
;
875 uint64_t deadline
= 0;
877 if (semaphore
== SEMAPHORE_NULL
)
878 return KERN_INVALID_ARGUMENT
;
880 if(BAD_MACH_TIMESPEC(&wait_time
))
881 return KERN_INVALID_VALUE
;
883 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
884 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
886 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
888 return (semaphore_wait_internal(semaphore
,
891 (void(*)(kern_return_t
))0));
896 * Trap: semaphore_timedwait_trap
898 * Trap version of a semaphore_timedwait. The timeout parameter
899 * is passed in two distinct parts and re-assembled on this side
900 * of the trap interface (to accomodate calling conventions that
901 * pass structures as pointers instead of inline in registers without
902 * having to add a copyin).
904 * A timeout of {0,0} is considered non-blocking.
907 semaphore_timedwait_trap(
908 struct semaphore_timedwait_trap_args
*args
)
911 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
916 semaphore_timedwait_trap_internal(
917 mach_port_name_t name
,
920 void (*caller_cont
)(kern_return_t
))
922 semaphore_t semaphore
;
923 mach_timespec_t wait_time
;
926 wait_time
.tv_sec
= sec
;
927 wait_time
.tv_nsec
= nsec
;
928 if(BAD_MACH_TIMESPEC(&wait_time
))
929 return KERN_INVALID_VALUE
;
931 kr
= port_name_to_semaphore(name
, &semaphore
);
932 if (kr
== KERN_SUCCESS
) {
933 int option
= SEMAPHORE_OPTION_NONE
;
934 uint64_t deadline
= 0;
936 if (sec
== 0 && nsec
== 0)
937 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
939 deadline
= semaphore_deadline(sec
, nsec
);
941 kr
= semaphore_wait_internal(semaphore
,
945 semaphore_dereference(semaphore
);
951 * Routine: semaphore_wait_signal
953 * Atomically register a wait on a semaphore and THEN signal
954 * another. This is the in-kernel entry point that does not
955 * block at a continuation and does not free a signal_semaphore
959 semaphore_wait_signal(
960 semaphore_t wait_semaphore
,
961 semaphore_t signal_semaphore
)
963 if (wait_semaphore
== SEMAPHORE_NULL
)
964 return KERN_INVALID_ARGUMENT
;
966 return(semaphore_wait_internal(wait_semaphore
,
968 0ULL, SEMAPHORE_OPTION_NONE
,
969 (void(*)(kern_return_t
))0));
973 * Trap: semaphore_wait_signal_trap
975 * Atomically register a wait on a semaphore and THEN signal
976 * another. This is the trap version from user space.
979 semaphore_wait_signal_trap(
980 struct semaphore_wait_signal_trap_args
*args
)
982 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
986 semaphore_wait_signal_trap_internal(
987 mach_port_name_t wait_name
,
988 mach_port_name_t signal_name
,
989 void (*caller_cont
)(kern_return_t
))
991 semaphore_t wait_semaphore
;
992 semaphore_t signal_semaphore
;
995 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
996 if (kr
== KERN_SUCCESS
) {
997 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
998 if (kr
== KERN_SUCCESS
) {
999 kr
= semaphore_wait_internal(wait_semaphore
,
1001 0ULL, SEMAPHORE_OPTION_NONE
,
1003 semaphore_dereference(wait_semaphore
);
1005 semaphore_dereference(signal_semaphore
);
1012 * Routine: semaphore_timedwait_signal
1014 * Atomically register a wait on a semaphore and THEN signal
1015 * another. This is the in-kernel entry point that does not
1016 * block at a continuation.
1018 * A timeout of {0,0} is considered non-blocking.
1021 semaphore_timedwait_signal(
1022 semaphore_t wait_semaphore
,
1023 semaphore_t signal_semaphore
,
1024 mach_timespec_t wait_time
)
1026 int option
= SEMAPHORE_OPTION_NONE
;
1027 uint64_t deadline
= 0;
1029 if (wait_semaphore
== SEMAPHORE_NULL
)
1030 return KERN_INVALID_ARGUMENT
;
1032 if(BAD_MACH_TIMESPEC(&wait_time
))
1033 return KERN_INVALID_VALUE
;
1035 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
1036 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1038 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1040 return(semaphore_wait_internal(wait_semaphore
,
1043 (void(*)(kern_return_t
))0));
1047 * Trap: semaphore_timedwait_signal_trap
1049 * Atomically register a timed wait on a semaphore and THEN signal
1050 * another. This is the trap version from user space.
1053 semaphore_timedwait_signal_trap(
1054 struct semaphore_timedwait_signal_trap_args
*args
)
1056 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1060 semaphore_timedwait_signal_trap_internal(
1061 mach_port_name_t wait_name
,
1062 mach_port_name_t signal_name
,
1065 void (*caller_cont
)(kern_return_t
))
1067 semaphore_t wait_semaphore
;
1068 semaphore_t signal_semaphore
;
1069 mach_timespec_t wait_time
;
1072 wait_time
.tv_sec
= sec
;
1073 wait_time
.tv_nsec
= nsec
;
1074 if(BAD_MACH_TIMESPEC(&wait_time
))
1075 return KERN_INVALID_VALUE
;
1077 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1078 if (kr
== KERN_SUCCESS
) {
1079 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1080 if (kr
== KERN_SUCCESS
) {
1081 int option
= SEMAPHORE_OPTION_NONE
;
1082 uint64_t deadline
= 0;
1084 if (sec
== 0 && nsec
== 0)
1085 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1087 deadline
= semaphore_deadline(sec
, nsec
);
1089 kr
= semaphore_wait_internal(wait_semaphore
,
1093 semaphore_dereference(wait_semaphore
);
1095 semaphore_dereference(signal_semaphore
);
1102 * Routine: semaphore_reference
1104 * Take out a reference on a semaphore. This keeps the data structure
1105 * in existence (but the semaphore may be deactivated).
1108 semaphore_reference(
1109 semaphore_t semaphore
)
1111 os_ref_retain(&semaphore
->ref_count
);
1115 * Routine: semaphore_dereference
1117 * Release a reference on a semaphore. If this is the last reference,
1118 * the semaphore data structure is deallocated.
1121 semaphore_dereference(
1122 semaphore_t semaphore
)
1124 uint32_t collisions
;
1127 if (semaphore
== NULL
)
1130 if (os_ref_release(&semaphore
->ref_count
) > 0) {
1135 * Last ref, clean up the port [if any]
1136 * associated with the semaphore, destroy
1137 * it (if still active) and then free
1140 ipc_port_t port
= semaphore
->port
;
1142 if (IP_VALID(port
)) {
1143 assert(!port
->ip_srights
);
1144 ipc_port_dealloc_kernel(port
);
1148 * Lock the semaphore to lock in the owner task reference.
1149 * Then continue to try to lock the task (inverse order).
1151 spl_level
= splsched();
1152 semaphore_lock(semaphore
);
1153 for (collisions
= 0; semaphore
->active
; collisions
++) {
1154 task_t task
= semaphore
->owner
;
1156 assert(task
!= TASK_NULL
);
1158 if (task_lock_try(task
)) {
1159 semaphore_destroy_internal(task
, semaphore
);
1160 /* semaphore unlocked */
1166 /* failed to get out-of-order locks */
1167 semaphore_unlock(semaphore
);
1169 mutex_pause(collisions
);
1170 spl_level
= splsched();
1171 semaphore_lock(semaphore
);
1173 semaphore_unlock(semaphore
);
1177 zfree(semaphore_zone
, semaphore
);
1180 #define WAITQ_TO_SEMA(wq) ((semaphore_t) ((uintptr_t)(wq) - offsetof(struct semaphore, waitq)))
1182 kdp_sema_find_owner(struct waitq
* waitq
, __assert_only event64_t event
, thread_waitinfo_t
* waitinfo
)
1184 semaphore_t sem
= WAITQ_TO_SEMA(waitq
);
1185 assert(event
== SEMAPHORE_EVENT
);
1186 assert(kdp_is_in_zone(sem
, "semaphores"));
1188 waitinfo
->context
= VM_KERNEL_UNSLIDE_OR_PERM(sem
->port
);
1190 waitinfo
->owner
= pid_from_task(sem
->owner
);