2 * Copyright (c) 2000-2007 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/wait_queue.h>
58 #include <kern/zalloc.h>
59 #include <kern/mach_param.h>
61 static unsigned int semaphore_event
;
62 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
64 zone_t semaphore_zone
;
65 unsigned int semaphore_max
= SEMAPHORE_MAX
;
67 /* Forward declarations */
71 semaphore_wait_trap_internal(
72 mach_port_name_t name
,
73 void (*caller_cont
)(kern_return_t
));
76 semaphore_wait_signal_trap_internal(
77 mach_port_name_t wait_name
,
78 mach_port_name_t signal_name
,
79 void (*caller_cont
)(kern_return_t
));
82 semaphore_timedwait_trap_internal(
83 mach_port_name_t name
,
86 void (*caller_cont
)(kern_return_t
));
89 semaphore_timedwait_signal_trap_internal(
90 mach_port_name_t wait_name
,
91 mach_port_name_t signal_name
,
94 void (*caller_cont
)(kern_return_t
));
97 semaphore_signal_internal_trap(mach_port_name_t sema_name
);
100 semaphore_signal_internal(
101 semaphore_t semaphore
,
106 semaphore_convert_wait_result(
110 semaphore_wait_continue(void);
113 semaphore_wait_internal(
114 semaphore_t wait_semaphore
,
115 semaphore_t signal_semaphore
,
116 mach_timespec_t
*wait_timep
,
117 void (*caller_cont
)(kern_return_t
));
120 * ROUTINE: semaphore_init [private]
122 * Initialize the semaphore mechanisms.
123 * Right now, we only need to initialize the semaphore zone.
128 semaphore_zone
= zinit(sizeof(struct semaphore
),
129 semaphore_max
* sizeof(struct semaphore
),
130 sizeof(struct semaphore
),
135 * Routine: semaphore_create
137 * Creates a semaphore.
138 * The port representing the semaphore is returned as a parameter.
143 semaphore_t
*new_semaphore
,
147 semaphore_t s
= SEMAPHORE_NULL
;
151 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
152 *new_semaphore
= SEMAPHORE_NULL
;
153 return KERN_INVALID_ARGUMENT
;
156 s
= (semaphore_t
) zalloc (semaphore_zone
);
158 if (s
== SEMAPHORE_NULL
) {
159 *new_semaphore
= SEMAPHORE_NULL
;
160 return KERN_RESOURCE_SHORTAGE
;
163 wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
168 * Create and initialize the semaphore port
170 s
->port
= ipc_port_alloc_kernel();
171 if (s
->port
== IP_NULL
) {
172 /* This will deallocate the semaphore */
173 semaphore_dereference(s
);
174 *new_semaphore
= SEMAPHORE_NULL
;
175 return KERN_RESOURCE_SHORTAGE
;
178 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
181 * Associate the new semaphore with the task by adding
182 * the new semaphore to the task's semaphore list.
184 * Associate the task with the new semaphore by having the
185 * semaphores task pointer point to the owning task's structure.
188 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
189 task
->semaphores_owned
++;
200 * Routine: semaphore_destroy
202 * Destroys a semaphore. This call will only succeed if the
203 * specified task is the SAME task name specified at the semaphore's
206 * All threads currently blocked on the semaphore are awoken. These
207 * threads will return with the KERN_TERMINATED error.
212 semaphore_t semaphore
)
218 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
219 return KERN_INVALID_ARGUMENT
;
225 if (semaphore
->owner
!= task
) {
227 return KERN_INVALID_ARGUMENT
;
229 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
230 semaphore
->owner
= TASK_NULL
;
231 task
->semaphores_owned
--;
234 spl_level
= splsched();
235 semaphore_lock(semaphore
);
238 * Deactivate semaphore
240 assert(semaphore
->active
);
241 semaphore
->active
= FALSE
;
244 * Wakeup blocked threads
246 old_count
= semaphore
->count
;
247 semaphore
->count
= 0;
250 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
255 semaphore_unlock(semaphore
);
262 * Drop the semaphore reference, which in turn deallocates the
263 * semaphore structure if the reference count goes to zero.
265 ipc_port_dealloc_kernel(semaphore
->port
);
266 semaphore_dereference(semaphore
);
271 * Routine: semaphore_signal_internal
273 * Signals the semaphore as direct.
275 * Semaphore is locked.
278 semaphore_signal_internal(
279 semaphore_t semaphore
,
286 spl_level
= splsched();
287 semaphore_lock(semaphore
);
289 if (!semaphore
->active
) {
290 semaphore_unlock(semaphore
);
292 return KERN_TERMINATED
;
295 if (thread
!= THREAD_NULL
) {
296 if (semaphore
->count
< 0) {
297 kr
= wait_queue_wakeup64_thread_locked(
298 &semaphore
->wait_queue
,
304 semaphore_unlock(semaphore
);
305 kr
= KERN_NOT_WAITING
;
311 if (options
& SEMAPHORE_SIGNAL_ALL
) {
312 int old_count
= semaphore
->count
;
315 semaphore
->count
= 0; /* always reset */
316 kr
= wait_queue_wakeup64_all_locked(
317 &semaphore
->wait_queue
,
322 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
324 semaphore_unlock(semaphore
);
331 if (semaphore
->count
< 0) {
332 if (wait_queue_wakeup64_one_locked(
333 &semaphore
->wait_queue
,
336 FALSE
) == KERN_SUCCESS
) {
337 semaphore_unlock(semaphore
);
341 semaphore
->count
= 0; /* all waiters gone */
344 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
348 semaphore_unlock(semaphore
);
350 return KERN_NOT_WAITING
;
354 * Routine: semaphore_signal_thread
356 * If the specified thread is blocked on the semaphore, it is
357 * woken up. If a NULL thread was supplied, then any one
358 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
359 * and the semaphore is unchanged.
362 semaphore_signal_thread(
363 semaphore_t semaphore
,
368 if (semaphore
== SEMAPHORE_NULL
)
369 return KERN_INVALID_ARGUMENT
;
371 ret
= semaphore_signal_internal(semaphore
,
373 SEMAPHORE_OPTION_NONE
);
378 * Routine: semaphore_signal_thread_trap
380 * Trap interface to the semaphore_signal_thread function.
383 semaphore_signal_thread_trap(
384 struct semaphore_signal_thread_trap_args
*args
)
386 mach_port_name_t sema_name
= args
->signal_name
;
387 mach_port_name_t thread_name
= args
->thread_name
;
388 semaphore_t semaphore
;
393 * MACH_PORT_NULL is not an error. It means that we want to
394 * select any one thread that is already waiting, but not to
395 * pre-post the semaphore.
397 if (thread_name
!= MACH_PORT_NULL
) {
398 thread
= port_name_to_thread(thread_name
);
399 if (thread
== THREAD_NULL
)
400 return KERN_INVALID_ARGUMENT
;
402 thread
= THREAD_NULL
;
404 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
405 if (kr
== KERN_SUCCESS
) {
406 kr
= semaphore_signal_internal(semaphore
,
408 SEMAPHORE_OPTION_NONE
);
409 semaphore_dereference(semaphore
);
411 if (thread
!= THREAD_NULL
) {
412 thread_deallocate(thread
);
420 * Routine: semaphore_signal
422 * Traditional (in-kernel client and MIG interface) semaphore
423 * signal routine. Most users will access the trap version.
425 * This interface in not defined to return info about whether
426 * this call found a thread waiting or not. The internal
427 * routines (and future external routines) do. We have to
428 * convert those into plain KERN_SUCCESS returns.
432 semaphore_t semaphore
)
436 if (semaphore
== SEMAPHORE_NULL
)
437 return KERN_INVALID_ARGUMENT
;
439 kr
= semaphore_signal_internal(semaphore
,
441 SEMAPHORE_SIGNAL_PREPOST
);
442 if (kr
== KERN_NOT_WAITING
)
448 * Routine: semaphore_signal_trap
450 * Trap interface to the semaphore_signal function.
453 semaphore_signal_trap(
454 struct semaphore_signal_trap_args
*args
)
456 mach_port_name_t sema_name
= args
->signal_name
;
458 return (semaphore_signal_internal_trap(sema_name
));
462 semaphore_signal_internal_trap(mach_port_name_t sema_name
)
464 semaphore_t semaphore
;
467 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
468 if (kr
== KERN_SUCCESS
) {
469 kr
= semaphore_signal_internal(semaphore
,
471 SEMAPHORE_SIGNAL_PREPOST
);
472 semaphore_dereference(semaphore
);
473 if (kr
== KERN_NOT_WAITING
)
480 * Routine: semaphore_signal_all
482 * Awakens ALL threads currently blocked on the semaphore.
483 * The semaphore count returns to zero.
486 semaphore_signal_all(
487 semaphore_t semaphore
)
491 if (semaphore
== SEMAPHORE_NULL
)
492 return KERN_INVALID_ARGUMENT
;
494 kr
= semaphore_signal_internal(semaphore
,
496 SEMAPHORE_SIGNAL_ALL
);
497 if (kr
== KERN_NOT_WAITING
)
503 * Routine: semaphore_signal_all_trap
505 * Trap interface to the semaphore_signal_all function.
508 semaphore_signal_all_trap(
509 struct semaphore_signal_all_trap_args
*args
)
511 mach_port_name_t sema_name
= args
->signal_name
;
512 semaphore_t semaphore
;
515 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
516 if (kr
== KERN_SUCCESS
) {
517 kr
= semaphore_signal_internal(semaphore
,
519 SEMAPHORE_SIGNAL_ALL
);
520 semaphore_dereference(semaphore
);
521 if (kr
== KERN_NOT_WAITING
)
528 * Routine: semaphore_convert_wait_result
530 * Generate the return code after a semaphore wait/block. It
531 * takes the wait result as an input and coverts that to an
532 * appropriate result.
535 semaphore_convert_wait_result(int wait_result
)
537 switch (wait_result
) {
538 case THREAD_AWAKENED
:
541 case THREAD_TIMED_OUT
:
542 return KERN_OPERATION_TIMED_OUT
;
544 case THREAD_INTERRUPTED
:
548 return KERN_TERMINATED
;
551 panic("semaphore_block\n");
557 * Routine: semaphore_wait_continue
559 * Common continuation routine after waiting on a semphore.
560 * It returns directly to user space.
563 semaphore_wait_continue(void)
565 thread_t self
= current_thread();
566 int wait_result
= self
->wait_result
;
567 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
569 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
570 semaphore_dereference(self
->sth_waitsemaphore
);
571 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
572 semaphore_dereference(self
->sth_signalsemaphore
);
574 assert(caller_cont
!= (void (*)(kern_return_t
))0);
575 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
579 * Routine: semaphore_wait_internal
581 * Decrements the semaphore count by one. If the count is
582 * negative after the decrement, the calling thread blocks
583 * (possibly at a continuation and/or with a timeout).
587 * A reference is held on the signal semaphore.
590 semaphore_wait_internal(
591 semaphore_t wait_semaphore
,
592 semaphore_t signal_semaphore
,
593 mach_timespec_t
*wait_timep
,
594 void (*caller_cont
)(kern_return_t
))
596 boolean_t nonblocking
;
599 kern_return_t kr
= KERN_ALREADY_WAITING
;
601 spl_level
= splsched();
602 semaphore_lock(wait_semaphore
);
605 * Decide if we really have to wait.
607 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
608 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
611 if (!wait_semaphore
->active
) {
612 kr
= KERN_TERMINATED
;
613 } else if (wait_semaphore
->count
> 0) {
614 wait_semaphore
->count
--;
616 } else if (nonblocking
) {
617 kr
= KERN_OPERATION_TIMED_OUT
;
620 thread_t self
= current_thread();
622 wait_semaphore
->count
= -1; /* we don't keep an actual count */
626 * If it is a timed wait, calculate the wake up deadline.
628 if (wait_timep
!= (mach_timespec_t
*)0) {
629 nanoseconds_to_absolutetime((uint64_t)wait_timep
->tv_sec
*
630 NSEC_PER_SEC
+ wait_timep
->tv_nsec
, &abstime
);
631 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
636 (void)wait_queue_assert_wait64_locked(
637 &wait_semaphore
->wait_queue
,
639 THREAD_ABORTSAFE
, abstime
,
643 semaphore_unlock(wait_semaphore
);
647 * wait_semaphore is unlocked so we are free to go ahead and
648 * signal the signal_semaphore (if one was provided).
650 if (signal_semaphore
!= SEMAPHORE_NULL
) {
651 kern_return_t signal_kr
;
654 * lock the signal semaphore reference we got and signal it.
655 * This will NOT block (we cannot block after having asserted
656 * our intention to wait above).
658 signal_kr
= semaphore_signal_internal(signal_semaphore
,
660 SEMAPHORE_SIGNAL_PREPOST
);
662 if (signal_kr
== KERN_NOT_WAITING
)
663 signal_kr
= KERN_SUCCESS
;
664 else if (signal_kr
== KERN_TERMINATED
) {
666 * Uh!Oh! The semaphore we were to signal died.
667 * We have to get ourselves out of the wait in
668 * case we get stuck here forever (it is assumed
669 * that the semaphore we were posting is gating
670 * the decision by someone else to post the
671 * semaphore we are waiting on). People will
672 * discover the other dead semaphore soon enough.
673 * If we got out of the wait cleanly (someone
674 * already posted a wakeup to us) then return that
675 * (most important) result. Otherwise,
676 * return the KERN_TERMINATED status.
678 thread_t self
= current_thread();
680 clear_wait(self
, THREAD_INTERRUPTED
);
681 kr
= semaphore_convert_wait_result(self
->wait_result
);
682 if (kr
== KERN_ABORTED
)
683 kr
= KERN_TERMINATED
;
688 * If we had an error, or we didn't really need to wait we can
689 * return now that we have signalled the signal semaphore.
691 if (kr
!= KERN_ALREADY_WAITING
)
695 * Now, we can block. If the caller supplied a continuation
696 * pointer of his own for after the block, block with the
697 * appropriate semaphore continuation. Thiswill gather the
698 * semaphore results, release references on the semaphore(s),
699 * and then call the caller's continuation.
702 thread_t self
= current_thread();
704 self
->sth_continuation
= caller_cont
;
705 self
->sth_waitsemaphore
= wait_semaphore
;
706 self
->sth_signalsemaphore
= signal_semaphore
;
707 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
710 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
713 return (semaphore_convert_wait_result(wait_result
));
718 * Routine: semaphore_wait
720 * Traditional (non-continuation) interface presented to
721 * in-kernel clients to wait on a semaphore.
725 semaphore_t semaphore
)
728 if (semaphore
== SEMAPHORE_NULL
)
729 return KERN_INVALID_ARGUMENT
;
731 return(semaphore_wait_internal(semaphore
,
733 (mach_timespec_t
*)0,
734 (void (*)(kern_return_t
))0));
738 * Trap: semaphore_wait_trap
740 * Trap version of semaphore wait. Called on behalf of user-level
746 struct semaphore_wait_trap_args
*args
)
748 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
754 semaphore_wait_trap_internal(
755 mach_port_name_t name
,
756 void (*caller_cont
)(kern_return_t
))
758 semaphore_t semaphore
;
761 kr
= port_name_to_semaphore(name
, &semaphore
);
762 if (kr
== KERN_SUCCESS
) {
763 kr
= semaphore_wait_internal(semaphore
,
765 (mach_timespec_t
*)0,
767 semaphore_dereference(semaphore
);
773 * Routine: semaphore_timedwait
775 * Traditional (non-continuation) interface presented to
776 * in-kernel clients to wait on a semaphore with a timeout.
778 * A timeout of {0,0} is considered non-blocking.
782 semaphore_t semaphore
,
783 mach_timespec_t wait_time
)
785 if (semaphore
== SEMAPHORE_NULL
)
786 return KERN_INVALID_ARGUMENT
;
788 if(BAD_MACH_TIMESPEC(&wait_time
))
789 return KERN_INVALID_VALUE
;
791 return (semaphore_wait_internal(semaphore
,
794 (void(*)(kern_return_t
))0));
799 * Trap: semaphore_timedwait_trap
801 * Trap version of a semaphore_timedwait. The timeout parameter
802 * is passed in two distinct parts and re-assembled on this side
803 * of the trap interface (to accomodate calling conventions that
804 * pass structures as pointers instead of inline in registers without
805 * having to add a copyin).
807 * A timeout of {0,0} is considered non-blocking.
810 semaphore_timedwait_trap(
811 struct semaphore_timedwait_trap_args
*args
)
814 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
819 semaphore_timedwait_trap_internal(
820 mach_port_name_t name
,
823 void (*caller_cont
)(kern_return_t
))
826 semaphore_t semaphore
;
827 mach_timespec_t wait_time
;
830 wait_time
.tv_sec
= sec
;
831 wait_time
.tv_nsec
= nsec
;
832 if(BAD_MACH_TIMESPEC(&wait_time
))
833 return KERN_INVALID_VALUE
;
835 kr
= port_name_to_semaphore(name
, &semaphore
);
836 if (kr
== KERN_SUCCESS
) {
837 kr
= semaphore_wait_internal(semaphore
,
841 semaphore_dereference(semaphore
);
847 * Routine: semaphore_wait_signal
849 * Atomically register a wait on a semaphore and THEN signal
850 * another. This is the in-kernel entry point that does not
851 * block at a continuation and does not free a signal_semaphore
855 semaphore_wait_signal(
856 semaphore_t wait_semaphore
,
857 semaphore_t signal_semaphore
)
859 if (wait_semaphore
== SEMAPHORE_NULL
)
860 return KERN_INVALID_ARGUMENT
;
862 return(semaphore_wait_internal(wait_semaphore
,
864 (mach_timespec_t
*)0,
865 (void(*)(kern_return_t
))0));
869 * Trap: semaphore_wait_signal_trap
871 * Atomically register a wait on a semaphore and THEN signal
872 * another. This is the trap version from user space.
875 semaphore_wait_signal_trap(
876 struct semaphore_wait_signal_trap_args
*args
)
878 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
882 semaphore_wait_signal_trap_internal(
883 mach_port_name_t wait_name
,
884 mach_port_name_t signal_name
,
885 void (*caller_cont
)(kern_return_t
))
887 semaphore_t wait_semaphore
;
888 semaphore_t signal_semaphore
;
891 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
892 if (kr
== KERN_SUCCESS
) {
893 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
894 if (kr
== KERN_SUCCESS
) {
895 kr
= semaphore_wait_internal(wait_semaphore
,
897 (mach_timespec_t
*)0,
899 semaphore_dereference(wait_semaphore
);
901 semaphore_dereference(signal_semaphore
);
908 * Routine: semaphore_timedwait_signal
910 * Atomically register a wait on a semaphore and THEN signal
911 * another. This is the in-kernel entry point that does not
912 * block at a continuation.
914 * A timeout of {0,0} is considered non-blocking.
917 semaphore_timedwait_signal(
918 semaphore_t wait_semaphore
,
919 semaphore_t signal_semaphore
,
920 mach_timespec_t wait_time
)
922 if (wait_semaphore
== SEMAPHORE_NULL
)
923 return KERN_INVALID_ARGUMENT
;
925 if(BAD_MACH_TIMESPEC(&wait_time
))
926 return KERN_INVALID_VALUE
;
928 return(semaphore_wait_internal(wait_semaphore
,
931 (void(*)(kern_return_t
))0));
935 * Trap: semaphore_timedwait_signal_trap
937 * Atomically register a timed wait on a semaphore and THEN signal
938 * another. This is the trap version from user space.
941 semaphore_timedwait_signal_trap(
942 struct semaphore_timedwait_signal_trap_args
*args
)
944 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
948 semaphore_timedwait_signal_trap_internal(
949 mach_port_name_t wait_name
,
950 mach_port_name_t signal_name
,
953 void (*caller_cont
)(kern_return_t
))
955 semaphore_t wait_semaphore
;
956 semaphore_t signal_semaphore
;
957 mach_timespec_t wait_time
;
960 wait_time
.tv_sec
= sec
;
961 wait_time
.tv_nsec
= nsec
;
962 if(BAD_MACH_TIMESPEC(&wait_time
))
963 return KERN_INVALID_VALUE
;
965 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
966 if (kr
== KERN_SUCCESS
) {
967 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
968 if (kr
== KERN_SUCCESS
) {
969 kr
= semaphore_wait_internal(wait_semaphore
,
973 semaphore_dereference(wait_semaphore
);
975 semaphore_dereference(signal_semaphore
);
982 * Routine: semaphore_reference
984 * Take out a reference on a semaphore. This keeps the data structure
985 * in existence (but the semaphore may be deactivated).
989 semaphore_t semaphore
)
993 spl_level
= splsched();
994 semaphore_lock(semaphore
);
996 semaphore
->ref_count
++;
998 semaphore_unlock(semaphore
);
1003 * Routine: semaphore_dereference
1005 * Release a reference on a semaphore. If this is the last reference,
1006 * the semaphore data structure is deallocated.
1009 semaphore_dereference(
1010 semaphore_t semaphore
)
1015 if (semaphore
!= NULL
) {
1016 spl_level
= splsched();
1017 semaphore_lock(semaphore
);
1019 ref_count
= --(semaphore
->ref_count
);
1021 semaphore_unlock(semaphore
);
1024 if (ref_count
== 0) {
1025 assert(wait_queue_empty(&semaphore
->wait_queue
));
1026 zfree(semaphore_zone
, semaphore
);