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.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains the semaphore operations.
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>
42 #include <sys/cmn_err.h>
43 #include <sys/debug.h>
45 #include <sys/sobject.h>
46 #include <sys/cpuvar.h>
47 #include <sys/sleepq.h>
50 static void sema_unsleep(kthread_t
*t
);
51 static void sema_change_pri(kthread_t
*t
, pri_t pri
, pri_t
*t_prip
);
52 static kthread_t
*sema_owner(ksema_t
*);
55 * The sobj_ops vector exports a set of functions needed when a thread
56 * is asleep on a synchronization object of this type.
58 static sobj_ops_t sema_sobj_ops
= {
59 SOBJ_SEMA
, sema_owner
, sema_unsleep
, sema_change_pri
63 * SEMA_BLOCK(sema_impl_t *s, disp_lock_t *lockp)
65 #define SEMA_BLOCK(s, lockp) \
70 klwp_t *lwp = ttolwp(curthread); \
71 ASSERT(THREAD_LOCK_HELD(curthread)); \
72 ASSERT(curthread != CPU->cpu_idle_thread); \
73 ASSERT(CPU_ON_INTR(CPU) == 0); \
74 ASSERT(curthread->t_wchan0 == NULL); \
75 ASSERT(curthread->t_wchan == NULL); \
76 ASSERT(curthread->t_state == TS_ONPROC); \
77 CL_SLEEP(curthread); \
78 THREAD_SLEEP(curthread, lockp); \
79 curthread->t_wchan = (caddr_t)s; \
80 curthread->t_sobj_ops = &sema_sobj_ops; \
81 DTRACE_SCHED(sleep); \
83 lwp->lwp_ru.nvcsw++; \
84 (void) new_mstate(curthread, LMS_SLEEP); \
86 cpri = DISP_PRIO(curthread); \
88 while ((tp = *tpp) != NULL) { \
89 if (cpri > DISP_PRIO(tp)) \
94 curthread->t_link = tp; \
95 ASSERT(s->s_slpq != NULL); \
100 sema_init(ksema_t
*sp
, unsigned count
, char *name
, ksema_type_t type
, void *arg
)
102 ((sema_impl_t
*)sp
)->s_count
= count
;
103 ((sema_impl_t
*)sp
)->s_slpq
= NULL
;
107 sema_destroy(ksema_t
*sp
)
109 ASSERT(((sema_impl_t
*)sp
)->s_slpq
== NULL
);
113 * Put a thread on the sleep queue for this semaphore.
116 sema_queue(ksema_t
*sp
, kthread_t
*t
)
123 ASSERT(THREAD_LOCK_HELD(t
));
124 s
= (sema_impl_t
*)sp
;
127 while ((tp
= *tpp
) != NULL
) {
128 if (cpri
> DISP_PRIO(tp
))
137 * Remove a thread from the sleep queue for this
141 sema_dequeue(ksema_t
*sp
, kthread_t
*t
)
147 ASSERT(THREAD_LOCK_HELD(t
));
148 s
= (sema_impl_t
*)sp
;
150 while ((tp
= *tpp
) != NULL
) {
162 sema_owner(ksema_t
*sp
)
164 return ((kthread_t
*)NULL
);
168 * Wakeup a thread sleeping on a semaphore, and put it
169 * on the dispatch queue.
170 * Called via SOBJ_UNSLEEP().
173 sema_unsleep(kthread_t
*t
)
179 ASSERT(THREAD_LOCK_HELD(t
));
180 s
= (sema_impl_t
*)t
->t_wchan
;
182 while ((tp
= *tpp
) != NULL
) {
186 t
->t_sobj_ops
= NULL
;
190 * Change thread to transition state and
191 * drop the semaphore sleep queue lock.
193 THREAD_TRANSITION(t
);
202 * operations to perform when changing the priority
203 * of a thread asleep on a semaphore.
204 * Called via SOBJ_CHANGE_PRI() and SOBJ_CHANGE_EPRI().
207 sema_change_pri(kthread_t
*t
, pri_t pri
, pri_t
*t_prip
)
211 if ((sp
= (ksema_t
*)t
->t_wchan
) != NULL
) {
216 panic("sema_change_pri: %p not on sleep queue", (void *)t
);
220 * the semaphore is granted when the semaphore's
221 * count is greater than zero and blocks when equal
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) {
236 disp_lock_exit(sqlp
);
239 thread_lock_high(curthread
);
241 thread_unlock_nopreempt(curthread
);
243 disp_lock_enter(sqlp
);
246 disp_lock_exit(sqlp
);
250 * similiar to sema_p except that it blocks at an interruptible
251 * priority. if a signal is present then return 1 otherwise 0.
254 sema_p_sig(ksema_t
*sp
)
256 kthread_t
*t
= curthread
;
257 klwp_t
*lwp
= ttolwp(t
);
268 cancel_pending
= schedctl_cancel_pending();
269 s
= (sema_impl_t
*)sp
;
270 sqlp
= &SQHASH(s
)->sq_lock
;
271 disp_lock_enter(sqlp
);
272 ASSERT(s
->s_count
>= 0);
273 while (s
->s_count
== 0) {
274 proc_t
*p
= ttoproc(t
);
276 t
->t_flag
|= T_WAKEABLE
;
279 lwp
->lwp_sysabort
= 0;
280 thread_unlock_nopreempt(t
);
281 if (ISSIG(t
, JUSTLOOKING
) || MUSTRETURN(p
, t
) || cancel_pending
)
284 t
->t_flag
&= ~T_WAKEABLE
;
285 if (ISSIG(t
, FORREAL
) || lwp
->lwp_sysabort
||
286 MUSTRETURN(p
, t
) || (cancelled
= cancel_pending
) != 0) {
289 lwp
->lwp_sysabort
= 0;
290 disp_lock_enter(sqlp
);
293 * in case sema_v and interrupt happen
294 * at the same time, we need to pass the
295 * sema_v to the next thread.
297 if ((sq
!= NULL
) && (s
->s_count
> 0)) {
299 ASSERT(THREAD_LOCK_HELD(tp
));
302 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
303 tp
->t_sobj_ops
= NULL
;
305 ASSERT(tp
->t_state
== TS_SLEEP
);
308 disp_lock_exit_high(sqlp
);
311 disp_lock_exit(sqlp
);
314 schedctl_cancel_eintr();
318 disp_lock_enter(sqlp
);
321 disp_lock_exit(sqlp
);
326 * the semaphore's count is incremented by one. a blocked thread
327 * is awakened and re-tries to acquire the semaphore.
336 s
= (sema_impl_t
*)sp
;
337 sqlp
= &SQHASH(s
)->sq_lock
;
338 disp_lock_enter(sqlp
);
340 disp_lock_exit(sqlp
);
347 ASSERT(THREAD_LOCK_HELD(tp
));
350 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
351 tp
->t_sobj_ops
= NULL
;
353 ASSERT(tp
->t_state
== TS_SLEEP
);
356 disp_lock_exit_high(sqlp
);
359 disp_lock_exit(sqlp
);
364 * try to acquire the semaphore. if the semaphore is greater than
365 * zero, then the semaphore is granted and returns 1. otherwise
369 sema_tryp(ksema_t
*sp
)
376 s
= (sema_impl_t
*)sp
;
378 disp_lock_enter(&sqh
->sq_lock
);
379 if (s
->s_count
> 0) {
383 disp_lock_exit(&sqh
->sq_lock
);
388 sema_held(ksema_t
*sp
)
393 s
= (sema_impl_t
*)sp
;
397 return (s
->s_count
<= 0);