2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * File: kern/sync_sema.c
28 * Author: Joseph CaraDonna
30 * Contains RT distributed semaphore synchronization services.
33 #include <mach/mach_types.h>
34 #include <mach/kern_return.h>
35 #include <mach/semaphore.h>
36 #include <mach/sync_policy.h>
38 #include <kern/misc_protos.h>
39 #include <kern/sync_sema.h>
41 #include <kern/ipc_kobject.h>
42 #include <kern/ipc_sync.h>
43 #include <kern/ipc_tt.h>
44 #include <kern/thread.h>
45 #include <kern/clock.h>
46 #include <ipc/ipc_port.h>
47 #include <ipc/ipc_space.h>
48 #include <kern/host.h>
49 #include <kern/wait_queue.h>
50 #include <kern/zalloc.h>
51 #include <kern/mach_param.h>
53 static unsigned int semaphore_event
;
54 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
56 zone_t semaphore_zone
;
57 unsigned int semaphore_max
= SEMAPHORE_MAX
;
60 * ROUTINE: semaphore_init [private]
62 * Initialize the semaphore mechanisms.
63 * Right now, we only need to initialize the semaphore zone.
68 semaphore_zone
= zinit(sizeof(struct semaphore
),
69 semaphore_max
* sizeof(struct semaphore
),
70 sizeof(struct semaphore
),
75 * Routine: semaphore_create
77 * Creates a semaphore.
78 * The port representing the semaphore is returned as a parameter.
83 semaphore_t
*new_semaphore
,
87 semaphore_t s
= SEMAPHORE_NULL
;
91 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
92 *new_semaphore
= SEMAPHORE_NULL
;
93 return KERN_INVALID_ARGUMENT
;
96 s
= (semaphore_t
) zalloc (semaphore_zone
);
98 if (s
== SEMAPHORE_NULL
) {
99 *new_semaphore
= SEMAPHORE_NULL
;
100 return KERN_RESOURCE_SHORTAGE
;
103 wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
108 * Create and initialize the semaphore port
110 s
->port
= ipc_port_alloc_kernel();
111 if (s
->port
== IP_NULL
) {
112 /* This will deallocate the semaphore */
113 semaphore_dereference(s
);
114 *new_semaphore
= SEMAPHORE_NULL
;
115 return KERN_RESOURCE_SHORTAGE
;
118 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
121 * Associate the new semaphore with the task by adding
122 * the new semaphore to the task's semaphore list.
124 * Associate the task with the new semaphore by having the
125 * semaphores task pointer point to the owning task's structure.
128 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
129 task
->semaphores_owned
++;
140 * Routine: semaphore_destroy
142 * Destroys a semaphore. This call will only succeed if the
143 * specified task is the SAME task name specified at the semaphore's
146 * All threads currently blocked on the semaphore are awoken. These
147 * threads will return with the KERN_TERMINATED error.
152 semaphore_t semaphore
)
159 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
160 return KERN_INVALID_ARGUMENT
;
166 if (semaphore
->owner
!= task
) {
168 return KERN_INVALID_ARGUMENT
;
170 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
171 semaphore
->owner
= TASK_NULL
;
172 task
->semaphores_owned
--;
175 spl_level
= splsched();
176 semaphore_lock(semaphore
);
179 * Deactivate semaphore
181 assert(semaphore
->active
);
182 semaphore
->active
= FALSE
;
185 * Wakeup blocked threads
187 old_count
= semaphore
->count
;
188 semaphore
->count
= 0;
191 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
196 semaphore_unlock(semaphore
);
203 * Drop the semaphore reference, which in turn deallocates the
204 * semaphore structure if the reference count goes to zero.
206 ipc_port_dealloc_kernel(semaphore
->port
);
207 semaphore_dereference(semaphore
);
212 * Routine: semaphore_signal_internal
214 * Signals the semaphore as direct.
216 * Semaphore is locked.
219 semaphore_signal_internal(
220 semaphore_t semaphore
,
221 thread_act_t thread_act
,
227 spl_level
= splsched();
228 semaphore_lock(semaphore
);
230 if (!semaphore
->active
) {
231 semaphore_unlock(semaphore
);
233 return KERN_TERMINATED
;
236 if (thread_act
!= THR_ACT_NULL
) {
237 if (semaphore
->count
< 0) {
238 kr
= wait_queue_wakeup64_thread_locked(
239 &semaphore
->wait_queue
,
245 semaphore_unlock(semaphore
);
246 kr
= KERN_NOT_WAITING
;
252 if (options
& SEMAPHORE_SIGNAL_ALL
) {
253 int old_count
= semaphore
->count
;
256 semaphore
->count
= 0; /* always reset */
257 kr
= wait_queue_wakeup64_all_locked(
258 &semaphore
->wait_queue
,
263 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
265 semaphore_unlock(semaphore
);
272 if (semaphore
->count
< 0) {
273 if (wait_queue_wakeup64_one_locked(
274 &semaphore
->wait_queue
,
277 FALSE
) == KERN_SUCCESS
) {
278 semaphore_unlock(semaphore
);
282 semaphore
->count
= 0; /* all waiters gone */
285 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
289 semaphore_unlock(semaphore
);
291 return KERN_NOT_WAITING
;
295 * Routine: semaphore_signal_thread
297 * If the specified thread_act is blocked on the semaphore, it is
298 * woken up. If a NULL thread_act was supplied, then any one
299 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
300 * and the semaphore is unchanged.
303 semaphore_signal_thread(
304 semaphore_t semaphore
,
305 thread_act_t thread_act
)
309 if (semaphore
== SEMAPHORE_NULL
)
310 return KERN_INVALID_ARGUMENT
;
312 ret
= semaphore_signal_internal(semaphore
,
314 SEMAPHORE_OPTION_NONE
);
319 * Routine: semaphore_signal_thread_trap
321 * Trap interface to the semaphore_signal_thread function.
324 semaphore_signal_thread_trap(
325 mach_port_name_t sema_name
,
326 mach_port_name_t thread_name
)
329 semaphore_t semaphore
;
330 thread_act_t thread_act
;
334 * MACH_PORT_NULL is not an error. It means that we want to
335 * select any one thread that is already waiting, but not to
336 * pre-post the semaphore.
338 if (thread_name
!= MACH_PORT_NULL
) {
339 thread_act
= port_name_to_act(thread_name
);
340 if (thread_act
== THR_ACT_NULL
)
341 return KERN_INVALID_ARGUMENT
;
343 thread_act
= THR_ACT_NULL
;
345 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
346 if (kr
!= KERN_SUCCESS
) {
347 act_deallocate(thread_act
);
350 kr
= semaphore_signal_internal(semaphore
,
352 SEMAPHORE_OPTION_NONE
);
353 semaphore_dereference(semaphore
);
354 act_deallocate(thread_act
);
361 * Routine: semaphore_signal
363 * Traditional (in-kernel client and MIG interface) semaphore
364 * signal routine. Most users will access the trap version.
366 * This interface in not defined to return info about whether
367 * this call found a thread waiting or not. The internal
368 * routines (and future external routines) do. We have to
369 * convert those into plain KERN_SUCCESS returns.
373 semaphore_t semaphore
)
377 if (semaphore
== SEMAPHORE_NULL
)
378 return KERN_INVALID_ARGUMENT
;
380 kr
= semaphore_signal_internal(semaphore
,
382 SEMAPHORE_SIGNAL_PREPOST
);
383 if (kr
== KERN_NOT_WAITING
)
389 * Routine: semaphore_signal_trap
391 * Trap interface to the semaphore_signal function.
394 semaphore_signal_trap(
395 mach_port_name_t sema_name
)
398 semaphore_t semaphore
;
401 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
402 if (kr
!= KERN_SUCCESS
) {
405 kr
= semaphore_signal_internal(semaphore
,
407 SEMAPHORE_SIGNAL_PREPOST
);
408 semaphore_dereference(semaphore
);
409 if (kr
== KERN_NOT_WAITING
)
415 * Routine: semaphore_signal_all
417 * Awakens ALL threads currently blocked on the semaphore.
418 * The semaphore count returns to zero.
421 semaphore_signal_all(
422 semaphore_t semaphore
)
426 if (semaphore
== SEMAPHORE_NULL
)
427 return KERN_INVALID_ARGUMENT
;
429 kr
= semaphore_signal_internal(semaphore
,
431 SEMAPHORE_SIGNAL_ALL
);
432 if (kr
== KERN_NOT_WAITING
)
438 * Routine: semaphore_signal_all_trap
440 * Trap interface to the semaphore_signal_all function.
443 semaphore_signal_all_trap(
444 mach_port_name_t sema_name
)
447 semaphore_t semaphore
;
450 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
451 if (kr
!= KERN_SUCCESS
) {
454 kr
= semaphore_signal_internal(semaphore
,
456 SEMAPHORE_SIGNAL_ALL
);
457 semaphore_dereference(semaphore
);
458 if (kr
== KERN_NOT_WAITING
)
464 * Routine: semaphore_convert_wait_result
466 * Generate the return code after a semaphore wait/block. It
467 * takes the wait result as an input and coverts that to an
468 * appropriate result.
471 semaphore_convert_wait_result(int wait_result
)
473 switch (wait_result
) {
474 case THREAD_AWAKENED
:
477 case THREAD_TIMED_OUT
:
478 return KERN_OPERATION_TIMED_OUT
;
480 case THREAD_INTERRUPTED
:
484 return KERN_TERMINATED
;
487 panic("semaphore_block\n");
493 * Routine: semaphore_wait_continue
495 * Common continuation routine after waiting on a semphore.
496 * It returns directly to user space.
499 semaphore_wait_continue(void)
501 thread_t self
= current_thread();
502 int wait_result
= self
->wait_result
;
503 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
505 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
506 semaphore_dereference(self
->sth_waitsemaphore
);
507 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
508 semaphore_dereference(self
->sth_signalsemaphore
);
510 assert(caller_cont
!= (void (*)(kern_return_t
))0);
511 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
515 * Routine: semaphore_timedwait_continue
517 * Common continuation routine after doing a timed wait on a
518 * semaphore. It clears the timer before calling the semaphore
519 * routine saved in the thread struct.
522 semaphore_timedwait_continue(void)
524 thread_t self
= current_thread();
525 int wait_result
= self
->wait_result
;
526 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
528 if (wait_result
!= THREAD_TIMED_OUT
)
529 thread_cancel_timer();
531 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
532 semaphore_dereference(self
->sth_waitsemaphore
);
533 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
534 semaphore_dereference(self
->sth_signalsemaphore
);
536 assert(caller_cont
!= (void (*)(kern_return_t
))0);
537 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
542 * Routine: semaphore_wait_internal
544 * Decrements the semaphore count by one. If the count is
545 * negative after the decrement, the calling thread blocks
546 * (possibly at a continuation and/or with a timeout).
550 * A reference is held on the signal semaphore.
553 semaphore_wait_internal(
554 semaphore_t wait_semaphore
,
555 semaphore_t signal_semaphore
,
556 mach_timespec_t
*wait_timep
,
557 void (*caller_cont
)(kern_return_t
))
559 void (*continuation
)(void);
560 uint64_t abstime
, nsinterval
;
561 boolean_t nonblocking
;
564 kern_return_t kr
= KERN_ALREADY_WAITING
;
566 spl_level
= splsched();
567 semaphore_lock(wait_semaphore
);
570 * Decide if we really have to wait.
572 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
573 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
576 if (!wait_semaphore
->active
) {
577 kr
= KERN_TERMINATED
;
578 } else if (wait_semaphore
->count
> 0) {
579 wait_semaphore
->count
--;
581 } else if (nonblocking
) {
582 kr
= KERN_OPERATION_TIMED_OUT
;
584 wait_semaphore
->count
= -1; /* we don't keep an actual count */
585 (void)wait_queue_assert_wait64_locked(
586 &wait_semaphore
->wait_queue
,
589 FALSE
); /* unlock? */
591 semaphore_unlock(wait_semaphore
);
595 * wait_semaphore is unlocked so we are free to go ahead and
596 * signal the signal_semaphore (if one was provided).
598 if (signal_semaphore
!= SEMAPHORE_NULL
) {
599 kern_return_t signal_kr
;
602 * lock the signal semaphore reference we got and signal it.
603 * This will NOT block (we cannot block after having asserted
604 * our intention to wait above).
606 signal_kr
= semaphore_signal_internal(signal_semaphore
,
608 SEMAPHORE_SIGNAL_PREPOST
);
610 if (signal_kr
== KERN_NOT_WAITING
)
611 signal_kr
= KERN_SUCCESS
;
612 else if (signal_kr
== KERN_TERMINATED
) {
614 * Uh!Oh! The semaphore we were to signal died.
615 * We have to get ourselves out of the wait in
616 * case we get stuck here forever (it is assumed
617 * that the semaphore we were posting is gating
618 * the decision by someone else to post the
619 * semaphore we are waiting on). People will
620 * discover the other dead semaphore soon enough.
621 * If we got out of the wait cleanly (someone
622 * already posted a wakeup to us) then return that
623 * (most important) result. Otherwise,
624 * return the KERN_TERMINATED status.
626 thread_t self
= current_thread();
628 clear_wait(self
, THREAD_INTERRUPTED
);
629 kr
= semaphore_convert_wait_result(self
->wait_result
);
630 if (kr
== KERN_ABORTED
)
631 kr
= KERN_TERMINATED
;
636 * If we had an error, or we didn't really need to wait we can
637 * return now that we have signalled the signal semaphore.
639 if (kr
!= KERN_ALREADY_WAITING
)
643 * If it is a timed wait, go ahead and set up the timer.
645 if (wait_timep
!= (mach_timespec_t
*)0) {
646 clock_interval_to_absolutetime_interval(wait_timep
->tv_sec
,
649 clock_interval_to_absolutetime_interval(wait_timep
->tv_nsec
,
652 abstime
+= nsinterval
;
653 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
654 thread_set_timer_deadline(abstime
);
655 continuation
= semaphore_timedwait_continue
;
657 continuation
= semaphore_wait_continue
;
661 * Now, we can block. If the caller supplied a continuation
662 * pointer of his own for after the block, block with the
663 * appropriate semaphore continuation. Thiswill gather the
664 * semaphore results, release references on the semaphore(s),
665 * and then call the caller's continuation.
668 thread_t self
= current_thread();
670 self
->sth_continuation
= caller_cont
;
671 self
->sth_waitsemaphore
= wait_semaphore
;
672 self
->sth_signalsemaphore
= signal_semaphore
;
673 wait_result
= thread_block(continuation
);
675 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
679 * If we came back here (not continuation case) cancel
680 * any pending timers, convert the wait result to an
681 * appropriate semaphore return value, and then return
684 if (wait_timep
&& (wait_result
!= THREAD_TIMED_OUT
))
685 thread_cancel_timer();
687 return (semaphore_convert_wait_result(wait_result
));
692 * Routine: semaphore_wait
694 * Traditional (non-continuation) interface presented to
695 * in-kernel clients to wait on a semaphore.
699 semaphore_t semaphore
)
702 if (semaphore
== SEMAPHORE_NULL
)
703 return KERN_INVALID_ARGUMENT
;
705 return(semaphore_wait_internal(semaphore
,
707 (mach_timespec_t
*)0,
708 (void (*)(kern_return_t
))0));
712 * Trap: semaphore_wait_trap
714 * Trap version of semaphore wait. Called on behalf of user-level
719 mach_port_name_t name
)
721 semaphore_t semaphore
;
724 kr
= port_name_to_semaphore(name
, &semaphore
);
725 if (kr
!= KERN_SUCCESS
)
728 kr
= semaphore_wait_internal(semaphore
,
730 (mach_timespec_t
*)0,
731 thread_syscall_return
);
732 semaphore_dereference(semaphore
);
737 * Routine: semaphore_timedwait
739 * Traditional (non-continuation) interface presented to
740 * in-kernel clients to wait on a semaphore with a timeout.
742 * A timeout of {0,0} is considered non-blocking.
746 semaphore_t semaphore
,
747 mach_timespec_t wait_time
)
749 if (semaphore
== SEMAPHORE_NULL
)
750 return KERN_INVALID_ARGUMENT
;
752 if(BAD_MACH_TIMESPEC(&wait_time
))
753 return KERN_INVALID_VALUE
;
755 return (semaphore_wait_internal(semaphore
,
758 (void(*)(kern_return_t
))0));
763 * Trap: semaphore_timedwait_trap
765 * Trap version of a semaphore_timedwait. The timeout parameter
766 * is passed in two distinct parts and re-assembled on this side
767 * of the trap interface (to accomodate calling conventions that
768 * pass structures as pointers instead of inline in registers without
769 * having to add a copyin).
771 * A timeout of {0,0} is considered non-blocking.
774 semaphore_timedwait_trap(
775 mach_port_name_t name
,
779 semaphore_t semaphore
;
780 mach_timespec_t wait_time
;
783 wait_time
.tv_sec
= sec
;
784 wait_time
.tv_nsec
= nsec
;
785 if(BAD_MACH_TIMESPEC(&wait_time
))
786 return KERN_INVALID_VALUE
;
788 kr
= port_name_to_semaphore(name
, &semaphore
);
789 if (kr
!= KERN_SUCCESS
)
792 kr
= semaphore_wait_internal(semaphore
,
795 thread_syscall_return
);
796 semaphore_dereference(semaphore
);
801 * Routine: semaphore_wait_signal
803 * Atomically register a wait on a semaphore and THEN signal
804 * another. This is the in-kernel entry point that does not
805 * block at a continuation and does not free a signal_semaphore
809 semaphore_wait_signal(
810 semaphore_t wait_semaphore
,
811 semaphore_t signal_semaphore
)
813 if (wait_semaphore
== SEMAPHORE_NULL
)
814 return KERN_INVALID_ARGUMENT
;
816 return(semaphore_wait_internal(wait_semaphore
,
818 (mach_timespec_t
*)0,
819 (void(*)(kern_return_t
))0));
823 * Trap: semaphore_wait_signal_trap
825 * Atomically register a wait on a semaphore and THEN signal
826 * another. This is the trap version from user space.
829 semaphore_wait_signal_trap(
830 mach_port_name_t wait_name
,
831 mach_port_name_t signal_name
)
833 semaphore_t wait_semaphore
;
834 semaphore_t signal_semaphore
;
837 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
838 if (kr
!= KERN_SUCCESS
)
841 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
842 if (kr
!= KERN_SUCCESS
) {
843 semaphore_dereference(signal_semaphore
);
847 kr
= semaphore_wait_internal(wait_semaphore
,
849 (mach_timespec_t
*)0,
850 thread_syscall_return
);
852 semaphore_dereference(wait_semaphore
);
853 semaphore_dereference(signal_semaphore
);
859 * Routine: semaphore_timedwait_signal
861 * Atomically register a wait on a semaphore and THEN signal
862 * another. This is the in-kernel entry point that does not
863 * block at a continuation.
865 * A timeout of {0,0} is considered non-blocking.
868 semaphore_timedwait_signal(
869 semaphore_t wait_semaphore
,
870 semaphore_t signal_semaphore
,
871 mach_timespec_t wait_time
)
873 if (wait_semaphore
== SEMAPHORE_NULL
)
874 return KERN_INVALID_ARGUMENT
;
876 if(BAD_MACH_TIMESPEC(&wait_time
))
877 return KERN_INVALID_VALUE
;
879 return(semaphore_wait_internal(wait_semaphore
,
882 (void(*)(kern_return_t
))0));
886 * Trap: semaphore_timedwait_signal_trap
888 * Atomically register a timed wait on a semaphore and THEN signal
889 * another. This is the trap version from user space.
892 semaphore_timedwait_signal_trap(
893 mach_port_name_t wait_name
,
894 mach_port_name_t signal_name
,
898 semaphore_t wait_semaphore
;
899 semaphore_t signal_semaphore
;
900 mach_timespec_t wait_time
;
903 wait_time
.tv_sec
= sec
;
904 wait_time
.tv_nsec
= nsec
;
905 if(BAD_MACH_TIMESPEC(&wait_time
))
906 return KERN_INVALID_VALUE
;
908 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
909 if (kr
!= KERN_SUCCESS
)
912 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
913 if (kr
!= KERN_SUCCESS
) {
914 semaphore_dereference(signal_semaphore
);
918 kr
= semaphore_wait_internal(wait_semaphore
,
921 thread_syscall_return
);
923 semaphore_dereference(wait_semaphore
);
924 semaphore_dereference(signal_semaphore
);
930 * Routine: semaphore_reference
932 * Take out a reference on a semaphore. This keeps the data structure
933 * in existence (but the semaphore may be deactivated).
937 semaphore_t semaphore
)
941 spl_level
= splsched();
942 semaphore_lock(semaphore
);
944 semaphore
->ref_count
++;
946 semaphore_unlock(semaphore
);
951 * Routine: semaphore_dereference
953 * Release a reference on a semaphore. If this is the last reference,
954 * the semaphore data structure is deallocated.
957 semaphore_dereference(
958 semaphore_t semaphore
)
963 if (semaphore
!= NULL
) {
964 spl_level
= splsched();
965 semaphore_lock(semaphore
);
967 ref_count
= --(semaphore
->ref_count
);
969 semaphore_unlock(semaphore
);
972 if (ref_count
== 0) {
973 assert(wait_queue_empty(&semaphore
->wait_queue
));
974 zfree(semaphore_zone
, (vm_offset_t
)semaphore
);