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);
563 uint64_t abstime
, nsinterval
;
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 wait_semaphore
->count
= -1; /* we don't keep an actual count */
588 (void)wait_queue_assert_wait64_locked(
589 &wait_semaphore
->wait_queue
,
592 FALSE
); /* unlock? */
594 semaphore_unlock(wait_semaphore
);
598 * wait_semaphore is unlocked so we are free to go ahead and
599 * signal the signal_semaphore (if one was provided).
601 if (signal_semaphore
!= SEMAPHORE_NULL
) {
602 kern_return_t signal_kr
;
605 * lock the signal semaphore reference we got and signal it.
606 * This will NOT block (we cannot block after having asserted
607 * our intention to wait above).
609 signal_kr
= semaphore_signal_internal(signal_semaphore
,
611 SEMAPHORE_SIGNAL_PREPOST
);
613 if (signal_kr
== KERN_NOT_WAITING
)
614 signal_kr
= KERN_SUCCESS
;
615 else if (signal_kr
== KERN_TERMINATED
) {
617 * Uh!Oh! The semaphore we were to signal died.
618 * We have to get ourselves out of the wait in
619 * case we get stuck here forever (it is assumed
620 * that the semaphore we were posting is gating
621 * the decision by someone else to post the
622 * semaphore we are waiting on). People will
623 * discover the other dead semaphore soon enough.
624 * If we got out of the wait cleanly (someone
625 * already posted a wakeup to us) then return that
626 * (most important) result. Otherwise,
627 * return the KERN_TERMINATED status.
629 thread_t self
= current_thread();
631 clear_wait(self
, THREAD_INTERRUPTED
);
632 kr
= semaphore_convert_wait_result(self
->wait_result
);
633 if (kr
== KERN_ABORTED
)
634 kr
= KERN_TERMINATED
;
639 * If we had an error, or we didn't really need to wait we can
640 * return now that we have signalled the signal semaphore.
642 if (kr
!= KERN_ALREADY_WAITING
)
646 * If it is a timed wait, go ahead and set up the timer.
648 if (wait_timep
!= (mach_timespec_t
*)0) {
649 clock_interval_to_absolutetime_interval(wait_timep
->tv_sec
,
652 clock_interval_to_absolutetime_interval(wait_timep
->tv_nsec
,
655 abstime
+= nsinterval
;
656 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
657 thread_set_timer_deadline(abstime
);
658 continuation
= semaphore_timedwait_continue
;
660 continuation
= semaphore_wait_continue
;
664 * Now, we can block. If the caller supplied a continuation
665 * pointer of his own for after the block, block with the
666 * appropriate semaphore continuation. Thiswill gather the
667 * semaphore results, release references on the semaphore(s),
668 * and then call the caller's continuation.
671 thread_t self
= current_thread();
673 self
->sth_continuation
= caller_cont
;
674 self
->sth_waitsemaphore
= wait_semaphore
;
675 self
->sth_signalsemaphore
= signal_semaphore
;
676 wait_result
= thread_block(continuation
);
678 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
682 * If we came back here (not continuation case) cancel
683 * any pending timers, convert the wait result to an
684 * appropriate semaphore return value, and then return
687 if (wait_timep
&& (wait_result
!= THREAD_TIMED_OUT
))
688 thread_cancel_timer();
690 return (semaphore_convert_wait_result(wait_result
));
695 * Routine: semaphore_wait
697 * Traditional (non-continuation) interface presented to
698 * in-kernel clients to wait on a semaphore.
702 semaphore_t semaphore
)
705 if (semaphore
== SEMAPHORE_NULL
)
706 return KERN_INVALID_ARGUMENT
;
708 return(semaphore_wait_internal(semaphore
,
710 (mach_timespec_t
*)0,
711 (void (*)(kern_return_t
))0));
715 * Trap: semaphore_wait_trap
717 * Trap version of semaphore wait. Called on behalf of user-level
722 mach_port_name_t name
)
724 semaphore_t semaphore
;
727 kr
= port_name_to_semaphore(name
, &semaphore
);
728 if (kr
!= KERN_SUCCESS
)
731 kr
= semaphore_wait_internal(semaphore
,
733 (mach_timespec_t
*)0,
734 thread_syscall_return
);
735 semaphore_dereference(semaphore
);
740 * Routine: semaphore_timedwait
742 * Traditional (non-continuation) interface presented to
743 * in-kernel clients to wait on a semaphore with a timeout.
745 * A timeout of {0,0} is considered non-blocking.
749 semaphore_t semaphore
,
750 mach_timespec_t wait_time
)
752 if (semaphore
== SEMAPHORE_NULL
)
753 return KERN_INVALID_ARGUMENT
;
755 if(BAD_MACH_TIMESPEC(&wait_time
))
756 return KERN_INVALID_VALUE
;
758 return (semaphore_wait_internal(semaphore
,
761 (void(*)(kern_return_t
))0));
766 * Trap: semaphore_timedwait_trap
768 * Trap version of a semaphore_timedwait. The timeout parameter
769 * is passed in two distinct parts and re-assembled on this side
770 * of the trap interface (to accomodate calling conventions that
771 * pass structures as pointers instead of inline in registers without
772 * having to add a copyin).
774 * A timeout of {0,0} is considered non-blocking.
777 semaphore_timedwait_trap(
778 mach_port_name_t name
,
782 semaphore_t semaphore
;
783 mach_timespec_t wait_time
;
786 wait_time
.tv_sec
= sec
;
787 wait_time
.tv_nsec
= nsec
;
788 if(BAD_MACH_TIMESPEC(&wait_time
))
789 return KERN_INVALID_VALUE
;
791 kr
= port_name_to_semaphore(name
, &semaphore
);
792 if (kr
!= KERN_SUCCESS
)
795 kr
= semaphore_wait_internal(semaphore
,
798 thread_syscall_return
);
799 semaphore_dereference(semaphore
);
804 * Routine: semaphore_wait_signal
806 * Atomically register a wait on a semaphore and THEN signal
807 * another. This is the in-kernel entry point that does not
808 * block at a continuation and does not free a signal_semaphore
812 semaphore_wait_signal(
813 semaphore_t wait_semaphore
,
814 semaphore_t signal_semaphore
)
816 if (wait_semaphore
== SEMAPHORE_NULL
)
817 return KERN_INVALID_ARGUMENT
;
819 return(semaphore_wait_internal(wait_semaphore
,
821 (mach_timespec_t
*)0,
822 (void(*)(kern_return_t
))0));
826 * Trap: semaphore_wait_signal_trap
828 * Atomically register a wait on a semaphore and THEN signal
829 * another. This is the trap version from user space.
832 semaphore_wait_signal_trap(
833 mach_port_name_t wait_name
,
834 mach_port_name_t signal_name
)
836 semaphore_t wait_semaphore
;
837 semaphore_t signal_semaphore
;
840 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
841 if (kr
!= KERN_SUCCESS
)
844 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
845 if (kr
!= KERN_SUCCESS
) {
846 semaphore_dereference(signal_semaphore
);
850 kr
= semaphore_wait_internal(wait_semaphore
,
852 (mach_timespec_t
*)0,
853 thread_syscall_return
);
855 semaphore_dereference(wait_semaphore
);
856 semaphore_dereference(signal_semaphore
);
862 * Routine: semaphore_timedwait_signal
864 * Atomically register a wait on a semaphore and THEN signal
865 * another. This is the in-kernel entry point that does not
866 * block at a continuation.
868 * A timeout of {0,0} is considered non-blocking.
871 semaphore_timedwait_signal(
872 semaphore_t wait_semaphore
,
873 semaphore_t signal_semaphore
,
874 mach_timespec_t wait_time
)
876 if (wait_semaphore
== SEMAPHORE_NULL
)
877 return KERN_INVALID_ARGUMENT
;
879 if(BAD_MACH_TIMESPEC(&wait_time
))
880 return KERN_INVALID_VALUE
;
882 return(semaphore_wait_internal(wait_semaphore
,
885 (void(*)(kern_return_t
))0));
889 * Trap: semaphore_timedwait_signal_trap
891 * Atomically register a timed wait on a semaphore and THEN signal
892 * another. This is the trap version from user space.
895 semaphore_timedwait_signal_trap(
896 mach_port_name_t wait_name
,
897 mach_port_name_t signal_name
,
901 semaphore_t wait_semaphore
;
902 semaphore_t signal_semaphore
;
903 mach_timespec_t wait_time
;
906 wait_time
.tv_sec
= sec
;
907 wait_time
.tv_nsec
= nsec
;
908 if(BAD_MACH_TIMESPEC(&wait_time
))
909 return KERN_INVALID_VALUE
;
911 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
912 if (kr
!= KERN_SUCCESS
)
915 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
916 if (kr
!= KERN_SUCCESS
) {
917 semaphore_dereference(signal_semaphore
);
921 kr
= semaphore_wait_internal(wait_semaphore
,
924 thread_syscall_return
);
926 semaphore_dereference(wait_semaphore
);
927 semaphore_dereference(signal_semaphore
);
933 * Routine: semaphore_reference
935 * Take out a reference on a semaphore. This keeps the data structure
936 * in existence (but the semaphore may be deactivated).
940 semaphore_t semaphore
)
944 spl_level
= splsched();
945 semaphore_lock(semaphore
);
947 semaphore
->ref_count
++;
949 semaphore_unlock(semaphore
);
954 * Routine: semaphore_dereference
956 * Release a reference on a semaphore. If this is the last reference,
957 * the semaphore data structure is deallocated.
960 semaphore_dereference(
961 semaphore_t semaphore
)
966 if (semaphore
!= NULL
) {
967 spl_level
= splsched();
968 semaphore_lock(semaphore
);
970 ref_count
= --(semaphore
->ref_count
);
972 semaphore_unlock(semaphore
);
975 if (ref_count
== 0) {
976 assert(wait_queue_empty(&semaphore
->wait_queue
));
977 zfree(semaphore_zone
, (vm_offset_t
)semaphore
);