source: mainline/uspace/lib/c/generic/fibril_synch.c@ ab9f443

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ab9f443 was 47c9a8c, checked in by Jakub Jermar <jakub@…>, 13 years ago

Small improvements in the async framework.

  • Provide functions for initialization of amsg_t and awaiter_t.
  • Add async_forget() to mark messages that can be discarded right on their arrival because the recipient is no longer interested in the reply.
  • Add a couple of asserts to make sure that the async_wait_for(), async_wait_timeout() and async_forget() are not used inappropriately.
  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[f3afd24]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
[1e4cada]35#include <fibril_synch.h>
[f3afd24]36#include <fibril.h>
37#include <async.h>
38#include <adt/list.h>
39#include <futex.h>
[cadfa8e]40#include <sys/time.h>
41#include <errno.h>
[f3afd24]42#include <assert.h>
[1fa010c]43#include <stacktrace.h>
44#include <stdlib.h>
[d161715]45#include <stdio.h>
[e26a4633]46#include "private/async.h"
[f3afd24]47
[8619f25]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)
[64d2b10]58 async_poke();
[8619f25]59}
60
[1fa010c]61static void print_deadlock(fibril_owner_info_t *oi)
62{
63 fibril_t *f = (fibril_t *) fibril_get_id();
64
[12c38f5]65 printf("Deadlock detected.\n");
66 stacktrace_print();
[1fa010c]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;
[84b7384]75 stacktrace_print_fp_pc(context_get_fp(&oi->owned_by->ctx),
[12c38f5]76 oi->owned_by->ctx.pc);
[1fa010c]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 }
[55bd76c]81}
[1fa010c]82
[55bd76c]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 }
[1fa010c]93}
94
[55bd76c]95
[f3afd24]96void fibril_mutex_initialize(fibril_mutex_t *fm)
97{
[3e20fd48]98 fm->oi.owned_by = NULL;
[f3afd24]99 fm->counter = 1;
100 list_initialize(&fm->waiters);
101}
102
103void fibril_mutex_lock(fibril_mutex_t *fm)
104{
[525df28]105 fibril_t *f = (fibril_t *) fibril_get_id();
106
[0c70f7e]107 if (fibril_get_sercount() != 0)
[47b7006]108 abort();
[0c70f7e]109
[f3afd24]110 futex_down(&async_futex);
111 if (fm->counter-- <= 0) {
[854ad23]112 awaiter_t wdata;
113
[47c9a8c]114 awaiter_initialize(&wdata);
[854ad23]115 wdata.fid = fibril_get_id();
116 wdata.wu_event.inlist = true;
117 list_append(&wdata.wu_event.link, &fm->waiters);
[55bd76c]118 check_for_deadlock(&fm->oi);
[525df28]119 f->waits_for = &fm->oi;
[f3afd24]120 fibril_switch(FIBRIL_TO_MANAGER);
121 } else {
[525df28]122 fm->oi.owned_by = f;
[f3afd24]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--;
[3e20fd48]134 fm->oi.owned_by = (fibril_t *) fibril_get_id();
[f3afd24]135 locked = true;
136 }
137 futex_up(&async_futex);
138
139 return locked;
140}
141
[9ae22ba]142static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
[f3afd24]143{
144 if (fm->counter++ < 0) {
145 link_t *tmp;
[854ad23]146 awaiter_t *wdp;
[525df28]147 fibril_t *f;
[f3afd24]148
[b72efe8]149 tmp = list_first(&fm->waiters);
150 assert(tmp != NULL);
[854ad23]151 wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
[bbb01b98]152 wdp->active = true;
[854ad23]153 wdp->wu_event.inlist = false;
[525df28]154
155 f = (fibril_t *) wdp->fid;
156 fm->oi.owned_by = f;
157 f->waits_for = NULL;
158
[854ad23]159 list_remove(&wdp->wu_event.link);
160 fibril_add_ready(wdp->fid);
[8619f25]161 optimize_execution_power();
[3e20fd48]162 } else {
163 fm->oi.owned_by = NULL;
[f3afd24]164 }
[9ae22ba]165}
166
167void fibril_mutex_unlock(fibril_mutex_t *fm)
168{
[b0a76d5]169 assert(fibril_mutex_is_locked(fm));
[9ae22ba]170 futex_down(&async_futex);
171 _fibril_mutex_unlock_unsafe(fm);
[f3afd24]172 futex_up(&async_futex);
173}
174
[b0a76d5]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
[f3afd24]187void fibril_rwlock_initialize(fibril_rwlock_t *frw)
188{
[3e20fd48]189 frw->oi.owned_by = NULL;
[92d34f0b]190 frw->writers = 0;
191 frw->readers = 0;
192 list_initialize(&frw->waiters);
[f3afd24]193}
194
195void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
196{
[9414abc]197 fibril_t *f = (fibril_t *) fibril_get_id();
198
[0c70f7e]199 if (fibril_get_sercount() != 0)
[47b7006]200 abort();
[0c70f7e]201
[92d34f0b]202 futex_down(&async_futex);
203 if (frw->writers) {
[b69bec5]204 awaiter_t wdata;
205
[47c9a8c]206 awaiter_initialize(&wdata);
[b69bec5]207 wdata.fid = (fid_t) f;
208 wdata.wu_event.inlist = true;
[92d34f0b]209 f->flags &= ~FIBRIL_WRITER;
[b69bec5]210 list_append(&wdata.wu_event.link, &frw->waiters);
[55bd76c]211 check_for_deadlock(&frw->oi);
[649efcd]212 f->waits_for = &frw->oi;
[92d34f0b]213 fibril_switch(FIBRIL_TO_MANAGER);
214 } else {
[9414abc]215 /* Consider the first reader the owner. */
216 if (frw->readers++ == 0)
217 frw->oi.owned_by = f;
[92d34f0b]218 futex_up(&async_futex);
219 }
[f3afd24]220}
221
222void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
223{
[649efcd]224 fibril_t *f = (fibril_t *) fibril_get_id();
225
[0c70f7e]226 if (fibril_get_sercount() != 0)
[47b7006]227 abort();
[0c70f7e]228
[92d34f0b]229 futex_down(&async_futex);
230 if (frw->writers || frw->readers) {
[b69bec5]231 awaiter_t wdata;
232
[47c9a8c]233 awaiter_initialize(&wdata);
[b69bec5]234 wdata.fid = (fid_t) f;
235 wdata.wu_event.inlist = true;
[92d34f0b]236 f->flags |= FIBRIL_WRITER;
[b69bec5]237 list_append(&wdata.wu_event.link, &frw->waiters);
[55bd76c]238 check_for_deadlock(&frw->oi);
[649efcd]239 f->waits_for = &frw->oi;
[92d34f0b]240 fibril_switch(FIBRIL_TO_MANAGER);
241 } else {
[649efcd]242 frw->oi.owned_by = f;
[92d34f0b]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) {
[9414abc]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 }
[92d34f0b]267 goto out;
[9414abc]268 }
[92d34f0b]269 } else {
270 frw->writers--;
271 }
272
273 assert(!frw->readers && !frw->writers);
274
[649efcd]275 frw->oi.owned_by = NULL;
276
[92d34f0b]277 while (!list_empty(&frw->waiters)) {
[b72efe8]278 link_t *tmp = list_first(&frw->waiters);
[b69bec5]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;
[92d34f0b]284
[649efcd]285 f->waits_for = NULL;
286
[92d34f0b]287 if (f->flags & FIBRIL_WRITER) {
288 if (frw->readers)
289 break;
[b69bec5]290 wdp->active = true;
291 wdp->wu_event.inlist = false;
292 list_remove(&wdp->wu_event.link);
293 fibril_add_ready(wdp->fid);
[92d34f0b]294 frw->writers++;
[649efcd]295 frw->oi.owned_by = f;
[8619f25]296 optimize_execution_power();
[92d34f0b]297 break;
298 } else {
[b69bec5]299 wdp->active = true;
300 wdp->wu_event.inlist = false;
301 list_remove(&wdp->wu_event.link);
302 fibril_add_ready(wdp->fid);
[9414abc]303 if (frw->readers++ == 0) {
304 /* Consider the first reader the owner. */
305 frw->oi.owned_by = f;
306 }
[8619f25]307 optimize_execution_power();
[92d34f0b]308 }
309 }
310out:
311 futex_up(&async_futex);
[f3afd24]312}
313
314void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
315{
[b0a76d5]316 assert(fibril_rwlock_is_read_locked(frw));
[92d34f0b]317 _fibril_rwlock_common_unlock(frw);
[f3afd24]318}
319
320void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
321{
[b0a76d5]322 assert(fibril_rwlock_is_write_locked(frw));
[92d34f0b]323 _fibril_rwlock_common_unlock(frw);
[f3afd24]324}
325
[b0a76d5]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
[c81b6f2]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
[9ae22ba]358void fibril_condvar_initialize(fibril_condvar_t *fcv)
359{
360 list_initialize(&fcv->waiters);
361}
362
[cadfa8e]363int
364fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
365 suseconds_t timeout)
[9ae22ba]366{
[cadfa8e]367 awaiter_t wdata;
368
[b0a76d5]369 assert(fibril_mutex_is_locked(fm));
370
[cadfa8e]371 if (timeout < 0)
372 return ETIMEOUT;
373
[47c9a8c]374 awaiter_initialize(&wdata);
[cadfa8e]375 wdata.fid = fibril_get_id();
376 wdata.to_event.inlist = timeout > 0;
377 wdata.wu_event.inlist = true;
[9ae22ba]378
379 futex_down(&async_futex);
[cadfa8e]380 if (timeout) {
381 gettimeofday(&wdata.to_event.expires, NULL);
382 tv_add(&wdata.to_event.expires, timeout);
383 async_insert_timeout(&wdata);
384 }
385 list_append(&wdata.wu_event.link, &fcv->waiters);
[9ae22ba]386 _fibril_mutex_unlock_unsafe(fm);
387 fibril_switch(FIBRIL_TO_MANAGER);
388 fibril_mutex_lock(fm);
[cadfa8e]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);
[9ae22ba]407}
408
409static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once)
410{
411 link_t *tmp;
[cadfa8e]412 awaiter_t *wdp;
[9ae22ba]413
414 futex_down(&async_futex);
415 while (!list_empty(&fcv->waiters)) {
[b72efe8]416 tmp = list_first(&fcv->waiters);
[cadfa8e]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 }
[9ae22ba]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
[2a3214e]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;
[7cf7ded]469 fibril_mutex_unlock(&timer->lock);
470 timer->fun(timer->arg);
471 fibril_mutex_lock(&timer->lock);
[2a3214e]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
[f3afd24]572/** @}
573 */
Note: See TracBrowser for help on using the repository browser.