2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
30 * File: kern/sync_sema.c
31 * Author: Joseph CaraDonna
33 * Contains RT distributed semaphore synchronization services.
36 #include <mach/mach_types.h>
37 #include <mach/kern_return.h>
38 #include <mach/semaphore.h>
39 #include <mach/sync_policy.h>
41 #include <kern/misc_protos.h>
42 #include <kern/sync_sema.h>
44 #include <kern/ipc_kobject.h>
45 #include <kern/ipc_sync.h>
46 #include <kern/ipc_tt.h>
47 #include <kern/thread.h>
48 #include <kern/clock.h>
49 #include <ipc/ipc_port.h>
50 #include <ipc/ipc_space.h>
51 #include <kern/host.h>
52 #include <kern/wait_queue.h>
53 #include <kern/zalloc.h>
54 #include <kern/mach_param.h>
56 static unsigned int semaphore_event
;
57 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
59 zone_t semaphore_zone
;
60 unsigned int semaphore_max
= SEMAPHORE_MAX
;
63 * ROUTINE: semaphore_init [private]
65 * Initialize the semaphore mechanisms.
66 * Right now, we only need to initialize the semaphore zone.
71 semaphore_zone
= zinit(sizeof(struct semaphore
),
72 semaphore_max
* sizeof(struct semaphore
),
73 sizeof(struct semaphore
),
78 * Routine: semaphore_create
80 * Creates a semaphore.
81 * The port representing the semaphore is returned as a parameter.
86 semaphore_t
*new_semaphore
,
90 semaphore_t s
= SEMAPHORE_NULL
;
94 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
95 *new_semaphore
= SEMAPHORE_NULL
;
96 return KERN_INVALID_ARGUMENT
;
99 s
= (semaphore_t
) zalloc (semaphore_zone
);
101 if (s
== SEMAPHORE_NULL
) {
102 *new_semaphore
= SEMAPHORE_NULL
;
103 return KERN_RESOURCE_SHORTAGE
;
106 wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
111 * Create and initialize the semaphore port
113 s
->port
= ipc_port_alloc_kernel();
114 if (s
->port
== IP_NULL
) {
115 /* This will deallocate the semaphore */
116 semaphore_dereference(s
);
117 *new_semaphore
= SEMAPHORE_NULL
;
118 return KERN_RESOURCE_SHORTAGE
;
121 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
124 * Associate the new semaphore with the task by adding
125 * the new semaphore to the task's semaphore list.
127 * Associate the task with the new semaphore by having the
128 * semaphores task pointer point to the owning task's structure.
131 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
132 task
->semaphores_owned
++;
143 * Routine: semaphore_destroy
145 * Destroys a semaphore. This call will only succeed if the
146 * specified task is the SAME task name specified at the semaphore's
149 * All threads currently blocked on the semaphore are awoken. These
150 * threads will return with the KERN_TERMINATED error.
155 semaphore_t semaphore
)
162 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
163 return KERN_INVALID_ARGUMENT
;
169 if (semaphore
->owner
!= task
) {
171 return KERN_INVALID_ARGUMENT
;
173 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
174 semaphore
->owner
= TASK_NULL
;
175 task
->semaphores_owned
--;
178 spl_level
= splsched();
179 semaphore_lock(semaphore
);
182 * Deactivate semaphore
184 assert(semaphore
->active
);
185 semaphore
->active
= FALSE
;
188 * Wakeup blocked threads
190 old_count
= semaphore
->count
;
191 semaphore
->count
= 0;
194 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
199 semaphore_unlock(semaphore
);
206 * Drop the semaphore reference, which in turn deallocates the
207 * semaphore structure if the reference count goes to zero.
209 ipc_port_dealloc_kernel(semaphore
->port
);
210 semaphore_dereference(semaphore
);
215 * Routine: semaphore_signal_internal
217 * Signals the semaphore as direct.
219 * Semaphore is locked.
222 semaphore_signal_internal(
223 semaphore_t semaphore
,
224 thread_act_t thread_act
,
230 spl_level
= splsched();
231 semaphore_lock(semaphore
);
233 if (!semaphore
->active
) {
234 semaphore_unlock(semaphore
);
236 return KERN_TERMINATED
;
239 if (thread_act
!= THR_ACT_NULL
) {
240 if (semaphore
->count
< 0) {
241 kr
= wait_queue_wakeup64_thread_locked(
242 &semaphore
->wait_queue
,
248 semaphore_unlock(semaphore
);
249 kr
= KERN_NOT_WAITING
;
255 if (options
& SEMAPHORE_SIGNAL_ALL
) {
256 int old_count
= semaphore
->count
;
259 semaphore
->count
= 0; /* always reset */
260 kr
= wait_queue_wakeup64_all_locked(
261 &semaphore
->wait_queue
,
266 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
268 semaphore_unlock(semaphore
);
275 if (semaphore
->count
< 0) {
276 if (wait_queue_wakeup64_one_locked(
277 &semaphore
->wait_queue
,
280 FALSE
) == KERN_SUCCESS
) {
281 semaphore_unlock(semaphore
);
285 semaphore
->count
= 0; /* all waiters gone */
288 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
292 semaphore_unlock(semaphore
);
294 return KERN_NOT_WAITING
;
298 * Routine: semaphore_signal_thread
300 * If the specified thread_act is blocked on the semaphore, it is
301 * woken up. If a NULL thread_act was supplied, then any one
302 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
303 * and the semaphore is unchanged.
306 semaphore_signal_thread(
307 semaphore_t semaphore
,
308 thread_act_t thread_act
)
312 if (semaphore
== SEMAPHORE_NULL
)
313 return KERN_INVALID_ARGUMENT
;
315 ret
= semaphore_signal_internal(semaphore
,
317 SEMAPHORE_OPTION_NONE
);
322 * Routine: semaphore_signal_thread_trap
324 * Trap interface to the semaphore_signal_thread function.
327 semaphore_signal_thread_trap(
328 mach_port_name_t sema_name
,
329 mach_port_name_t thread_name
)
332 semaphore_t semaphore
;
333 thread_act_t thread_act
;
337 * MACH_PORT_NULL is not an error. It means that we want to
338 * select any one thread that is already waiting, but not to
339 * pre-post the semaphore.
341 if (thread_name
!= MACH_PORT_NULL
) {
342 thread_act
= port_name_to_act(thread_name
);
343 if (thread_act
== THR_ACT_NULL
)
344 return KERN_INVALID_ARGUMENT
;
346 thread_act
= THR_ACT_NULL
;
348 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
349 if (kr
!= KERN_SUCCESS
) {
350 act_deallocate(thread_act
);
353 kr
= semaphore_signal_internal(semaphore
,
355 SEMAPHORE_OPTION_NONE
);
356 semaphore_dereference(semaphore
);
357 act_deallocate(thread_act
);
364 * Routine: semaphore_signal
366 * Traditional (in-kernel client and MIG interface) semaphore
367 * signal routine. Most users will access the trap version.
369 * This interface in not defined to return info about whether
370 * this call found a thread waiting or not. The internal
371 * routines (and future external routines) do. We have to
372 * convert those into plain KERN_SUCCESS returns.
376 semaphore_t semaphore
)
380 if (semaphore
== SEMAPHORE_NULL
)
381 return KERN_INVALID_ARGUMENT
;
383 kr
= semaphore_signal_internal(semaphore
,
385 SEMAPHORE_SIGNAL_PREPOST
);
386 if (kr
== KERN_NOT_WAITING
)
392 * Routine: semaphore_signal_trap
394 * Trap interface to the semaphore_signal function.
397 semaphore_signal_trap(
398 mach_port_name_t sema_name
)
401 semaphore_t semaphore
;
404 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
405 if (kr
!= KERN_SUCCESS
) {
408 kr
= semaphore_signal_internal(semaphore
,
410 SEMAPHORE_SIGNAL_PREPOST
);
411 semaphore_dereference(semaphore
);
412 if (kr
== KERN_NOT_WAITING
)
418 * Routine: semaphore_signal_all
420 * Awakens ALL threads currently blocked on the semaphore.
421 * The semaphore count returns to zero.
424 semaphore_signal_all(
425 semaphore_t semaphore
)
429 if (semaphore
== SEMAPHORE_NULL
)
430 return KERN_INVALID_ARGUMENT
;
432 kr
= semaphore_signal_internal(semaphore
,
434 SEMAPHORE_SIGNAL_ALL
);
435 if (kr
== KERN_NOT_WAITING
)
441 * Routine: semaphore_signal_all_trap
443 * Trap interface to the semaphore_signal_all function.
446 semaphore_signal_all_trap(
447 mach_port_name_t sema_name
)
450 semaphore_t semaphore
;
453 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
454 if (kr
!= KERN_SUCCESS
) {
457 kr
= semaphore_signal_internal(semaphore
,
459 SEMAPHORE_SIGNAL_ALL
);
460 semaphore_dereference(semaphore
);
461 if (kr
== KERN_NOT_WAITING
)
467 * Routine: semaphore_convert_wait_result
469 * Generate the return code after a semaphore wait/block. It
470 * takes the wait result as an input and coverts that to an
471 * appropriate result.
474 semaphore_convert_wait_result(int wait_result
)
476 switch (wait_result
) {
477 case THREAD_AWAKENED
:
480 case THREAD_TIMED_OUT
:
481 return KERN_OPERATION_TIMED_OUT
;
483 case THREAD_INTERRUPTED
:
487 return KERN_TERMINATED
;
490 panic("semaphore_block\n");
496 * Routine: semaphore_wait_continue
498 * Common continuation routine after waiting on a semphore.
499 * It returns directly to user space.
502 semaphore_wait_continue(void)
504 thread_t self
= current_thread();
505 int wait_result
= self
->wait_result
;
506 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
508 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
509 semaphore_dereference(self
->sth_waitsemaphore
);
510 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
511 semaphore_dereference(self
->sth_signalsemaphore
);
513 assert(caller_cont
!= (void (*)(kern_return_t
))0);
514 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
518 * Routine: semaphore_timedwait_continue
520 * Common continuation routine after doing a timed wait on a
521 * semaphore. It clears the timer before calling the semaphore
522 * routine saved in the thread struct.
525 semaphore_timedwait_continue(void)
527 thread_t self
= current_thread();
528 int wait_result
= self
->wait_result
;
529 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
531 if (wait_result
!= THREAD_TIMED_OUT
)
532 thread_cancel_timer();
534 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
535 semaphore_dereference(self
->sth_waitsemaphore
);
536 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
537 semaphore_dereference(self
->sth_signalsemaphore
);
539 assert(caller_cont
!= (void (*)(kern_return_t
))0);
540 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
545 * Routine: semaphore_wait_internal
547 * Decrements the semaphore count by one. If the count is
548 * negative after the decrement, the calling thread blocks
549 * (possibly at a continuation and/or with a timeout).
553 * A reference is held on the signal semaphore.
556 semaphore_wait_internal(
557 semaphore_t wait_semaphore
,
558 semaphore_t signal_semaphore
,
559 mach_timespec_t
*wait_timep
,
560 void (*caller_cont
)(kern_return_t
))
562 void (*continuation
)(void);
564 boolean_t nonblocking
;
567 kern_return_t kr
= KERN_ALREADY_WAITING
;
569 spl_level
= splsched();
570 semaphore_lock(wait_semaphore
);
573 * Decide if we really have to wait.
575 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
576 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
579 if (!wait_semaphore
->active
) {
580 kr
= KERN_TERMINATED
;
581 } else if (wait_semaphore
->count
> 0) {
582 wait_semaphore
->count
--;
584 } else if (nonblocking
) {
585 kr
= KERN_OPERATION_TIMED_OUT
;
587 thread_t self
= current_thread();
589 wait_semaphore
->count
= -1; /* we don't keep an actual count */
591 (void)wait_queue_assert_wait64_locked(
592 &wait_semaphore
->wait_queue
,
598 semaphore_unlock(wait_semaphore
);
602 * wait_semaphore is unlocked so we are free to go ahead and
603 * signal the signal_semaphore (if one was provided).
605 if (signal_semaphore
!= SEMAPHORE_NULL
) {
606 kern_return_t signal_kr
;
609 * lock the signal semaphore reference we got and signal it.
610 * This will NOT block (we cannot block after having asserted
611 * our intention to wait above).
613 signal_kr
= semaphore_signal_internal(signal_semaphore
,
615 SEMAPHORE_SIGNAL_PREPOST
);
617 if (signal_kr
== KERN_NOT_WAITING
)
618 signal_kr
= KERN_SUCCESS
;
619 else if (signal_kr
== KERN_TERMINATED
) {
621 * Uh!Oh! The semaphore we were to signal died.
622 * We have to get ourselves out of the wait in
623 * case we get stuck here forever (it is assumed
624 * that the semaphore we were posting is gating
625 * the decision by someone else to post the
626 * semaphore we are waiting on). People will
627 * discover the other dead semaphore soon enough.
628 * If we got out of the wait cleanly (someone
629 * already posted a wakeup to us) then return that
630 * (most important) result. Otherwise,
631 * return the KERN_TERMINATED status.
633 thread_t self
= current_thread();
635 clear_wait(self
, THREAD_INTERRUPTED
);
636 kr
= semaphore_convert_wait_result(self
->wait_result
);
637 if (kr
== KERN_ABORTED
)
638 kr
= KERN_TERMINATED
;
643 * If we had an error, or we didn't really need to wait we can
644 * return now that we have signalled the signal semaphore.
646 if (kr
!= KERN_ALREADY_WAITING
)
650 * If it is a timed wait, go ahead and set up the timer.
652 if (wait_timep
!= (mach_timespec_t
*)0) {
653 nanoseconds_to_absolutetime((uint64_t)wait_timep
->tv_sec
*
654 NSEC_PER_SEC
+ wait_timep
->tv_nsec
, &abstime
);
655 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
656 thread_set_timer_deadline(abstime
);
657 continuation
= semaphore_timedwait_continue
;
659 continuation
= semaphore_wait_continue
;
663 * Now, we can block. If the caller supplied a continuation
664 * pointer of his own for after the block, block with the
665 * appropriate semaphore continuation. Thiswill gather the
666 * semaphore results, release references on the semaphore(s),
667 * and then call the caller's continuation.
670 thread_t self
= current_thread();
672 self
->sth_continuation
= caller_cont
;
673 self
->sth_waitsemaphore
= wait_semaphore
;
674 self
->sth_signalsemaphore
= signal_semaphore
;
675 wait_result
= thread_block(continuation
);
677 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
681 * If we came back here (not continuation case) cancel
682 * any pending timers, convert the wait result to an
683 * appropriate semaphore return value, and then return
686 if (wait_timep
&& (wait_result
!= THREAD_TIMED_OUT
))
687 thread_cancel_timer();
689 return (semaphore_convert_wait_result(wait_result
));
694 * Routine: semaphore_wait
696 * Traditional (non-continuation) interface presented to
697 * in-kernel clients to wait on a semaphore.
701 semaphore_t semaphore
)
704 if (semaphore
== SEMAPHORE_NULL
)
705 return KERN_INVALID_ARGUMENT
;
707 return(semaphore_wait_internal(semaphore
,
709 (mach_timespec_t
*)0,
710 (void (*)(kern_return_t
))0));
714 * Trap: semaphore_wait_trap
716 * Trap version of semaphore wait. Called on behalf of user-level
721 mach_port_name_t name
)
723 semaphore_t semaphore
;
726 kr
= port_name_to_semaphore(name
, &semaphore
);
727 if (kr
!= KERN_SUCCESS
)
730 kr
= semaphore_wait_internal(semaphore
,
732 (mach_timespec_t
*)0,
733 thread_syscall_return
);
734 semaphore_dereference(semaphore
);
739 * Routine: semaphore_timedwait
741 * Traditional (non-continuation) interface presented to
742 * in-kernel clients to wait on a semaphore with a timeout.
744 * A timeout of {0,0} is considered non-blocking.
748 semaphore_t semaphore
,
749 mach_timespec_t wait_time
)
751 if (semaphore
== SEMAPHORE_NULL
)
752 return KERN_INVALID_ARGUMENT
;
754 if(BAD_MACH_TIMESPEC(&wait_time
))
755 return KERN_INVALID_VALUE
;
757 return (semaphore_wait_internal(semaphore
,
760 (void(*)(kern_return_t
))0));
765 * Trap: semaphore_timedwait_trap
767 * Trap version of a semaphore_timedwait. The timeout parameter
768 * is passed in two distinct parts and re-assembled on this side
769 * of the trap interface (to accomodate calling conventions that
770 * pass structures as pointers instead of inline in registers without
771 * having to add a copyin).
773 * A timeout of {0,0} is considered non-blocking.
776 semaphore_timedwait_trap(
777 mach_port_name_t name
,
781 semaphore_t semaphore
;
782 mach_timespec_t wait_time
;
785 wait_time
.tv_sec
= sec
;
786 wait_time
.tv_nsec
= nsec
;
787 if(BAD_MACH_TIMESPEC(&wait_time
))
788 return KERN_INVALID_VALUE
;
790 kr
= port_name_to_semaphore(name
, &semaphore
);
791 if (kr
!= KERN_SUCCESS
)
794 kr
= semaphore_wait_internal(semaphore
,
797 thread_syscall_return
);
798 semaphore_dereference(semaphore
);
803 * Routine: semaphore_wait_signal
805 * Atomically register a wait on a semaphore and THEN signal
806 * another. This is the in-kernel entry point that does not
807 * block at a continuation and does not free a signal_semaphore
811 semaphore_wait_signal(
812 semaphore_t wait_semaphore
,
813 semaphore_t signal_semaphore
)
815 if (wait_semaphore
== SEMAPHORE_NULL
)
816 return KERN_INVALID_ARGUMENT
;
818 return(semaphore_wait_internal(wait_semaphore
,
820 (mach_timespec_t
*)0,
821 (void(*)(kern_return_t
))0));
825 * Trap: semaphore_wait_signal_trap
827 * Atomically register a wait on a semaphore and THEN signal
828 * another. This is the trap version from user space.
831 semaphore_wait_signal_trap(
832 mach_port_name_t wait_name
,
833 mach_port_name_t signal_name
)
835 semaphore_t wait_semaphore
;
836 semaphore_t signal_semaphore
;
839 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
840 if (kr
!= KERN_SUCCESS
)
843 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
844 if (kr
!= KERN_SUCCESS
) {
845 semaphore_dereference(signal_semaphore
);
849 kr
= semaphore_wait_internal(wait_semaphore
,
851 (mach_timespec_t
*)0,
852 thread_syscall_return
);
854 semaphore_dereference(wait_semaphore
);
855 semaphore_dereference(signal_semaphore
);
861 * Routine: semaphore_timedwait_signal
863 * Atomically register a wait on a semaphore and THEN signal
864 * another. This is the in-kernel entry point that does not
865 * block at a continuation.
867 * A timeout of {0,0} is considered non-blocking.
870 semaphore_timedwait_signal(
871 semaphore_t wait_semaphore
,
872 semaphore_t signal_semaphore
,
873 mach_timespec_t wait_time
)
875 if (wait_semaphore
== SEMAPHORE_NULL
)
876 return KERN_INVALID_ARGUMENT
;
878 if(BAD_MACH_TIMESPEC(&wait_time
))
879 return KERN_INVALID_VALUE
;
881 return(semaphore_wait_internal(wait_semaphore
,
884 (void(*)(kern_return_t
))0));
888 * Trap: semaphore_timedwait_signal_trap
890 * Atomically register a timed wait on a semaphore and THEN signal
891 * another. This is the trap version from user space.
894 semaphore_timedwait_signal_trap(
895 mach_port_name_t wait_name
,
896 mach_port_name_t signal_name
,
900 semaphore_t wait_semaphore
;
901 semaphore_t signal_semaphore
;
902 mach_timespec_t wait_time
;
905 wait_time
.tv_sec
= sec
;
906 wait_time
.tv_nsec
= nsec
;
907 if(BAD_MACH_TIMESPEC(&wait_time
))
908 return KERN_INVALID_VALUE
;
910 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
911 if (kr
!= KERN_SUCCESS
)
914 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
915 if (kr
!= KERN_SUCCESS
) {
916 semaphore_dereference(signal_semaphore
);
920 kr
= semaphore_wait_internal(wait_semaphore
,
923 thread_syscall_return
);
925 semaphore_dereference(wait_semaphore
);
926 semaphore_dereference(signal_semaphore
);
932 * Routine: semaphore_reference
934 * Take out a reference on a semaphore. This keeps the data structure
935 * in existence (but the semaphore may be deactivated).
939 semaphore_t semaphore
)
943 spl_level
= splsched();
944 semaphore_lock(semaphore
);
946 semaphore
->ref_count
++;
948 semaphore_unlock(semaphore
);
953 * Routine: semaphore_dereference
955 * Release a reference on a semaphore. If this is the last reference,
956 * the semaphore data structure is deallocated.
959 semaphore_dereference(
960 semaphore_t semaphore
)
965 if (semaphore
!= NULL
) {
966 spl_level
= splsched();
967 semaphore_lock(semaphore
);
969 ref_count
= --(semaphore
->ref_count
);
971 semaphore_unlock(semaphore
);
974 if (ref_count
== 0) {
975 assert(wait_queue_empty(&semaphore
->wait_queue
));
976 zfree(semaphore_zone
, (vm_offset_t
)semaphore
);