2 * Copyright (c) 2000-2004 Apple Computer, 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
));
98 semaphore_signal_internal(
99 semaphore_t semaphore
,
104 semaphore_convert_wait_result(
108 semaphore_wait_continue(void);
111 semaphore_wait_internal(
112 semaphore_t wait_semaphore
,
113 semaphore_t signal_semaphore
,
114 mach_timespec_t
*wait_timep
,
115 void (*caller_cont
)(kern_return_t
));
118 * ROUTINE: semaphore_init [private]
120 * Initialize the semaphore mechanisms.
121 * Right now, we only need to initialize the semaphore zone.
126 semaphore_zone
= zinit(sizeof(struct semaphore
),
127 semaphore_max
* sizeof(struct semaphore
),
128 sizeof(struct semaphore
),
133 * Routine: semaphore_create
135 * Creates a semaphore.
136 * The port representing the semaphore is returned as a parameter.
141 semaphore_t
*new_semaphore
,
145 semaphore_t s
= SEMAPHORE_NULL
;
149 if (task
== TASK_NULL
|| value
< 0 || policy
> SYNC_POLICY_MAX
) {
150 *new_semaphore
= SEMAPHORE_NULL
;
151 return KERN_INVALID_ARGUMENT
;
154 s
= (semaphore_t
) zalloc (semaphore_zone
);
156 if (s
== SEMAPHORE_NULL
) {
157 *new_semaphore
= SEMAPHORE_NULL
;
158 return KERN_RESOURCE_SHORTAGE
;
161 wait_queue_init(&s
->wait_queue
, policy
); /* also inits lock */
166 * Create and initialize the semaphore port
168 s
->port
= ipc_port_alloc_kernel();
169 if (s
->port
== IP_NULL
) {
170 /* This will deallocate the semaphore */
171 semaphore_dereference(s
);
172 *new_semaphore
= SEMAPHORE_NULL
;
173 return KERN_RESOURCE_SHORTAGE
;
176 ipc_kobject_set (s
->port
, (ipc_kobject_t
) s
, IKOT_SEMAPHORE
);
179 * Associate the new semaphore with the task by adding
180 * the new semaphore to the task's semaphore list.
182 * Associate the task with the new semaphore by having the
183 * semaphores task pointer point to the owning task's structure.
186 enqueue_head(&task
->semaphore_list
, (queue_entry_t
) s
);
187 task
->semaphores_owned
++;
198 * Routine: semaphore_destroy
200 * Destroys a semaphore. This call will only succeed if the
201 * specified task is the SAME task name specified at the semaphore's
204 * All threads currently blocked on the semaphore are awoken. These
205 * threads will return with the KERN_TERMINATED error.
210 semaphore_t semaphore
)
216 if (task
== TASK_NULL
|| semaphore
== SEMAPHORE_NULL
)
217 return KERN_INVALID_ARGUMENT
;
223 if (semaphore
->owner
!= task
) {
225 return KERN_INVALID_ARGUMENT
;
227 remqueue(&task
->semaphore_list
, (queue_entry_t
) semaphore
);
228 semaphore
->owner
= TASK_NULL
;
229 task
->semaphores_owned
--;
232 spl_level
= splsched();
233 semaphore_lock(semaphore
);
236 * Deactivate semaphore
238 assert(semaphore
->active
);
239 semaphore
->active
= FALSE
;
242 * Wakeup blocked threads
244 old_count
= semaphore
->count
;
245 semaphore
->count
= 0;
248 wait_queue_wakeup64_all_locked(&semaphore
->wait_queue
,
253 semaphore_unlock(semaphore
);
260 * Drop the semaphore reference, which in turn deallocates the
261 * semaphore structure if the reference count goes to zero.
263 ipc_port_dealloc_kernel(semaphore
->port
);
264 semaphore_dereference(semaphore
);
269 * Routine: semaphore_signal_internal
271 * Signals the semaphore as direct.
273 * Semaphore is locked.
276 semaphore_signal_internal(
277 semaphore_t semaphore
,
284 spl_level
= splsched();
285 semaphore_lock(semaphore
);
287 if (!semaphore
->active
) {
288 semaphore_unlock(semaphore
);
290 return KERN_TERMINATED
;
293 if (thread
!= THREAD_NULL
) {
294 if (semaphore
->count
< 0) {
295 kr
= wait_queue_wakeup64_thread_locked(
296 &semaphore
->wait_queue
,
302 semaphore_unlock(semaphore
);
303 kr
= KERN_NOT_WAITING
;
309 if (options
& SEMAPHORE_SIGNAL_ALL
) {
310 int old_count
= semaphore
->count
;
313 semaphore
->count
= 0; /* always reset */
314 kr
= wait_queue_wakeup64_all_locked(
315 &semaphore
->wait_queue
,
320 if (options
& SEMAPHORE_SIGNAL_PREPOST
)
322 semaphore_unlock(semaphore
);
329 if (semaphore
->count
< 0) {
330 if (wait_queue_wakeup64_one_locked(
331 &semaphore
->wait_queue
,
334 FALSE
) == KERN_SUCCESS
) {
335 semaphore_unlock(semaphore
);
339 semaphore
->count
= 0; /* all waiters gone */
342 if (options
& SEMAPHORE_SIGNAL_PREPOST
) {
346 semaphore_unlock(semaphore
);
348 return KERN_NOT_WAITING
;
352 * Routine: semaphore_signal_thread
354 * If the specified thread is blocked on the semaphore, it is
355 * woken up. If a NULL thread was supplied, then any one
356 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
357 * and the semaphore is unchanged.
360 semaphore_signal_thread(
361 semaphore_t semaphore
,
366 if (semaphore
== SEMAPHORE_NULL
)
367 return KERN_INVALID_ARGUMENT
;
369 ret
= semaphore_signal_internal(semaphore
,
371 SEMAPHORE_OPTION_NONE
);
376 * Routine: semaphore_signal_thread_trap
378 * Trap interface to the semaphore_signal_thread function.
381 semaphore_signal_thread_trap(
382 struct semaphore_signal_thread_trap_args
*args
)
384 mach_port_name_t sema_name
= args
->signal_name
;
385 mach_port_name_t thread_name
= args
->thread_name
;
386 semaphore_t semaphore
;
391 * MACH_PORT_NULL is not an error. It means that we want to
392 * select any one thread that is already waiting, but not to
393 * pre-post the semaphore.
395 if (thread_name
!= MACH_PORT_NULL
) {
396 thread
= port_name_to_thread(thread_name
);
397 if (thread
== THREAD_NULL
)
398 return KERN_INVALID_ARGUMENT
;
400 thread
= THREAD_NULL
;
402 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
403 if (kr
== KERN_SUCCESS
) {
404 kr
= semaphore_signal_internal(semaphore
,
406 SEMAPHORE_OPTION_NONE
);
407 semaphore_dereference(semaphore
);
409 if (thread
!= THREAD_NULL
) {
410 thread_deallocate(thread
);
418 * Routine: semaphore_signal
420 * Traditional (in-kernel client and MIG interface) semaphore
421 * signal routine. Most users will access the trap version.
423 * This interface in not defined to return info about whether
424 * this call found a thread waiting or not. The internal
425 * routines (and future external routines) do. We have to
426 * convert those into plain KERN_SUCCESS returns.
430 semaphore_t semaphore
)
434 if (semaphore
== SEMAPHORE_NULL
)
435 return KERN_INVALID_ARGUMENT
;
437 kr
= semaphore_signal_internal(semaphore
,
439 SEMAPHORE_SIGNAL_PREPOST
);
440 if (kr
== KERN_NOT_WAITING
)
446 * Routine: semaphore_signal_trap
448 * Trap interface to the semaphore_signal function.
451 semaphore_signal_trap(
452 struct semaphore_signal_trap_args
*args
)
454 mach_port_name_t sema_name
= args
->signal_name
;
455 semaphore_t semaphore
;
458 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
459 if (kr
== KERN_SUCCESS
) {
460 kr
= semaphore_signal_internal(semaphore
,
462 SEMAPHORE_SIGNAL_PREPOST
);
463 semaphore_dereference(semaphore
);
464 if (kr
== KERN_NOT_WAITING
)
471 * Routine: semaphore_signal_all
473 * Awakens ALL threads currently blocked on the semaphore.
474 * The semaphore count returns to zero.
477 semaphore_signal_all(
478 semaphore_t semaphore
)
482 if (semaphore
== SEMAPHORE_NULL
)
483 return KERN_INVALID_ARGUMENT
;
485 kr
= semaphore_signal_internal(semaphore
,
487 SEMAPHORE_SIGNAL_ALL
);
488 if (kr
== KERN_NOT_WAITING
)
494 * Routine: semaphore_signal_all_trap
496 * Trap interface to the semaphore_signal_all function.
499 semaphore_signal_all_trap(
500 struct semaphore_signal_all_trap_args
*args
)
502 mach_port_name_t sema_name
= args
->signal_name
;
503 semaphore_t semaphore
;
506 kr
= port_name_to_semaphore(sema_name
, &semaphore
);
507 if (kr
== KERN_SUCCESS
) {
508 kr
= semaphore_signal_internal(semaphore
,
510 SEMAPHORE_SIGNAL_ALL
);
511 semaphore_dereference(semaphore
);
512 if (kr
== KERN_NOT_WAITING
)
519 * Routine: semaphore_convert_wait_result
521 * Generate the return code after a semaphore wait/block. It
522 * takes the wait result as an input and coverts that to an
523 * appropriate result.
526 semaphore_convert_wait_result(int wait_result
)
528 switch (wait_result
) {
529 case THREAD_AWAKENED
:
532 case THREAD_TIMED_OUT
:
533 return KERN_OPERATION_TIMED_OUT
;
535 case THREAD_INTERRUPTED
:
539 return KERN_TERMINATED
;
542 panic("semaphore_block\n");
548 * Routine: semaphore_wait_continue
550 * Common continuation routine after waiting on a semphore.
551 * It returns directly to user space.
554 semaphore_wait_continue(void)
556 thread_t self
= current_thread();
557 int wait_result
= self
->wait_result
;
558 void (*caller_cont
)(kern_return_t
) = self
->sth_continuation
;
560 assert(self
->sth_waitsemaphore
!= SEMAPHORE_NULL
);
561 semaphore_dereference(self
->sth_waitsemaphore
);
562 if (self
->sth_signalsemaphore
!= SEMAPHORE_NULL
)
563 semaphore_dereference(self
->sth_signalsemaphore
);
565 assert(caller_cont
!= (void (*)(kern_return_t
))0);
566 (*caller_cont
)(semaphore_convert_wait_result(wait_result
));
570 * Routine: semaphore_wait_internal
572 * Decrements the semaphore count by one. If the count is
573 * negative after the decrement, the calling thread blocks
574 * (possibly at a continuation and/or with a timeout).
578 * A reference is held on the signal semaphore.
581 semaphore_wait_internal(
582 semaphore_t wait_semaphore
,
583 semaphore_t signal_semaphore
,
584 mach_timespec_t
*wait_timep
,
585 void (*caller_cont
)(kern_return_t
))
587 boolean_t nonblocking
;
590 kern_return_t kr
= KERN_ALREADY_WAITING
;
592 spl_level
= splsched();
593 semaphore_lock(wait_semaphore
);
596 * Decide if we really have to wait.
598 nonblocking
= (wait_timep
!= (mach_timespec_t
*)0) ?
599 (wait_timep
->tv_sec
== 0 && wait_timep
->tv_nsec
== 0) :
602 if (!wait_semaphore
->active
) {
603 kr
= KERN_TERMINATED
;
604 } else if (wait_semaphore
->count
> 0) {
605 wait_semaphore
->count
--;
607 } else if (nonblocking
) {
608 kr
= KERN_OPERATION_TIMED_OUT
;
611 thread_t self
= current_thread();
613 wait_semaphore
->count
= -1; /* we don't keep an actual count */
617 * If it is a timed wait, calculate the wake up deadline.
619 if (wait_timep
!= (mach_timespec_t
*)0) {
620 nanoseconds_to_absolutetime((uint64_t)wait_timep
->tv_sec
*
621 NSEC_PER_SEC
+ wait_timep
->tv_nsec
, &abstime
);
622 clock_absolutetime_interval_to_deadline(abstime
, &abstime
);
627 (void)wait_queue_assert_wait64_locked(
628 &wait_semaphore
->wait_queue
,
630 THREAD_ABORTSAFE
, abstime
,
634 semaphore_unlock(wait_semaphore
);
638 * wait_semaphore is unlocked so we are free to go ahead and
639 * signal the signal_semaphore (if one was provided).
641 if (signal_semaphore
!= SEMAPHORE_NULL
) {
642 kern_return_t signal_kr
;
645 * lock the signal semaphore reference we got and signal it.
646 * This will NOT block (we cannot block after having asserted
647 * our intention to wait above).
649 signal_kr
= semaphore_signal_internal(signal_semaphore
,
651 SEMAPHORE_SIGNAL_PREPOST
);
653 if (signal_kr
== KERN_NOT_WAITING
)
654 signal_kr
= KERN_SUCCESS
;
655 else if (signal_kr
== KERN_TERMINATED
) {
657 * Uh!Oh! The semaphore we were to signal died.
658 * We have to get ourselves out of the wait in
659 * case we get stuck here forever (it is assumed
660 * that the semaphore we were posting is gating
661 * the decision by someone else to post the
662 * semaphore we are waiting on). People will
663 * discover the other dead semaphore soon enough.
664 * If we got out of the wait cleanly (someone
665 * already posted a wakeup to us) then return that
666 * (most important) result. Otherwise,
667 * return the KERN_TERMINATED status.
669 thread_t self
= current_thread();
671 clear_wait(self
, THREAD_INTERRUPTED
);
672 kr
= semaphore_convert_wait_result(self
->wait_result
);
673 if (kr
== KERN_ABORTED
)
674 kr
= KERN_TERMINATED
;
679 * If we had an error, or we didn't really need to wait we can
680 * return now that we have signalled the signal semaphore.
682 if (kr
!= KERN_ALREADY_WAITING
)
686 * Now, we can block. If the caller supplied a continuation
687 * pointer of his own for after the block, block with the
688 * appropriate semaphore continuation. Thiswill gather the
689 * semaphore results, release references on the semaphore(s),
690 * and then call the caller's continuation.
693 thread_t self
= current_thread();
695 self
->sth_continuation
= caller_cont
;
696 self
->sth_waitsemaphore
= wait_semaphore
;
697 self
->sth_signalsemaphore
= signal_semaphore
;
698 wait_result
= thread_block((thread_continue_t
)semaphore_wait_continue
);
701 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
704 return (semaphore_convert_wait_result(wait_result
));
709 * Routine: semaphore_wait
711 * Traditional (non-continuation) interface presented to
712 * in-kernel clients to wait on a semaphore.
716 semaphore_t semaphore
)
719 if (semaphore
== SEMAPHORE_NULL
)
720 return KERN_INVALID_ARGUMENT
;
722 return(semaphore_wait_internal(semaphore
,
724 (mach_timespec_t
*)0,
725 (void (*)(kern_return_t
))0));
729 * Trap: semaphore_wait_trap
731 * Trap version of semaphore wait. Called on behalf of user-level
737 struct semaphore_wait_trap_args
*args
)
739 return(semaphore_wait_trap_internal(args
->wait_name
, thread_syscall_return
));
745 semaphore_wait_trap_internal(
746 mach_port_name_t name
,
747 void (*caller_cont
)(kern_return_t
))
749 semaphore_t semaphore
;
752 kr
= port_name_to_semaphore(name
, &semaphore
);
753 if (kr
== KERN_SUCCESS
) {
754 kr
= semaphore_wait_internal(semaphore
,
756 (mach_timespec_t
*)0,
758 semaphore_dereference(semaphore
);
764 * Routine: semaphore_timedwait
766 * Traditional (non-continuation) interface presented to
767 * in-kernel clients to wait on a semaphore with a timeout.
769 * A timeout of {0,0} is considered non-blocking.
773 semaphore_t semaphore
,
774 mach_timespec_t wait_time
)
776 if (semaphore
== SEMAPHORE_NULL
)
777 return KERN_INVALID_ARGUMENT
;
779 if(BAD_MACH_TIMESPEC(&wait_time
))
780 return KERN_INVALID_VALUE
;
782 return (semaphore_wait_internal(semaphore
,
785 (void(*)(kern_return_t
))0));
790 * Trap: semaphore_timedwait_trap
792 * Trap version of a semaphore_timedwait. The timeout parameter
793 * is passed in two distinct parts and re-assembled on this side
794 * of the trap interface (to accomodate calling conventions that
795 * pass structures as pointers instead of inline in registers without
796 * having to add a copyin).
798 * A timeout of {0,0} is considered non-blocking.
801 semaphore_timedwait_trap(
802 struct semaphore_timedwait_trap_args
*args
)
805 return(semaphore_timedwait_trap_internal(args
->wait_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
810 semaphore_timedwait_trap_internal(
811 mach_port_name_t name
,
814 void (*caller_cont
)(kern_return_t
))
817 semaphore_t semaphore
;
818 mach_timespec_t wait_time
;
821 wait_time
.tv_sec
= sec
;
822 wait_time
.tv_nsec
= nsec
;
823 if(BAD_MACH_TIMESPEC(&wait_time
))
824 return KERN_INVALID_VALUE
;
826 kr
= port_name_to_semaphore(name
, &semaphore
);
827 if (kr
== KERN_SUCCESS
) {
828 kr
= semaphore_wait_internal(semaphore
,
832 semaphore_dereference(semaphore
);
838 * Routine: semaphore_wait_signal
840 * Atomically register a wait on a semaphore and THEN signal
841 * another. This is the in-kernel entry point that does not
842 * block at a continuation and does not free a signal_semaphore
846 semaphore_wait_signal(
847 semaphore_t wait_semaphore
,
848 semaphore_t signal_semaphore
)
850 if (wait_semaphore
== SEMAPHORE_NULL
)
851 return KERN_INVALID_ARGUMENT
;
853 return(semaphore_wait_internal(wait_semaphore
,
855 (mach_timespec_t
*)0,
856 (void(*)(kern_return_t
))0));
860 * Trap: semaphore_wait_signal_trap
862 * Atomically register a wait on a semaphore and THEN signal
863 * another. This is the trap version from user space.
866 semaphore_wait_signal_trap(
867 struct semaphore_wait_signal_trap_args
*args
)
869 return(semaphore_wait_signal_trap_internal(args
->wait_name
, args
->signal_name
, thread_syscall_return
));
873 semaphore_wait_signal_trap_internal(
874 mach_port_name_t wait_name
,
875 mach_port_name_t signal_name
,
876 void (*caller_cont
)(kern_return_t
))
878 semaphore_t wait_semaphore
;
879 semaphore_t signal_semaphore
;
882 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
883 if (kr
== KERN_SUCCESS
) {
884 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
885 if (kr
== KERN_SUCCESS
) {
886 kr
= semaphore_wait_internal(wait_semaphore
,
888 (mach_timespec_t
*)0,
890 semaphore_dereference(wait_semaphore
);
892 semaphore_dereference(signal_semaphore
);
899 * Routine: semaphore_timedwait_signal
901 * Atomically register a wait on a semaphore and THEN signal
902 * another. This is the in-kernel entry point that does not
903 * block at a continuation.
905 * A timeout of {0,0} is considered non-blocking.
908 semaphore_timedwait_signal(
909 semaphore_t wait_semaphore
,
910 semaphore_t signal_semaphore
,
911 mach_timespec_t wait_time
)
913 if (wait_semaphore
== SEMAPHORE_NULL
)
914 return KERN_INVALID_ARGUMENT
;
916 if(BAD_MACH_TIMESPEC(&wait_time
))
917 return KERN_INVALID_VALUE
;
919 return(semaphore_wait_internal(wait_semaphore
,
922 (void(*)(kern_return_t
))0));
926 * Trap: semaphore_timedwait_signal_trap
928 * Atomically register a timed wait on a semaphore and THEN signal
929 * another. This is the trap version from user space.
932 semaphore_timedwait_signal_trap(
933 struct semaphore_timedwait_signal_trap_args
*args
)
935 return(semaphore_timedwait_signal_trap_internal(args
->wait_name
, args
->signal_name
, args
->sec
, args
->nsec
, thread_syscall_return
));
939 semaphore_timedwait_signal_trap_internal(
940 mach_port_name_t wait_name
,
941 mach_port_name_t signal_name
,
944 void (*caller_cont
)(kern_return_t
))
946 semaphore_t wait_semaphore
;
947 semaphore_t signal_semaphore
;
948 mach_timespec_t wait_time
;
951 wait_time
.tv_sec
= sec
;
952 wait_time
.tv_nsec
= nsec
;
953 if(BAD_MACH_TIMESPEC(&wait_time
))
954 return KERN_INVALID_VALUE
;
956 kr
= port_name_to_semaphore(signal_name
, &signal_semaphore
);
957 if (kr
== KERN_SUCCESS
) {
958 kr
= port_name_to_semaphore(wait_name
, &wait_semaphore
);
959 if (kr
== KERN_SUCCESS
) {
960 kr
= semaphore_wait_internal(wait_semaphore
,
964 semaphore_dereference(wait_semaphore
);
966 semaphore_dereference(signal_semaphore
);
973 * Routine: semaphore_reference
975 * Take out a reference on a semaphore. This keeps the data structure
976 * in existence (but the semaphore may be deactivated).
980 semaphore_t semaphore
)
984 spl_level
= splsched();
985 semaphore_lock(semaphore
);
987 semaphore
->ref_count
++;
989 semaphore_unlock(semaphore
);
994 * Routine: semaphore_dereference
996 * Release a reference on a semaphore. If this is the last reference,
997 * the semaphore data structure is deallocated.
1000 semaphore_dereference(
1001 semaphore_t semaphore
)
1006 if (semaphore
!= NULL
) {
1007 spl_level
= splsched();
1008 semaphore_lock(semaphore
);
1010 ref_count
= --(semaphore
->ref_count
);
1012 semaphore_unlock(semaphore
);
1015 if (ref_count
== 0) {
1016 assert(wait_queue_empty(&semaphore
->wait_queue
));
1017 zfree(semaphore_zone
, semaphore
);