4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
32 * This file contains the semaphore operations.
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>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
47 #include <sys/sobject.h>
48 #include <sys/cpuvar.h>
49 #include <sys/sleepq.h>
52 static void sema_unsleep(kthread_t
*t
);
53 static void sema_change_pri(kthread_t
*t
, pri_t pri
, pri_t
*t_prip
);
54 static kthread_t
*sema_owner(ksema_t
*);
57 * The sobj_ops vector exports a set of functions needed when a thread
58 * is asleep on a synchronization object of this type.
60 static sobj_ops_t sema_sobj_ops
= {
61 SOBJ_SEMA
, sema_owner
, sema_unsleep
, sema_change_pri
65 * SEMA_BLOCK(sema_impl_t *s, disp_lock_t *lockp)
67 #define SEMA_BLOCK(s, lockp) \
72 klwp_t *lwp = ttolwp(curthread); \
73 ASSERT(THREAD_LOCK_HELD(curthread)); \
74 ASSERT(curthread != CPU->cpu_idle_thread); \
75 ASSERT(CPU_ON_INTR(CPU) == 0); \
76 ASSERT(curthread->t_wchan0 == NULL); \
77 ASSERT(curthread->t_wchan == NULL); \
78 ASSERT(curthread->t_state == TS_ONPROC); \
79 CL_SLEEP(curthread); \
80 THREAD_SLEEP(curthread, lockp); \
81 curthread->t_wchan = (caddr_t)s; \
82 curthread->t_sobj_ops = &sema_sobj_ops; \
83 DTRACE_SCHED(sleep); \
85 lwp->lwp_ru.nvcsw++; \
86 (void) new_mstate(curthread, LMS_SLEEP); \
88 cpri = DISP_PRIO(curthread); \
90 while ((tp = *tpp) != NULL) { \
91 if (cpri > DISP_PRIO(tp)) \
96 curthread->t_link = tp; \
97 ASSERT(s->s_slpq != NULL); \
102 sema_init(ksema_t
*sp
, unsigned count
, char *name
, ksema_type_t type
, void *arg
)
104 ((sema_impl_t
*)sp
)->s_count
= count
;
105 ((sema_impl_t
*)sp
)->s_slpq
= NULL
;
109 sema_destroy(ksema_t
*sp
)
111 ASSERT(((sema_impl_t
*)sp
)->s_slpq
== NULL
);
115 * Put a thread on the sleep queue for this semaphore.
118 sema_queue(ksema_t
*sp
, kthread_t
*t
)
125 ASSERT(THREAD_LOCK_HELD(t
));
126 s
= (sema_impl_t
*)sp
;
129 while ((tp
= *tpp
) != NULL
) {
130 if (cpri
> DISP_PRIO(tp
))
139 * Remove a thread from the sleep queue for this
143 sema_dequeue(ksema_t
*sp
, kthread_t
*t
)
149 ASSERT(THREAD_LOCK_HELD(t
));
150 s
= (sema_impl_t
*)sp
;
152 while ((tp
= *tpp
) != NULL
) {
164 sema_owner(ksema_t
*sp
)
170 * Wakeup a thread sleeping on a semaphore, and put it
171 * on the dispatch queue.
172 * Called via SOBJ_UNSLEEP().
175 sema_unsleep(kthread_t
*t
)
181 ASSERT(THREAD_LOCK_HELD(t
));
182 s
= (sema_impl_t
*)t
->t_wchan
;
184 while ((tp
= *tpp
) != NULL
) {
188 t
->t_sobj_ops
= NULL
;
192 * Change thread to transition state and
193 * drop the semaphore sleep queue lock.
195 THREAD_TRANSITION(t
);
204 * operations to perform when changing the priority
205 * of a thread asleep on a semaphore.
206 * Called via SOBJ_CHANGE_PRI() and SOBJ_CHANGE_EPRI().
209 sema_change_pri(kthread_t
*t
, pri_t pri
, pri_t
*t_prip
)
213 if ((sp
= (ksema_t
*)t
->t_wchan
) != NULL
) {
218 panic("sema_change_pri: %p not on sleep queue", (void *)t
);
222 * the semaphore is granted when the semaphore's
223 * count is greater than zero and blocks when equal
232 /* no-op during panic */
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
);
243 thread_unlock_nopreempt(curthread
);
245 disp_lock_enter(sqlp
);
248 disp_lock_exit(sqlp
);
252 * similiar to sema_p except that it blocks at an interruptible
253 * priority. if a signal is present then return 1 otherwise 0.
256 sema_p_sig(ksema_t
*sp
)
258 kthread_t
*t
= curthread
;
259 klwp_t
*lwp
= ttolwp(t
);
270 cancel_pending
= schedctl_cancel_pending();
271 s
= (sema_impl_t
*)sp
;
272 sqlp
= &SQHASH(s
)->sq_lock
;
273 disp_lock_enter(sqlp
);
274 ASSERT(s
->s_count
>= 0);
275 while (s
->s_count
== 0) {
276 proc_t
*p
= ttoproc(t
);
278 t
->t_flag
|= T_WAKEABLE
;
281 lwp
->lwp_sysabort
= 0;
282 thread_unlock_nopreempt(t
);
283 if (ISSIG(t
, JUSTLOOKING
) || MUSTRETURN(p
, t
) || cancel_pending
)
286 t
->t_flag
&= ~T_WAKEABLE
;
287 if (ISSIG(t
, FORREAL
) || lwp
->lwp_sysabort
||
288 MUSTRETURN(p
, t
) || (cancelled
= cancel_pending
) != 0) {
291 lwp
->lwp_sysabort
= 0;
292 disp_lock_enter(sqlp
);
295 * in case sema_v and interrupt happen
296 * at the same time, we need to pass the
297 * sema_v to the next thread.
299 if ((sq
!= NULL
) && (s
->s_count
> 0)) {
301 ASSERT(THREAD_LOCK_HELD(tp
));
304 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
305 tp
->t_sobj_ops
= NULL
;
307 ASSERT(tp
->t_state
== TS_SLEEP
);
310 disp_lock_exit_high(sqlp
);
313 disp_lock_exit(sqlp
);
316 schedctl_cancel_eintr();
320 disp_lock_enter(sqlp
);
323 disp_lock_exit(sqlp
);
328 * the semaphore's count is incremented by one. a blocked thread
329 * is awakened and re-tries to acquire the semaphore.
338 /* no-op during panic */
342 s
= (sema_impl_t
*)sp
;
343 sqlp
= &SQHASH(s
)->sq_lock
;
344 disp_lock_enter(sqlp
);
349 ASSERT(THREAD_LOCK_HELD(tp
));
352 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
353 tp
->t_sobj_ops
= NULL
;
355 ASSERT(tp
->t_state
== TS_SLEEP
);
358 disp_lock_exit_high(sqlp
);
361 disp_lock_exit(sqlp
);
366 * try to acquire the semaphore. if the semaphore is greater than
367 * zero, then the semaphore is granted and returns 1. otherwise
371 sema_tryp(ksema_t
*sp
)
378 /* no-op during panic */
382 s
= (sema_impl_t
*)sp
;
384 disp_lock_enter(&sqh
->sq_lock
);
385 if (s
->s_count
> 0) {
389 disp_lock_exit(&sqh
->sq_lock
);
394 sema_held(ksema_t
*sp
)
398 /* no-op during panic */
402 s
= (sema_impl_t
*)sp
;
403 return (s
->s_count
<= 0);