7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 /*
30 * This file contains the semaphore operations.
31 */
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/schedctl.h>
37 #include <sys/semaphore.h>
38 #include <sys/sema_impl.h>
39 #include <sys/t_lock.h>
40 #include <sys/thread.h>
41 #include <sys/proc.h>
42 #include <sys/cmn_err.h>
43 #include <sys/debug.h>
44 #include <sys/disp.h>
45 #include <sys/sobject.h>
46 #include <sys/cpuvar.h>
47 #include <sys/sleepq.h>
210
211 if ((sp = (ksema_t *)t->t_wchan) != NULL) {
212 sema_dequeue(sp, t);
213 *t_prip = pri;
214 sema_queue(sp, t);
215 } else
216 panic("sema_change_pri: %p not on sleep queue", (void *)t);
217 }
218
219 /*
220 * the semaphore is granted when the semaphore's
221 * count is greater than zero and blocks when equal
222 * to zero.
223 */
224 void
225 sema_p(ksema_t *sp)
226 {
227 sema_impl_t *s;
228 disp_lock_t *sqlp;
229
230 s = (sema_impl_t *)sp;
231 sqlp = &SQHASH(s)->sq_lock;
232 disp_lock_enter(sqlp);
233 ASSERT(s->s_count >= 0);
234 while (s->s_count == 0) {
235 if (panicstr) {
236 disp_lock_exit(sqlp);
237 return;
238 }
239 thread_lock_high(curthread);
240 SEMA_BLOCK(s, sqlp);
241 thread_unlock_nopreempt(curthread);
242 swtch();
243 disp_lock_enter(sqlp);
244 }
245 s->s_count--;
246 disp_lock_exit(sqlp);
247 }
248
249 /*
250 * similiar to sema_p except that it blocks at an interruptible
251 * priority. if a signal is present then return 1 otherwise 0.
252 */
253 int
254 sema_p_sig(ksema_t *sp)
255 {
256 kthread_t *t = curthread;
257 klwp_t *lwp = ttolwp(t);
258 int cancel_pending;
316 }
317 lwp->lwp_asleep = 0;
318 disp_lock_enter(sqlp);
319 }
320 s->s_count--;
321 disp_lock_exit(sqlp);
322 return (0);
323 }
324
325 /*
326 * the semaphore's count is incremented by one. a blocked thread
327 * is awakened and re-tries to acquire the semaphore.
328 */
329 void
330 sema_v(ksema_t *sp)
331 {
332 sema_impl_t *s;
333 kthread_t *sq, *tp;
334 disp_lock_t *sqlp;
335
336 s = (sema_impl_t *)sp;
337 sqlp = &SQHASH(s)->sq_lock;
338 disp_lock_enter(sqlp);
339 if (panicstr) {
340 disp_lock_exit(sqlp);
341 return;
342 }
343 s->s_count++;
344 sq = s->s_slpq;
345 if (sq != NULL) {
346 tp = sq;
347 ASSERT(THREAD_LOCK_HELD(tp));
348 sq = sq->t_link;
349 tp->t_link = NULL;
350 DTRACE_SCHED1(wakeup, kthread_t *, tp);
351 tp->t_sobj_ops = NULL;
352 tp->t_wchan = NULL;
353 ASSERT(tp->t_state == TS_SLEEP);
354 CL_WAKEUP(tp);
355 s->s_slpq = sq;
356 disp_lock_exit_high(sqlp);
357 thread_unlock(tp);
358 } else {
359 disp_lock_exit(sqlp);
360 }
361 }
362
363 /*
364 * try to acquire the semaphore. if the semaphore is greater than
365 * zero, then the semaphore is granted and returns 1. otherwise
366 * return 0.
367 */
368 int
369 sema_tryp(ksema_t *sp)
370 {
371 sema_impl_t *s;
372 sleepq_head_t *sqh;
373
374 int gotit = 0;
375
376 s = (sema_impl_t *)sp;
377 sqh = SQHASH(s);
378 disp_lock_enter(&sqh->sq_lock);
379 if (s->s_count > 0) {
380 s->s_count--;
381 gotit = 1;
382 }
383 disp_lock_exit(&sqh->sq_lock);
384 return (gotit);
385 }
386
387 int
388 sema_held(ksema_t *sp)
389 {
390 sema_impl_t *s;
391
392
393 s = (sema_impl_t *)sp;
394 if (panicstr)
395 return (1);
396 else
397 return (s->s_count <= 0);
398 }
|
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
29 */
30
31 /*
32 * This file contains the semaphore operations.
33 */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/schedctl.h>
39 #include <sys/semaphore.h>
40 #include <sys/sema_impl.h>
41 #include <sys/t_lock.h>
42 #include <sys/thread.h>
43 #include <sys/proc.h>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
46 #include <sys/disp.h>
47 #include <sys/sobject.h>
48 #include <sys/cpuvar.h>
49 #include <sys/sleepq.h>
212
213 if ((sp = (ksema_t *)t->t_wchan) != NULL) {
214 sema_dequeue(sp, t);
215 *t_prip = pri;
216 sema_queue(sp, t);
217 } else
218 panic("sema_change_pri: %p not on sleep queue", (void *)t);
219 }
220
221 /*
222 * the semaphore is granted when the semaphore's
223 * count is greater than zero and blocks when equal
224 * to zero.
225 */
226 void
227 sema_p(ksema_t *sp)
228 {
229 sema_impl_t *s;
230 disp_lock_t *sqlp;
231
232 /* no-op during panic */
233 if (panicstr)
234 return;
235
236 s = (sema_impl_t *)sp;
237 sqlp = &SQHASH(s)->sq_lock;
238 disp_lock_enter(sqlp);
239 ASSERT(s->s_count >= 0);
240 while (s->s_count == 0) {
241 thread_lock_high(curthread);
242 SEMA_BLOCK(s, sqlp);
243 thread_unlock_nopreempt(curthread);
244 swtch();
245 disp_lock_enter(sqlp);
246 }
247 s->s_count--;
248 disp_lock_exit(sqlp);
249 }
250
251 /*
252 * similiar to sema_p except that it blocks at an interruptible
253 * priority. if a signal is present then return 1 otherwise 0.
254 */
255 int
256 sema_p_sig(ksema_t *sp)
257 {
258 kthread_t *t = curthread;
259 klwp_t *lwp = ttolwp(t);
260 int cancel_pending;
318 }
319 lwp->lwp_asleep = 0;
320 disp_lock_enter(sqlp);
321 }
322 s->s_count--;
323 disp_lock_exit(sqlp);
324 return (0);
325 }
326
327 /*
328 * the semaphore's count is incremented by one. a blocked thread
329 * is awakened and re-tries to acquire the semaphore.
330 */
331 void
332 sema_v(ksema_t *sp)
333 {
334 sema_impl_t *s;
335 kthread_t *sq, *tp;
336 disp_lock_t *sqlp;
337
338 /* no-op during panic */
339 if (panicstr)
340 return;
341
342 s = (sema_impl_t *)sp;
343 sqlp = &SQHASH(s)->sq_lock;
344 disp_lock_enter(sqlp);
345 s->s_count++;
346 sq = s->s_slpq;
347 if (sq != NULL) {
348 tp = sq;
349 ASSERT(THREAD_LOCK_HELD(tp));
350 sq = sq->t_link;
351 tp->t_link = NULL;
352 DTRACE_SCHED1(wakeup, kthread_t *, tp);
353 tp->t_sobj_ops = NULL;
354 tp->t_wchan = NULL;
355 ASSERT(tp->t_state == TS_SLEEP);
356 CL_WAKEUP(tp);
357 s->s_slpq = sq;
358 disp_lock_exit_high(sqlp);
359 thread_unlock(tp);
360 } else {
361 disp_lock_exit(sqlp);
362 }
363 }
364
365 /*
366 * try to acquire the semaphore. if the semaphore is greater than
367 * zero, then the semaphore is granted and returns 1. otherwise
368 * return 0.
369 */
370 int
371 sema_tryp(ksema_t *sp)
372 {
373 sema_impl_t *s;
374 sleepq_head_t *sqh;
375
376 int gotit = 0;
377
378 /* no-op during panic */
379 if (panicstr)
380 return (1);
381
382 s = (sema_impl_t *)sp;
383 sqh = SQHASH(s);
384 disp_lock_enter(&sqh->sq_lock);
385 if (s->s_count > 0) {
386 s->s_count--;
387 gotit = 1;
388 }
389 disp_lock_exit(&sqh->sq_lock);
390 return (gotit);
391 }
392
393 int
394 sema_held(ksema_t *sp)
395 {
396 sema_impl_t *s;
397
398 /* no-op during panic */
399 if (panicstr)
400 return (1);
401
402 s = (sema_impl_t *)sp;
403 return (s->s_count <= 0);
404 }
|