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 unsigned int semaphore_event
;
54 #define SEMAPHORE_EVENT ((event_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_wakeup_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_wakeup_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_wakeup_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_wakeup_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 AbsoluteTime 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 wait_queue_assert_wait_locked(&wait_semaphore
->wait_queue
,
588 FALSE
); /* unlock? */
590 semaphore_unlock(wait_semaphore
);
594 * wait_semaphore is unlocked so we are free to go ahead and
595 * signal the signal_semaphore (if one was provided).
597 if (signal_semaphore
!= SEMAPHORE_NULL
) {
598 kern_return_t signal_kr
;
601 * lock the signal semaphore reference we got and signal it.
602 * This will NOT block (we cannot block after having asserted
603 * our intention to wait above).
605 signal_kr
= semaphore_signal_internal(signal_semaphore
,
607 SEMAPHORE_SIGNAL_PREPOST
);
609 if (signal_kr
== KERN_NOT_WAITING
)
610 signal_kr
= KERN_SUCCESS
;
611 else if (signal_kr
== KERN_TERMINATED
) {
613 * Uh!Oh! The semaphore we were to signal died.
614 * We have to get ourselves out of the wait in
615 * case we get stuck here forever (it is assumed
616 * that the semaphore we were posting is gating
617 * the decision by someone else to post the
618 * semaphore we are waiting on). People will
619 * discover the other dead semaphore soon enough.
620 * If we got out of the wait cleanly (someone
621 * already posted a wakeup to us) then return that
622 * (most important) result. Otherwise,
623 * return the KERN_TERMINATED status.
625 thread_t self
= current_thread();
627 clear_wait(self
, THREAD_INTERRUPTED
);
628 kr
= semaphore_convert_wait_result(self
->wait_result
);
629 if (kr
== KERN_ABORTED
)
630 kr
= KERN_TERMINATED
;
635 * If we had an error, or we didn't really need to wait we can
636 * return now that we have signalled the signal semaphore.
638 if (kr
!= KERN_ALREADY_WAITING
)
642 * If it is a timed wait, go ahead and set up the timer.
644 if (wait_timep
!= (mach_timespec_t
*)0) {
645 clock_interval_to_absolutetime_interval(wait_timep
->tv_sec
,
648 clock_interval_to_absolutetime_interval(wait_timep
->tv_nsec
,
651 ADD_ABSOLUTETIME(&abstime
, &nsinterval
);
652 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
653 thread_set_timer_deadline(abstime
);
654 continuation
= semaphore_timedwait_continue
;
656 continuation
= semaphore_wait_continue
;
660 * Now, we can block. If the caller supplied a continuation
661 * pointer of his own for after the block, block with the
662 * appropriate semaphore continuation. Thiswill gather the
663 * semaphore results, release references on the semaphore(s),
664 * and then call the caller's continuation.
667 thread_t self
= current_thread();
669 self
->sth_continuation
= caller_cont
;
670 self
->sth_waitsemaphore
= wait_semaphore
;
671 self
->sth_signalsemaphore
= signal_semaphore
;
672 wait_result
= thread_block(continuation
);
674 wait_result
= thread_block((void (*)(void))0);
678 * If we came back here (not continuation case) cancel
679 * any pending timers, convert the wait result to an
680 * appropriate semaphore return value, and then return
683 if (wait_timep
&& (wait_result
!= THREAD_TIMED_OUT
))
684 thread_cancel_timer();
686 return (semaphore_convert_wait_result(wait_result
));
691 * Routine: semaphore_wait
693 * Traditional (non-continuation) interface presented to
694 * in-kernel clients to wait on a semaphore.
698 semaphore_t semaphore
)
701 if (semaphore
== SEMAPHORE_NULL
)
702 return KERN_INVALID_ARGUMENT
;
704 return(semaphore_wait_internal(semaphore
,
706 (mach_timespec_t
*)0,
707 (void (*)(kern_return_t
))0));
711 * Trap: semaphore_wait_trap
713 * Trap version of semaphore wait. Called on behalf of user-level
718 mach_port_name_t name
)
720 semaphore_t semaphore
;
723 kr
= port_name_to_semaphore(name
, &semaphore
);
724 if (kr
!= KERN_SUCCESS
)
727 kr
= semaphore_wait_internal(semaphore
,
729 (mach_timespec_t
*)0,
730 thread_syscall_return
);
731 semaphore_dereference(semaphore
);
736 * Routine: semaphore_timedwait
738 * Traditional (non-continuation) interface presented to
739 * in-kernel clients to wait on a semaphore with a timeout.
741 * A timeout of {0,0} is considered non-blocking.
745 semaphore_t semaphore
,
746 mach_timespec_t wait_time
)
748 if (semaphore
== SEMAPHORE_NULL
)
749 return KERN_INVALID_ARGUMENT
;
751 if(BAD_MACH_TIMESPEC(&wait_time
))
752 return KERN_INVALID_VALUE
;
754 return (semaphore_wait_internal(semaphore
,
757 (void(*)(kern_return_t
))0));
762 * Trap: semaphore_timedwait_trap
764 * Trap version of a semaphore_timedwait. The timeout parameter
765 * is passed in two distinct parts and re-assembled on this side
766 * of the trap interface (to accomodate calling conventions that
767 * pass structures as pointers instead of inline in registers without
768 * having to add a copyin).
770 * A timeout of {0,0} is considered non-blocking.
773 semaphore_timedwait_trap(
774 mach_port_name_t name
,
778 semaphore_t semaphore
;
779 mach_timespec_t wait_time
;
782 wait_time
.tv_sec
= sec
;
783 wait_time
.tv_nsec
= nsec
;
784 if(BAD_MACH_TIMESPEC(&wait_time
))
785 return KERN_INVALID_VALUE
;
787 kr
= port_name_to_semaphore(name
, &semaphore
);
788 if (kr
!= KERN_SUCCESS
)
791 kr
= semaphore_wait_internal(semaphore
,
794 thread_syscall_return
);
795 semaphore_dereference(semaphore
);
800 * Routine: semaphore_wait_signal
802 * Atomically register a wait on a semaphore and THEN signal
803 * another. This is the in-kernel entry point that does not
804 * block at a continuation and does not free a signal_semaphore
808 semaphore_wait_signal(
809 semaphore_t wait_semaphore
,
810 semaphore_t signal_semaphore
)
812 if (wait_semaphore
== SEMAPHORE_NULL
)
813 return KERN_INVALID_ARGUMENT
;
815 return(semaphore_wait_internal(wait_semaphore
,
817 (mach_timespec_t
*)0,
818 (void(*)(kern_return_t
))0));
822 * Trap: semaphore_wait_signal_trap
824 * Atomically register a wait on a semaphore and THEN signal
825 * another. This is the trap version from user space.
828 semaphore_wait_signal_trap(
829 mach_port_name_t wait_name
,
830 mach_port_name_t signal_name
)
832 semaphore_t wait_semaphore
;
833 semaphore_t signal_semaphore
;
836 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
837 if (kr
!= KERN_SUCCESS
)
840 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
841 if (kr
!= KERN_SUCCESS
) {
842 semaphore_dereference(signal_semaphore
);
846 kr
= semaphore_wait_internal(wait_semaphore
,
848 (mach_timespec_t
*)0,
849 thread_syscall_return
);
851 semaphore_dereference(wait_semaphore
);
852 semaphore_dereference(signal_semaphore
);
858 * Routine: semaphore_timedwait_signal
860 * Atomically register a wait on a semaphore and THEN signal
861 * another. This is the in-kernel entry point that does not
862 * block at a continuation.
864 * A timeout of {0,0} is considered non-blocking.
867 semaphore_timedwait_signal(
868 semaphore_t wait_semaphore
,
869 semaphore_t signal_semaphore
,
870 mach_timespec_t wait_time
)
872 if (wait_semaphore
== SEMAPHORE_NULL
)
873 return KERN_INVALID_ARGUMENT
;
875 if(BAD_MACH_TIMESPEC(&wait_time
))
876 return KERN_INVALID_VALUE
;
878 return(semaphore_wait_internal(wait_semaphore
,
881 (void(*)(kern_return_t
))0));
885 * Trap: semaphore_timedwait_signal_trap
887 * Atomically register a timed wait on a semaphore and THEN signal
888 * another. This is the trap version from user space.
891 semaphore_timedwait_signal_trap(
892 mach_port_name_t wait_name
,
893 mach_port_name_t signal_name
,
897 semaphore_t wait_semaphore
;
898 semaphore_t signal_semaphore
;
899 mach_timespec_t wait_time
;
902 wait_time
.tv_sec
= sec
;
903 wait_time
.tv_nsec
= nsec
;
904 if(BAD_MACH_TIMESPEC(&wait_time
))
905 return KERN_INVALID_VALUE
;
907 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
908 if (kr
!= KERN_SUCCESS
)
911 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
912 if (kr
!= KERN_SUCCESS
) {
913 semaphore_dereference(signal_semaphore
);
917 kr
= semaphore_wait_internal(wait_semaphore
,
920 thread_syscall_return
);
922 semaphore_dereference(wait_semaphore
);
923 semaphore_dereference(signal_semaphore
);
929 * Routine: semaphore_reference
931 * Take out a reference on a semaphore. This keeps the data structure
932 * in existence (but the semaphore may be deactivated).
936 semaphore_t semaphore
)
940 spl_level
= splsched();
941 semaphore_lock(semaphore
);
943 semaphore
->ref_count
++;
945 semaphore_unlock(semaphore
);
950 * Routine: semaphore_dereference
952 * Release a reference on a semaphore. If this is the last reference,
953 * the semaphore data structure is deallocated.
956 semaphore_dereference(
957 semaphore_t semaphore
)
962 if (semaphore
!= NULL
) {
963 spl_level
= splsched();
964 semaphore_lock(semaphore
);
966 ref_count
= --(semaphore
->ref_count
);
968 semaphore_unlock(semaphore
);
971 if (ref_count
== 0) {
972 assert(wait_queue_empty(&semaphore
->wait_queue
));
973 zfree(semaphore_zone
, (vm_offset_t
)semaphore
);