1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include "lint.h" 30*7c478bd9Sstevel@tonic-gate #include "thr_uberdata.h" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate static uint32_t _semvaluemax; 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * Check to see if anyone is waiting for this semaphore. 36*7c478bd9Sstevel@tonic-gate */ 37*7c478bd9Sstevel@tonic-gate #pragma weak sema_held = _sema_held 38*7c478bd9Sstevel@tonic-gate int 39*7c478bd9Sstevel@tonic-gate _sema_held(sema_t *sp) 40*7c478bd9Sstevel@tonic-gate { 41*7c478bd9Sstevel@tonic-gate return (sp->count == 0); 42*7c478bd9Sstevel@tonic-gate } 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #pragma weak sema_init = _sema_init 45*7c478bd9Sstevel@tonic-gate /* ARGSUSED2 */ 46*7c478bd9Sstevel@tonic-gate int 47*7c478bd9Sstevel@tonic-gate _sema_init(sema_t *sp, unsigned int count, int type, void *arg) 48*7c478bd9Sstevel@tonic-gate { 49*7c478bd9Sstevel@tonic-gate if (_semvaluemax == 0) 50*7c478bd9Sstevel@tonic-gate _semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX); 51*7c478bd9Sstevel@tonic-gate if ((type != USYNC_THREAD && type != USYNC_PROCESS) || 52*7c478bd9Sstevel@tonic-gate (count > _semvaluemax)) 53*7c478bd9Sstevel@tonic-gate return (EINVAL); 54*7c478bd9Sstevel@tonic-gate (void) _memset(sp, 0, sizeof (*sp)); 55*7c478bd9Sstevel@tonic-gate sp->count = count; 56*7c478bd9Sstevel@tonic-gate sp->type = (uint16_t)type; 57*7c478bd9Sstevel@tonic-gate sp->magic = SEMA_MAGIC; 58*7c478bd9Sstevel@tonic-gate return (0); 59*7c478bd9Sstevel@tonic-gate } 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #pragma weak sema_destroy = _sema_destroy 62*7c478bd9Sstevel@tonic-gate int 63*7c478bd9Sstevel@tonic-gate _sema_destroy(sema_t *sp) 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate sp->magic = 0; 66*7c478bd9Sstevel@tonic-gate tdb_sync_obj_deregister(sp); 67*7c478bd9Sstevel@tonic-gate return (0); 68*7c478bd9Sstevel@tonic-gate } 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate static int 71*7c478bd9Sstevel@tonic-gate sema_wait_impl(sema_t *sp, timespec_t *tsp) 72*7c478bd9Sstevel@tonic-gate { 73*7c478bd9Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp; 74*7c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 75*7c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 76*7c478bd9Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp); 77*7c478bd9Sstevel@tonic-gate hrtime_t begin_sleep = 0; 78*7c478bd9Sstevel@tonic-gate uint_t count; 79*7c478bd9Sstevel@tonic-gate int error = 0; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * All variations of sema_wait() are cancellation points. 83*7c478bd9Sstevel@tonic-gate */ 84*7c478bd9Sstevel@tonic-gate _cancelon(); 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate if (ssp) 87*7c478bd9Sstevel@tonic-gate tdb_incr(ssp->sema_wait); 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate self->ul_sp = stkptr(); 90*7c478bd9Sstevel@tonic-gate self->ul_wchan = lsp; 91*7c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_SLEEP, udp)) { 92*7c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_SLEEP; 93*7c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventdata = lsp; 94*7c478bd9Sstevel@tonic-gate tdb_event(TD_SLEEP, udp); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate /* just a guess, but it looks like we will sleep */ 97*7c478bd9Sstevel@tonic-gate if (ssp && lsp->count == 0) { 98*7c478bd9Sstevel@tonic-gate begin_sleep = gethrtime(); 99*7c478bd9Sstevel@tonic-gate if (lsp->count == 0) /* still looks like sleep */ 100*7c478bd9Sstevel@tonic-gate tdb_incr(ssp->sema_wait_sleep); 101*7c478bd9Sstevel@tonic-gate else /* we changed our mind */ 102*7c478bd9Sstevel@tonic-gate begin_sleep = 0; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */ 106*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 1); 107*7c478bd9Sstevel@tonic-gate if (self->ul_cursig != 0 || 108*7c478bd9Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending)) 109*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 110*7c478bd9Sstevel@tonic-gate /* the kernel always does FIFO queueing */ 111*7c478bd9Sstevel@tonic-gate error = ___lwp_sema_timedwait(lsp, tsp, 1); 112*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 113*7c478bd9Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt && /* single threaded */ 114*7c478bd9Sstevel@tonic-gate lsp->count != 0) { /* and non-blocking */ 115*7c478bd9Sstevel@tonic-gate /* 116*7c478bd9Sstevel@tonic-gate * Since we are single-threaded, we don't need the 117*7c478bd9Sstevel@tonic-gate * protection of queue_lock(). However, we do need 118*7c478bd9Sstevel@tonic-gate * to block signals while modifying the count. 119*7c478bd9Sstevel@tonic-gate */ 120*7c478bd9Sstevel@tonic-gate sigoff(self); 121*7c478bd9Sstevel@tonic-gate lsp->count--; 122*7c478bd9Sstevel@tonic-gate sigon(self); 123*7c478bd9Sstevel@tonic-gate } else { /* multithreaded or blocking */ 124*7c478bd9Sstevel@tonic-gate queue_head_t *qp; 125*7c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 126*7c478bd9Sstevel@tonic-gate int more; 127*7c478bd9Sstevel@tonic-gate lwpid_t lwpid = 0; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate qp = queue_lock(lsp, CV); 130*7c478bd9Sstevel@tonic-gate while (error == 0 && lsp->count == 0) { 131*7c478bd9Sstevel@tonic-gate /* 132*7c478bd9Sstevel@tonic-gate * SUSV3 requires FIFO queueing for semaphores, 133*7c478bd9Sstevel@tonic-gate * at least for SCHED_FIFO and SCHED_RR scheduling. 134*7c478bd9Sstevel@tonic-gate */ 135*7c478bd9Sstevel@tonic-gate enqueue(qp, self, lsp, CV | FIFOQ); 136*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = 1; 137*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 1); 138*7c478bd9Sstevel@tonic-gate queue_unlock(qp); 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * We may have received SIGCANCEL before we 141*7c478bd9Sstevel@tonic-gate * called queue_lock(). If so and we are 142*7c478bd9Sstevel@tonic-gate * cancelable we should return EINTR. 143*7c478bd9Sstevel@tonic-gate */ 144*7c478bd9Sstevel@tonic-gate if (self->ul_cursig != 0 || 145*7c478bd9Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending)) 146*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 147*7c478bd9Sstevel@tonic-gate error = __lwp_park(tsp, 0); 148*7c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 149*7c478bd9Sstevel@tonic-gate qp = queue_lock(lsp, CV); 150*7c478bd9Sstevel@tonic-gate if (self->ul_sleepq) /* timeout or spurious wakeup */ 151*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = dequeue_self(qp, lsp); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate if (error == 0) 154*7c478bd9Sstevel@tonic-gate lsp->count--; 155*7c478bd9Sstevel@tonic-gate if (lsp->count != 0 && lsp->sema_waiters) { 156*7c478bd9Sstevel@tonic-gate if ((ulwp = dequeue(qp, lsp, &more)) == NULL) 157*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = 0; 158*7c478bd9Sstevel@tonic-gate else { 159*7c478bd9Sstevel@tonic-gate no_preempt(self); 160*7c478bd9Sstevel@tonic-gate lwpid = ulwp->ul_lwpid; 161*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = (more? 1 : 0); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate queue_unlock(qp); 165*7c478bd9Sstevel@tonic-gate if (lwpid) { 166*7c478bd9Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 167*7c478bd9Sstevel@tonic-gate preempt(self); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate self->ul_wchan = NULL; 172*7c478bd9Sstevel@tonic-gate self->ul_sp = 0; 173*7c478bd9Sstevel@tonic-gate if (ssp) { 174*7c478bd9Sstevel@tonic-gate if (error == 0) { 175*7c478bd9Sstevel@tonic-gate /* we just decremented the count */ 176*7c478bd9Sstevel@tonic-gate count = lsp->count; 177*7c478bd9Sstevel@tonic-gate if (ssp->sema_min_count > count) 178*7c478bd9Sstevel@tonic-gate ssp->sema_min_count = count; 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate if (begin_sleep) 181*7c478bd9Sstevel@tonic-gate ssp->sema_wait_sleep_time += gethrtime() - begin_sleep; 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate if (error == EINTR) 185*7c478bd9Sstevel@tonic-gate _canceloff(); 186*7c478bd9Sstevel@tonic-gate else 187*7c478bd9Sstevel@tonic-gate _canceloff_nocancel(); 188*7c478bd9Sstevel@tonic-gate return (error); 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate #pragma weak sema_wait = _sema_wait 192*7c478bd9Sstevel@tonic-gate int 193*7c478bd9Sstevel@tonic-gate _sema_wait(sema_t *sp) 194*7c478bd9Sstevel@tonic-gate { 195*7c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 196*7c478bd9Sstevel@tonic-gate return (sema_wait_impl(sp, NULL)); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate #pragma weak sema_reltimedwait = _sema_reltimedwait 200*7c478bd9Sstevel@tonic-gate int 201*7c478bd9Sstevel@tonic-gate _sema_reltimedwait(sema_t *sp, timespec_t *reltime) 202*7c478bd9Sstevel@tonic-gate { 203*7c478bd9Sstevel@tonic-gate timespec_t tslocal = *reltime; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 206*7c478bd9Sstevel@tonic-gate return (sema_wait_impl(sp, &tslocal)); 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate #pragma weak sema_timedwait = _sema_timedwait 210*7c478bd9Sstevel@tonic-gate int 211*7c478bd9Sstevel@tonic-gate _sema_timedwait(sema_t *sp, timespec_t *abstime) 212*7c478bd9Sstevel@tonic-gate { 213*7c478bd9Sstevel@tonic-gate timespec_t tslocal; 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 216*7c478bd9Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 217*7c478bd9Sstevel@tonic-gate return (sema_wait_impl(sp, &tslocal)); 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate #pragma weak sema_trywait = _sema_trywait 221*7c478bd9Sstevel@tonic-gate int 222*7c478bd9Sstevel@tonic-gate _sema_trywait(sema_t *sp) 223*7c478bd9Sstevel@tonic-gate { 224*7c478bd9Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp; 225*7c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 226*7c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 227*7c478bd9Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp); 228*7c478bd9Sstevel@tonic-gate uint_t count; 229*7c478bd9Sstevel@tonic-gate int error = 0; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate if (ssp) 234*7c478bd9Sstevel@tonic-gate tdb_incr(ssp->sema_trywait); 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */ 237*7c478bd9Sstevel@tonic-gate error = __lwp_sema_trywait(lsp); 238*7c478bd9Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt) { /* single threaded */ 239*7c478bd9Sstevel@tonic-gate sigoff(self); 240*7c478bd9Sstevel@tonic-gate if (lsp->count == 0) 241*7c478bd9Sstevel@tonic-gate error = EBUSY; 242*7c478bd9Sstevel@tonic-gate else 243*7c478bd9Sstevel@tonic-gate lsp->count--; 244*7c478bd9Sstevel@tonic-gate sigon(self); 245*7c478bd9Sstevel@tonic-gate } else { /* multithreaded */ 246*7c478bd9Sstevel@tonic-gate queue_head_t *qp; 247*7c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 248*7c478bd9Sstevel@tonic-gate int more; 249*7c478bd9Sstevel@tonic-gate lwpid_t lwpid = 0; 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate qp = queue_lock(lsp, CV); 252*7c478bd9Sstevel@tonic-gate if (lsp->count == 0) 253*7c478bd9Sstevel@tonic-gate error = EBUSY; 254*7c478bd9Sstevel@tonic-gate else if (--lsp->count != 0 && lsp->sema_waiters) { 255*7c478bd9Sstevel@tonic-gate if ((ulwp = dequeue(qp, lsp, &more)) == NULL) 256*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = 0; 257*7c478bd9Sstevel@tonic-gate else { 258*7c478bd9Sstevel@tonic-gate no_preempt(self); 259*7c478bd9Sstevel@tonic-gate lwpid = ulwp->ul_lwpid; 260*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = (more? 1 : 0); 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate queue_unlock(qp); 264*7c478bd9Sstevel@tonic-gate if (lwpid) { 265*7c478bd9Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 266*7c478bd9Sstevel@tonic-gate preempt(self); 267*7c478bd9Sstevel@tonic-gate } 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate if (error == 0) { 271*7c478bd9Sstevel@tonic-gate if (ssp) { 272*7c478bd9Sstevel@tonic-gate /* we just decremented the count */ 273*7c478bd9Sstevel@tonic-gate count = lsp->count; 274*7c478bd9Sstevel@tonic-gate if (ssp->sema_min_count > count) 275*7c478bd9Sstevel@tonic-gate ssp->sema_min_count = count; 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate } else { 278*7c478bd9Sstevel@tonic-gate if (ssp) 279*7c478bd9Sstevel@tonic-gate tdb_incr(ssp->sema_trywait_fail); 280*7c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_LOCK_TRY, udp)) { 281*7c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_LOCK_TRY; 282*7c478bd9Sstevel@tonic-gate tdb_event(TD_LOCK_TRY, udp); 283*7c478bd9Sstevel@tonic-gate } 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate return (error); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate #pragma weak sema_post = _sema_post 290*7c478bd9Sstevel@tonic-gate int 291*7c478bd9Sstevel@tonic-gate _sema_post(sema_t *sp) 292*7c478bd9Sstevel@tonic-gate { 293*7c478bd9Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp; 294*7c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 295*7c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 296*7c478bd9Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp); 297*7c478bd9Sstevel@tonic-gate uint_t count; 298*7c478bd9Sstevel@tonic-gate int error = 0; 299*7c478bd9Sstevel@tonic-gate 300*7c478bd9Sstevel@tonic-gate if (ssp) 301*7c478bd9Sstevel@tonic-gate tdb_incr(ssp->sema_post); 302*7c478bd9Sstevel@tonic-gate if (_semvaluemax == 0) 303*7c478bd9Sstevel@tonic-gate _semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX); 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */ 306*7c478bd9Sstevel@tonic-gate error = __lwp_sema_post(lsp); 307*7c478bd9Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt) { /* single threaded */ 308*7c478bd9Sstevel@tonic-gate sigoff(self); 309*7c478bd9Sstevel@tonic-gate if (lsp->count >= _semvaluemax) 310*7c478bd9Sstevel@tonic-gate error = EOVERFLOW; 311*7c478bd9Sstevel@tonic-gate else 312*7c478bd9Sstevel@tonic-gate lsp->count++; 313*7c478bd9Sstevel@tonic-gate sigon(self); 314*7c478bd9Sstevel@tonic-gate } else { /* multithreaded */ 315*7c478bd9Sstevel@tonic-gate queue_head_t *qp; 316*7c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 317*7c478bd9Sstevel@tonic-gate int more; 318*7c478bd9Sstevel@tonic-gate lwpid_t lwpid = 0; 319*7c478bd9Sstevel@tonic-gate 320*7c478bd9Sstevel@tonic-gate qp = queue_lock(lsp, CV); 321*7c478bd9Sstevel@tonic-gate if (lsp->count >= _semvaluemax) 322*7c478bd9Sstevel@tonic-gate error = EOVERFLOW; 323*7c478bd9Sstevel@tonic-gate else if (lsp->count++ == 0 && lsp->sema_waiters) { 324*7c478bd9Sstevel@tonic-gate if ((ulwp = dequeue(qp, lsp, &more)) == NULL) 325*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = 0; 326*7c478bd9Sstevel@tonic-gate else { 327*7c478bd9Sstevel@tonic-gate no_preempt(self); 328*7c478bd9Sstevel@tonic-gate lwpid = ulwp->ul_lwpid; 329*7c478bd9Sstevel@tonic-gate lsp->sema_waiters = (more? 1 : 0); 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate } 332*7c478bd9Sstevel@tonic-gate queue_unlock(qp); 333*7c478bd9Sstevel@tonic-gate if (lwpid) { 334*7c478bd9Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 335*7c478bd9Sstevel@tonic-gate preempt(self); 336*7c478bd9Sstevel@tonic-gate } 337*7c478bd9Sstevel@tonic-gate } 338*7c478bd9Sstevel@tonic-gate 339*7c478bd9Sstevel@tonic-gate if (error == 0) { 340*7c478bd9Sstevel@tonic-gate if (ssp) { 341*7c478bd9Sstevel@tonic-gate /* we just incremented the count */ 342*7c478bd9Sstevel@tonic-gate count = lsp->count; 343*7c478bd9Sstevel@tonic-gate if (ssp->sema_max_count < count) 344*7c478bd9Sstevel@tonic-gate ssp->sema_max_count = count; 345*7c478bd9Sstevel@tonic-gate } 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate return (error); 349*7c478bd9Sstevel@tonic-gate } 350