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
56247Sraf * Common Development and Distribution License (the "License").
66247Sraf * 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 */
216247Sraf
220Sstevel@tonic-gate /*
236247Sraf * 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 #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate
320Sstevel@tonic-gate static uint32_t _semvaluemax;
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * Check to see if anyone is waiting for this semaphore.
360Sstevel@tonic-gate */
376812Sraf #pragma weak _sema_held = sema_held
380Sstevel@tonic-gate int
sema_held(sema_t * sp)396812Sraf sema_held(sema_t *sp)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate return (sp->count == 0);
420Sstevel@tonic-gate }
430Sstevel@tonic-gate
446812Sraf #pragma weak _sema_init = sema_init
456812Sraf /* ARGSUSED3 */
460Sstevel@tonic-gate int
sema_init(sema_t * sp,unsigned int count,int type,void * arg)476812Sraf sema_init(sema_t *sp, unsigned int count, int type, void *arg)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate if (_semvaluemax == 0)
500Sstevel@tonic-gate _semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);
510Sstevel@tonic-gate if ((type != USYNC_THREAD && type != USYNC_PROCESS) ||
520Sstevel@tonic-gate (count > _semvaluemax))
530Sstevel@tonic-gate return (EINVAL);
546515Sraf (void) memset(sp, 0, sizeof (*sp));
550Sstevel@tonic-gate sp->count = count;
560Sstevel@tonic-gate sp->type = (uint16_t)type;
570Sstevel@tonic-gate sp->magic = SEMA_MAGIC;
58*7255Sraf
59*7255Sraf /*
60*7255Sraf * This should be at the beginning of the function,
61*7255Sraf * but for the sake of old broken applications that
62*7255Sraf * do not have proper alignment for their semaphores
63*7255Sraf * (and don't check the return code from sema_init),
64*7255Sraf * we put it here, after initializing the semaphore regardless.
65*7255Sraf */
66*7255Sraf if (((uintptr_t)sp & (_LONG_LONG_ALIGNMENT - 1)) &&
67*7255Sraf curthread->ul_misaligned == 0)
68*7255Sraf return (EINVAL);
69*7255Sraf
700Sstevel@tonic-gate return (0);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
736812Sraf #pragma weak _sema_destroy = sema_destroy
740Sstevel@tonic-gate int
sema_destroy(sema_t * sp)756812Sraf sema_destroy(sema_t *sp)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate sp->magic = 0;
780Sstevel@tonic-gate tdb_sync_obj_deregister(sp);
790Sstevel@tonic-gate return (0);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate static int
sema_wait_impl(sema_t * sp,timespec_t * tsp)830Sstevel@tonic-gate sema_wait_impl(sema_t *sp, timespec_t *tsp)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp;
860Sstevel@tonic-gate ulwp_t *self = curthread;
870Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
880Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
890Sstevel@tonic-gate hrtime_t begin_sleep = 0;
900Sstevel@tonic-gate uint_t count;
910Sstevel@tonic-gate int error = 0;
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * All variations of sema_wait() are cancellation points.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate _cancelon();
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (ssp)
990Sstevel@tonic-gate tdb_incr(ssp->sema_wait);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate self->ul_sp = stkptr();
1020Sstevel@tonic-gate self->ul_wchan = lsp;
1030Sstevel@tonic-gate if (__td_event_report(self, TD_SLEEP, udp)) {
1040Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_SLEEP;
1050Sstevel@tonic-gate self->ul_td_evbuf.eventdata = lsp;
1060Sstevel@tonic-gate tdb_event(TD_SLEEP, udp);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate /* just a guess, but it looks like we will sleep */
1090Sstevel@tonic-gate if (ssp && lsp->count == 0) {
1100Sstevel@tonic-gate begin_sleep = gethrtime();
1110Sstevel@tonic-gate if (lsp->count == 0) /* still looks like sleep */
1120Sstevel@tonic-gate tdb_incr(ssp->sema_wait_sleep);
1130Sstevel@tonic-gate else /* we changed our mind */
1140Sstevel@tonic-gate begin_sleep = 0;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */
1180Sstevel@tonic-gate set_parking_flag(self, 1);
1190Sstevel@tonic-gate if (self->ul_cursig != 0 ||
1200Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending))
1210Sstevel@tonic-gate set_parking_flag(self, 0);
1220Sstevel@tonic-gate /* the kernel always does FIFO queueing */
1230Sstevel@tonic-gate error = ___lwp_sema_timedwait(lsp, tsp, 1);
1240Sstevel@tonic-gate set_parking_flag(self, 0);
1250Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt && /* single threaded */
1260Sstevel@tonic-gate lsp->count != 0) { /* and non-blocking */
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * Since we are single-threaded, we don't need the
1290Sstevel@tonic-gate * protection of queue_lock(). However, we do need
1300Sstevel@tonic-gate * to block signals while modifying the count.
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate sigoff(self);
1330Sstevel@tonic-gate lsp->count--;
1340Sstevel@tonic-gate sigon(self);
1350Sstevel@tonic-gate } else { /* multithreaded or blocking */
1360Sstevel@tonic-gate queue_head_t *qp;
1370Sstevel@tonic-gate ulwp_t *ulwp;
1380Sstevel@tonic-gate lwpid_t lwpid = 0;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate qp = queue_lock(lsp, CV);
1410Sstevel@tonic-gate while (error == 0 && lsp->count == 0) {
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * SUSV3 requires FIFO queueing for semaphores,
1440Sstevel@tonic-gate * at least for SCHED_FIFO and SCHED_RR scheduling.
1450Sstevel@tonic-gate */
1466247Sraf enqueue(qp, self, 1);
1470Sstevel@tonic-gate lsp->sema_waiters = 1;
1480Sstevel@tonic-gate set_parking_flag(self, 1);
1490Sstevel@tonic-gate queue_unlock(qp);
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * We may have received SIGCANCEL before we
1520Sstevel@tonic-gate * called queue_lock(). If so and we are
1530Sstevel@tonic-gate * cancelable we should return EINTR.
1540Sstevel@tonic-gate */
1550Sstevel@tonic-gate if (self->ul_cursig != 0 ||
1560Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending))
1570Sstevel@tonic-gate set_parking_flag(self, 0);
1580Sstevel@tonic-gate error = __lwp_park(tsp, 0);
1590Sstevel@tonic-gate set_parking_flag(self, 0);
1600Sstevel@tonic-gate qp = queue_lock(lsp, CV);
1610Sstevel@tonic-gate if (self->ul_sleepq) /* timeout or spurious wakeup */
1626247Sraf lsp->sema_waiters = dequeue_self(qp);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate if (error == 0)
1650Sstevel@tonic-gate lsp->count--;
1660Sstevel@tonic-gate if (lsp->count != 0 && lsp->sema_waiters) {
1676247Sraf int more;
1686247Sraf if ((ulwp = dequeue(qp, &more)) != NULL) {
1690Sstevel@tonic-gate no_preempt(self);
1700Sstevel@tonic-gate lwpid = ulwp->ul_lwpid;
1710Sstevel@tonic-gate }
1726247Sraf lsp->sema_waiters = more;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate queue_unlock(qp);
1750Sstevel@tonic-gate if (lwpid) {
1760Sstevel@tonic-gate (void) __lwp_unpark(lwpid);
1770Sstevel@tonic-gate preempt(self);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate self->ul_wchan = NULL;
1820Sstevel@tonic-gate self->ul_sp = 0;
1830Sstevel@tonic-gate if (ssp) {
1840Sstevel@tonic-gate if (error == 0) {
1850Sstevel@tonic-gate /* we just decremented the count */
1860Sstevel@tonic-gate count = lsp->count;
1870Sstevel@tonic-gate if (ssp->sema_min_count > count)
1880Sstevel@tonic-gate ssp->sema_min_count = count;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate if (begin_sleep)
1910Sstevel@tonic-gate ssp->sema_wait_sleep_time += gethrtime() - begin_sleep;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (error == EINTR)
1950Sstevel@tonic-gate _canceloff();
1960Sstevel@tonic-gate else
1970Sstevel@tonic-gate _canceloff_nocancel();
1980Sstevel@tonic-gate return (error);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2016812Sraf #pragma weak _sema_wait = sema_wait
2020Sstevel@tonic-gate int
sema_wait(sema_t * sp)2036812Sraf sema_wait(sema_t *sp)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2060Sstevel@tonic-gate return (sema_wait_impl(sp, NULL));
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate int
sema_reltimedwait(sema_t * sp,const timespec_t * reltime)2106812Sraf sema_reltimedwait(sema_t *sp, const timespec_t *reltime)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate timespec_t tslocal = *reltime;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2150Sstevel@tonic-gate return (sema_wait_impl(sp, &tslocal));
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate int
sema_timedwait(sema_t * sp,const timespec_t * abstime)2196812Sraf sema_timedwait(sema_t *sp, const timespec_t *abstime)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate timespec_t tslocal;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2240Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
2250Sstevel@tonic-gate return (sema_wait_impl(sp, &tslocal));
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2286812Sraf #pragma weak _sema_trywait = sema_trywait
2290Sstevel@tonic-gate int
sema_trywait(sema_t * sp)2306812Sraf sema_trywait(sema_t *sp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp;
2330Sstevel@tonic-gate ulwp_t *self = curthread;
2340Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
2350Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
2360Sstevel@tonic-gate uint_t count;
2370Sstevel@tonic-gate int error = 0;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if (ssp)
2420Sstevel@tonic-gate tdb_incr(ssp->sema_trywait);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */
2456812Sraf error = _lwp_sema_trywait(lsp);
2460Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt) { /* single threaded */
2470Sstevel@tonic-gate sigoff(self);
2480Sstevel@tonic-gate if (lsp->count == 0)
2490Sstevel@tonic-gate error = EBUSY;
2500Sstevel@tonic-gate else
2510Sstevel@tonic-gate lsp->count--;
2520Sstevel@tonic-gate sigon(self);
2530Sstevel@tonic-gate } else { /* multithreaded */
2540Sstevel@tonic-gate queue_head_t *qp;
2550Sstevel@tonic-gate ulwp_t *ulwp;
2560Sstevel@tonic-gate lwpid_t lwpid = 0;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate qp = queue_lock(lsp, CV);
2590Sstevel@tonic-gate if (lsp->count == 0)
2600Sstevel@tonic-gate error = EBUSY;
2610Sstevel@tonic-gate else if (--lsp->count != 0 && lsp->sema_waiters) {
2626247Sraf int more;
2636247Sraf if ((ulwp = dequeue(qp, &more)) != NULL) {
2640Sstevel@tonic-gate no_preempt(self);
2650Sstevel@tonic-gate lwpid = ulwp->ul_lwpid;
2660Sstevel@tonic-gate }
2676247Sraf lsp->sema_waiters = more;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate queue_unlock(qp);
2700Sstevel@tonic-gate if (lwpid) {
2710Sstevel@tonic-gate (void) __lwp_unpark(lwpid);
2720Sstevel@tonic-gate preempt(self);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate if (error == 0) {
2770Sstevel@tonic-gate if (ssp) {
2780Sstevel@tonic-gate /* we just decremented the count */
2790Sstevel@tonic-gate count = lsp->count;
2800Sstevel@tonic-gate if (ssp->sema_min_count > count)
2810Sstevel@tonic-gate ssp->sema_min_count = count;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate } else {
2840Sstevel@tonic-gate if (ssp)
2850Sstevel@tonic-gate tdb_incr(ssp->sema_trywait_fail);
2860Sstevel@tonic-gate if (__td_event_report(self, TD_LOCK_TRY, udp)) {
2870Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
2880Sstevel@tonic-gate tdb_event(TD_LOCK_TRY, udp);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate return (error);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2956812Sraf #pragma weak _sema_post = sema_post
2960Sstevel@tonic-gate int
sema_post(sema_t * sp)2976812Sraf sema_post(sema_t *sp)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate lwp_sema_t *lsp = (lwp_sema_t *)sp;
3000Sstevel@tonic-gate ulwp_t *self = curthread;
3010Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
3020Sstevel@tonic-gate tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
3030Sstevel@tonic-gate uint_t count;
3040Sstevel@tonic-gate int error = 0;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate if (ssp)
3070Sstevel@tonic-gate tdb_incr(ssp->sema_post);
3080Sstevel@tonic-gate if (_semvaluemax == 0)
3090Sstevel@tonic-gate _semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (lsp->type == USYNC_PROCESS) { /* kernel-level */
3126812Sraf error = _lwp_sema_post(lsp);
3130Sstevel@tonic-gate } else if (!udp->uberflags.uf_mt) { /* single threaded */
3140Sstevel@tonic-gate sigoff(self);
3150Sstevel@tonic-gate if (lsp->count >= _semvaluemax)
3160Sstevel@tonic-gate error = EOVERFLOW;
3170Sstevel@tonic-gate else
3180Sstevel@tonic-gate lsp->count++;
3190Sstevel@tonic-gate sigon(self);
3200Sstevel@tonic-gate } else { /* multithreaded */
3210Sstevel@tonic-gate queue_head_t *qp;
3220Sstevel@tonic-gate ulwp_t *ulwp;
3230Sstevel@tonic-gate lwpid_t lwpid = 0;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate qp = queue_lock(lsp, CV);
3260Sstevel@tonic-gate if (lsp->count >= _semvaluemax)
3270Sstevel@tonic-gate error = EOVERFLOW;
3280Sstevel@tonic-gate else if (lsp->count++ == 0 && lsp->sema_waiters) {
3296247Sraf int more;
3306247Sraf if ((ulwp = dequeue(qp, &more)) != NULL) {
3310Sstevel@tonic-gate no_preempt(self);
3320Sstevel@tonic-gate lwpid = ulwp->ul_lwpid;
3330Sstevel@tonic-gate }
3346247Sraf lsp->sema_waiters = more;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate queue_unlock(qp);
3370Sstevel@tonic-gate if (lwpid) {
3380Sstevel@tonic-gate (void) __lwp_unpark(lwpid);
3390Sstevel@tonic-gate preempt(self);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (error == 0) {
3440Sstevel@tonic-gate if (ssp) {
3450Sstevel@tonic-gate /* we just incremented the count */
3460Sstevel@tonic-gate count = lsp->count;
3470Sstevel@tonic-gate if (ssp->sema_max_count < count)
3480Sstevel@tonic-gate ssp->sema_max_count = count;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate return (error);
3530Sstevel@tonic-gate }
354