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 */
691 (void)waitq_assert_wait64_locked(
692 &wait_semaphore
->waitq
,
695 TIMEOUT_URGENCY_USER_NORMAL
,
696 deadline
, TIMEOUT_NO_LEEWAY
,
700 semaphore_unlock(wait_semaphore
);
704 * wait_semaphore is unlocked so we are free to go ahead and
705 * signal the signal_semaphore (if one was provided).
707 if (signal_semaphore
!= SEMAPHORE_NULL
) {
708 kern_return_t signal_kr
;
711 * lock the signal semaphore reference we got and signal it.
712 * This will NOT block (we cannot block after having asserted
713 * our intention to wait above).
715 signal_kr
= semaphore_signal_internal(signal_semaphore
,
717 SEMAPHORE_SIGNAL_PREPOST
);
719 if (signal_kr
== KERN_NOT_WAITING
)
720 signal_kr
= KERN_SUCCESS
;
721 else if (signal_kr
== KERN_TERMINATED
) {
723 * Uh!Oh! The semaphore we were to signal died.
724 * We have to get ourselves out of the wait in
725 * case we get stuck here forever (it is assumed
726 * that the semaphore we were posting is gating
727 * the decision by someone else to post the
728 * semaphore we are waiting on). People will
729 * discover the other dead semaphore soon enough.
730 * If we got out of the wait cleanly (someone
731 * already posted a wakeup to us) then return that
732 * (most important) result. Otherwise,
733 * return the KERN_TERMINATED status.
735 thread_t self
= current_thread();
737 clear_wait(self
, THREAD_INTERRUPTED
);
738 kr
= semaphore_convert_wait_result(self
->wait_result
);
739 if (kr
== KERN_ABORTED
)
740 kr
= KERN_TERMINATED
;
745 * If we had an error, or we didn't really need to wait we can
746 * return now that we have signalled the signal semaphore.
748 if (kr
!= KERN_ALREADY_WAITING
)
752 * Now, we can block. If the caller supplied a continuation
753 * pointer of his own for after the block, block with the
754 * appropriate semaphore continuation. Thiswill gather the
755 * semaphore results, release references on the semaphore(s),
756 * and then call the caller's continuation.
759 thread_t self
= current_thread();
761 self
->sth_continuation
= caller_cont
;
762 self
->sth_waitsemaphore
= wait_semaphore
;
763 self
->sth_signalsemaphore
= signal_semaphore
;
764 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
767 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
770 return (semaphore_convert_wait_result(wait_result
));
775 * Routine: semaphore_wait
777 * Traditional (non-continuation) interface presented to
778 * in-kernel clients to wait on a semaphore.
782 semaphore_t semaphore
)
785 if (semaphore
== SEMAPHORE_NULL
)
786 return KERN_INVALID_ARGUMENT
;
788 return(semaphore_wait_internal(semaphore
,
790 0ULL, SEMAPHORE_OPTION_NONE
,
791 (void (*)(kern_return_t
))0));
795 semaphore_wait_noblock(
796 semaphore_t semaphore
)
799 if (semaphore
== SEMAPHORE_NULL
)
800 return KERN_INVALID_ARGUMENT
;
802 return(semaphore_wait_internal(semaphore
,
804 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK
,
805 (void (*)(kern_return_t
))0));
809 semaphore_wait_deadline(
810 semaphore_t semaphore
,
814 if (semaphore
== SEMAPHORE_NULL
)
815 return KERN_INVALID_ARGUMENT
;
817 return(semaphore_wait_internal(semaphore
,
819 deadline
, SEMAPHORE_OPTION_NONE
,
820 (void (*)(kern_return_t
))0));
824 * Trap: semaphore_wait_trap
826 * Trap version of semaphore wait. Called on behalf of user-level
832 struct semaphore_wait_trap_args
*args
)
834 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
840 semaphore_wait_trap_internal(
841 mach_port_name_t name
,
842 void (*caller_cont
)(kern_return_t
))
844 semaphore_t semaphore
;
847 kr
= port_name_to_semaphore(name
, &semaphore
);
848 if (kr
== KERN_SUCCESS
) {
849 kr
= semaphore_wait_internal(semaphore
,
851 0ULL, SEMAPHORE_OPTION_NONE
,
853 semaphore_dereference(semaphore
);
859 * Routine: semaphore_timedwait
861 * Traditional (non-continuation) interface presented to
862 * in-kernel clients to wait on a semaphore with a timeout.
864 * A timeout of {0,0} is considered non-blocking.
868 semaphore_t semaphore
,
869 mach_timespec_t wait_time
)
871 int option
= SEMAPHORE_OPTION_NONE
;
872 uint64_t deadline
= 0;
874 if (semaphore
== SEMAPHORE_NULL
)
875 return KERN_INVALID_ARGUMENT
;
877 if(BAD_MACH_TIMESPEC(&wait_time
))
878 return KERN_INVALID_VALUE
;
880 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
881 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
883 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
885 return (semaphore_wait_internal(semaphore
,
888 (void(*)(kern_return_t
))0));
893 * Trap: semaphore_timedwait_trap
895 * Trap version of a semaphore_timedwait. The timeout parameter
896 * is passed in two distinct parts and re-assembled on this side
897 * of the trap interface (to accomodate calling conventions that
898 * pass structures as pointers instead of inline in registers without
899 * having to add a copyin).
901 * A timeout of {0,0} is considered non-blocking.
904 semaphore_timedwait_trap(
905 struct semaphore_timedwait_trap_args
*args
)
908 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
913 semaphore_timedwait_trap_internal(
914 mach_port_name_t name
,
917 void (*caller_cont
)(kern_return_t
))
919 semaphore_t semaphore
;
920 mach_timespec_t wait_time
;
923 wait_time
.tv_sec
= sec
;
924 wait_time
.tv_nsec
= nsec
;
925 if(BAD_MACH_TIMESPEC(&wait_time
))
926 return KERN_INVALID_VALUE
;
928 kr
= port_name_to_semaphore(name
, &semaphore
);
929 if (kr
== KERN_SUCCESS
) {
930 int option
= SEMAPHORE_OPTION_NONE
;
931 uint64_t deadline
= 0;
933 if (sec
== 0 && nsec
== 0)
934 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
936 deadline
= semaphore_deadline(sec
, nsec
);
938 kr
= semaphore_wait_internal(semaphore
,
942 semaphore_dereference(semaphore
);
948 * Routine: semaphore_wait_signal
950 * Atomically register a wait on a semaphore and THEN signal
951 * another. This is the in-kernel entry point that does not
952 * block at a continuation and does not free a signal_semaphore
956 semaphore_wait_signal(
957 semaphore_t wait_semaphore
,
958 semaphore_t signal_semaphore
)
960 if (wait_semaphore
== SEMAPHORE_NULL
)
961 return KERN_INVALID_ARGUMENT
;
963 return(semaphore_wait_internal(wait_semaphore
,
965 0ULL, SEMAPHORE_OPTION_NONE
,
966 (void(*)(kern_return_t
))0));
970 * Trap: semaphore_wait_signal_trap
972 * Atomically register a wait on a semaphore and THEN signal
973 * another. This is the trap version from user space.
976 semaphore_wait_signal_trap(
977 struct semaphore_wait_signal_trap_args
*args
)
979 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
983 semaphore_wait_signal_trap_internal(
984 mach_port_name_t wait_name
,
985 mach_port_name_t signal_name
,
986 void (*caller_cont
)(kern_return_t
))
988 semaphore_t wait_semaphore
;
989 semaphore_t signal_semaphore
;
992 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
993 if (kr
== KERN_SUCCESS
) {
994 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
995 if (kr
== KERN_SUCCESS
) {
996 kr
= semaphore_wait_internal(wait_semaphore
,
998 0ULL, SEMAPHORE_OPTION_NONE
,
1000 semaphore_dereference(wait_semaphore
);
1002 semaphore_dereference(signal_semaphore
);
1009 * Routine: semaphore_timedwait_signal
1011 * Atomically register a wait on a semaphore and THEN signal
1012 * another. This is the in-kernel entry point that does not
1013 * block at a continuation.
1015 * A timeout of {0,0} is considered non-blocking.
1018 semaphore_timedwait_signal(
1019 semaphore_t wait_semaphore
,
1020 semaphore_t signal_semaphore
,
1021 mach_timespec_t wait_time
)
1023 int option
= SEMAPHORE_OPTION_NONE
;
1024 uint64_t deadline
= 0;
1026 if (wait_semaphore
== SEMAPHORE_NULL
)
1027 return KERN_INVALID_ARGUMENT
;
1029 if(BAD_MACH_TIMESPEC(&wait_time
))
1030 return KERN_INVALID_VALUE
;
1032 if (wait_time
.tv_sec
== 0 && wait_time
.tv_nsec
== 0)
1033 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1035 deadline
= semaphore_deadline(wait_time
.tv_sec
, wait_time
.tv_nsec
);
1037 return(semaphore_wait_internal(wait_semaphore
,
1040 (void(*)(kern_return_t
))0));
1044 * Trap: semaphore_timedwait_signal_trap
1046 * Atomically register a timed wait on a semaphore and THEN signal
1047 * another. This is the trap version from user space.
1050 semaphore_timedwait_signal_trap(
1051 struct semaphore_timedwait_signal_trap_args
*args
)
1053 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
1057 semaphore_timedwait_signal_trap_internal(
1058 mach_port_name_t wait_name
,
1059 mach_port_name_t signal_name
,
1062 void (*caller_cont
)(kern_return_t
))
1064 semaphore_t wait_semaphore
;
1065 semaphore_t signal_semaphore
;
1066 mach_timespec_t wait_time
;
1069 wait_time
.tv_sec
= sec
;
1070 wait_time
.tv_nsec
= nsec
;
1071 if(BAD_MACH_TIMESPEC(&wait_time
))
1072 return KERN_INVALID_VALUE
;
1074 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
1075 if (kr
== KERN_SUCCESS
) {
1076 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
1077 if (kr
== KERN_SUCCESS
) {
1078 int option
= SEMAPHORE_OPTION_NONE
;
1079 uint64_t deadline
= 0;
1081 if (sec
== 0 && nsec
== 0)
1082 option
= SEMAPHORE_TIMEOUT_NOBLOCK
;
1084 deadline
= semaphore_deadline(sec
, nsec
);
1086 kr
= semaphore_wait_internal(wait_semaphore
,
1090 semaphore_dereference(wait_semaphore
);
1092 semaphore_dereference(signal_semaphore
);
1099 * Routine: semaphore_reference
1101 * Take out a reference on a semaphore. This keeps the data structure
1102 * in existence (but the semaphore may be deactivated).
1105 semaphore_reference(
1106 semaphore_t semaphore
)
1108 (void)hw_atomic_add(&semaphore
->ref_count
, 1);
1112 * Routine: semaphore_dereference
1114 * Release a reference on a semaphore. If this is the last reference,
1115 * the semaphore data structure is deallocated.
1118 semaphore_dereference(
1119 semaphore_t semaphore
)
1121 uint32_t collisions
;
1124 if (semaphore
== NULL
)
1127 if (hw_atomic_sub(&semaphore
->ref_count
, 1) != 0)
1131 * Last ref, clean up the port [if any]
1132 * associated with the semaphore, destroy
1133 * it (if still active) and then free
1136 ipc_port_t port
= semaphore
->port
;
1138 if (IP_VALID(port
)) {
1139 assert(!port
->ip_srights
);
1140 ipc_port_dealloc_kernel(port
);
1144 * Lock the semaphore to lock in the owner task reference.
1145 * Then continue to try to lock the task (inverse order).
1147 spl_level
= splsched();
1148 semaphore_lock(semaphore
);
1149 for (collisions
= 0; semaphore
->active
; collisions
++) {
1150 task_t task
= semaphore
->owner
;
1152 assert(task
!= TASK_NULL
);
1154 if (task_lock_try(task
)) {
1155 semaphore_destroy_internal(task
, semaphore
);
1156 /* semaphore unlocked */
1162 /* failed to get out-of-order locks */
1163 semaphore_unlock(semaphore
);
1165 mutex_pause(collisions
);
1166 spl_level
= splsched();
1167 semaphore_lock(semaphore
);
1169 semaphore_unlock(semaphore
);
1173 zfree(semaphore_zone
, semaphore
);