xref: /illumos-gate/usr/src/lib/libc/port/threads/sema.c (revision 4a38094c)
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
5d4204c85Sraf  * Common Development and Distribution License (the "License").
6d4204c85Sraf  * 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  */
21d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "lint.h"
287c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static uint32_t _semvaluemax;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Check to see if anyone is waiting for this semaphore.
347c478bd9Sstevel@tonic-gate  */
357257d1b4Sraf #pragma weak _sema_held = sema_held
367c478bd9Sstevel@tonic-gate int
sema_held(sema_t * sp)377257d1b4Sraf sema_held(sema_t *sp)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	return (sp->count == 0);
407c478bd9Sstevel@tonic-gate }
417c478bd9Sstevel@tonic-gate 
427257d1b4Sraf #pragma weak _sema_init = sema_init
437c478bd9Sstevel@tonic-gate int
sema_init(sema_t * sp,unsigned int count,int type,void * arg __unused)44*4a38094cSToomas Soome sema_init(sema_t *sp, unsigned int count, int type, void *arg __unused)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	if (_semvaluemax == 0)
477c478bd9Sstevel@tonic-gate 		_semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);
487c478bd9Sstevel@tonic-gate 	if ((type != USYNC_THREAD && type != USYNC_PROCESS) ||
497c478bd9Sstevel@tonic-gate 	    (count > _semvaluemax))
507c478bd9Sstevel@tonic-gate 		return (EINVAL);
518cd45542Sraf 	(void) memset(sp, 0, sizeof (*sp));
527c478bd9Sstevel@tonic-gate 	sp->count = count;
537c478bd9Sstevel@tonic-gate 	sp->type = (uint16_t)type;
547c478bd9Sstevel@tonic-gate 	sp->magic = SEMA_MAGIC;
557c5714f6Sraf 
567c5714f6Sraf 	/*
577c5714f6Sraf 	 * This should be at the beginning of the function,
587c5714f6Sraf 	 * but for the sake of old broken applications that
597c5714f6Sraf 	 * do not have proper alignment for their semaphores
607c5714f6Sraf 	 * (and don't check the return code from sema_init),
617c5714f6Sraf 	 * we put it here, after initializing the semaphore regardless.
627c5714f6Sraf 	 */
637c5714f6Sraf 	if (((uintptr_t)sp & (_LONG_LONG_ALIGNMENT - 1)) &&
647c5714f6Sraf 	    curthread->ul_misaligned == 0)
657c5714f6Sraf 		return (EINVAL);
667c5714f6Sraf 
677c478bd9Sstevel@tonic-gate 	return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707257d1b4Sraf #pragma weak _sema_destroy = sema_destroy
717c478bd9Sstevel@tonic-gate int
sema_destroy(sema_t * sp)727257d1b4Sraf sema_destroy(sema_t *sp)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	sp->magic = 0;
757c478bd9Sstevel@tonic-gate 	tdb_sync_obj_deregister(sp);
767c478bd9Sstevel@tonic-gate 	return (0);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int
sema_wait_impl(sema_t * sp,timespec_t * tsp)807c478bd9Sstevel@tonic-gate sema_wait_impl(sema_t *sp, timespec_t *tsp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	lwp_sema_t *lsp = (lwp_sema_t *)sp;
837c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
847c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
857c478bd9Sstevel@tonic-gate 	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
867c478bd9Sstevel@tonic-gate 	hrtime_t begin_sleep = 0;
877c478bd9Sstevel@tonic-gate 	uint_t count;
887c478bd9Sstevel@tonic-gate 	int error = 0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * All variations of sema_wait() are cancellation points.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	_cancelon();
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (ssp)
967c478bd9Sstevel@tonic-gate 		tdb_incr(ssp->sema_wait);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	self->ul_sp = stkptr();
997c478bd9Sstevel@tonic-gate 	self->ul_wchan = lsp;
1007c478bd9Sstevel@tonic-gate 	if (__td_event_report(self, TD_SLEEP, udp)) {
1017c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_SLEEP;
1027c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = lsp;
1037c478bd9Sstevel@tonic-gate 		tdb_event(TD_SLEEP, udp);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	/* just a guess, but it looks like we will sleep */
1067c478bd9Sstevel@tonic-gate 	if (ssp && lsp->count == 0) {
1077c478bd9Sstevel@tonic-gate 		begin_sleep = gethrtime();
1087c478bd9Sstevel@tonic-gate 		if (lsp->count == 0)	/* still looks like sleep */
1097c478bd9Sstevel@tonic-gate 			tdb_incr(ssp->sema_wait_sleep);
1107c478bd9Sstevel@tonic-gate 		else			/* we changed our mind */
1117c478bd9Sstevel@tonic-gate 			begin_sleep = 0;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (lsp->type == USYNC_PROCESS) {		/* kernel-level */
1157c478bd9Sstevel@tonic-gate 		set_parking_flag(self, 1);
1167c478bd9Sstevel@tonic-gate 		if (self->ul_cursig != 0 ||
1177c478bd9Sstevel@tonic-gate 		    (self->ul_cancelable && self->ul_cancel_pending))
1187c478bd9Sstevel@tonic-gate 			set_parking_flag(self, 0);
1197c478bd9Sstevel@tonic-gate 		/* the kernel always does FIFO queueing */
1207c478bd9Sstevel@tonic-gate 		error = ___lwp_sema_timedwait(lsp, tsp, 1);
1217c478bd9Sstevel@tonic-gate 		set_parking_flag(self, 0);
1227c478bd9Sstevel@tonic-gate 	} else if (!udp->uberflags.uf_mt &&		/* single threaded */
1237c478bd9Sstevel@tonic-gate 	    lsp->count != 0) {				/* and non-blocking */
1247c478bd9Sstevel@tonic-gate 		/*
1257c478bd9Sstevel@tonic-gate 		 * Since we are single-threaded, we don't need the
1267c478bd9Sstevel@tonic-gate 		 * protection of queue_lock().  However, we do need
1277c478bd9Sstevel@tonic-gate 		 * to block signals while modifying the count.
1287c478bd9Sstevel@tonic-gate 		 */
1297c478bd9Sstevel@tonic-gate 		sigoff(self);
1307c478bd9Sstevel@tonic-gate 		lsp->count--;
1317c478bd9Sstevel@tonic-gate 		sigon(self);
1327c478bd9Sstevel@tonic-gate 	} else {				/* multithreaded or blocking */
1337c478bd9Sstevel@tonic-gate 		queue_head_t *qp;
1347c478bd9Sstevel@tonic-gate 		ulwp_t *ulwp;
1357c478bd9Sstevel@tonic-gate 		lwpid_t lwpid = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		qp = queue_lock(lsp, CV);
1387c478bd9Sstevel@tonic-gate 		while (error == 0 && lsp->count == 0) {
1397c478bd9Sstevel@tonic-gate 			/*
1407c478bd9Sstevel@tonic-gate 			 * SUSV3 requires FIFO queueing for semaphores,
1417c478bd9Sstevel@tonic-gate 			 * at least for SCHED_FIFO and SCHED_RR scheduling.
1427c478bd9Sstevel@tonic-gate 			 */
143d4204c85Sraf 			enqueue(qp, self, 1);
1447c478bd9Sstevel@tonic-gate 			lsp->sema_waiters = 1;
1457c478bd9Sstevel@tonic-gate 			set_parking_flag(self, 1);
1467c478bd9Sstevel@tonic-gate 			queue_unlock(qp);
1477c478bd9Sstevel@tonic-gate 			/*
1487c478bd9Sstevel@tonic-gate 			 * We may have received SIGCANCEL before we
1497c478bd9Sstevel@tonic-gate 			 * called queue_lock().  If so and we are
1507c478bd9Sstevel@tonic-gate 			 * cancelable we should return EINTR.
1517c478bd9Sstevel@tonic-gate 			 */
1527c478bd9Sstevel@tonic-gate 			if (self->ul_cursig != 0 ||
1537c478bd9Sstevel@tonic-gate 			    (self->ul_cancelable && self->ul_cancel_pending))
1547c478bd9Sstevel@tonic-gate 				set_parking_flag(self, 0);
1557c478bd9Sstevel@tonic-gate 			error = __lwp_park(tsp, 0);
1567c478bd9Sstevel@tonic-gate 			set_parking_flag(self, 0);
1577c478bd9Sstevel@tonic-gate 			qp = queue_lock(lsp, CV);
1587c478bd9Sstevel@tonic-gate 			if (self->ul_sleepq)	/* timeout or spurious wakeup */
159d4204c85Sraf 				lsp->sema_waiters = dequeue_self(qp);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		if (error == 0)
1627c478bd9Sstevel@tonic-gate 			lsp->count--;
1637c478bd9Sstevel@tonic-gate 		if (lsp->count != 0 && lsp->sema_waiters) {
164d4204c85Sraf 			int more;
165d4204c85Sraf 			if ((ulwp = dequeue(qp, &more)) != NULL) {
1667c478bd9Sstevel@tonic-gate 				no_preempt(self);
1677c478bd9Sstevel@tonic-gate 				lwpid = ulwp->ul_lwpid;
1687c478bd9Sstevel@tonic-gate 			}
169d4204c85Sraf 			lsp->sema_waiters = more;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 		queue_unlock(qp);
1727c478bd9Sstevel@tonic-gate 		if (lwpid) {
1737c478bd9Sstevel@tonic-gate 			(void) __lwp_unpark(lwpid);
1747c478bd9Sstevel@tonic-gate 			preempt(self);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	self->ul_wchan = NULL;
1797c478bd9Sstevel@tonic-gate 	self->ul_sp = 0;
1807c478bd9Sstevel@tonic-gate 	if (ssp) {
1817c478bd9Sstevel@tonic-gate 		if (error == 0) {
1827c478bd9Sstevel@tonic-gate 			/* we just decremented the count */
1837c478bd9Sstevel@tonic-gate 			count = lsp->count;
1847c478bd9Sstevel@tonic-gate 			if (ssp->sema_min_count > count)
1857c478bd9Sstevel@tonic-gate 				ssp->sema_min_count = count;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		if (begin_sleep)
1887c478bd9Sstevel@tonic-gate 			ssp->sema_wait_sleep_time += gethrtime() - begin_sleep;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (error == EINTR)
1927c478bd9Sstevel@tonic-gate 		_canceloff();
1937c478bd9Sstevel@tonic-gate 	else
1947c478bd9Sstevel@tonic-gate 		_canceloff_nocancel();
1957c478bd9Sstevel@tonic-gate 	return (error);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987257d1b4Sraf #pragma weak _sema_wait = sema_wait
1997c478bd9Sstevel@tonic-gate int
sema_wait(sema_t * sp)2007257d1b4Sraf sema_wait(sema_t *sp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2037c478bd9Sstevel@tonic-gate 	return (sema_wait_impl(sp, NULL));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate int
sema_reltimedwait(sema_t * sp,const timespec_t * reltime)2077257d1b4Sraf sema_reltimedwait(sema_t *sp, const timespec_t *reltime)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2127c478bd9Sstevel@tonic-gate 	return (sema_wait_impl(sp, &tslocal));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate int
sema_timedwait(sema_t * sp,const timespec_t * abstime)2167257d1b4Sraf sema_timedwait(sema_t *sp, const timespec_t *abstime)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	timespec_t tslocal;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2217c478bd9Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
2227c478bd9Sstevel@tonic-gate 	return (sema_wait_impl(sp, &tslocal));
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257257d1b4Sraf #pragma weak _sema_trywait = sema_trywait
2267c478bd9Sstevel@tonic-gate int
sema_trywait(sema_t * sp)2277257d1b4Sraf sema_trywait(sema_t *sp)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	lwp_sema_t *lsp = (lwp_sema_t *)sp;
2307c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2317c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
2327c478bd9Sstevel@tonic-gate 	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
2337c478bd9Sstevel@tonic-gate 	uint_t count;
2347c478bd9Sstevel@tonic-gate 	int error = 0;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (ssp)
2397c478bd9Sstevel@tonic-gate 		tdb_incr(ssp->sema_trywait);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (lsp->type == USYNC_PROCESS) {	/* kernel-level */
2427257d1b4Sraf 		error = _lwp_sema_trywait(lsp);
2437c478bd9Sstevel@tonic-gate 	} else if (!udp->uberflags.uf_mt) {	/* single threaded */
2447c478bd9Sstevel@tonic-gate 		sigoff(self);
2457c478bd9Sstevel@tonic-gate 		if (lsp->count == 0)
2467c478bd9Sstevel@tonic-gate 			error = EBUSY;
2477c478bd9Sstevel@tonic-gate 		else
2487c478bd9Sstevel@tonic-gate 			lsp->count--;
2497c478bd9Sstevel@tonic-gate 		sigon(self);
2507c478bd9Sstevel@tonic-gate 	} else {				/* multithreaded */
2517c478bd9Sstevel@tonic-gate 		queue_head_t *qp;
2527c478bd9Sstevel@tonic-gate 		ulwp_t *ulwp;
2537c478bd9Sstevel@tonic-gate 		lwpid_t lwpid = 0;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		qp = queue_lock(lsp, CV);
2567c478bd9Sstevel@tonic-gate 		if (lsp->count == 0)
2577c478bd9Sstevel@tonic-gate 			error = EBUSY;
2587c478bd9Sstevel@tonic-gate 		else if (--lsp->count != 0 && lsp->sema_waiters) {
259d4204c85Sraf 			int more;
260d4204c85Sraf 			if ((ulwp = dequeue(qp, &more)) != NULL) {
2617c478bd9Sstevel@tonic-gate 				no_preempt(self);
2627c478bd9Sstevel@tonic-gate 				lwpid = ulwp->ul_lwpid;
2637c478bd9Sstevel@tonic-gate 			}
264d4204c85Sraf 			lsp->sema_waiters = more;
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 		queue_unlock(qp);
2677c478bd9Sstevel@tonic-gate 		if (lwpid) {
2687c478bd9Sstevel@tonic-gate 			(void) __lwp_unpark(lwpid);
2697c478bd9Sstevel@tonic-gate 			preempt(self);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (error == 0) {
2747c478bd9Sstevel@tonic-gate 		if (ssp) {
2757c478bd9Sstevel@tonic-gate 			/* we just decremented the count */
2767c478bd9Sstevel@tonic-gate 			count = lsp->count;
2777c478bd9Sstevel@tonic-gate 			if (ssp->sema_min_count > count)
2787c478bd9Sstevel@tonic-gate 				ssp->sema_min_count = count;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 	} else {
2817c478bd9Sstevel@tonic-gate 		if (ssp)
2827c478bd9Sstevel@tonic-gate 			tdb_incr(ssp->sema_trywait_fail);
2837c478bd9Sstevel@tonic-gate 		if (__td_event_report(self, TD_LOCK_TRY, udp)) {
2847c478bd9Sstevel@tonic-gate 			self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
2857c478bd9Sstevel@tonic-gate 			tdb_event(TD_LOCK_TRY, udp);
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	return (error);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927257d1b4Sraf #pragma weak _sema_post = sema_post
2937c478bd9Sstevel@tonic-gate int
sema_post(sema_t * sp)2947257d1b4Sraf sema_post(sema_t *sp)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	lwp_sema_t *lsp = (lwp_sema_t *)sp;
2977c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2987c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
2997c478bd9Sstevel@tonic-gate 	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
3007c478bd9Sstevel@tonic-gate 	uint_t count;
3017c478bd9Sstevel@tonic-gate 	int error = 0;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (ssp)
3047c478bd9Sstevel@tonic-gate 		tdb_incr(ssp->sema_post);
3057c478bd9Sstevel@tonic-gate 	if (_semvaluemax == 0)
3067c478bd9Sstevel@tonic-gate 		_semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (lsp->type == USYNC_PROCESS) {	/* kernel-level */
3097257d1b4Sraf 		error = _lwp_sema_post(lsp);
3107c478bd9Sstevel@tonic-gate 	} else if (!udp->uberflags.uf_mt) {	/* single threaded */
3117c478bd9Sstevel@tonic-gate 		sigoff(self);
3127c478bd9Sstevel@tonic-gate 		if (lsp->count >= _semvaluemax)
3137c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
3147c478bd9Sstevel@tonic-gate 		else
3157c478bd9Sstevel@tonic-gate 			lsp->count++;
3167c478bd9Sstevel@tonic-gate 		sigon(self);
3177c478bd9Sstevel@tonic-gate 	} else {				/* multithreaded */
3187c478bd9Sstevel@tonic-gate 		queue_head_t *qp;
3197c478bd9Sstevel@tonic-gate 		ulwp_t *ulwp;
3207c478bd9Sstevel@tonic-gate 		lwpid_t lwpid = 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		qp = queue_lock(lsp, CV);
3237c478bd9Sstevel@tonic-gate 		if (lsp->count >= _semvaluemax)
3247c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
3257c478bd9Sstevel@tonic-gate 		else if (lsp->count++ == 0 && lsp->sema_waiters) {
326d4204c85Sraf 			int more;
327d4204c85Sraf 			if ((ulwp = dequeue(qp, &more)) != NULL) {
3287c478bd9Sstevel@tonic-gate 				no_preempt(self);
3297c478bd9Sstevel@tonic-gate 				lwpid = ulwp->ul_lwpid;
3307c478bd9Sstevel@tonic-gate 			}
331d4204c85Sraf 			lsp->sema_waiters = more;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		queue_unlock(qp);
3347c478bd9Sstevel@tonic-gate 		if (lwpid) {
3357c478bd9Sstevel@tonic-gate 			(void) __lwp_unpark(lwpid);
3367c478bd9Sstevel@tonic-gate 			preempt(self);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (error == 0) {
3417c478bd9Sstevel@tonic-gate 		if (ssp) {
3427c478bd9Sstevel@tonic-gate 			/* we just incremented the count */
3437c478bd9Sstevel@tonic-gate 			count = lsp->count;
3447c478bd9Sstevel@tonic-gate 			if (ssp->sema_max_count < count)
3457c478bd9Sstevel@tonic-gate 				ssp->sema_max_count = count;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (error);
3507c478bd9Sstevel@tonic-gate }
351