xref: /onnv-gate/usr/src/uts/common/os/semaphore.c (revision 5891:0d5c6468bb04)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54319Skupfer  * Common Development and Distribution License (the "License").
64319Skupfer  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*5891Sraf 
220Sstevel@tonic-gate /*
23*5891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This file contains the semaphore operations.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/systm.h>
36*5891Sraf #include <sys/schedctl.h>
370Sstevel@tonic-gate #include <sys/semaphore.h>
380Sstevel@tonic-gate #include <sys/sema_impl.h>
390Sstevel@tonic-gate #include <sys/t_lock.h>
400Sstevel@tonic-gate #include <sys/thread.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/disp.h>
450Sstevel@tonic-gate #include <sys/sobject.h>
460Sstevel@tonic-gate #include <sys/cpuvar.h>
470Sstevel@tonic-gate #include <sys/sleepq.h>
480Sstevel@tonic-gate #include <sys/sdt.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void sema_unsleep(kthread_t *t);
510Sstevel@tonic-gate static void sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip);
520Sstevel@tonic-gate static kthread_t *sema_owner(ksema_t *);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
560Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static sobj_ops_t sema_sobj_ops = {
590Sstevel@tonic-gate 	SOBJ_SEMA, sema_owner, sema_unsleep, sema_change_pri
600Sstevel@tonic-gate };
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * SEMA_BLOCK(sema_impl_t *s, disp_lock_t *lockp)
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate #define	SEMA_BLOCK(s, lockp)						\
660Sstevel@tonic-gate 	{								\
670Sstevel@tonic-gate 		kthread_t	*tp;					\
680Sstevel@tonic-gate 		kthread_t	**tpp;					\
690Sstevel@tonic-gate 		pri_t		cpri;					\
700Sstevel@tonic-gate 		klwp_t	*lwp = ttolwp(curthread);			\
710Sstevel@tonic-gate 		ASSERT(THREAD_LOCK_HELD(curthread));			\
720Sstevel@tonic-gate 		ASSERT(curthread != CPU->cpu_idle_thread);		\
730Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);				\
740Sstevel@tonic-gate 		ASSERT(curthread->t_wchan0 == NULL);			\
750Sstevel@tonic-gate 		ASSERT(curthread->t_wchan == NULL);			\
760Sstevel@tonic-gate 		ASSERT(curthread->t_state == TS_ONPROC);		\
770Sstevel@tonic-gate 		CL_SLEEP(curthread);					\
780Sstevel@tonic-gate 		THREAD_SLEEP(curthread, lockp);				\
790Sstevel@tonic-gate 		curthread->t_wchan = (caddr_t)s;			\
800Sstevel@tonic-gate 		curthread->t_sobj_ops = &sema_sobj_ops;			\
810Sstevel@tonic-gate 		DTRACE_SCHED(sleep);					\
820Sstevel@tonic-gate 		if (lwp != NULL) {					\
830Sstevel@tonic-gate 			lwp->lwp_ru.nvcsw++;				\
840Sstevel@tonic-gate 			(void) new_mstate(curthread, LMS_SLEEP);	\
850Sstevel@tonic-gate 		}							\
860Sstevel@tonic-gate 		cpri = DISP_PRIO(curthread);				\
870Sstevel@tonic-gate 		tpp = &s->s_slpq;					\
880Sstevel@tonic-gate 		while ((tp = *tpp) != NULL) {				\
890Sstevel@tonic-gate 			if (cpri > DISP_PRIO(tp))			\
900Sstevel@tonic-gate 				break;					\
910Sstevel@tonic-gate 			tpp = &tp->t_link;				\
920Sstevel@tonic-gate 		}							\
930Sstevel@tonic-gate 		*tpp = curthread;					\
940Sstevel@tonic-gate 		curthread->t_link = tp;					\
950Sstevel@tonic-gate 		ASSERT(s->s_slpq != NULL);				\
964319Skupfer 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* ARGSUSED */
990Sstevel@tonic-gate void
sema_init(ksema_t * sp,unsigned count,char * name,ksema_type_t type,void * arg)1000Sstevel@tonic-gate sema_init(ksema_t *sp, unsigned count, char *name, ksema_type_t type, void *arg)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	((sema_impl_t *)sp)->s_count = count;
1030Sstevel@tonic-gate 	((sema_impl_t *)sp)->s_slpq = NULL;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate void
sema_destroy(ksema_t * sp)1070Sstevel@tonic-gate sema_destroy(ksema_t *sp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	ASSERT(((sema_impl_t *)sp)->s_slpq == NULL);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Put a thread on the sleep queue for this semaphore.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate static void
sema_queue(ksema_t * sp,kthread_t * t)1160Sstevel@tonic-gate sema_queue(ksema_t *sp, kthread_t *t)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	kthread_t	**tpp;
1190Sstevel@tonic-gate 	kthread_t	*tp;
1200Sstevel@tonic-gate 	pri_t		cpri;
1210Sstevel@tonic-gate 	sema_impl_t	*s;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1240Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
1250Sstevel@tonic-gate 	tpp = &s->s_slpq;
1260Sstevel@tonic-gate 	cpri = DISP_PRIO(t);
1270Sstevel@tonic-gate 	while ((tp = *tpp) != NULL) {
1280Sstevel@tonic-gate 		if (cpri > DISP_PRIO(tp))
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 		tpp = &tp->t_link;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	*tpp = t;
1330Sstevel@tonic-gate 	t->t_link = tp;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Remove a thread from the sleep queue for this
1380Sstevel@tonic-gate  * semaphore.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate static void
sema_dequeue(ksema_t * sp,kthread_t * t)1410Sstevel@tonic-gate sema_dequeue(ksema_t *sp, kthread_t *t)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	kthread_t	**tpp;
1440Sstevel@tonic-gate 	kthread_t	*tp;
1450Sstevel@tonic-gate 	sema_impl_t	*s;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1480Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
1490Sstevel@tonic-gate 	tpp = &s->s_slpq;
1500Sstevel@tonic-gate 	while ((tp = *tpp) != NULL) {
1510Sstevel@tonic-gate 		if (tp == t) {
1520Sstevel@tonic-gate 			*tpp = t->t_link;
1530Sstevel@tonic-gate 			t->t_link = NULL;
1540Sstevel@tonic-gate 			return;
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 		tpp = &tp->t_link;
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* ARGSUSED */
1610Sstevel@tonic-gate static kthread_t *
sema_owner(ksema_t * sp)1620Sstevel@tonic-gate sema_owner(ksema_t *sp)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	return ((kthread_t *)NULL);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Wakeup a thread sleeping on a semaphore, and put it
1690Sstevel@tonic-gate  * on the dispatch queue.
1700Sstevel@tonic-gate  * Called via SOBJ_UNSLEEP().
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate static void
sema_unsleep(kthread_t * t)1730Sstevel@tonic-gate sema_unsleep(kthread_t *t)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	kthread_t	**tpp;
1760Sstevel@tonic-gate 	kthread_t	*tp;
1770Sstevel@tonic-gate 	sema_impl_t	*s;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1800Sstevel@tonic-gate 	s = (sema_impl_t *)t->t_wchan;
1810Sstevel@tonic-gate 	tpp = &s->s_slpq;
1820Sstevel@tonic-gate 	while ((tp = *tpp) != NULL) {
1830Sstevel@tonic-gate 		if (tp == t) {
1840Sstevel@tonic-gate 			*tpp = t->t_link;
1850Sstevel@tonic-gate 			t->t_link = NULL;
1860Sstevel@tonic-gate 			t->t_sobj_ops = NULL;
1870Sstevel@tonic-gate 			t->t_wchan = NULL;
1880Sstevel@tonic-gate 			t->t_wchan0 = NULL;
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * Change thread to transition state and
1910Sstevel@tonic-gate 			 * drop the semaphore sleep queue lock.
1920Sstevel@tonic-gate 			 */
1930Sstevel@tonic-gate 			THREAD_TRANSITION(t);
1940Sstevel@tonic-gate 			CL_SETRUN(t);
1950Sstevel@tonic-gate 			return;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		tpp = &tp->t_link;
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * operations to perform when changing the priority
2030Sstevel@tonic-gate  * of a thread asleep on a semaphore.
2040Sstevel@tonic-gate  * Called via SOBJ_CHANGE_PRI() and SOBJ_CHANGE_EPRI().
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate static void
sema_change_pri(kthread_t * t,pri_t pri,pri_t * t_prip)2070Sstevel@tonic-gate sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	ksema_t *sp;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if ((sp = (ksema_t *)t->t_wchan) != NULL) {
2120Sstevel@tonic-gate 		sema_dequeue(sp, t);
2130Sstevel@tonic-gate 		*t_prip = pri;
2140Sstevel@tonic-gate 		sema_queue(sp, t);
2150Sstevel@tonic-gate 	} else
2160Sstevel@tonic-gate 		panic("sema_change_pri: %p not on sleep queue", (void *)t);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * the semaphore is granted when the semaphore's
2210Sstevel@tonic-gate  * count is greater than zero and blocks when equal
2220Sstevel@tonic-gate  * to zero.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate void
sema_p(ksema_t * sp)2250Sstevel@tonic-gate sema_p(ksema_t *sp)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	sema_impl_t	*s;
2280Sstevel@tonic-gate 	disp_lock_t	*sqlp;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
2310Sstevel@tonic-gate 	sqlp = &SQHASH(s)->sq_lock;
2320Sstevel@tonic-gate 	disp_lock_enter(sqlp);
2330Sstevel@tonic-gate 	ASSERT(s->s_count >= 0);
2340Sstevel@tonic-gate 	while (s->s_count == 0) {
2350Sstevel@tonic-gate 		if (panicstr) {
2360Sstevel@tonic-gate 			disp_lock_exit(sqlp);
2370Sstevel@tonic-gate 			return;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 		thread_lock_high(curthread);
2400Sstevel@tonic-gate 		SEMA_BLOCK(s, sqlp);
2410Sstevel@tonic-gate 		thread_unlock_nopreempt(curthread);
2420Sstevel@tonic-gate 		swtch();
2430Sstevel@tonic-gate 		disp_lock_enter(sqlp);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	s->s_count--;
2460Sstevel@tonic-gate 	disp_lock_exit(sqlp);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * similiar to sema_p except that it blocks at an interruptible
2510Sstevel@tonic-gate  * priority. if a signal is present then return 1 otherwise 0.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate int
sema_p_sig(ksema_t * sp)2540Sstevel@tonic-gate sema_p_sig(ksema_t *sp)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	kthread_t	*t = curthread;
2570Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
258*5891Sraf 	int		cancel_pending;
259*5891Sraf 	int		cancelled = 0;
2600Sstevel@tonic-gate 	sema_impl_t	*s;
2610Sstevel@tonic-gate 	disp_lock_t	*sqlp;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (lwp == NULL) {
2640Sstevel@tonic-gate 		sema_p(sp);
2650Sstevel@tonic-gate 		return (0);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
268*5891Sraf 	cancel_pending = schedctl_cancel_pending();
2690Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
2700Sstevel@tonic-gate 	sqlp = &SQHASH(s)->sq_lock;
2710Sstevel@tonic-gate 	disp_lock_enter(sqlp);
2720Sstevel@tonic-gate 	ASSERT(s->s_count >= 0);
2730Sstevel@tonic-gate 	while (s->s_count == 0) {
2740Sstevel@tonic-gate 		proc_t *p = ttoproc(t);
2750Sstevel@tonic-gate 		thread_lock_high(t);
2760Sstevel@tonic-gate 		t->t_flag |= T_WAKEABLE;
2770Sstevel@tonic-gate 		SEMA_BLOCK(s, sqlp);
2780Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
2790Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
2800Sstevel@tonic-gate 		thread_unlock_nopreempt(t);
281*5891Sraf 		if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
2820Sstevel@tonic-gate 			setrun(t);
2830Sstevel@tonic-gate 		swtch();
2840Sstevel@tonic-gate 		t->t_flag &= ~T_WAKEABLE;
285*5891Sraf 		if (ISSIG(t, FORREAL) || lwp->lwp_sysabort ||
286*5891Sraf 		    MUSTRETURN(p, t) || (cancelled = cancel_pending) != 0) {
2870Sstevel@tonic-gate 			kthread_t *sq, *tp;
2880Sstevel@tonic-gate 			lwp->lwp_asleep = 0;
2890Sstevel@tonic-gate 			lwp->lwp_sysabort = 0;
2900Sstevel@tonic-gate 			disp_lock_enter(sqlp);
2910Sstevel@tonic-gate 			sq = s->s_slpq;
2920Sstevel@tonic-gate 			/*
2930Sstevel@tonic-gate 			 * in case sema_v and interrupt happen
2940Sstevel@tonic-gate 			 * at the same time, we need to pass the
2950Sstevel@tonic-gate 			 * sema_v to the next thread.
2960Sstevel@tonic-gate 			 */
2970Sstevel@tonic-gate 			if ((sq != NULL) && (s->s_count > 0)) {
2980Sstevel@tonic-gate 				tp = sq;
2990Sstevel@tonic-gate 				ASSERT(THREAD_LOCK_HELD(tp));
3000Sstevel@tonic-gate 				sq = sq->t_link;
3010Sstevel@tonic-gate 				tp->t_link = NULL;
3020Sstevel@tonic-gate 				DTRACE_SCHED1(wakeup, kthread_t *, tp);
3030Sstevel@tonic-gate 				tp->t_sobj_ops = NULL;
3040Sstevel@tonic-gate 				tp->t_wchan = NULL;
3050Sstevel@tonic-gate 				ASSERT(tp->t_state == TS_SLEEP);
3060Sstevel@tonic-gate 				CL_WAKEUP(tp);
3070Sstevel@tonic-gate 				s->s_slpq = sq;
3080Sstevel@tonic-gate 				disp_lock_exit_high(sqlp);
3090Sstevel@tonic-gate 				thread_unlock(tp);
3100Sstevel@tonic-gate 			} else {
3110Sstevel@tonic-gate 				disp_lock_exit(sqlp);
3120Sstevel@tonic-gate 			}
313*5891Sraf 			if (cancelled)
314*5891Sraf 				schedctl_cancel_eintr();
3150Sstevel@tonic-gate 			return (1);
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 		lwp->lwp_asleep = 0;
3180Sstevel@tonic-gate 		disp_lock_enter(sqlp);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	s->s_count--;
3210Sstevel@tonic-gate 	disp_lock_exit(sqlp);
3220Sstevel@tonic-gate 	return (0);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * the semaphore's count is incremented by one. a blocked thread
3270Sstevel@tonic-gate  * is awakened and re-tries to acquire the semaphore.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate void
sema_v(ksema_t * sp)3300Sstevel@tonic-gate sema_v(ksema_t *sp)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	sema_impl_t 	*s;
3330Sstevel@tonic-gate 	kthread_t 	*sq, *tp;
3340Sstevel@tonic-gate 	disp_lock_t	*sqlp;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
3370Sstevel@tonic-gate 	sqlp = &SQHASH(s)->sq_lock;
3380Sstevel@tonic-gate 	disp_lock_enter(sqlp);
3390Sstevel@tonic-gate 	if (panicstr) {
3400Sstevel@tonic-gate 		disp_lock_exit(sqlp);
3410Sstevel@tonic-gate 		return;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	s->s_count++;
3440Sstevel@tonic-gate 	sq = s->s_slpq;
3450Sstevel@tonic-gate 	if (sq != NULL) {
3460Sstevel@tonic-gate 		tp = sq;
3470Sstevel@tonic-gate 		ASSERT(THREAD_LOCK_HELD(tp));
3480Sstevel@tonic-gate 		sq = sq->t_link;
3490Sstevel@tonic-gate 		tp->t_link = NULL;
3500Sstevel@tonic-gate 		DTRACE_SCHED1(wakeup, kthread_t *, tp);
3510Sstevel@tonic-gate 		tp->t_sobj_ops = NULL;
3520Sstevel@tonic-gate 		tp->t_wchan = NULL;
3530Sstevel@tonic-gate 		ASSERT(tp->t_state == TS_SLEEP);
3540Sstevel@tonic-gate 		CL_WAKEUP(tp);
3550Sstevel@tonic-gate 		s->s_slpq = sq;
3560Sstevel@tonic-gate 		disp_lock_exit_high(sqlp);
3570Sstevel@tonic-gate 		thread_unlock(tp);
3580Sstevel@tonic-gate 	} else {
3590Sstevel@tonic-gate 		disp_lock_exit(sqlp);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * try to acquire the semaphore. if the semaphore is greater than
3650Sstevel@tonic-gate  * zero, then the semaphore is granted and returns 1. otherwise
3660Sstevel@tonic-gate  * return 0.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate int
sema_tryp(ksema_t * sp)3690Sstevel@tonic-gate sema_tryp(ksema_t *sp)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	sema_impl_t	*s;
3720Sstevel@tonic-gate 	sleepq_head_t	*sqh;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	int	gotit = 0;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
3770Sstevel@tonic-gate 	sqh = SQHASH(s);
3780Sstevel@tonic-gate 	disp_lock_enter(&sqh->sq_lock);
3790Sstevel@tonic-gate 	if (s->s_count > 0) {
3800Sstevel@tonic-gate 		s->s_count--;
3810Sstevel@tonic-gate 		gotit = 1;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	disp_lock_exit(&sqh->sq_lock);
3840Sstevel@tonic-gate 	return (gotit);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate int
sema_held(ksema_t * sp)3880Sstevel@tonic-gate sema_held(ksema_t *sp)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	sema_impl_t	*s;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	s = (sema_impl_t *)sp;
3940Sstevel@tonic-gate 	if (panicstr)
3950Sstevel@tonic-gate 		return (1);
3960Sstevel@tonic-gate 	else
3970Sstevel@tonic-gate 		return (s->s_count <= 0);
3980Sstevel@tonic-gate }
399