summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/threads/sema.c
blob: c9512b9338f021f1fdf3d67eeb783741bae5d054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "lint.h"
#include "thr_uberdata.h"

static uint32_t _semvaluemax;

/*
 * Check to see if anyone is waiting for this semaphore.
 */
#pragma weak _sema_held = sema_held
int
sema_held(sema_t *sp)
{
	return (sp->count == 0);
}

#pragma weak _sema_init = sema_init
/* ARGSUSED3 */
int
sema_init(sema_t *sp, unsigned int count, int type, void *arg)
{
	if (_semvaluemax == 0)
		_semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);
	if ((type != USYNC_THREAD && type != USYNC_PROCESS) ||
	    (count > _semvaluemax))
		return (EINVAL);
	(void) memset(sp, 0, sizeof (*sp));
	sp->count = count;
	sp->type = (uint16_t)type;
	sp->magic = SEMA_MAGIC;

	/*
	 * This should be at the beginning of the function,
	 * but for the sake of old broken applications that
	 * do not have proper alignment for their semaphores
	 * (and don't check the return code from sema_init),
	 * we put it here, after initializing the semaphore regardless.
	 */
	if (((uintptr_t)sp & (_LONG_LONG_ALIGNMENT - 1)) &&
	    curthread->ul_misaligned == 0)
		return (EINVAL);

	return (0);
}

#pragma weak _sema_destroy = sema_destroy
int
sema_destroy(sema_t *sp)
{
	sp->magic = 0;
	tdb_sync_obj_deregister(sp);
	return (0);
}

static int
sema_wait_impl(sema_t *sp, timespec_t *tsp)
{
	lwp_sema_t *lsp = (lwp_sema_t *)sp;
	ulwp_t *self = curthread;
	uberdata_t *udp = self->ul_uberdata;
	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
	hrtime_t begin_sleep = 0;
	uint_t count;
	int error = 0;

	/*
	 * All variations of sema_wait() are cancellation points.
	 */
	_cancelon();

	if (ssp)
		tdb_incr(ssp->sema_wait);

	self->ul_sp = stkptr();
	self->ul_wchan = lsp;
	if (__td_event_report(self, TD_SLEEP, udp)) {
		self->ul_td_evbuf.eventnum = TD_SLEEP;
		self->ul_td_evbuf.eventdata = lsp;
		tdb_event(TD_SLEEP, udp);
	}
	/* just a guess, but it looks like we will sleep */
	if (ssp && lsp->count == 0) {
		begin_sleep = gethrtime();
		if (lsp->count == 0)	/* still looks like sleep */
			tdb_incr(ssp->sema_wait_sleep);
		else			/* we changed our mind */
			begin_sleep = 0;
	}

	if (lsp->type == USYNC_PROCESS) {		/* kernel-level */
		set_parking_flag(self, 1);
		if (self->ul_cursig != 0 ||
		    (self->ul_cancelable && self->ul_cancel_pending))
			set_parking_flag(self, 0);
		/* the kernel always does FIFO queueing */
		error = ___lwp_sema_timedwait(lsp, tsp, 1);
		set_parking_flag(self, 0);
	} else if (!udp->uberflags.uf_mt &&		/* single threaded */
	    lsp->count != 0) {				/* and non-blocking */
		/*
		 * Since we are single-threaded, we don't need the
		 * protection of queue_lock().  However, we do need
		 * to block signals while modifying the count.
		 */
		sigoff(self);
		lsp->count--;
		sigon(self);
	} else {				/* multithreaded or blocking */
		queue_head_t *qp;
		ulwp_t *ulwp;
		lwpid_t lwpid = 0;

		qp = queue_lock(lsp, CV);
		while (error == 0 && lsp->count == 0) {
			/*
			 * SUSV3 requires FIFO queueing for semaphores,
			 * at least for SCHED_FIFO and SCHED_RR scheduling.
			 */
			enqueue(qp, self, 1);
			lsp->sema_waiters = 1;
			set_parking_flag(self, 1);
			queue_unlock(qp);
			/*
			 * We may have received SIGCANCEL before we
			 * called queue_lock().  If so and we are
			 * cancelable we should return EINTR.
			 */
			if (self->ul_cursig != 0 ||
			    (self->ul_cancelable && self->ul_cancel_pending))
				set_parking_flag(self, 0);
			error = __lwp_park(tsp, 0);
			set_parking_flag(self, 0);
			qp = queue_lock(lsp, CV);
			if (self->ul_sleepq)	/* timeout or spurious wakeup */
				lsp->sema_waiters = dequeue_self(qp);
		}
		if (error == 0)
			lsp->count--;
		if (lsp->count != 0 && lsp->sema_waiters) {
			int more;
			if ((ulwp = dequeue(qp, &more)) != NULL) {
				no_preempt(self);
				lwpid = ulwp->ul_lwpid;
			}
			lsp->sema_waiters = more;
		}
		queue_unlock(qp);
		if (lwpid) {
			(void) __lwp_unpark(lwpid);
			preempt(self);
		}
	}

	self->ul_wchan = NULL;
	self->ul_sp = 0;
	if (ssp) {
		if (error == 0) {
			/* we just decremented the count */
			count = lsp->count;
			if (ssp->sema_min_count > count)
				ssp->sema_min_count = count;
		}
		if (begin_sleep)
			ssp->sema_wait_sleep_time += gethrtime() - begin_sleep;
	}

	if (error == EINTR)
		_canceloff();
	else
		_canceloff_nocancel();
	return (error);
}

