2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * File: kern/sync_sema.c
34 * Author: Joseph CaraDonna
36 * Contains RT distributed semaphore synchronization services.
39 #include <mach/mach_types.h>
40 #include <mach/mach_traps.h>
41 #include <mach/kern_return.h>
42 #include <mach/semaphore.h>
43 #include <mach/sync_policy.h>
44 #include <mach/task.h>
46 #include <kern/misc_protos.h>
47 #include <kern/sync_sema.h>
49 #include <kern/ipc_kobject.h>
50 #include <kern/ipc_sync.h>
51 #include <kern/ipc_tt.h>
52 #include <kern/thread.h>
53 #include <kern/clock.h>
54 #include <ipc/ipc_port.h>
55 #include <ipc/ipc_space.h>
56 #include <kern/host.h>
57 #include <kern/waitq.h>
58 #include <kern/zalloc.h>
59 #include <kern/mach_param.h>
61 #include <libkern/OSAtomic.h>
63 static unsigned int semaphore_event
;
64 #define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event)
66 zone_t semaphore_zone
;
67 unsigned int semaphore_max
;
69 /* Forward declarations */
73 semaphore_wait_trap_internal(
74 mach_port_name_t name
,
75 void (*caller_cont
)(kern_return_t
));
78 semaphore_wait_signal_trap_internal(
79 mach_port_name_t wait_name
,
80 mach_port_name_t signal_name
,
81 void (*caller_cont
)(kern_return_t
));
84 semaphore_timedwait_trap_internal(
85 mach_port_name_t name
,
88 void (*caller_cont
)(kern_return_t
));
91 semaphore_timedwait_signal_trap_internal(
92 mach_port_name_t wait_name
,
93 mach_port_name_t signal_name
,
96 void (*caller_cont
)(kern_return_t
));
99 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
102 semaphore_signal_internal(
103 semaphore_t semaphore
,
108 semaphore_convert_wait_result(
112 semaphore_wait_continue(void);
115 semaphore_wait_internal(
116 semaphore_t wait_semaphore
,
117 semaphore_t signal_semaphore
,
120 void (*caller_cont
)(kern_return_t
));
122 static __inline__
uint64_t
129 nanoseconds_to_absolutetime((uint64_t)sec
* NSEC_PER_SEC
+ nsec
, &abstime
);
130 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
136 * ROUTINE: semaphore_init [private]
138 * Initialize the semaphore mechanisms.
139 * Right now, we only need to initialize the semaphore zone.
144 semaphore_zone
= zinit(sizeof(struct semaphore
),
145 semaphore_max
* sizeof(struct semaphore
),
146 sizeof(struct semaphore
),
148 zone_change(semaphore_zone
, Z_NOENCRYPT
, TRUE
);
152 * Routine: semaphore_create
154 * Creates a semaphore.
155 * The port representing the semaphore is returned as a parameter.
160 semaphore_t
*new_semaphore
,
164 semaphore_t s
= SEMAPHORE_NULL
;
168 *new_semaphore
= SEMAPHORE_NULL
;
169 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
)
170 return KERN_INVALID_ARGUMENT
;
172 s
= (semaphore_t
) zalloc (semaphore_zone
);
174 if (s
== SEMAPHORE_NULL
)
175 return KERN_RESOURCE_SHORTAGE
;
177 kret
= waitq_init(&s
->waitq
, policy
| SYNC_POLICY_DISABLE_IRQ
); /* also inits lock */
178 if (kret
!= KERN_SUCCESS
) {
179 zfree(semaphore_zone
, s
);
184 * Initialize the semaphore values.
193 * Associate the new semaphore with the task by adding
194 * the new semaphore to the task's semaphore list.
197 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
198 task
->semaphores_owned
++;
207 * Routine: semaphore_destroy_internal
209 * Disassociate a semaphore from its owning task, mark it inactive,
210 * and set any waiting threads running with THREAD_RESTART.
214 * semaphore is locked
215 * semaphore is owned by the specified task
217 * with semaphore unlocked
220 semaphore_destroy_internal(
222 semaphore_t semaphore
)
226 /* unlink semaphore from owning task */
227 assert(semaphore
->owner
== task
);
228 remqueue((queue_entry_t
) semaphore
);
229 semaphore
->owner
= TASK_NULL
;
230 task
->semaphores_owned
--;
233 * Deactivate semaphore
235 assert(semaphore
->active
);
236 semaphore
->active
= FALSE
;
239 * Wakeup blocked threads
241 old_count
= semaphore
->count
;
242 semaphore
->count
= 0;
245 waitq_wakeup64_all_locked(&semaphore
->waitq
,
247 THREAD_RESTART
, NULL
,
248 WAITQ_ALL_PRIORITIES
,
250 /* waitq/semaphore is unlocked */
252 semaphore_unlock(semaphore
);
257 * Routine: semaphore_destroy
259 * Destroys a semaphore and consume the caller's reference on the
265 semaphore_t semaphore
)
269 if (semaphore
== SEMAPHORE_NULL
)
270 return KERN_INVALID_ARGUMENT
;
272 if (task
== TASK_NULL
) {
273 semaphore_dereference(semaphore
);
274 return KERN_INVALID_ARGUMENT
;
278 spl_level
= splsched();
279 semaphore_lock(semaphore
);
281 if (semaphore
->owner
!= task
) {
282 semaphore_unlock(semaphore
);
285 return KERN_INVALID_ARGUMENT
;
288 semaphore_destroy_internal(task
, semaphore
);
289 /* semaphore unlocked */
294 semaphore_dereference(semaphore
);
299 * Routine: semaphore_destroy_all
301 * Destroy all the semaphores associated with a given task.
303 #define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
306 semaphore_destroy_all(
314 while (!queue_empty(&task
->semaphore_list
)) {
315 semaphore_t semaphore
;
317 semaphore
= (semaphore_t
) queue_first(&task
->semaphore_list
);
320 spl_level
= splsched();
321 semaphore_lock(semaphore
);
323 semaphore_destroy_internal(task
, semaphore
);
324 /* semaphore unlocked */
326 /* throttle number of semaphores per interrupt disablement */
327 if (++count
== SEMASPERSPL
) {
339 * Routine: semaphore_signal_internal
341 * Signals the semaphore as direct.
343 * Semaphore is locked.
346 semaphore_signal_internal(
347 semaphore_t semaphore
,
354 spl_level
= splsched();
355 semaphore_lock(semaphore
);
357 if (!semaphore
->active
) {
358 semaphore_unlock(semaphore
);
360 return KERN_TERMINATED
;
363 if (thread
!= THREAD_NULL
) {
364 if (semaphore
->count
< 0) {
365 kr
= waitq_wakeup64_thread_locked(
371 /* waitq/semaphore is unlocked */
373 kr
= KERN_NOT_WAITING
;
374 semaphore_unlock(semaphore
);
380 if (options
& SEMAPHORE_SIGNAL_ALL
) {
381 int old_count
= semaphore
->count
;
383 kr
= KERN_NOT_WAITING
;
385 semaphore
->count
= 0; /* always reset */
386 kr
= waitq_wakeup64_all_locked(
389 THREAD_AWAKENED
, NULL
,
390 WAITQ_ALL_PRIORITIES
,
392 /* waitq / semaphore is unlocked */
394 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
397 semaphore_unlock(semaphore
);
403 if (semaphore
->count
< 0) {
404 kr
= waitq_wakeup64_one_locked(
407 THREAD_AWAKENED
, NULL
,
408 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
;
446 ret
= semaphore_signal_internal(semaphore
,
448 SEMAPHORE_OPTION_NONE
);
453 * Routine: semaphore_signal_thread_trap
455 * Trap interface to the semaphore_signal_thread function.
458 semaphore_signal_thread_trap(
459 struct semaphore_signal_thread_trap_args
*args
)
461 mach_port_name_t sema_name
= args
->signal_name
;
462 mach_port_name_t thread_name
= args
->thread_name
;
463 semaphore_t semaphore
;
468 * MACH_PORT_NULL is not an error. It means that we want to
469 * select any one thread that is already waiting, but not to
470 * pre-post the semaphore.
472 if (thread_name
!= MACH_PORT_NULL
) {
473 thread
= port_name_to_thread(thread_name
);
474 if (thread
== THREAD_NULL
)
475 return KERN_INVALID_ARGUMENT
;
477 thread
= THREAD_NULL
;
479 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
480 if (kr
== KERN_SUCCESS
) {
481 kr
= semaphore_signal_internal(semaphore
,
483 SEMAPHORE_OPTION_NONE
);
484 semaphore_dereference(semaphore
);
486 if (thread
!= THREAD_NULL
) {
487 thread_deallocate(thread
);
495 * Routine: semaphore_signal
497 * Traditional (in-kernel client and MIG interface) semaphore
498 * signal routine. Most users will access the trap version.
500 * This interface in not defined to return info about whether
501 * this call found a thread waiting or not. The internal
502 * routines (and future external routines) do. We have to
503 * convert those into plain KERN_SUCCESS returns.
507 semaphore_t semaphore
)
511 if (semaphore
== SEMAPHORE_NULL
)
512 return KERN_INVALID_ARGUMENT
;
514 kr
= semaphore_signal_internal(semaphore
,
516 SEMAPHORE_SIGNAL_PREPOST
);
517 if (kr
== KERN_NOT_WAITING
)
523 * Routine: semaphore_signal_trap
525 * Trap interface to the semaphore_signal function.
528 semaphore_signal_trap(
529 struct semaphore_signal_trap_args
*args
)
531 mach_port_name_t sema_name
= args
->signal_name
;
533 return (semaphore_signal_internal_trap(sema_name
));
537 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
539 semaphore_t semaphore
;
542 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
543 if (kr
== KERN_SUCCESS
) {
544 kr
= semaphore_signal_internal(semaphore
,
546 SEMAPHORE_SIGNAL_PREPOST
);
547 semaphore_dereference(semaphore
);
548 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
;
569 kr
= semaphore_signal_internal(semaphore
,
571 SEMAPHORE_SIGNAL_ALL
);
572 if (kr
== KERN_NOT_WAITING
)
578 * Routine: semaphore_signal_all_trap
580 * Trap interface to the semaphore_signal_all function.
583 semaphore_signal_all_trap(
584 struct semaphore_signal_all_trap_args
*args
)
586 mach_port_name_t sema_name
= args
->signal_name
;
587 semaphore_t semaphore
;
590 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
591 if (kr
== KERN_SUCCESS
) {
592 kr
= semaphore_signal_internal(semaphore
,
594 SEMAPHORE_SIGNAL_ALL
);
595 semaphore_dereference(semaphore
);
596 if (kr
== KERN_NOT_WAITING
)
603 * Routine: semaphore_convert_wait_result
605 * Generate the return code after a semaphore wait/block. It
606 * takes the wait result as an input and coverts that to an
607 * appropriate result.
610 semaphore_convert_wait_result(int wait_result
)
612 switch (wait_result
) {
613 case THREAD_AWAKENED
:
616 case THREAD_TIMED_OUT
:
617 return KERN_OPERATION_TIMED_OUT
;
619 case THREAD_INTERRUPTED
:
623 return KERN_TERMINATED
;
626 panic("semaphore_block\n");
632 * Routine: semaphore_wait_continue
634 * Common continuation routine after waiting on a semphore.
635 * It returns directly to user space.
638 semaphore_wait_continue(void)
640 thread_t self
= current_thread();
641 int wait_result
= self
->wait_result
;
642 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
644 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
645 semaphore_dereference(self
->sth_waitsemaphore
);
646 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
647 semaphore_dereference(self
->sth_signalsemaphore
);
649 assert(caller_cont
!= (void (*)(kern_return_t
))0);
650 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
654 * Routine: semaphore_wait_internal
656 * Decrements the semaphore count by one. If the count is
657 * negative after the decrement, the calling thread blocks
658 * (possibly at a continuation and/or with a timeout).
662 * A reference is held on the signal semaphore.
665 semaphore_wait_internal(
666 semaphore_t wait_semaphore
,
667 semaphore_t signal_semaphore
,
670 void (*caller_cont
)(kern_return_t
))
674 kern_return_t kr
= KERN_ALREADY_WAITING
;
676 spl_level
= splsched();
677 semaphore_lock(wait_semaphore
);
679 if (!wait_semaphore
->active
) {
680 kr
= KERN_TERMINATED
;
681 } else if (wait_semaphore
->count
> 0) {
682 wait_semaphore
->count
--;
684 } else if (option
& SEMAPHORE_TIMEOUT_NOBLOCK
) {
685 kr
= KERN_OPERATION_TIMED_OUT
;
687 thread_t self
= current_thread();
689 wait_semaphore
->count
= -1; /* we don't keep an actual count */
690 (void)waitq_assert_wait64_locked(
691 &wait_semaphore
->waitq
,
694 TIMEOUT_URGENCY_USER_NORMAL
,
695 deadline
, TIMEOUT_NO_LEEWAY
,
698 semaphore_unlock(wait_semaphore
);
702 * wait_semaphore is unlocked so we are free to go ahead and
703 * signal the signal_semaphore (if one was provided).
705 if (signal_semaphore
!= SEMAPHORE_NULL
) {
706 kern_return_t signal_kr
;
709 * lock the signal semaphore reference we got and signal it.
710 * This will NOT block (we cannot block after having asserted
711 * our intention to wait above).
713 signal_kr
= semaphore_signal_internal(signal_semaphore
,
715 SEMAPHORE_SIGNAL_PREPOST
);
717 if (signal_kr
== KERN_NOT_WAITING
)
718 signal_kr
= KERN_SUCCESS
;
719 else if (signal_kr
== KERN_TERMINATED
) {
721 * Uh!Oh! The semaphore we were to signal died.
722 * We have to get ourselves out of the wait in
723 * case we get stuck here forever (it is assumed
724 * that the semaphore we were posting is gating
725 * the decision by someone else to post the
726 * semaphore we are waiting on). People will
727 * discover the other dead semaphore soon enough.
728 * If we got out of the wait cleanly (someone
729 * already posted a wakeup to us) then return that
730 * (most important) result. Otherwise,
731 * return the KERN_TERMINATED status.
733 thread_t self
= current_thread();
735 clear_wait(self
, THREAD_INTERRUPTED
);
736 kr
= semaphore_convert_wait_result(self
->wait_result
);
737 if (kr
== KERN_ABORTED
)
738 kr
= KERN_TERMINATED
;
743 * If we had an error, or we didn't really need to wait we can
744 * return now that we have signalled the signal semaphore.
746 if (kr
!= KERN_ALREADY_WAITING
)
750 * Now, we can block. If the caller supplied a continuation
751 * pointer of his own for after the block, block with the
752 * appropriate semaphore continuation. Thiswill gather the
753 * semaphore results, release references on the semaphore(s),
754 * and then call the caller's continuation.
757 thread_t self
= current_thread();
759 self
->sth_continuation
= caller_cont
;
760 self
->sth_waitsemaphore
= wait_semaphore
;
761 self
->sth_signalsemaphore
= signal_semaphore
;
762 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
765 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
768 return (semaphore_convert_wait_result(wait_result
));
773 * Routine: semaphore_wait
775 * Traditional (non-continuation) interface presented to
776 * in-kernel clients to wait on a semaphore.
780 semaphore_t semaphore
)
783 if (semaphore
== SEMAPHORE_NULL
)
784 return KERN_INVALID_ARGUMENT
;
786 return(semaphore_wait_internal(semaphore
,
788 0ULL, SEMAPHORE_OPTION_NONE
,
789 (void (*)(kern_return_t
))0));
793 semaphore_wait_noblock(
794 semaphore_t semaphore
)
797 if (semaphore
== SEMAPHORE_NULL
)
798 return KERN_INVALID_ARGUMENT
;
800 return(semaphore_wait_internal(semaphore
,
802 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
803 (void (*)(kern_return_t
))0));
807 semaphore_wait_deadline(
808 semaphore_t semaphore
,
812 if (semaphore
== SEMAPHORE_NULL
)
813 return KERN_INVALID_ARGUMENT
;
815 return(semaphore_wait_internal(semaphore
,
817 deadline
, SEMAPHORE_OPTION_NONE
,
818 (void (*)(kern_return_t
))0));
822 * Trap: semaphore_wait_trap
824 * Trap version of semaphore wait. Called on behalf of user-level
830 struct semaphore_wait_trap_args
*args
)
832 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
838 semaphore_wait_trap_internal(
839 mach_port_name_t name
,
840 void (*caller_cont
)(kern_return_t
))
842 semaphore_t semaphore
;
845 kr
= port_name_to_semaphore(name
, &semaphore
);
846 if (kr
== KERN_SUCCESS
) {
847 kr
= semaphore_wait_internal(semaphore
,
849 0ULL, SEMAPHORE_OPTION_NONE
,
851 semaphore_dereference(semaphore
);
857 * Routine: semaphore_timedwait
859 * Traditional (non-continuation) interface presented to
860 * in-kernel clients to wait on a semaphore with a timeout.
862 * A timeout of {0,0} is considered non-blocking.
866 semaphore_t semaphore
,
867 mach_timespec_t wait_time
)
869 int option
= SEMAPHORE_OPTION_NONE
;
870 uint64_t deadline
= 0;
872 if (semaphore
== SEMAPHORE_NULL
)
873 return KERN_INVALID_ARGUMENT
;
875 if(BAD_MACH_TIMESPEC(&wait_time
))
876 return KERN_INVALID_VALUE
;
878 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
879 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
881 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
883 return (semaphore_wait_internal(semaphore
,
886 (void(*)(kern_return_t
))0));
891 * Trap: semaphore_timedwait_trap
893 * Trap version of a semaphore_timedwait. The timeout parameter
894 * is passed in two distinct parts and re-assembled on this side
895 * of the trap interface (to accomodate calling conventions that
896 * pass structures as pointers instead of inline in registers without
897 * having to add a copyin).
899 * A timeout of {0,0} is considered non-blocking.
902 semaphore_timedwait_trap(
903 struct semaphore_timedwait_trap_args
*args
)
906 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
911 semaphore_timedwait_trap_internal(
912 mach_port_name_t name
,
915 void (*caller_cont
)(kern_return_t
))
917 semaphore_t semaphore
;
918 mach_timespec_t wait_time
;
921 wait_time
.tv_sec
= sec
;
922 wait_time
.tv_nsec
= nsec
;
923 if(BAD_MACH_TIMESPEC(&wait_time
))
924 return KERN_INVALID_VALUE
;
926 kr
= port_name_to_semaphore(name
, &semaphore
);
927 if (kr
== KERN_SUCCESS
) {
928 int option
= SEMAPHORE_OPTION_NONE
;
929 uint64_t deadline
= 0;
931 if (sec
== 0 && nsec
== 0)
932 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
934 deadline
= semaphore_deadline(sec
, nsec
);
936 kr
= semaphore_wait_internal(semaphore
,
940 semaphore_dereference(semaphore
);
946 * Routine: semaphore_wait_signal
948 * Atomically register a wait on a semaphore and THEN signal
949 * another. This is the in-kernel entry point that does not
950 * block at a continuation and does not free a signal_semaphore
954 semaphore_wait_signal(
955 semaphore_t wait_semaphore
,
956 semaphore_t signal_semaphore
)
958 if (wait_semaphore
== SEMAPHORE_NULL
)
959 return KERN_INVALID_ARGUMENT
;
961 return(semaphore_wait_internal(wait_semaphore
,
963 0ULL, SEMAPHORE_OPTION_NONE
,
964 (void(*)(kern_return_t
))0));
968 * Trap: semaphore_wait_signal_trap
970 * Atomically register a wait on a semaphore and THEN signal
971 * another. This is the trap version from user space.
974 semaphore_wait_signal_trap(
975 struct semaphore_wait_signal_trap_args
*args
)
977 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
981 semaphore_wait_signal_trap_internal(
982 mach_port_name_t wait_name
,
983 mach_port_name_t signal_name
,
984 void (*caller_cont
)(kern_return_t
))
986 semaphore_t wait_semaphore
;
987 semaphore_t signal_semaphore
;
990 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
991 if (kr
== KERN_SUCCESS
) {
992 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
993 if (kr
== KERN_SUCCESS
) {
994 kr
= semaphore_wait_internal(wait_semaphore
,
996 0ULL, SEMAPHORE_OPTION_NONE
,
998 semaphore_dereference(wait_semaphore
);
1000 semaphore_dereference(signal_semaphore
);
1007 * Routine: semaphore_timedwait_signal
1009 * Atomically register a wait on a semaphore and THEN signal
1010 * another. This is the in-kernel entry point that does not
1011 * block at a continuation.
1013 * A timeout of {0,0} is considered non-blocking.
1016 semaphore_timedwait_signal(
1017 semaphore_t wait_semaphore
,
1018 semaphore_t signal_semaphore
,
1019 mach_timespec_t wait_time
)
1021 int option
= SEMAPHORE_OPTION_NONE
;
1022 uint64_t deadline
= 0;
1024 if (wait_semaphore
== SEMAPHORE_NULL
)
1025 return KERN_INVALID_ARGUMENT
;
1027 if(BAD_MACH_TIMESPEC(&wait_time
))
1028 return KERN_INVALID_VALUE
;
1030 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
1031 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1033 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1035 return(semaphore_wait_internal(wait_semaphore
,
1038 (void(*)(kern_return_t
))0));
1042 * Trap: semaphore_timedwait_signal_trap
1044 * Atomically register a timed wait on a semaphore and THEN signal
1045 * another. This is the trap version from user space.
1048 semaphore_timedwait_signal_trap(
1049 struct semaphore_timedwait_signal_trap_args
*args
)
1051 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1055 semaphore_timedwait_signal_trap_internal(
1056 mach_port_name_t wait_name
,
1057 mach_port_name_t signal_name
,
1060 void (*caller_cont
)(kern_return_t
))
1062 semaphore_t wait_semaphore
;
1063 semaphore_t signal_semaphore
;
1064 mach_timespec_t wait_time
;
1067 wait_time
.tv_sec
= sec
;
1068 wait_time
.tv_nsec
= nsec
;
1069 if(BAD_MACH_TIMESPEC(&wait_time
))
1070 return KERN_INVALID_VALUE
;
1072 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1073 if (kr
== KERN_SUCCESS
) {
1074 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1075 if (kr
== KERN_SUCCESS
) {
1076 int option
= SEMAPHORE_OPTION_NONE
;
1077 uint64_t deadline
= 0;
1079 if (sec
== 0 && nsec
== 0)
1080 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1082 deadline
= semaphore_deadline(sec
, nsec
);
1084 kr
= semaphore_wait_internal(wait_semaphore
,
1088 semaphore_dereference(wait_semaphore
);
1090 semaphore_dereference(signal_semaphore
);
1097 * Routine: semaphore_reference
1099 * Take out a reference on a semaphore. This keeps the data structure
1100 * in existence (but the semaphore may be deactivated).
1103 semaphore_reference(
1104 semaphore_t semaphore
)
1106 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1110 * Routine: semaphore_dereference
1112 * Release a reference on a semaphore. If this is the last reference,
1113 * the semaphore data structure is deallocated.
1116 semaphore_dereference(
1117 semaphore_t semaphore
)
1119 uint32_t collisions
;
1122 if (semaphore
== NULL
)
1125 if (hw_atomic_sub(&semaphore
->ref_count
, 1) != 0)
1129 * Last ref, clean up the port [if any]
1130 * associated with the semaphore, destroy
1131 * it (if still active) and then free
1134 ipc_port_t port
= semaphore
->port
;
1136 if (IP_VALID(port
)) {
1137 assert(!port
->ip_srights
);
1138 ipc_port_dealloc_kernel(port
);
1142 * Lock the semaphore to lock in the owner task reference.
1143 * Then continue to try to lock the task (inverse order).
1145 spl_level
= splsched();
1146 semaphore_lock(semaphore
);
1147 for (collisions
= 0; semaphore
->active
; collisions
++) {
1148 task_t task
= semaphore
->owner
;
1150 assert(task
!= TASK_NULL
);
1152 if (task_lock_try(task
)) {
1153 semaphore_destroy_internal(task
, semaphore
);
1154 /* semaphore unlocked */
1160 /* failed to get out-of-order locks */
1161 semaphore_unlock(semaphore
);
1163 mutex_pause(collisions
);
1164 spl_level
= splsched();
1165 semaphore_lock(semaphore
);
1167 semaphore_unlock(semaphore
);
1171 zfree(semaphore_zone
, semaphore
);