Print this page
XXXX semaphores behavior is inconsistent in panicstr case
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/os/semaphore.c
+++ new/usr/src/uts/common/os/semaphore.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 27 /*
30 28 * This file contains the semaphore operations.
31 29 */
32 30
33 31 #include <sys/param.h>
34 32 #include <sys/types.h>
35 33 #include <sys/systm.h>
36 34 #include <sys/schedctl.h>
37 35 #include <sys/semaphore.h>
38 36 #include <sys/sema_impl.h>
39 37 #include <sys/t_lock.h>
40 38 #include <sys/thread.h>
41 39 #include <sys/proc.h>
42 40 #include <sys/cmn_err.h>
43 41 #include <sys/debug.h>
44 42 #include <sys/disp.h>
45 43 #include <sys/sobject.h>
46 44 #include <sys/cpuvar.h>
47 45 #include <sys/sleepq.h>
48 46 #include <sys/sdt.h>
49 47
50 48 static void sema_unsleep(kthread_t *t);
51 49 static void sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip);
52 50 static kthread_t *sema_owner(ksema_t *);
53 51
54 52 /*
55 53 * The sobj_ops vector exports a set of functions needed when a thread
56 54 * is asleep on a synchronization object of this type.
57 55 */
58 56 static sobj_ops_t sema_sobj_ops = {
59 57 SOBJ_SEMA, sema_owner, sema_unsleep, sema_change_pri
60 58 };
61 59
62 60 /*
63 61 * SEMA_BLOCK(sema_impl_t *s, disp_lock_t *lockp)
64 62 */
65 63 #define SEMA_BLOCK(s, lockp) \
66 64 { \
67 65 kthread_t *tp; \
68 66 kthread_t **tpp; \
69 67 pri_t cpri; \
70 68 klwp_t *lwp = ttolwp(curthread); \
71 69 ASSERT(THREAD_LOCK_HELD(curthread)); \
72 70 ASSERT(curthread != CPU->cpu_idle_thread); \
73 71 ASSERT(CPU_ON_INTR(CPU) == 0); \
74 72 ASSERT(curthread->t_wchan0 == NULL); \
75 73 ASSERT(curthread->t_wchan == NULL); \
76 74 ASSERT(curthread->t_state == TS_ONPROC); \
77 75 CL_SLEEP(curthread); \
78 76 THREAD_SLEEP(curthread, lockp); \
79 77 curthread->t_wchan = (caddr_t)s; \
80 78 curthread->t_sobj_ops = &sema_sobj_ops; \
81 79 DTRACE_SCHED(sleep); \
82 80 if (lwp != NULL) { \
83 81 lwp->lwp_ru.nvcsw++; \
84 82 (void) new_mstate(curthread, LMS_SLEEP); \
85 83 } \
86 84 cpri = DISP_PRIO(curthread); \
87 85 tpp = &s->s_slpq; \
88 86 while ((tp = *tpp) != NULL) { \
89 87 if (cpri > DISP_PRIO(tp)) \
90 88 break; \
91 89 tpp = &tp->t_link; \
92 90 } \
93 91 *tpp = curthread; \
94 92 curthread->t_link = tp; \
95 93 ASSERT(s->s_slpq != NULL); \
96 94 }
97 95
98 96 /* ARGSUSED */
99 97 void
100 98 sema_init(ksema_t *sp, unsigned count, char *name, ksema_type_t type, void *arg)
101 99 {
102 100 ((sema_impl_t *)sp)->s_count = count;
103 101 ((sema_impl_t *)sp)->s_slpq = NULL;
104 102 }
105 103
106 104 void
107 105 sema_destroy(ksema_t *sp)
108 106 {
109 107 ASSERT(((sema_impl_t *)sp)->s_slpq == NULL);
110 108 }
111 109
112 110 /*
113 111 * Put a thread on the sleep queue for this semaphore.
114 112 */
115 113 static void
116 114 sema_queue(ksema_t *sp, kthread_t *t)
117 115 {
118 116 kthread_t **tpp;
119 117 kthread_t *tp;
120 118 pri_t cpri;
121 119 sema_impl_t *s;
122 120
123 121 ASSERT(THREAD_LOCK_HELD(t));
124 122 s = (sema_impl_t *)sp;
125 123 tpp = &s->s_slpq;
126 124 cpri = DISP_PRIO(t);
127 125 while ((tp = *tpp) != NULL) {
128 126 if (cpri > DISP_PRIO(tp))
129 127 break;
130 128 tpp = &tp->t_link;
131 129 }
132 130 *tpp = t;
133 131 t->t_link = tp;
134 132 }
135 133
136 134 /*
137 135 * Remove a thread from the sleep queue for this
138 136 * semaphore.
139 137 */
140 138 static void
141 139 sema_dequeue(ksema_t *sp, kthread_t *t)
142 140 {
143 141 kthread_t **tpp;
144 142 kthread_t *tp;
145 143 sema_impl_t *s;
146 144
147 145 ASSERT(THREAD_LOCK_HELD(t));
148 146 s = (sema_impl_t *)sp;
149 147 tpp = &s->s_slpq;
150 148 while ((tp = *tpp) != NULL) {
151 149 if (tp == t) {
152 150 *tpp = t->t_link;
153 151 t->t_link = NULL;
154 152 return;
155 153 }
156 154 tpp = &tp->t_link;
157 155 }
158 156 }
159 157
160 158 /* ARGSUSED */
161 159 static kthread_t *
162 160 sema_owner(ksema_t *sp)
163 161 {
164 162 return ((kthread_t *)NULL);
165 163 }
166 164
167 165 /*
168 166 * Wakeup a thread sleeping on a semaphore, and put it
169 167 * on the dispatch queue.
170 168 * Called via SOBJ_UNSLEEP().
171 169 */
172 170 static void
173 171 sema_unsleep(kthread_t *t)
174 172 {
175 173 kthread_t **tpp;
176 174 kthread_t *tp;
177 175 sema_impl_t *s;
178 176
179 177 ASSERT(THREAD_LOCK_HELD(t));
180 178 s = (sema_impl_t *)t->t_wchan;
181 179 tpp = &s->s_slpq;
182 180 while ((tp = *tpp) != NULL) {
183 181 if (tp == t) {
184 182 *tpp = t->t_link;
185 183 t->t_link = NULL;
186 184 t->t_sobj_ops = NULL;
187 185 t->t_wchan = NULL;
188 186 t->t_wchan0 = NULL;
189 187 /*
190 188 * Change thread to transition state and
191 189 * drop the semaphore sleep queue lock.
192 190 */
193 191 THREAD_TRANSITION(t);
194 192 CL_SETRUN(t);
195 193 return;
196 194 }
197 195 tpp = &tp->t_link;
198 196 }
199 197 }
200 198
201 199 /*
202 200 * operations to perform when changing the priority
203 201 * of a thread asleep on a semaphore.
204 202 * Called via SOBJ_CHANGE_PRI() and SOBJ_CHANGE_EPRI().
205 203 */
206 204 static void
207 205 sema_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
208 206 {
209 207 ksema_t *sp;
210 208
211 209 if ((sp = (ksema_t *)t->t_wchan) != NULL) {
212 210 sema_dequeue(sp, t);
213 211 *t_prip = pri;
214 212 sema_queue(sp, t);
215 213 } else
216 214 panic("sema_change_pri: %p not on sleep queue", (void *)t);
217 215 }
218 216
219 217 /*
220 218 * the semaphore is granted when the semaphore's
221 219 * count is greater than zero and blocks when equal
222 220 * to zero.
223 221 */
↓ open down ↓ |
185 lines elided |
↑ open up ↑ |
224 222 void
225 223 sema_p(ksema_t *sp)
226 224 {
227 225 sema_impl_t *s;
228 226 disp_lock_t *sqlp;
229 227
230 228 s = (sema_impl_t *)sp;
231 229 sqlp = &SQHASH(s)->sq_lock;
232 230 disp_lock_enter(sqlp);
233 231 ASSERT(s->s_count >= 0);
232 + if (panicstr) {
233 + disp_lock_exit(sqlp);
234 + return;
235 + }
234 236 while (s->s_count == 0) {
235 - if (panicstr) {
236 - disp_lock_exit(sqlp);
237 - return;
238 - }
239 237 thread_lock_high(curthread);
240 238 SEMA_BLOCK(s, sqlp);
241 239 thread_unlock_nopreempt(curthread);
242 240 swtch();
243 241 disp_lock_enter(sqlp);
244 242 }
245 243 s->s_count--;
246 244 disp_lock_exit(sqlp);
247 245 }
248 246
249 247 /*
250 248 * similiar to sema_p except that it blocks at an interruptible
251 249 * priority. if a signal is present then return 1 otherwise 0.
252 250 */
253 251 int
254 252 sema_p_sig(ksema_t *sp)
255 253 {
256 254 kthread_t *t = curthread;
257 255 klwp_t *lwp = ttolwp(t);
258 256 int cancel_pending;
259 257 int cancelled = 0;
260 258 sema_impl_t *s;
261 259 disp_lock_t *sqlp;
262 260
263 261 if (lwp == NULL) {
264 262 sema_p(sp);
265 263 return (0);
266 264 }
267 265
268 266 cancel_pending = schedctl_cancel_pending();
269 267 s = (sema_impl_t *)sp;
270 268 sqlp = &SQHASH(s)->sq_lock;
271 269 disp_lock_enter(sqlp);
272 270 ASSERT(s->s_count >= 0);
273 271 while (s->s_count == 0) {
274 272 proc_t *p = ttoproc(t);
275 273 thread_lock_high(t);
276 274 t->t_flag |= T_WAKEABLE;
277 275 SEMA_BLOCK(s, sqlp);
278 276 lwp->lwp_asleep = 1;
279 277 lwp->lwp_sysabort = 0;
280 278 thread_unlock_nopreempt(t);
281 279 if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
282 280 setrun(t);
283 281 swtch();
284 282 t->t_flag &= ~T_WAKEABLE;
285 283 if (ISSIG(t, FORREAL) || lwp->lwp_sysabort ||
286 284 MUSTRETURN(p, t) || (cancelled = cancel_pending) != 0) {
287 285 kthread_t *sq, *tp;
288 286 lwp->lwp_asleep = 0;
289 287 lwp->lwp_sysabort = 0;
290 288 disp_lock_enter(sqlp);
291 289 sq = s->s_slpq;
292 290 /*
293 291 * in case sema_v and interrupt happen
294 292 * at the same time, we need to pass the
295 293 * sema_v to the next thread.
296 294 */
297 295 if ((sq != NULL) && (s->s_count > 0)) {
298 296 tp = sq;
299 297 ASSERT(THREAD_LOCK_HELD(tp));
300 298 sq = sq->t_link;
301 299 tp->t_link = NULL;
302 300 DTRACE_SCHED1(wakeup, kthread_t *, tp);
303 301 tp->t_sobj_ops = NULL;
304 302 tp->t_wchan = NULL;
305 303 ASSERT(tp->t_state == TS_SLEEP);
306 304 CL_WAKEUP(tp);
307 305 s->s_slpq = sq;
308 306 disp_lock_exit_high(sqlp);
309 307 thread_unlock(tp);
310 308 } else {
311 309 disp_lock_exit(sqlp);
312 310 }
313 311 if (cancelled)
314 312 schedctl_cancel_eintr();
315 313 return (1);
316 314 }
317 315 lwp->lwp_asleep = 0;
318 316 disp_lock_enter(sqlp);
319 317 }
320 318 s->s_count--;
321 319 disp_lock_exit(sqlp);
322 320 return (0);
323 321 }
324 322
325 323 /*
326 324 * the semaphore's count is incremented by one. a blocked thread
327 325 * is awakened and re-tries to acquire the semaphore.
328 326 */
329 327 void
330 328 sema_v(ksema_t *sp)
331 329 {
332 330 sema_impl_t *s;
333 331 kthread_t *sq, *tp;
334 332 disp_lock_t *sqlp;
335 333
336 334 s = (sema_impl_t *)sp;
337 335 sqlp = &SQHASH(s)->sq_lock;
338 336 disp_lock_enter(sqlp);
339 337 if (panicstr) {
340 338 disp_lock_exit(sqlp);
341 339 return;
342 340 }
343 341 s->s_count++;
344 342 sq = s->s_slpq;
345 343 if (sq != NULL) {
346 344 tp = sq;
347 345 ASSERT(THREAD_LOCK_HELD(tp));
348 346 sq = sq->t_link;
349 347 tp->t_link = NULL;
350 348 DTRACE_SCHED1(wakeup, kthread_t *, tp);
351 349 tp->t_sobj_ops = NULL;
352 350 tp->t_wchan = NULL;
353 351 ASSERT(tp->t_state == TS_SLEEP);
354 352 CL_WAKEUP(tp);
355 353 s->s_slpq = sq;
356 354 disp_lock_exit_high(sqlp);
357 355 thread_unlock(tp);
358 356 } else {
359 357 disp_lock_exit(sqlp);
360 358 }
361 359 }
362 360
363 361 /*
364 362 * try to acquire the semaphore. if the semaphore is greater than
365 363 * zero, then the semaphore is granted and returns 1. otherwise
366 364 * return 0.
367 365 */
368 366 int
↓ open down ↓ |
120 lines elided |
↑ open up ↑ |
369 367 sema_tryp(ksema_t *sp)
370 368 {
371 369 sema_impl_t *s;
372 370 sleepq_head_t *sqh;
373 371
374 372 int gotit = 0;
375 373
376 374 s = (sema_impl_t *)sp;
377 375 sqh = SQHASH(s);
378 376 disp_lock_enter(&sqh->sq_lock);
377 + if (panicstr) {
378 + disp_lock_exit(sqlp);
379 + return (1);
380 + }
379 381 if (s->s_count > 0) {
380 382 s->s_count--;
381 383 gotit = 1;
382 384 }
383 385 disp_lock_exit(&sqh->sq_lock);
384 386 return (gotit);
385 387 }
386 388
387 389 int
388 390 sema_held(ksema_t *sp)
389 391 {
390 392 sema_impl_t *s;
391 393
392 394
393 395 s = (sema_impl_t *)sp;
394 396 if (panicstr)
395 397 return (1);
396 398 else
397 399 return (s->s_count <= 0);
398 400 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX