source: mainline/uspace/lib/c/generic/fibril_synch.c@ 3ca2e36

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3ca2e36 was 45cbcaf4, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

avoid deadlocks by replacing gettimeofday() with getuptime() in lib/async.c and lib/fibril_sync.c

  • Property mode set to 100644
File size: 12.8 KB
Line 
1/*
2 * Copyright (c) 2009 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <fibril_synch.h>
36#include <fibril.h>
37#include <async.h>
38#include <adt/list.h>
39#include <futex.h>
40#include <sys/time.h>
41#include <errno.h>
42#include <assert.h>
43#include <stacktrace.h>
44#include <stdlib.h>
45#include <stdio.h>
46#include "private/async.h"
47
48static void optimize_execution_power(void)
49{
50 /*
51 * When waking up a worker fibril previously blocked in fibril
52 * synchronization, chances are that there is an idle manager fibril
53 * waiting for IPC, that could start executing the awakened worker
54 * fibril right away. We try to detect this and bring the manager
55 * fibril back to fruitful work.
56 */
57 if (atomic_get(&threads_in_ipc_wait) > 0)
58 async_poke();
59}
60
61static void print_deadlock(fibril_owner_info_t *oi)
62{
63 fibril_t *f = (fibril_t *) fibril_get_id();
64
65 printf("Deadlock detected.\n");
66 stacktrace_print();
67
68 printf("Fibril %p waits for primitive %p.\n", f, oi);
69
70 while (oi && oi->owned_by) {
71 printf("Primitive %p is owned by fibril %p.\n",
72 oi, oi->owned_by);
73 if (oi->owned_by == f)
74 break;
75 stacktrace_print_fp_pc(context_get_fp(&oi->owned_by->ctx),
76 oi->owned_by->ctx.pc);
77 printf("Fibril %p waits for primitive %p.\n",
78 oi->owned_by, oi->owned_by->waits_for);
79 oi = oi->owned_by->waits_for;
80 }
81}
82
83
84static void check_for_deadlock(fibril_owner_info_t *oi)
85{
86 while (oi && oi->owned_by) {
87 if (oi->owned_by == (fibril_t *) fibril_get_id()) {
88 print_deadlock(oi);
89 abort();
90 }
91 oi = oi->owned_by->waits_for;
92 }
93}
94
95
96void fibril_mutex_initialize(fibril_mutex_t *fm)
97{
98 fm->oi.owned_by = NULL;
99 fm->counter = 1;
100 list_initialize(&fm->waiters);
101}
102
103void fibril_mutex_lock(fibril_mutex_t *fm)
104{
105 fibril_t *f = (fibril_t *) fibril_get_id();
106
107 if (fibril_get_sercount() != 0)
108 abort();
109
110 futex_down(&async_futex);
111 if (fm->counter-- <= 0) {
112 awaiter_t wdata;
113
114 awaiter_initialize(&wdata);
115 wdata.fid = fibril_get_id();
116 wdata.wu_event.inlist = true;
117 list_append(&wdata.wu_event.link, &fm->waiters);
118 check_for_deadlock(&fm->oi);
119 f->waits_for = &fm->oi;
120 fibril_switch(FIBRIL_TO_MANAGER);
121 } else {
122 fm->oi.owned_by = f;
123 futex_up(&async_futex);
124 }
125}
126
127bool fibril_mutex_trylock(fibril_mutex_t *fm)
128{
129 bool locked = false;
130
131 futex_down(&async_futex);
132 if (fm->counter > 0) {
133 fm->counter--;
134 fm->oi.owned_by = (fibril_t *) fibril_get_id();
135 locked = true;
136 }
137 futex_up(&async_futex);
138
139 return locked;
140}
141
142static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
143{
144 if (fm->counter++ < 0) {
145 link_t *tmp;
146 awaiter_t *wdp;
147 fibril_t *f;
148
149 tmp = list_first(&fm->waiters);
150 assert(tmp != NULL);
151 wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
152 wdp->active = true;
153 wdp->wu_event.inlist = false;
154
155 f = (fibril_t *) wdp->fid;
156 fm->oi.owned_by = f;
157 f->waits_for = NULL;
158
159 list_remove(&wdp->wu_event.link);
160 fibril_add_ready(wdp->fid);
161 optimize_execution_power();
162 } else {
163 fm->oi.owned_by = NULL;
164 }
165}
166
167void fibril_mutex_unlock(fibril_mutex_t *fm)
168{
169 assert(fibril_mutex_is_locked(fm));
170 futex_down(&async_futex);
171 _fibril_mutex_unlock_unsafe(fm);
172 futex_up(&async_futex);
173}
174
175bool fibril_mutex_is_locked(fibril_mutex_t *fm)
176{
177 bool locked = false;
178
179 futex_down(&async_futex);
180 if (fm->counter <= 0)
181 locked = true;
182 futex_up(&async_futex);
183
184 return locked;
185}
186
187void fibril_rwlock_initialize(fibril_rwlock_t *frw)
188{
189 frw->oi.owned_by = NULL;
190 frw->writers = 0;
191 frw->readers = 0;
192 list_initialize(&frw->waiters);
193}
194
195void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
196{
197 fibril_t *f = (fibril_t *) fibril_get_id();
198
199 if (fibril_get_sercount() != 0)
200 abort();
201
202 futex_down(&async_futex);
203 if (frw->writers) {
204 awaiter_t wdata;
205
206 awaiter_initialize(&wdata);
207 wdata.fid = (fid_t) f;
208 wdata.wu_event.inlist = true;
209 f->flags &= ~FIBRIL_WRITER;
210 list_append(&wdata.wu_event.link, &frw->waiters);
211 check_for_deadlock(&frw->oi);
212 f->waits_for = &frw->oi;
213 fibril_switch(FIBRIL_TO_MANAGER);
214 } else {
215 /* Consider the first reader the owner. */
216 if (frw->readers++ == 0)
217 frw->oi.owned_by = f;
218 futex_up(&async_futex);
219 }
220}
221
222void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
223{
224 fibril_t *f = (fibril_t *) fibril_get_id();
225
226 if (fibril_get_sercount() != 0)
227 abort();
228
229 futex_down(&async_futex);
230 if (frw->writers || frw->readers) {
231 awaiter_t wdata;
232
233 awaiter_initialize(&wdata);
234 wdata.fid = (fid_t) f;
235 wdata.wu_event.inlist = true;
236 f->flags |= FIBRIL_WRITER;
237 list_append(&wdata.wu_event.link, &frw->waiters);
238 check_for_deadlock(&frw->oi);
239 f->waits_for = &frw->oi;
240 fibril_switch(FIBRIL_TO_MANAGER);
241 } else {
242 frw->oi.owned_by = f;
243 frw->writers++;
244 futex_up(&async_futex);
245 }
246}
247
248static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw)
249{
250 futex_down(&async_futex);
251 if (frw->readers) {
252 if (--frw->readers) {
253 if (frw->oi.owned_by == (fibril_t *) fibril_get_id()) {
254 /*
255 * If this reader firbril was considered the
256 * owner of this rwlock, clear the ownership
257 * information even if there are still more
258 * readers.
259 *
260 * This is the limitation of the detection
261 * mechanism rooted in the fact that tracking
262 * all readers would require dynamically
263 * allocated memory for keeping linkage info.
264 */
265 frw->oi.owned_by = NULL;
266 }
267 goto out;
268 }
269 } else {
270 frw->writers--;
271 }
272
273 assert(!frw->readers && !frw->writers);
274
275 frw->oi.owned_by = NULL;
276
277 while (!list_empty(&frw->waiters)) {
278 link_t *tmp = list_first(&frw->waiters);
279 awaiter_t *wdp;
280 fibril_t *f;
281
282 wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
283 f = (fibril_t *) wdp->fid;
284
285 f->waits_for = NULL;
286
287 if (f->flags & FIBRIL_WRITER) {
288 if (frw->readers)
289 break;
290 wdp->active = true;
291 wdp->wu_event.inlist = false;
292 list_remove(&wdp->wu_event.link);
293 fibril_add_ready(wdp->fid);
294 frw->writers++;
295 frw->oi.owned_by = f;
296 optimize_execution_power();
297 break;
298 } else {
299 wdp->active = true;
300 wdp->wu_event.inlist = false;
301 list_remove(&wdp->wu_event.link);
302 fibril_add_ready(wdp->fid);
303 if (frw->readers++ == 0) {
304 /* Consider the first reader the owner. */
305 frw->oi.owned_by = f;
306 }
307 optimize_execution_power();
308 }
309 }
310out:
311 futex_up(&async_futex);
312}
313
314void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
315{
316 assert(fibril_rwlock_is_read_locked(frw));
317 _fibril_rwlock_common_unlock(frw);
318}
319
320void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
321{
322 assert(fibril_rwlock_is_write_locked(frw));
323 _fibril_rwlock_common_unlock(frw);
324}
325
326bool fibril_rwlock_is_read_locked(fibril_rwlock_t *frw)
327{
328 bool locked = false;
329
330 futex_down(&async_futex);
331 if (frw->readers)
332 locked = true;
333 futex_up(&async_futex);
334
335 return locked;
336}
337
338bool fibril_rwlock_is_write_locked(fibril_rwlock_t *frw)
339{
340 bool locked = false;
341
342 futex_down(&async_futex);
343 if (frw->writers) {
344 assert(frw->writers == 1);
345 locked = true;
346 }
347 futex_up(&async_futex);
348
349 return locked;
350}
351
352bool fibril_rwlock_is_locked(fibril_rwlock_t *frw)
353{
354 return fibril_rwlock_is_read_locked(frw) ||
355 fibril_rwlock_is_write_locked(frw);
356}
357
358void fibril_condvar_initialize(fibril_condvar_t *fcv)
359{
360 list_initialize(&fcv->waiters);
361}
362
363int
364fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
365 suseconds_t timeout)
366{
367 awaiter_t wdata;
368
369 assert(fibril_mutex_is_locked(fm));
370
371 if (timeout < 0)
372 return ETIMEOUT;
373
374 awaiter_initialize(&wdata);
375 wdata.fid = fibril_get_id();
376 wdata.to_event.inlist = timeout > 0;
377 wdata.wu_event.inlist = true;
378
379 futex_down(&async_futex);
380 if (timeout) {
381 getuptime(&wdata.to_event.expires);
382 tv_add(&wdata.to_event.expires, timeout);
383 async_insert_timeout(&wdata);
384 }
385 list_append(&wdata.wu_event.link, &fcv->waiters);
386 _fibril_mutex_unlock_unsafe(fm);
387 fibril_switch(FIBRIL_TO_MANAGER);
388 fibril_mutex_lock(fm);
389
390 /* async_futex not held after fibril_switch() */
391 futex_down(&async_futex);
392 if (wdata.to_event.inlist)
393 list_remove(&wdata.to_event.link);
394 if (wdata.wu_event.inlist)
395 list_remove(&wdata.wu_event.link);
396 futex_up(&async_futex);
397
398 return wdata.to_event.occurred ? ETIMEOUT : EOK;
399}
400
401void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm)
402{
403 int rc;
404
405 rc = fibril_condvar_wait_timeout(fcv, fm, 0);
406 assert(rc == EOK);
407}
408
409static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once)
410{
411 link_t *tmp;
412 awaiter_t *wdp;
413
414 futex_down(&async_futex);
415 while (!list_empty(&fcv->waiters)) {
416 tmp = list_first(&fcv->waiters);
417 wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
418 list_remove(&wdp->wu_event.link);
419 wdp->wu_event.inlist = false;
420 if (!wdp->active) {
421 wdp->active = true;
422 fibril_add_ready(wdp->fid);
423 optimize_execution_power();
424 if (once)
425 break;
426 }
427 }
428 futex_up(&async_futex);
429}
430
431void fibril_condvar_signal(fibril_condvar_t *fcv)
432{
433 _fibril_condvar_wakeup_common(fcv, true);
434}
435
436void fibril_condvar_broadcast(fibril_condvar_t *fcv)
437{
438 _fibril_condvar_wakeup_common(fcv, false);
439}
440
441/** Timer fibril.
442 *
443 * @param arg Timer
444 */
445static int fibril_timer_func(void *arg)
446{
447 fibril_timer_t *timer = (fibril_timer_t *) arg;
448 int rc;
449
450 fibril_mutex_lock(&timer->lock);
451
452 while (true) {
453 while (timer->state != fts_active &&
454 timer->state != fts_cleanup) {
455
456 if (timer->state == fts_cleanup)
457 break;
458
459 fibril_condvar_wait(&timer->cv, &timer->lock);
460 }
461
462 if (timer->state == fts_cleanup)
463 break;
464
465 rc = fibril_condvar_wait_timeout(&timer->cv, &timer->lock,
466 timer->delay);
467 if (rc == ETIMEOUT) {
468 timer->state = fts_fired;
469 fibril_mutex_unlock(&timer->lock);
470 timer->fun(timer->arg);
471 fibril_mutex_lock(&timer->lock);
472 }
473 }
474
475 fibril_mutex_unlock(&timer->lock);
476 return 0;
477}
478
479/** Create new timer.
480 *
481 * @return New timer on success, @c NULL if out of memory.
482 */
483fibril_timer_t *fibril_timer_create(void)
484{
485 fid_t fid;
486 fibril_timer_t *timer;
487
488 timer = calloc(1, sizeof(fibril_timer_t));
489 if (timer == NULL)
490 return NULL;
491
492 fid = fibril_create(fibril_timer_func, (void *) timer);
493 if (fid == 0) {
494 free(timer);
495 return NULL;
496 }
497
498 fibril_mutex_initialize(&timer->lock);
499 fibril_condvar_initialize(&timer->cv);
500
501 timer->fibril = fid;
502 timer->state = fts_not_set;
503
504 fibril_add_ready(fid);
505
506 return timer;
507}
508
509/** Destroy timer.
510 *
511 * @param timer Timer, must not be active or accessed by other threads.
512 */
513void fibril_timer_destroy(fibril_timer_t *timer)
514{
515 fibril_mutex_lock(&timer->lock);
516 assert(timer->state != fts_active);
517 timer->state = fts_cleanup;
518 fibril_condvar_broadcast(&timer->cv);
519 fibril_mutex_unlock(&timer->lock);
520}
521
522/** Set timer.
523 *
524 * Set timer to execute a callback function after the specified
525 * interval.
526 *
527 * @param timer Timer
528 * @param delay Delay in microseconds
529 * @param fun Callback function
530 * @param arg Argument for @a fun
531 */
532void fibril_timer_set(fibril_timer_t *timer, suseconds_t delay,
533 fibril_timer_fun_t fun, void *arg)
534{
535 fibril_mutex_lock(&timer->lock);
536 timer->state = fts_active;
537 timer->delay = delay;
538 timer->fun = fun;
539 timer->arg = arg;
540 fibril_condvar_broadcast(&timer->cv);
541 fibril_mutex_unlock(&timer->lock);
542}
543
544/** Clear timer.
545 *
546 * Clears (cancels) timer and returns last state of the timer.
547 * This can be one of:
548 * - fts_not_set If the timer has not been set or has been cleared
549 * - fts_active Timer was set but did not fire
550 * - fts_fired Timer fired
551 *
552 * @param timer Timer
553 * @return Last timer state
554 */
555fibril_timer_state_t fibril_timer_clear(fibril_timer_t *timer)
556{
557 fibril_timer_state_t old_state;
558
559 fibril_mutex_lock(&timer->lock);
560 old_state = timer->state;
561 timer->state = fts_not_set;
562
563 timer->delay = 0;
564 timer->fun = NULL;
565 timer->arg = NULL;
566 fibril_condvar_broadcast(&timer->cv);
567 fibril_mutex_unlock(&timer->lock);
568
569 return old_state;
570}
571
572/** @}
573 */
Note: See TracBrowser for help on using the repository browser.