17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5d39fefdfSkupfer * Common Development and Distribution License (the "License").
6d39fefdfSkupfer * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21a574db85Sraf
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*958ad110SJoyce McIntosh /*
28*958ad110SJoyce McIntosh * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
29*958ad110SJoyce McIntosh */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * This file contains the semaphore operations.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
38a574db85Sraf #include <sys/schedctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/semaphore.h>
407c478bd9Sstevel@tonic-gate #include <sys/sema_impl.h>
417c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
427c478bd9Sstevel@tonic-gate #include <sys/thread.h>
437c478bd9Sstevel@tonic-gate #include <sys/proc.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/disp.h>
477c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/sleepq.h>
507c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static void sema_unsleep(kthread_t *t);
537c478bd9Sstevel@tonic-gate static void sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip);
547c478bd9Sstevel@tonic-gate static kthread_t *sema_owner(ksema_t *);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate * The sobj_ops vector exports a set of functions needed when a thread
587c478bd9Sstevel@tonic-gate * is asleep on a synchronization object of this type.
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate static sobj_ops_t sema_sobj_ops = {
617c478bd9Sstevel@tonic-gate SOBJ_SEMA, sema_owner, sema_unsleep, sema_change_pri
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * SEMA_BLOCK(sema_impl_t *s, disp_lock_t *lockp)
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate #define SEMA_BLOCK(s, lockp) \
687c478bd9Sstevel@tonic-gate { \
697c478bd9Sstevel@tonic-gate kthread_t *tp; \
707c478bd9Sstevel@tonic-gate kthread_t **tpp; \
717c478bd9Sstevel@tonic-gate pri_t cpri; \
727c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); \
737c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(curthread)); \
747c478bd9Sstevel@tonic-gate ASSERT(curthread != CPU->cpu_idle_thread); \
757c478bd9Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0); \
767c478bd9Sstevel@tonic-gate ASSERT(curthread->t_wchan0 == NULL); \
777c478bd9Sstevel@tonic-gate ASSERT(curthread->t_wchan == NULL); \
787c478bd9Sstevel@tonic-gate ASSERT(curthread->t_state == TS_ONPROC); \
797c478bd9Sstevel@tonic-gate CL_SLEEP(curthread); \
807c478bd9Sstevel@tonic-gate THREAD_SLEEP(curthread, lockp); \
817c478bd9Sstevel@tonic-gate curthread->t_wchan = (caddr_t)s; \
827c478bd9Sstevel@tonic-gate curthread->t_sobj_ops = &sema_sobj_ops; \
837c478bd9Sstevel@tonic-gate DTRACE_SCHED(sleep); \
847c478bd9Sstevel@tonic-gate if (lwp != NULL) { \
857c478bd9Sstevel@tonic-gate lwp->lwp_ru.nvcsw++; \
867c478bd9Sstevel@tonic-gate (void) new_mstate(curthread, LMS_SLEEP); \
877c478bd9Sstevel@tonic-gate } \
887c478bd9Sstevel@tonic-gate cpri = DISP_PRIO(curthread); \
897c478bd9Sstevel@tonic-gate tpp = &s->s_slpq; \
907c478bd9Sstevel@tonic-gate while ((tp = *tpp) != NULL) { \
917c478bd9Sstevel@tonic-gate if (cpri > DISP_PRIO(tp)) \
927c478bd9Sstevel@tonic-gate break; \
937c478bd9Sstevel@tonic-gate tpp = &tp->t_link; \
947c478bd9Sstevel@tonic-gate } \
957c478bd9Sstevel@tonic-gate *tpp = curthread; \
967c478bd9Sstevel@tonic-gate curthread->t_link = tp; \
977c478bd9Sstevel@tonic-gate ASSERT(s->s_slpq != NULL); \
98d39fefdfSkupfer }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /* ARGSUSED */
1017c478bd9Sstevel@tonic-gate void
sema_init(ksema_t * sp,unsigned count,char * name,ksema_type_t type,void * arg)1027c478bd9Sstevel@tonic-gate sema_init(ksema_t *sp, unsigned count, char *name, ksema_type_t type, void *arg)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate ((sema_impl_t *)sp)->s_count = count;
1057c478bd9Sstevel@tonic-gate ((sema_impl_t *)sp)->s_slpq = NULL;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate void
sema_destroy(ksema_t * sp)1097c478bd9Sstevel@tonic-gate sema_destroy(ksema_t *sp)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate ASSERT(((sema_impl_t *)sp)->s_slpq == NULL);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * Put a thread on the sleep queue for this semaphore.
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate static void
sema_queue(ksema_t * sp,kthread_t * t)1187c478bd9Sstevel@tonic-gate sema_queue(ksema_t *sp, kthread_t *t)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate kthread_t **tpp;
1217c478bd9Sstevel@tonic-gate kthread_t *tp;
1227c478bd9Sstevel@tonic-gate pri_t cpri;
1237c478bd9Sstevel@tonic-gate sema_impl_t *s;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
1267c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
1277c478bd9Sstevel@tonic-gate tpp = &s->s_slpq;
1287c478bd9Sstevel@tonic-gate cpri = DISP_PRIO(t);
1297c478bd9Sstevel@tonic-gate while ((tp = *tpp) != NULL) {
1307c478bd9Sstevel@tonic-gate if (cpri > DISP_PRIO(tp))
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate tpp = &tp->t_link;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate *tpp = t;
1357c478bd9Sstevel@tonic-gate t->t_link = tp;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Remove a thread from the sleep queue for this
1407c478bd9Sstevel@tonic-gate * semaphore.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate static void
sema_dequeue(ksema_t * sp,kthread_t * t)1437c478bd9Sstevel@tonic-gate sema_dequeue(ksema_t *sp, kthread_t *t)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate kthread_t **tpp;
1467c478bd9Sstevel@tonic-gate kthread_t *tp;
1477c478bd9Sstevel@tonic-gate sema_impl_t *s;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
1507c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
1517c478bd9Sstevel@tonic-gate tpp = &s->s_slpq;
1527c478bd9Sstevel@tonic-gate while ((tp = *tpp) != NULL) {
1537c478bd9Sstevel@tonic-gate if (tp == t) {
1547c478bd9Sstevel@tonic-gate *tpp = t->t_link;
1557c478bd9Sstevel@tonic-gate t->t_link = NULL;
1567c478bd9Sstevel@tonic-gate return;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate tpp = &tp->t_link;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /* ARGSUSED */
1637c478bd9Sstevel@tonic-gate static kthread_t *
sema_owner(ksema_t * sp)1647c478bd9Sstevel@tonic-gate sema_owner(ksema_t *sp)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate return ((kthread_t *)NULL);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Wakeup a thread sleeping on a semaphore, and put it
1717c478bd9Sstevel@tonic-gate * on the dispatch queue.
1727c478bd9Sstevel@tonic-gate * Called via SOBJ_UNSLEEP().
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate static void
sema_unsleep(kthread_t * t)1757c478bd9Sstevel@tonic-gate sema_unsleep(kthread_t *t)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate kthread_t **tpp;
1787c478bd9Sstevel@tonic-gate kthread_t *tp;
1797c478bd9Sstevel@tonic-gate sema_impl_t *s;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
1827c478bd9Sstevel@tonic-gate s = (sema_impl_t *)t->t_wchan;
1837c478bd9Sstevel@tonic-gate tpp = &s->s_slpq;
1847c478bd9Sstevel@tonic-gate while ((tp = *tpp) != NULL) {
1857c478bd9Sstevel@tonic-gate if (tp == t) {
1867c478bd9Sstevel@tonic-gate *tpp = t->t_link;
1877c478bd9Sstevel@tonic-gate t->t_link = NULL;
1887c478bd9Sstevel@tonic-gate t->t_sobj_ops = NULL;
1897c478bd9Sstevel@tonic-gate t->t_wchan = NULL;
1907c478bd9Sstevel@tonic-gate t->t_wchan0 = NULL;
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Change thread to transition state and
1937c478bd9Sstevel@tonic-gate * drop the semaphore sleep queue lock.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate THREAD_TRANSITION(t);
1967c478bd9Sstevel@tonic-gate CL_SETRUN(t);
1977c478bd9Sstevel@tonic-gate return;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate tpp = &tp->t_link;
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * operations to perform when changing the priority
2057c478bd9Sstevel@tonic-gate * of a thread asleep on a semaphore.
2067c478bd9Sstevel@tonic-gate * Called via SOBJ_CHANGE_PRI() and SOBJ_CHANGE_EPRI().
2077c478bd9Sstevel@tonic-gate */
2087c478bd9Sstevel@tonic-gate static void
sema_change_pri(kthread_t * t,pri_t pri,pri_t * t_prip)2097c478bd9Sstevel@tonic-gate sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate ksema_t *sp;
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if ((sp = (ksema_t *)t->t_wchan) != NULL) {
2147c478bd9Sstevel@tonic-gate sema_dequeue(sp, t);
2157c478bd9Sstevel@tonic-gate *t_prip = pri;
2167c478bd9Sstevel@tonic-gate sema_queue(sp, t);
2177c478bd9Sstevel@tonic-gate } else
2187c478bd9Sstevel@tonic-gate panic("sema_change_pri: %p not on sleep queue", (void *)t);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * the semaphore is granted when the semaphore's
2237c478bd9Sstevel@tonic-gate * count is greater than zero and blocks when equal
2247c478bd9Sstevel@tonic-gate * to zero.
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate void
sema_p(ksema_t * sp)2277c478bd9Sstevel@tonic-gate sema_p(ksema_t *sp)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate sema_impl_t *s;
2307c478bd9Sstevel@tonic-gate disp_lock_t *sqlp;
2317c478bd9Sstevel@tonic-gate
232*958ad110SJoyce McIntosh /* no-op during panic */
233*958ad110SJoyce McIntosh if (panicstr)
234*958ad110SJoyce McIntosh return;
235*958ad110SJoyce McIntosh
2367c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
2377c478bd9Sstevel@tonic-gate sqlp = &SQHASH(s)->sq_lock;
2387c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
2397c478bd9Sstevel@tonic-gate ASSERT(s->s_count >= 0);
2407c478bd9Sstevel@tonic-gate while (s->s_count == 0) {
2417c478bd9Sstevel@tonic-gate thread_lock_high(curthread);
2427c478bd9Sstevel@tonic-gate SEMA_BLOCK(s, sqlp);
2437c478bd9Sstevel@tonic-gate thread_unlock_nopreempt(curthread);
2447c478bd9Sstevel@tonic-gate swtch();
2457c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate s->s_count--;
2487c478bd9Sstevel@tonic-gate disp_lock_exit(sqlp);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * similiar to sema_p except that it blocks at an interruptible
2537c478bd9Sstevel@tonic-gate * priority. if a signal is present then return 1 otherwise 0.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate int
sema_p_sig(ksema_t * sp)2567c478bd9Sstevel@tonic-gate sema_p_sig(ksema_t *sp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate kthread_t *t = curthread;
2597c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
260a574db85Sraf int cancel_pending;
261a574db85Sraf int cancelled = 0;
2627c478bd9Sstevel@tonic-gate sema_impl_t *s;
2637c478bd9Sstevel@tonic-gate disp_lock_t *sqlp;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (lwp == NULL) {
2667c478bd9Sstevel@tonic-gate sema_p(sp);
2677c478bd9Sstevel@tonic-gate return (0);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
270a574db85Sraf cancel_pending = schedctl_cancel_pending();
2717c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
2727c478bd9Sstevel@tonic-gate sqlp = &SQHASH(s)->sq_lock;
2737c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
2747c478bd9Sstevel@tonic-gate ASSERT(s->s_count >= 0);
2757c478bd9Sstevel@tonic-gate while (s->s_count == 0) {
2767c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t);
2777c478bd9Sstevel@tonic-gate thread_lock_high(t);
2787c478bd9Sstevel@tonic-gate t->t_flag |= T_WAKEABLE;
2797c478bd9Sstevel@tonic-gate SEMA_BLOCK(s, sqlp);
2807c478bd9Sstevel@tonic-gate lwp->lwp_asleep = 1;
2817c478bd9Sstevel@tonic-gate lwp->lwp_sysabort = 0;
2827c478bd9Sstevel@tonic-gate thread_unlock_nopreempt(t);
283a574db85Sraf if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
2847c478bd9Sstevel@tonic-gate setrun(t);
2857c478bd9Sstevel@tonic-gate swtch();
2867c478bd9Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
287a574db85Sraf if (ISSIG(t, FORREAL) || lwp->lwp_sysabort ||
288a574db85Sraf MUSTRETURN(p, t) || (cancelled = cancel_pending) != 0) {
2897c478bd9Sstevel@tonic-gate kthread_t *sq, *tp;
2907c478bd9Sstevel@tonic-gate lwp->lwp_asleep = 0;
2917c478bd9Sstevel@tonic-gate lwp->lwp_sysabort = 0;
2927c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
2937c478bd9Sstevel@tonic-gate sq = s->s_slpq;
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate * in case sema_v and interrupt happen
2967c478bd9Sstevel@tonic-gate * at the same time, we need to pass the
2977c478bd9Sstevel@tonic-gate * sema_v to the next thread.
2987c478bd9Sstevel@tonic-gate */
2997c478bd9Sstevel@tonic-gate if ((sq != NULL) && (s->s_count > 0)) {
3007c478bd9Sstevel@tonic-gate tp = sq;
3017c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(tp));
3027c478bd9Sstevel@tonic-gate sq = sq->t_link;
3037c478bd9Sstevel@tonic-gate tp->t_link = NULL;
3047c478bd9Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, tp);
3057c478bd9Sstevel@tonic-gate tp->t_sobj_ops = NULL;
3067c478bd9Sstevel@tonic-gate tp->t_wchan = NULL;
3077c478bd9Sstevel@tonic-gate ASSERT(tp->t_state == TS_SLEEP);
3087c478bd9Sstevel@tonic-gate CL_WAKEUP(tp);
3097c478bd9Sstevel@tonic-gate s->s_slpq = sq;
3107c478bd9Sstevel@tonic-gate disp_lock_exit_high(sqlp);
3117c478bd9Sstevel@tonic-gate thread_unlock(tp);
3127c478bd9Sstevel@tonic-gate } else {
3137c478bd9Sstevel@tonic-gate disp_lock_exit(sqlp);
3147c478bd9Sstevel@tonic-gate }
315a574db85Sraf if (cancelled)
316a574db85Sraf schedctl_cancel_eintr();
3177c478bd9Sstevel@tonic-gate return (1);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate lwp->lwp_asleep = 0;
3207c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate s->s_count--;
3237c478bd9Sstevel@tonic-gate disp_lock_exit(sqlp);
3247c478bd9Sstevel@tonic-gate return (0);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * the semaphore's count is incremented by one. a blocked thread
3297c478bd9Sstevel@tonic-gate * is awakened and re-tries to acquire the semaphore.
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate void
sema_v(ksema_t * sp)3327c478bd9Sstevel@tonic-gate sema_v(ksema_t *sp)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate sema_impl_t *s;
3357c478bd9Sstevel@tonic-gate kthread_t *sq, *tp;
3367c478bd9Sstevel@tonic-gate disp_lock_t *sqlp;
3377c478bd9Sstevel@tonic-gate
338*958ad110SJoyce McIntosh /* no-op during panic */
339*958ad110SJoyce McIntosh if (panicstr)
340*958ad110SJoyce McIntosh return;
341*958ad110SJoyce McIntosh
3427c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
3437c478bd9Sstevel@tonic-gate sqlp = &SQHASH(s)->sq_lock;
3447c478bd9Sstevel@tonic-gate disp_lock_enter(sqlp);
3457c478bd9Sstevel@tonic-gate s->s_count++;
3467c478bd9Sstevel@tonic-gate sq = s->s_slpq;
3477c478bd9Sstevel@tonic-gate if (sq != NULL) {
3487c478bd9Sstevel@tonic-gate tp = sq;
3497c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(tp));
3507c478bd9Sstevel@tonic-gate sq = sq->t_link;
3517c478bd9Sstevel@tonic-gate tp->t_link = NULL;
3527c478bd9Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, tp);
3537c478bd9Sstevel@tonic-gate tp->t_sobj_ops = NULL;
3547c478bd9Sstevel@tonic-gate tp->t_wchan = NULL;
3557c478bd9Sstevel@tonic-gate ASSERT(tp->t_state == TS_SLEEP);
3567c478bd9Sstevel@tonic-gate CL_WAKEUP(tp);
3577c478bd9Sstevel@tonic-gate s->s_slpq = sq;
3587c478bd9Sstevel@tonic-gate disp_lock_exit_high(sqlp);
3597c478bd9Sstevel@tonic-gate thread_unlock(tp);
3607c478bd9Sstevel@tonic-gate } else {
3617c478bd9Sstevel@tonic-gate disp_lock_exit(sqlp);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate * try to acquire the semaphore. if the semaphore is greater than
3677c478bd9Sstevel@tonic-gate * zero, then the semaphore is granted and returns 1. otherwise
3687c478bd9Sstevel@tonic-gate * return 0.
3697c478bd9Sstevel@tonic-gate */
3707c478bd9Sstevel@tonic-gate int
sema_tryp(ksema_t * sp)3717c478bd9Sstevel@tonic-gate sema_tryp(ksema_t *sp)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate sema_impl_t *s;
3747c478bd9Sstevel@tonic-gate sleepq_head_t *sqh;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate int gotit = 0;
3777c478bd9Sstevel@tonic-gate
378*958ad110SJoyce McIntosh /* no-op during panic */
379*958ad110SJoyce McIntosh if (panicstr)
380*958ad110SJoyce McIntosh return (1);
381*958ad110SJoyce McIntosh
3827c478bd9Sstevel@tonic-gate s = (sema_impl_t *)sp;
3837c478bd9Sstevel@tonic-gate sqh = SQHASH(s);
3847c478bd9Sstevel@tonic-gate disp_lock_enter(&sqh->sq_lock);
3857c478bd9Sstevel@tonic-gate if (s->s_count > 0) {
3867c478bd9Sstevel@tonic-gate s->s_count--;
3877c478bd9Sstevel@tonic-gate gotit = 1;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate disp_lock_exit(&sqh->sq_lock);
3907c478bd9Sstevel@tonic-gate return (gotit);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate int
sema_held(ksema_t * sp)3947c478bd9Sstevel@tonic-gate sema_held(ksema_t *sp)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate sema_impl_t *s;
3977c478bd9Sstevel@tonic-gate
398*958ad110SJoyce McIntosh /* no-op during panic */
3997c478bd9Sstevel@tonic-gate if (panicstr)
4007c478bd9Sstevel@tonic-gate return (1);
401*958ad110SJoyce McIntosh
402*958ad110SJoyce McIntosh s = (sema_impl_t *)sp;
4037c478bd9Sstevel@tonic-gate return (s->s_count <= 0);
4047c478bd9Sstevel@tonic-gate }
405