#pragma weak _sema_wait = sema_wait
int
sema_wait(sema_t *sp)
{
	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
	return (sema_wait_impl(sp, NULL));
}

int
sema_reltimedwait(sema_t *sp, const timespec_t *reltime)
{
	timespec_t tslocal = *reltime;

	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
	return (sema_wait_impl(sp, &tslocal));
}

int
sema_timedwait(sema_t *sp, const timespec_t *abstime)
{
	timespec_t tslocal;

	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
	return (sema_wait_impl(sp, &tslocal));
}

#pragma weak _sema_trywait = sema_trywait
int
sema_trywait(sema_t *sp)
{
	lwp_sema_t *lsp = (lwp_sema_t *)sp;
	ulwp_t *self = curthread;
	uberdata_t *udp = self->ul_uberdata;
	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
	uint_t count;
	int error = 0;

	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);

	if (ssp)
		tdb_incr(ssp->sema_trywait);

	if (lsp->type == USYNC_PROCESS) {	/* kernel-level */
		error = _lwp_sema_trywait(lsp);
	} else if (!udp->uberflags.uf_mt) {	/* single threaded */
		sigoff(self);
		if (lsp->count == 0)
			error = EBUSY;
		else
			lsp->count--;
		sigon(self);
	} else {				/* multithreaded */
		queue_head_t *qp;
		ulwp_t *ulwp;
		lwpid_t lwpid = 0;

		qp = queue_lock(lsp, CV);
		if (lsp->count == 0)
			error = EBUSY;
		else if (--lsp->count != 0 && lsp->sema_waiters) {
			int more;
			if ((ulwp = dequeue(qp, &more)) != NULL) {
				no_preempt(self);
				lwpid = ulwp->ul_lwpid;
			}
			lsp->sema_waiters = more;
		}
		queue_unlock(qp);
		if (lwpid) {
			(void) __lwp_unpark(lwpid);
			preempt(self);
		}
	}

	if (error == 0) {
		if (ssp) {
			/* we just decremented the count */
			count = lsp->count;
			if (ssp->sema_min_count > count)
				ssp->sema_min_count = count;
		}
	} else {
		if (ssp)
			tdb_incr(ssp->sema_trywait_fail);
		if (__td_event_report(self, TD_LOCK_TRY, udp)) {
			self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
			tdb_event(TD_LOCK_TRY, udp);
		}
	}

	return (error);
}

#pragma weak _sema_post = sema_post
int
sema_post(sema_t *sp)
{
	lwp_sema_t *lsp = (lwp_sema_t *)sp;
	ulwp_t *self = curthread;
	uberdata_t *udp = self->ul_uberdata;
	tdb_sema_stats_t *ssp = SEMA_STATS(sp, udp);
	uint_t count;
	int error = 0;

	if (ssp)
		tdb_incr(ssp->sema_post);
	if (_semvaluemax == 0)
		_semvaluemax = (uint32_t)_sysconf(_SC_SEM_VALUE_MAX);

	if (lsp->type == USYNC_PROCESS) {	/* kernel-level */
		error = _lwp_sema_post(lsp);
	} else if (!udp->uberflags.uf_mt) {	/* single threaded */
		sigoff(self);
		if (lsp->count >= _semvaluemax)
			error = EOVERFLOW;
		else
			lsp->count++;
		sigon(self);
	} else {				/* multithreaded */
		queue_head_t *qp;
		ulwp_t *ulwp;
		lwpid_t lwpid = 0;

		qp = queue_lock(lsp, CV);
		if (lsp->count >= _semvaluemax)
			error = EOVERFLOW;
		else if (lsp->count++ == 0 && lsp->sema_waiters) {
			int more;
			if ((ulwp = dequeue(qp, &more)) != NULL) {
				no_preempt(self);
				lwpid = ulwp->ul_lwpid;
			}
			lsp->sema_waiters = more;
		}
		queue_unlock(qp);
		if (lwpid) {
			(void) __lwp_unpark(lwpid);
			preempt(self);
		}
	}

	if (error == 0) {
		if (ssp) {
			/* we just incremented the count */
			count = lsp->count;
			if (ssp->sema_max_count < count)
				ssp->sema_max_count = count;
		}
	}

	return (error);
}