source: mainline/uspace/lib/c/generic/fibril_synch.c@ 8dc9b72

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8dc9b72 was 7c15d6f, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Never clear timer inside its handler.

  • Property mode set to 100644
File size: 14.7 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_diff(&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->lockp);
451
452 while (timer->state != fts_cleanup) {
453 switch (timer->state) {
454 case fts_not_set:
455 case fts_fired:
456 fibril_condvar_wait(&timer->cv, timer->lockp);
457 break;
458 case fts_active:
459 rc = fibril_condvar_wait_timeout(&timer->cv,
460 timer->lockp, timer->delay);
461 if (rc == ETIMEOUT && timer->state == fts_active) {
462 timer->state = fts_fired;
463 timer->handler_fid = fibril_get_id();
464 fibril_mutex_unlock(timer->lockp);
465 timer->fun(timer->arg);
466 fibril_mutex_lock(timer->lockp);
467 timer->handler_fid = 0;
468 }
469 break;
470 case fts_cleanup:
471 case fts_clean:
472 assert(false);
473 break;
474 }
475 }
476
477 /* Acknowledge timer fibril has finished cleanup. */
478 timer->state = fts_clean;
479 fibril_mutex_unlock(timer->lockp);
480 free(timer);
481
482 return 0;
483}
484
485/** Create new timer.
486 *
487 * @return New timer on success, @c NULL if out of memory.
488 */
489fibril_timer_t *fibril_timer_create(fibril_mutex_t *lock)
490{
491 fid_t fid;
492 fibril_timer_t *timer;
493
494 timer = calloc(1, sizeof(fibril_timer_t));
495 if (timer == NULL)
496 return NULL;
497
498 fid = fibril_create(fibril_timer_func, (void *) timer);
499 if (fid == 0) {
500 free(timer);
501 return NULL;
502 }
503
504 fibril_mutex_initialize(&timer->lock);
505 fibril_condvar_initialize(&timer->cv);
506
507 timer->fibril = fid;
508 timer->state = fts_not_set;
509 timer->lockp = (lock != NULL) ? lock : &timer->lock;
510
511 fibril_add_ready(fid);
512 return timer;
513}
514
515/** Destroy timer.
516 *
517 * @param timer Timer, must not be active or accessed by other threads.
518 */
519void fibril_timer_destroy(fibril_timer_t *timer)
520{
521 fibril_mutex_lock(timer->lockp);
522 assert(timer->state == fts_not_set || timer->state == fts_fired);
523
524 /* Request timer fibril to terminate. */
525 timer->state = fts_cleanup;
526 fibril_condvar_broadcast(&timer->cv);
527 fibril_mutex_unlock(timer->lockp);
528}
529
530/** Set timer.
531 *
532 * Set timer to execute a callback function after the specified
533 * interval.
534 *
535 * @param timer Timer
536 * @param delay Delay in microseconds
537 * @param fun Callback function
538 * @param arg Argument for @a fun
539 */
540void fibril_timer_set(fibril_timer_t *timer, suseconds_t delay,
541 fibril_timer_fun_t fun, void *arg)
542{
543 fibril_mutex_lock(timer->lockp);
544 fibril_timer_set_locked(timer, delay, fun, arg);
545 fibril_mutex_unlock(timer->lockp);
546}
547
548/** Set locked timer.
549 *
550 * Set timer to execute a callback function after the specified
551 * interval. Must be called when the timer is locked.
552 *
553 * @param timer Timer
554 * @param delay Delay in microseconds
555 * @param fun Callback function
556 * @param arg Argument for @a fun
557 */
558void fibril_timer_set_locked(fibril_timer_t *timer, suseconds_t delay,
559 fibril_timer_fun_t fun, void *arg)
560{
561 assert(fibril_mutex_is_locked(timer->lockp));
562 assert(timer->state == fts_not_set || timer->state == fts_fired);
563 timer->state = fts_active;
564 timer->delay = delay;
565 timer->fun = fun;
566 timer->arg = arg;
567 fibril_condvar_broadcast(&timer->cv);
568}
569
570/** Clear timer.
571 *
572 * Clears (cancels) timer and returns last state of the timer.
573 * This can be one of:
574 * - fts_not_set If the timer has not been set or has been cleared
575 * - fts_active Timer was set but did not fire
576 * - fts_fired Timer fired
577 *
578 * @param timer Timer
579 * @return Last timer state
580 */
581fibril_timer_state_t fibril_timer_clear(fibril_timer_t *timer)
582{
583 fibril_timer_state_t old_state;
584
585 fibril_mutex_lock(timer->lockp);
586 old_state = fibril_timer_clear_locked(timer);
587 fibril_mutex_unlock(timer->lockp);
588
589 return old_state;
590}
591
592/** Clear locked timer.
593 *
594 * Clears (cancels) timer and returns last state of the timer.
595 * This can be one of:
596 * - fts_not_set If the timer has not been set or has been cleared
597 * - fts_active Timer was set but did not fire
598 * - fts_fired Timer fired
599 * Must be called when the timer is locked.
600 *
601 * @param timer Timer
602 * @return Last timer state
603 */
604fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *timer)
605{
606 fibril_timer_state_t old_state;
607
608 assert(fibril_mutex_is_locked(timer->lockp));
609
610 while (timer->handler_fid != 0) {
611 if (timer->handler_fid == fibril_get_id()) {
612 printf("Deadlock detected.\n");
613 stacktrace_print();
614 printf("Fibril %zx is trying to clear timer %p from "
615 "inside its handler %p.\n",
616 fibril_get_id(), timer, timer->fun);
617 abort();
618 }
619
620 fibril_condvar_wait(&timer->cv, timer->lockp);
621 }
622
623 old_state = timer->state;
624 timer->state = fts_not_set;
625
626 timer->delay = 0;
627 timer->fun = NULL;
628 timer->arg = NULL;
629 fibril_condvar_broadcast(&timer->cv);
630
631 return old_state;
632}
633
634/** @}
635 */
Note: See TracBrowser for help on using the repository browser.