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