2 * Copyright (c) 2000-2004 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/mach_traps.h>
35 #include <mach/kern_return.h>
36 #include <mach/semaphore.h>
37 #include <mach/sync_policy.h>
38 #include <mach/task.h>
40 #include <kern/misc_protos.h>
41 #include <kern/sync_sema.h>
43 #include <kern/ipc_kobject.h>
44 #include <kern/ipc_sync.h>
45 #include <kern/ipc_tt.h>
46 #include <kern/thread.h>
47 #include <kern/clock.h>
48 #include <ipc/ipc_port.h>
49 #include <ipc/ipc_space.h>
50 #include <kern/host.h>
51 #include <kern/wait_queue.h>
52 #include <kern/zalloc.h>
53 #include <kern/mach_param.h>
55 static unsigned int semaphore_event
;
56 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
58 zone_t semaphore_zone
;
59 unsigned int semaphore_max
= SEMAPHORE_MAX
;
61 /* Forward declarations */
65 semaphore_wait_trap_internal(
66 mach_port_name_t name
,
67 void (*caller_cont
)(kern_return_t
));
70 semaphore_wait_signal_trap_internal(
71 mach_port_name_t wait_name
,
72 mach_port_name_t signal_name
,
73 void (*caller_cont
)(kern_return_t
));
76 semaphore_timedwait_trap_internal(
77 mach_port_name_t name
,
80 void (*caller_cont
)(kern_return_t
));
83 semaphore_timedwait_signal_trap_internal(
84 mach_port_name_t wait_name
,
85 mach_port_name_t signal_name
,
88 void (*caller_cont
)(kern_return_t
));
92 semaphore_signal_internal(
93 semaphore_t semaphore
,
98 semaphore_convert_wait_result(
102 semaphore_wait_continue(void);
105 semaphore_wait_internal(
106 semaphore_t wait_semaphore
,
107 semaphore_t signal_semaphore
,
108 mach_timespec_t
*wait_timep
,
109 void (*caller_cont
)(kern_return_t
));
112 * ROUTINE: semaphore_init [private]
114 * Initialize the semaphore mechanisms.
115 * Right now, we only need to initialize the semaphore zone.
120 semaphore_zone
= zinit(sizeof(struct semaphore
),
121 semaphore_max
* sizeof(struct semaphore
),
122 sizeof(struct semaphore
),
127 * Routine: semaphore_create
129 * Creates a semaphore.
130 * The port representing the semaphore is returned as a parameter.
135 semaphore_t
*new_semaphore
,
139 semaphore_t s
= SEMAPHORE_NULL
;
143 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
144 *new_semaphore
= SEMAPHORE_NULL
;
145 return KERN_INVALID_ARGUMENT
;
148 s
= (semaphore_t
) zalloc (semaphore_zone
);
150 if (s
== SEMAPHORE_NULL
) {
151 *new_semaphore
= SEMAPHORE_NULL
;
152 return KERN_RESOURCE_SHORTAGE
;
155 wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
160 * Create and initialize the semaphore port
162 s
->port
= ipc_port_alloc_kernel();
163 if (s
->port
== IP_NULL
) {
164 /* This will deallocate the semaphore */
165 semaphore_dereference(s
);
166 *new_semaphore
= SEMAPHORE_NULL
;
167 return KERN_RESOURCE_SHORTAGE
;
170 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
173 * Associate the new semaphore with the task by adding
174 * the new semaphore to the task's semaphore list.
176 * Associate the task with the new semaphore by having the
177 * semaphores task pointer point to the owning task's structure.
180 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
181 task
->semaphores_owned
++;
192 * Routine: semaphore_destroy
194 * Destroys a semaphore. This call will only succeed if the
195 * specified task is the SAME task name specified at the semaphore's
198 * All threads currently blocked on the semaphore are awoken. These
199 * threads will return with the KERN_TERMINATED error.
204 semaphore_t semaphore
)
210 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
211 return KERN_INVALID_ARGUMENT
;
217 if (semaphore
->owner
!= task
) {
219 return KERN_INVALID_ARGUMENT
;
221 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
222 semaphore
->owner
= TASK_NULL
;
223 task
->semaphores_owned
--;
226 spl_level
= splsched();
227 semaphore_lock(semaphore
);
230 * Deactivate semaphore
232 assert(semaphore
->active
);
233 semaphore
->active
= FALSE
;
236 * Wakeup blocked threads
238 old_count
= semaphore
->count
;
239 semaphore
->count
= 0;
242 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
247 semaphore_unlock(semaphore
);
254 * Drop the semaphore reference, which in turn deallocates the
255 * semaphore structure if the reference count goes to zero.
257 ipc_port_dealloc_kernel(semaphore
->port
);
258 semaphore_dereference(semaphore
);
263 * Routine: semaphore_signal_internal
265 * Signals the semaphore as direct.
267 * Semaphore is locked.
270 semaphore_signal_internal(
271 semaphore_t semaphore
,
278 spl_level
= splsched();
279 semaphore_lock(semaphore
);
281 if (!semaphore
->active
) {
282 semaphore_unlock(semaphore
);
284 return KERN_TERMINATED
;
287 if (thread
!= THREAD_NULL
) {
288 if (semaphore
->count
< 0) {
289 kr
= wait_queue_wakeup64_thread_locked(
290 &semaphore
->wait_queue
,
296 semaphore_unlock(semaphore
);
297 kr
= KERN_NOT_WAITING
;
303 if (options
& SEMAPHORE_SIGNAL_ALL
) {
304 int old_count
= semaphore
->count
;
307 semaphore
->count
= 0; /* always reset */
308 kr
= wait_queue_wakeup64_all_locked(
309 &semaphore
->wait_queue
,
314 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
316 semaphore_unlock(semaphore
);
323 if (semaphore
->count
< 0) {
324 if (wait_queue_wakeup64_one_locked(
325 &semaphore
->wait_queue
,
328 FALSE
) == KERN_SUCCESS
) {
329 semaphore_unlock(semaphore
);
333 semaphore
->count
= 0; /* all waiters gone */
336 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
340 semaphore_unlock(semaphore
);
342 return KERN_NOT_WAITING
;
346 * Routine: semaphore_signal_thread
348 * If the specified thread is blocked on the semaphore, it is
349 * woken up. If a NULL thread was supplied, then any one
350 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
351 * and the semaphore is unchanged.
354 semaphore_signal_thread(
355 semaphore_t semaphore
,
360 if (semaphore
== SEMAPHORE_NULL
)
361 return KERN_INVALID_ARGUMENT
;
363 ret
= semaphore_signal_internal(semaphore
,
365 SEMAPHORE_OPTION_NONE
);
370 * Routine: semaphore_signal_thread_trap
372 * Trap interface to the semaphore_signal_thread function.
375 semaphore_signal_thread_trap(
376 struct semaphore_signal_thread_trap_args
*args
)
378 mach_port_name_t sema_name
= args
->signal_name
;
379 mach_port_name_t thread_name
= args
->thread_name
;
380 semaphore_t semaphore
;
385 * MACH_PORT_NULL is not an error. It means that we want to
386 * select any one thread that is already waiting, but not to
387 * pre-post the semaphore.
389 if (thread_name
!= MACH_PORT_NULL
) {
390 thread
= port_name_to_thread(thread_name
);
391 if (thread
== THREAD_NULL
)
392 return KERN_INVALID_ARGUMENT
;
394 thread
= THREAD_NULL
;
396 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
397 if (kr
== KERN_SUCCESS
) {
398 kr
= semaphore_signal_internal(semaphore
,
400 SEMAPHORE_OPTION_NONE
);
401 semaphore_dereference(semaphore
);
403 if (thread
!= THREAD_NULL
) {
404 thread_deallocate(thread
);
412 * Routine: semaphore_signal
414 * Traditional (in-kernel client and MIG interface) semaphore
415 * signal routine. Most users will access the trap version.
417 * This interface in not defined to return info about whether
418 * this call found a thread waiting or not. The internal
419 * routines (and future external routines) do. We have to
420 * convert those into plain KERN_SUCCESS returns.
424 semaphore_t semaphore
)
428 if (semaphore
== SEMAPHORE_NULL
)
429 return KERN_INVALID_ARGUMENT
;
431 kr
= semaphore_signal_internal(semaphore
,
433 SEMAPHORE_SIGNAL_PREPOST
);
434 if (kr
== KERN_NOT_WAITING
)
440 * Routine: semaphore_signal_trap
442 * Trap interface to the semaphore_signal function.
445 semaphore_signal_trap(
446 struct semaphore_signal_trap_args
*args
)
448 mach_port_name_t sema_name
= args
->signal_name
;
449 semaphore_t semaphore
;
452 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
453 if (kr
== KERN_SUCCESS
) {
454 kr
= semaphore_signal_internal(semaphore
,
456 SEMAPHORE_SIGNAL_PREPOST
);
457 semaphore_dereference(semaphore
);
458 if (kr
== KERN_NOT_WAITING
)
465 * Routine: semaphore_signal_all
467 * Awakens ALL threads currently blocked on the semaphore.
468 * The semaphore count returns to zero.
471 semaphore_signal_all(
472 semaphore_t semaphore
)
476 if (semaphore
== SEMAPHORE_NULL
)
477 return KERN_INVALID_ARGUMENT
;
479 kr
= semaphore_signal_internal(semaphore
,
481 SEMAPHORE_SIGNAL_ALL
);
482 if (kr
== KERN_NOT_WAITING
)
488 * Routine: semaphore_signal_all_trap
490 * Trap interface to the semaphore_signal_all function.
493 semaphore_signal_all_trap(
494 struct semaphore_signal_all_trap_args
*args
)
496 mach_port_name_t sema_name
= args
->signal_name
;
497 semaphore_t semaphore
;
500 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
501 if (kr
== KERN_SUCCESS
) {
502 kr
= semaphore_signal_internal(semaphore
,
504 SEMAPHORE_SIGNAL_ALL
);
505 semaphore_dereference(semaphore
);
506 if (kr
== KERN_NOT_WAITING
)
513 * Routine: semaphore_convert_wait_result
515 * Generate the return code after a semaphore wait/block. It
516 * takes the wait result as an input and coverts that to an
517 * appropriate result.
520 semaphore_convert_wait_result(int wait_result
)
522 switch (wait_result
) {
523 case THREAD_AWAKENED
:
526 case THREAD_TIMED_OUT
:
527 return KERN_OPERATION_TIMED_OUT
;
529 case THREAD_INTERRUPTED
:
533 return KERN_TERMINATED
;
536 panic("semaphore_block\n");
542 * Routine: semaphore_wait_continue
544 * Common continuation routine after waiting on a semphore.
545 * It returns directly to user space.
548 semaphore_wait_continue(void)
550 thread_t self
= current_thread();
551 int wait_result
= self
->wait_result
;
552 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
554 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
555 semaphore_dereference(self
->sth_waitsemaphore
);
556 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
557 semaphore_dereference(self
->sth_signalsemaphore
);
559 assert(caller_cont
!= (void (*)(kern_return_t
))0);
560 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
564 * Routine: semaphore_wait_internal
566 * Decrements the semaphore count by one. If the count is
567 * negative after the decrement, the calling thread blocks
568 * (possibly at a continuation and/or with a timeout).
572 * A reference is held on the signal semaphore.
575 semaphore_wait_internal(
576 semaphore_t wait_semaphore
,
577 semaphore_t signal_semaphore
,
578 mach_timespec_t
*wait_timep
,
579 void (*caller_cont
)(kern_return_t
))
581 boolean_t nonblocking
;
584 kern_return_t kr
= KERN_ALREADY_WAITING
;
586 spl_level
= splsched();
587 semaphore_lock(wait_semaphore
);
590 * Decide if we really have to wait.
592 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
593 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
596 if (!wait_semaphore
->active
) {
597 kr
= KERN_TERMINATED
;
598 } else if (wait_semaphore
->count
> 0) {
599 wait_semaphore
->count
--;
601 } else if (nonblocking
) {
602 kr
= KERN_OPERATION_TIMED_OUT
;
605 thread_t self
= current_thread();
607 wait_semaphore
->count
= -1; /* we don't keep an actual count */
611 * If it is a timed wait, calculate the wake up deadline.
613 if (wait_timep
!= (mach_timespec_t
*)0) {
614 nanoseconds_to_absolutetime((uint64_t)wait_timep
->tv_sec
*
615 NSEC_PER_SEC
+ wait_timep
->tv_nsec
, &abstime
);
616 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
621 (void)wait_queue_assert_wait64_locked(
622 &wait_semaphore
->wait_queue
,
624 THREAD_ABORTSAFE
, abstime
,
628 semaphore_unlock(wait_semaphore
);
632 * wait_semaphore is unlocked so we are free to go ahead and
633 * signal the signal_semaphore (if one was provided).
635 if (signal_semaphore
!= SEMAPHORE_NULL
) {
636 kern_return_t signal_kr
;
639 * lock the signal semaphore reference we got and signal it.
640 * This will NOT block (we cannot block after having asserted
641 * our intention to wait above).
643 signal_kr
= semaphore_signal_internal(signal_semaphore
,
645 SEMAPHORE_SIGNAL_PREPOST
);
647 if (signal_kr
== KERN_NOT_WAITING
)
648 signal_kr
= KERN_SUCCESS
;
649 else if (signal_kr
== KERN_TERMINATED
) {
651 * Uh!Oh! The semaphore we were to signal died.
652 * We have to get ourselves out of the wait in
653 * case we get stuck here forever (it is assumed
654 * that the semaphore we were posting is gating
655 * the decision by someone else to post the
656 * semaphore we are waiting on). People will
657 * discover the other dead semaphore soon enough.
658 * If we got out of the wait cleanly (someone
659 * already posted a wakeup to us) then return that
660 * (most important) result. Otherwise,
661 * return the KERN_TERMINATED status.
663 thread_t self
= current_thread();
665 clear_wait(self
, THREAD_INTERRUPTED
);
666 kr
= semaphore_convert_wait_result(self
->wait_result
);
667 if (kr
== KERN_ABORTED
)
668 kr
= KERN_TERMINATED
;
673 * If we had an error, or we didn't really need to wait we can
674 * return now that we have signalled the signal semaphore.
676 if (kr
!= KERN_ALREADY_WAITING
)
680 * Now, we can block. If the caller supplied a continuation
681 * pointer of his own for after the block, block with the
682 * appropriate semaphore continuation. Thiswill gather the
683 * semaphore results, release references on the semaphore(s),
684 * and then call the caller's continuation.
687 thread_t self
= current_thread();
689 self
->sth_continuation
= caller_cont
;
690 self
->sth_waitsemaphore
= wait_semaphore
;
691 self
->sth_signalsemaphore
= signal_semaphore
;
692 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
695 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
698 return (semaphore_convert_wait_result(wait_result
));
703 * Routine: semaphore_wait
705 * Traditional (non-continuation) interface presented to
706 * in-kernel clients to wait on a semaphore.
710 semaphore_t semaphore
)
713 if (semaphore
== SEMAPHORE_NULL
)
714 return KERN_INVALID_ARGUMENT
;
716 return(semaphore_wait_internal(semaphore
,
718 (mach_timespec_t
*)0,
719 (void (*)(kern_return_t
))0));
723 * Trap: semaphore_wait_trap
725 * Trap version of semaphore wait. Called on behalf of user-level
731 struct semaphore_wait_trap_args
*args
)
733 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
739 semaphore_wait_trap_internal(
740 mach_port_name_t name
,
741 void (*caller_cont
)(kern_return_t
))
743 semaphore_t semaphore
;
746 kr
= port_name_to_semaphore(name
, &semaphore
);
747 if (kr
== KERN_SUCCESS
) {
748 kr
= semaphore_wait_internal(semaphore
,
750 (mach_timespec_t
*)0,
752 semaphore_dereference(semaphore
);
758 * Routine: semaphore_timedwait
760 * Traditional (non-continuation) interface presented to
761 * in-kernel clients to wait on a semaphore with a timeout.
763 * A timeout of {0,0} is considered non-blocking.
767 semaphore_t semaphore
,
768 mach_timespec_t wait_time
)
770 if (semaphore
== SEMAPHORE_NULL
)
771 return KERN_INVALID_ARGUMENT
;
773 if(BAD_MACH_TIMESPEC(&wait_time
))
774 return KERN_INVALID_VALUE
;
776 return (semaphore_wait_internal(semaphore
,
779 (void(*)(kern_return_t
))0));
784 * Trap: semaphore_timedwait_trap
786 * Trap version of a semaphore_timedwait. The timeout parameter
787 * is passed in two distinct parts and re-assembled on this side
788 * of the trap interface (to accomodate calling conventions that
789 * pass structures as pointers instead of inline in registers without
790 * having to add a copyin).
792 * A timeout of {0,0} is considered non-blocking.
795 semaphore_timedwait_trap(
796 struct semaphore_timedwait_trap_args
*args
)
799 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
804 semaphore_timedwait_trap_internal(
805 mach_port_name_t name
,
808 void (*caller_cont
)(kern_return_t
))
811 semaphore_t semaphore
;
812 mach_timespec_t wait_time
;
815 wait_time
.tv_sec
= sec
;
816 wait_time
.tv_nsec
= nsec
;
817 if(BAD_MACH_TIMESPEC(&wait_time
))
818 return KERN_INVALID_VALUE
;
820 kr
= port_name_to_semaphore(name
, &semaphore
);
821 if (kr
== KERN_SUCCESS
) {
822 kr
= semaphore_wait_internal(semaphore
,
826 semaphore_dereference(semaphore
);
832 * Routine: semaphore_wait_signal
834 * Atomically register a wait on a semaphore and THEN signal
835 * another. This is the in-kernel entry point that does not
836 * block at a continuation and does not free a signal_semaphore
840 semaphore_wait_signal(
841 semaphore_t wait_semaphore
,
842 semaphore_t signal_semaphore
)
844 if (wait_semaphore
== SEMAPHORE_NULL
)
845 return KERN_INVALID_ARGUMENT
;
847 return(semaphore_wait_internal(wait_semaphore
,
849 (mach_timespec_t
*)0,
850 (void(*)(kern_return_t
))0));
854 * Trap: semaphore_wait_signal_trap
856 * Atomically register a wait on a semaphore and THEN signal
857 * another. This is the trap version from user space.
860 semaphore_wait_signal_trap(
861 struct semaphore_wait_signal_trap_args
*args
)
863 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
867 semaphore_wait_signal_trap_internal(
868 mach_port_name_t wait_name
,
869 mach_port_name_t signal_name
,
870 void (*caller_cont
)(kern_return_t
))
872 semaphore_t wait_semaphore
;
873 semaphore_t signal_semaphore
;
876 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
877 if (kr
== KERN_SUCCESS
) {
878 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
879 if (kr
== KERN_SUCCESS
) {
880 kr
= semaphore_wait_internal(wait_semaphore
,
882 (mach_timespec_t
*)0,
884 semaphore_dereference(wait_semaphore
);
886 semaphore_dereference(signal_semaphore
);
893 * Routine: semaphore_timedwait_signal
895 * Atomically register a wait on a semaphore and THEN signal
896 * another. This is the in-kernel entry point that does not
897 * block at a continuation.
899 * A timeout of {0,0} is considered non-blocking.
902 semaphore_timedwait_signal(
903 semaphore_t wait_semaphore
,
904 semaphore_t signal_semaphore
,
905 mach_timespec_t wait_time
)
907 if (wait_semaphore
== SEMAPHORE_NULL
)
908 return KERN_INVALID_ARGUMENT
;
910 if(BAD_MACH_TIMESPEC(&wait_time
))
911 return KERN_INVALID_VALUE
;
913 return(semaphore_wait_internal(wait_semaphore
,
916 (void(*)(kern_return_t
))0));
920 * Trap: semaphore_timedwait_signal_trap
922 * Atomically register a timed wait on a semaphore and THEN signal
923 * another. This is the trap version from user space.
926 semaphore_timedwait_signal_trap(
927 struct semaphore_timedwait_signal_trap_args
*args
)
929 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
933 semaphore_timedwait_signal_trap_internal(
934 mach_port_name_t wait_name
,
935 mach_port_name_t signal_name
,
938 void (*caller_cont
)(kern_return_t
))
940 semaphore_t wait_semaphore
;
941 semaphore_t signal_semaphore
;
942 mach_timespec_t wait_time
;
945 wait_time
.tv_sec
= sec
;
946 wait_time
.tv_nsec
= nsec
;
947 if(BAD_MACH_TIMESPEC(&wait_time
))
948 return KERN_INVALID_VALUE
;
950 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
951 if (kr
== KERN_SUCCESS
) {
952 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
953 if (kr
== KERN_SUCCESS
) {
954 kr
= semaphore_wait_internal(wait_semaphore
,
958 semaphore_dereference(wait_semaphore
);
960 semaphore_dereference(signal_semaphore
);
967 * Routine: semaphore_reference
969 * Take out a reference on a semaphore. This keeps the data structure
970 * in existence (but the semaphore may be deactivated).
974 semaphore_t semaphore
)
978 spl_level
= splsched();
979 semaphore_lock(semaphore
);
981 semaphore
->ref_count
++;
983 semaphore_unlock(semaphore
);
988 * Routine: semaphore_dereference
990 * Release a reference on a semaphore. If this is the last reference,
991 * the semaphore data structure is deallocated.
994 semaphore_dereference(
995 semaphore_t semaphore
)
1000 if (semaphore
!= NULL
) {
1001 spl_level
= splsched();
1002 semaphore_lock(semaphore
);
1004 ref_count
= --(semaphore
->ref_count
);
1006 semaphore_unlock(semaphore
);
1009 if (ref_count
== 0) {
1010 assert(wait_queue_empty(&semaphore
->wait_queue
));
1011 zfree(semaphore_zone
, semaphore
);