2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
35 * File: kern/sync_sema.c
36 * Author: Joseph CaraDonna
38 * Contains RT distributed semaphore synchronization services.
41 #include <mach/mach_types.h>
42 #include <mach/mach_traps.h>
43 #include <mach/kern_return.h>
44 #include <mach/semaphore.h>
45 #include <mach/sync_policy.h>
46 #include <mach/task.h>
48 #include <kern/misc_protos.h>
49 #include <kern/sync_sema.h>
51 #include <kern/ipc_kobject.h>
52 #include <kern/ipc_sync.h>
53 #include <kern/ipc_tt.h>
54 #include <kern/thread.h>
55 #include <kern/clock.h>
56 #include <ipc/ipc_port.h>
57 #include <ipc/ipc_space.h>
58 #include <kern/host.h>
59 #include <kern/wait_queue.h>
60 #include <kern/zalloc.h>
61 #include <kern/mach_param.h>
63 static unsigned int semaphore_event
;
64 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
66 zone_t semaphore_zone
;
67 unsigned int semaphore_max
= SEMAPHORE_MAX
;
69 /* Forward declarations */
73 semaphore_wait_trap_internal(
74 mach_port_name_t name
,
75 void (*caller_cont
)(kern_return_t
));
78 semaphore_wait_signal_trap_internal(
79 mach_port_name_t wait_name
,
80 mach_port_name_t signal_name
,
81 void (*caller_cont
)(kern_return_t
));
84 semaphore_timedwait_trap_internal(
85 mach_port_name_t name
,
88 void (*caller_cont
)(kern_return_t
));
91 semaphore_timedwait_signal_trap_internal(
92 mach_port_name_t wait_name
,
93 mach_port_name_t signal_name
,
96 void (*caller_cont
)(kern_return_t
));
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
;
457 semaphore_t semaphore
;
460 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
461 if (kr
== KERN_SUCCESS
) {
462 kr
= semaphore_signal_internal(semaphore
,
464 SEMAPHORE_SIGNAL_PREPOST
);
465 semaphore_dereference(semaphore
);
466 if (kr
== KERN_NOT_WAITING
)
473 * Routine: semaphore_signal_all
475 * Awakens ALL threads currently blocked on the semaphore.
476 * The semaphore count returns to zero.
479 semaphore_signal_all(
480 semaphore_t semaphore
)
484 if (semaphore
== SEMAPHORE_NULL
)
485 return KERN_INVALID_ARGUMENT
;
487 kr
= semaphore_signal_internal(semaphore
,
489 SEMAPHORE_SIGNAL_ALL
);
490 if (kr
== KERN_NOT_WAITING
)
496 * Routine: semaphore_signal_all_trap
498 * Trap interface to the semaphore_signal_all function.
501 semaphore_signal_all_trap(
502 struct semaphore_signal_all_trap_args
*args
)
504 mach_port_name_t sema_name
= args
->signal_name
;
505 semaphore_t semaphore
;
508 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
509 if (kr
== KERN_SUCCESS
) {
510 kr
= semaphore_signal_internal(semaphore
,
512 SEMAPHORE_SIGNAL_ALL
);
513 semaphore_dereference(semaphore
);
514 if (kr
== KERN_NOT_WAITING
)
521 * Routine: semaphore_convert_wait_result
523 * Generate the return code after a semaphore wait/block. It
524 * takes the wait result as an input and coverts that to an
525 * appropriate result.
528 semaphore_convert_wait_result(int wait_result
)
530 switch (wait_result
) {
531 case THREAD_AWAKENED
:
534 case THREAD_TIMED_OUT
:
535 return KERN_OPERATION_TIMED_OUT
;
537 case THREAD_INTERRUPTED
:
541 return KERN_TERMINATED
;
544 panic("semaphore_block\n");
550 * Routine: semaphore_wait_continue
552 * Common continuation routine after waiting on a semphore.
553 * It returns directly to user space.
556 semaphore_wait_continue(void)
558 thread_t self
= current_thread();
559 int wait_result
= self
->wait_result
;
560 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
562 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
563 semaphore_dereference(self
->sth_waitsemaphore
);
564 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
565 semaphore_dereference(self
->sth_signalsemaphore
);
567 assert(caller_cont
!= (void (*)(kern_return_t
))0);
568 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
572 * Routine: semaphore_wait_internal
574 * Decrements the semaphore count by one. If the count is
575 * negative after the decrement, the calling thread blocks
576 * (possibly at a continuation and/or with a timeout).
580 * A reference is held on the signal semaphore.
583 semaphore_wait_internal(
584 semaphore_t wait_semaphore
,
585 semaphore_t signal_semaphore
,
586 mach_timespec_t
*wait_timep
,
587 void (*caller_cont
)(kern_return_t
))
589 boolean_t nonblocking
;
592 kern_return_t kr
= KERN_ALREADY_WAITING
;
594 spl_level
= splsched();
595 semaphore_lock(wait_semaphore
);
598 * Decide if we really have to wait.
600 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
601 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
604 if (!wait_semaphore
->active
) {
605 kr
= KERN_TERMINATED
;
606 } else if (wait_semaphore
->count
> 0) {
607 wait_semaphore
->count
--;
609 } else if (nonblocking
) {
610 kr
= KERN_OPERATION_TIMED_OUT
;
613 thread_t self
= current_thread();
615 wait_semaphore
->count
= -1; /* we don't keep an actual count */
619 * If it is a timed wait, calculate the wake up deadline.
621 if (wait_timep
!= (mach_timespec_t
*)0) {
622 nanoseconds_to_absolutetime((uint64_t)wait_timep
->tv_sec
*
623 NSEC_PER_SEC
+ wait_timep
->tv_nsec
, &abstime
);
624 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
629 (void)wait_queue_assert_wait64_locked(
630 &wait_semaphore
->wait_queue
,
632 THREAD_ABORTSAFE
, abstime
,
636 semaphore_unlock(wait_semaphore
);
640 * wait_semaphore is unlocked so we are free to go ahead and
641 * signal the signal_semaphore (if one was provided).
643 if (signal_semaphore
!= SEMAPHORE_NULL
) {
644 kern_return_t signal_kr
;
647 * lock the signal semaphore reference we got and signal it.
648 * This will NOT block (we cannot block after having asserted
649 * our intention to wait above).
651 signal_kr
= semaphore_signal_internal(signal_semaphore
,
653 SEMAPHORE_SIGNAL_PREPOST
);
655 if (signal_kr
== KERN_NOT_WAITING
)
656 signal_kr
= KERN_SUCCESS
;
657 else if (signal_kr
== KERN_TERMINATED
) {
659 * Uh!Oh! The semaphore we were to signal died.
660 * We have to get ourselves out of the wait in
661 * case we get stuck here forever (it is assumed
662 * that the semaphore we were posting is gating
663 * the decision by someone else to post the
664 * semaphore we are waiting on). People will
665 * discover the other dead semaphore soon enough.
666 * If we got out of the wait cleanly (someone
667 * already posted a wakeup to us) then return that
668 * (most important) result. Otherwise,
669 * return the KERN_TERMINATED status.
671 thread_t self
= current_thread();
673 clear_wait(self
, THREAD_INTERRUPTED
);
674 kr
= semaphore_convert_wait_result(self
->wait_result
);
675 if (kr
== KERN_ABORTED
)
676 kr
= KERN_TERMINATED
;
681 * If we had an error, or we didn't really need to wait we can
682 * return now that we have signalled the signal semaphore.
684 if (kr
!= KERN_ALREADY_WAITING
)
688 * Now, we can block. If the caller supplied a continuation
689 * pointer of his own for after the block, block with the
690 * appropriate semaphore continuation. Thiswill gather the
691 * semaphore results, release references on the semaphore(s),
692 * and then call the caller's continuation.
695 thread_t self
= current_thread();
697 self
->sth_continuation
= caller_cont
;
698 self
->sth_waitsemaphore
= wait_semaphore
;
699 self
->sth_signalsemaphore
= signal_semaphore
;
700 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
703 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
706 return (semaphore_convert_wait_result(wait_result
));
711 * Routine: semaphore_wait
713 * Traditional (non-continuation) interface presented to
714 * in-kernel clients to wait on a semaphore.
718 semaphore_t semaphore
)
721 if (semaphore
== SEMAPHORE_NULL
)
722 return KERN_INVALID_ARGUMENT
;
724 return(semaphore_wait_internal(semaphore
,
726 (mach_timespec_t
*)0,
727 (void (*)(kern_return_t
))0));
731 * Trap: semaphore_wait_trap
733 * Trap version of semaphore wait. Called on behalf of user-level
739 struct semaphore_wait_trap_args
*args
)
741 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
747 semaphore_wait_trap_internal(
748 mach_port_name_t name
,
749 void (*caller_cont
)(kern_return_t
))
751 semaphore_t semaphore
;
754 kr
= port_name_to_semaphore(name
, &semaphore
);
755 if (kr
== KERN_SUCCESS
) {
756 kr
= semaphore_wait_internal(semaphore
,
758 (mach_timespec_t
*)0,
760 semaphore_dereference(semaphore
);
766 * Routine: semaphore_timedwait
768 * Traditional (non-continuation) interface presented to
769 * in-kernel clients to wait on a semaphore with a timeout.
771 * A timeout of {0,0} is considered non-blocking.
775 semaphore_t semaphore
,
776 mach_timespec_t wait_time
)
778 if (semaphore
== SEMAPHORE_NULL
)
779 return KERN_INVALID_ARGUMENT
;
781 if(BAD_MACH_TIMESPEC(&wait_time
))
782 return KERN_INVALID_VALUE
;
784 return (semaphore_wait_internal(semaphore
,
787 (void(*)(kern_return_t
))0));
792 * Trap: semaphore_timedwait_trap
794 * Trap version of a semaphore_timedwait. The timeout parameter
795 * is passed in two distinct parts and re-assembled on this side
796 * of the trap interface (to accomodate calling conventions that
797 * pass structures as pointers instead of inline in registers without
798 * having to add a copyin).
800 * A timeout of {0,0} is considered non-blocking.
803 semaphore_timedwait_trap(
804 struct semaphore_timedwait_trap_args
*args
)
807 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
812 semaphore_timedwait_trap_internal(
813 mach_port_name_t name
,
816 void (*caller_cont
)(kern_return_t
))
819 semaphore_t semaphore
;
820 mach_timespec_t wait_time
;
823 wait_time
.tv_sec
= sec
;
824 wait_time
.tv_nsec
= nsec
;
825 if(BAD_MACH_TIMESPEC(&wait_time
))
826 return KERN_INVALID_VALUE
;
828 kr
= port_name_to_semaphore(name
, &semaphore
);
829 if (kr
== KERN_SUCCESS
) {
830 kr
= semaphore_wait_internal(semaphore
,
834 semaphore_dereference(semaphore
);
840 * Routine: semaphore_wait_signal
842 * Atomically register a wait on a semaphore and THEN signal
843 * another. This is the in-kernel entry point that does not
844 * block at a continuation and does not free a signal_semaphore
848 semaphore_wait_signal(
849 semaphore_t wait_semaphore
,
850 semaphore_t signal_semaphore
)
852 if (wait_semaphore
== SEMAPHORE_NULL
)
853 return KERN_INVALID_ARGUMENT
;
855 return(semaphore_wait_internal(wait_semaphore
,
857 (mach_timespec_t
*)0,
858 (void(*)(kern_return_t
))0));
862 * Trap: semaphore_wait_signal_trap
864 * Atomically register a wait on a semaphore and THEN signal
865 * another. This is the trap version from user space.
868 semaphore_wait_signal_trap(
869 struct semaphore_wait_signal_trap_args
*args
)
871 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
875 semaphore_wait_signal_trap_internal(
876 mach_port_name_t wait_name
,
877 mach_port_name_t signal_name
,
878 void (*caller_cont
)(kern_return_t
))
880 semaphore_t wait_semaphore
;
881 semaphore_t signal_semaphore
;
884 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
885 if (kr
== KERN_SUCCESS
) {
886 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
887 if (kr
== KERN_SUCCESS
) {
888 kr
= semaphore_wait_internal(wait_semaphore
,
890 (mach_timespec_t
*)0,
892 semaphore_dereference(wait_semaphore
);
894 semaphore_dereference(signal_semaphore
);
901 * Routine: semaphore_timedwait_signal
903 * Atomically register a wait on a semaphore and THEN signal
904 * another. This is the in-kernel entry point that does not
905 * block at a continuation.
907 * A timeout of {0,0} is considered non-blocking.
910 semaphore_timedwait_signal(
911 semaphore_t wait_semaphore
,
912 semaphore_t signal_semaphore
,
913 mach_timespec_t wait_time
)
915 if (wait_semaphore
== SEMAPHORE_NULL
)
916 return KERN_INVALID_ARGUMENT
;
918 if(BAD_MACH_TIMESPEC(&wait_time
))
919 return KERN_INVALID_VALUE
;
921 return(semaphore_wait_internal(wait_semaphore
,
924 (void(*)(kern_return_t
))0));
928 * Trap: semaphore_timedwait_signal_trap
930 * Atomically register a timed wait on a semaphore and THEN signal
931 * another. This is the trap version from user space.
934 semaphore_timedwait_signal_trap(
935 struct semaphore_timedwait_signal_trap_args
*args
)
937 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
941 semaphore_timedwait_signal_trap_internal(
942 mach_port_name_t wait_name
,
943 mach_port_name_t signal_name
,
946 void (*caller_cont
)(kern_return_t
))
948 semaphore_t wait_semaphore
;
949 semaphore_t signal_semaphore
;
950 mach_timespec_t wait_time
;
953 wait_time
.tv_sec
= sec
;
954 wait_time
.tv_nsec
= nsec
;
955 if(BAD_MACH_TIMESPEC(&wait_time
))
956 return KERN_INVALID_VALUE
;
958 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
959 if (kr
== KERN_SUCCESS
) {
960 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
961 if (kr
== KERN_SUCCESS
) {
962 kr
= semaphore_wait_internal(wait_semaphore
,
966 semaphore_dereference(wait_semaphore
);
968 semaphore_dereference(signal_semaphore
);
975 * Routine: semaphore_reference
977 * Take out a reference on a semaphore. This keeps the data structure
978 * in existence (but the semaphore may be deactivated).
982 semaphore_t semaphore
)
986 spl_level
= splsched();
987 semaphore_lock(semaphore
);
989 semaphore
->ref_count
++;
991 semaphore_unlock(semaphore
);
996 * Routine: semaphore_dereference
998 * Release a reference on a semaphore. If this is the last reference,
999 * the semaphore data structure is deallocated.
1002 semaphore_dereference(
1003 semaphore_t semaphore
)
1008 if (semaphore
!= NULL
) {
1009 spl_level
= splsched();
1010 semaphore_lock(semaphore
);
1012 ref_count
= --(semaphore
->ref_count
);
1014 semaphore_unlock(semaphore
);
1017 if (ref_count
== 0) {
1018 assert(wait_queue_empty(&semaphore
->wait_queue
));
1019 zfree(semaphore_zone
, semaphore
);