| 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 <time.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 | #include <stacktrace.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <io/kio.h>
|
|---|
| 46 | #include <mem.h>
|
|---|
| 47 | #include <context.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "../private/async.h"
|
|---|
| 50 | #include "../private/fibril.h"
|
|---|
| 51 | #include "../private/futex.h"
|
|---|
| 52 |
|
|---|
| 53 | void fibril_rmutex_initialize(fibril_rmutex_t *m)
|
|---|
| 54 | {
|
|---|
| 55 | futex_initialize(&m->futex, 1);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void fibril_rmutex_destroy(fibril_rmutex_t *m)
|
|---|
| 59 | {
|
|---|
| 60 | futex_destroy(&m->futex);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Lock restricted mutex.
|
|---|
| 65 | * When a restricted mutex is locked, the fibril may not sleep or create new
|
|---|
| 66 | * threads. Any attempt to do so will abort the program.
|
|---|
| 67 | */
|
|---|
| 68 | void fibril_rmutex_lock(fibril_rmutex_t *m)
|
|---|
| 69 | {
|
|---|
| 70 | futex_lock(&m->futex);
|
|---|
| 71 | fibril_self()->rmutex_locks++;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | bool fibril_rmutex_trylock(fibril_rmutex_t *m)
|
|---|
| 75 | {
|
|---|
| 76 | if (futex_trylock(&m->futex)) {
|
|---|
| 77 | fibril_self()->rmutex_locks++;
|
|---|
| 78 | return true;
|
|---|
| 79 | } else {
|
|---|
| 80 | return false;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void fibril_rmutex_unlock(fibril_rmutex_t *m)
|
|---|
| 85 | {
|
|---|
| 86 | fibril_self()->rmutex_locks--;
|
|---|
| 87 | futex_unlock(&m->futex);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static fibril_local bool deadlocked = false;
|
|---|
| 91 |
|
|---|
| 92 | static futex_t fibril_synch_futex = FUTEX_INITIALIZER;
|
|---|
| 93 |
|
|---|
| 94 | typedef struct {
|
|---|
| 95 | link_t link;
|
|---|
| 96 | fibril_event_t event;
|
|---|
| 97 | fibril_mutex_t *mutex;
|
|---|
| 98 | fid_t fid;
|
|---|
| 99 | } awaiter_t;
|
|---|
| 100 |
|
|---|
| 101 | #define AWAITER_INIT { .fid = fibril_get_id() }
|
|---|
| 102 |
|
|---|
| 103 | static void print_deadlock(fibril_owner_info_t *oi)
|
|---|
| 104 | {
|
|---|
| 105 | // FIXME: Print to stderr.
|
|---|
| 106 |
|
|---|
| 107 | fibril_t *f = (fibril_t *) fibril_get_id();
|
|---|
| 108 |
|
|---|
| 109 | if (deadlocked) {
|
|---|
| 110 | kio_printf("Deadlock detected while printing deadlock. Aborting.\n");
|
|---|
| 111 | abort();
|
|---|
| 112 | }
|
|---|
| 113 | deadlocked = true;
|
|---|
| 114 |
|
|---|
| 115 | printf("Deadlock detected.\n");
|
|---|
| 116 | stacktrace_print();
|
|---|
| 117 |
|
|---|
| 118 | printf("Fibril %p waits for primitive %p.\n", f, oi);
|
|---|
| 119 |
|
|---|
| 120 | while (oi && oi->owned_by) {
|
|---|
| 121 | printf("Primitive %p is owned by fibril %p.\n",
|
|---|
| 122 | oi, oi->owned_by);
|
|---|
| 123 | if (oi->owned_by == f)
|
|---|
| 124 | break;
|
|---|
| 125 | stacktrace_print_fp_pc(
|
|---|
| 126 | context_get_fp(&oi->owned_by->ctx),
|
|---|
| 127 | context_get_pc(&oi->owned_by->ctx));
|
|---|
| 128 | printf("Fibril %p waits for primitive %p.\n",
|
|---|
| 129 | oi->owned_by, oi->owned_by->waits_for);
|
|---|
| 130 | oi = oi->owned_by->waits_for;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static void check_fibril_for_deadlock(fibril_owner_info_t *oi, fibril_t *fib)
|
|---|
| 135 | {
|
|---|
| 136 | futex_assert_is_locked(&fibril_synch_futex);
|
|---|
| 137 |
|
|---|
| 138 | while (oi && oi->owned_by) {
|
|---|
| 139 | if (oi->owned_by == fib) {
|
|---|
| 140 | futex_unlock(&fibril_synch_futex);
|
|---|
| 141 | print_deadlock(oi);
|
|---|
| 142 | abort();
|
|---|
| 143 | }
|
|---|
| 144 | oi = oi->owned_by->waits_for;
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static void check_for_deadlock(fibril_owner_info_t *oi)
|
|---|
| 149 | {
|
|---|
| 150 | check_fibril_for_deadlock(oi, fibril_self());
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void fibril_mutex_initialize(fibril_mutex_t *fm)
|
|---|
| 154 | {
|
|---|
| 155 | fm->oi.owned_by = NULL;
|
|---|
| 156 | fm->counter = 1;
|
|---|
| 157 | list_initialize(&fm->waiters);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | void fibril_mutex_lock(fibril_mutex_t *fm)
|
|---|
| 161 | {
|
|---|
| 162 | fibril_t *f = (fibril_t *) fibril_get_id();
|
|---|
| 163 |
|
|---|
| 164 | futex_lock(&fibril_synch_futex);
|
|---|
| 165 |
|
|---|
| 166 | if (fm->counter-- > 0) {
|
|---|
| 167 | fm->oi.owned_by = f;
|
|---|
| 168 | futex_unlock(&fibril_synch_futex);
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 173 | list_append(&wdata.link, &fm->waiters);
|
|---|
| 174 | check_for_deadlock(&fm->oi);
|
|---|
| 175 | f->waits_for = &fm->oi;
|
|---|
| 176 |
|
|---|
| 177 | futex_unlock(&fibril_synch_futex);
|
|---|
| 178 |
|
|---|
| 179 | fibril_wait_for(&wdata.event);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | bool fibril_mutex_trylock(fibril_mutex_t *fm)
|
|---|
| 183 | {
|
|---|
| 184 | bool locked = false;
|
|---|
| 185 |
|
|---|
| 186 | futex_lock(&fibril_synch_futex);
|
|---|
| 187 | if (fm->counter > 0) {
|
|---|
| 188 | fm->counter--;
|
|---|
| 189 | fm->oi.owned_by = (fibril_t *) fibril_get_id();
|
|---|
| 190 | locked = true;
|
|---|
| 191 | }
|
|---|
| 192 | futex_unlock(&fibril_synch_futex);
|
|---|
| 193 |
|
|---|
| 194 | return locked;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
|
|---|
| 198 | {
|
|---|
| 199 | assert(fm->oi.owned_by == (fibril_t *) fibril_get_id());
|
|---|
| 200 |
|
|---|
| 201 | if (fm->counter++ < 0) {
|
|---|
| 202 | awaiter_t *wdp = list_pop(&fm->waiters, awaiter_t, link);
|
|---|
| 203 | assert(wdp);
|
|---|
| 204 |
|
|---|
| 205 | fibril_t *f = (fibril_t *) wdp->fid;
|
|---|
| 206 | fm->oi.owned_by = f;
|
|---|
| 207 | f->waits_for = NULL;
|
|---|
| 208 |
|
|---|
| 209 | fibril_notify(&wdp->event);
|
|---|
| 210 | } else {
|
|---|
| 211 | fm->oi.owned_by = NULL;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | void fibril_mutex_unlock(fibril_mutex_t *fm)
|
|---|
| 216 | {
|
|---|
| 217 | futex_lock(&fibril_synch_futex);
|
|---|
| 218 | _fibril_mutex_unlock_unsafe(fm);
|
|---|
| 219 | futex_unlock(&fibril_synch_futex);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | bool fibril_mutex_is_locked(fibril_mutex_t *fm)
|
|---|
| 223 | {
|
|---|
| 224 | futex_lock(&fibril_synch_futex);
|
|---|
| 225 | bool locked = (fm->oi.owned_by == (fibril_t *) fibril_get_id());
|
|---|
| 226 | futex_unlock(&fibril_synch_futex);
|
|---|
| 227 | return locked;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | void fibril_rwlock_initialize(fibril_rwlock_t *frw)
|
|---|
| 231 | {
|
|---|
| 232 | frw->oi.owned_by = NULL;
|
|---|
| 233 | frw->writers = 0;
|
|---|
| 234 | frw->readers = 0;
|
|---|
| 235 | list_initialize(&frw->waiters);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
|
|---|
| 239 | {
|
|---|
| 240 | fibril_t *f = (fibril_t *) fibril_get_id();
|
|---|
| 241 |
|
|---|
| 242 | futex_lock(&fibril_synch_futex);
|
|---|
| 243 |
|
|---|
| 244 | if (!frw->writers) {
|
|---|
| 245 | /* Consider the first reader the owner. */
|
|---|
| 246 | if (frw->readers++ == 0)
|
|---|
| 247 | frw->oi.owned_by = f;
|
|---|
| 248 | futex_unlock(&fibril_synch_futex);
|
|---|
| 249 | return;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | f->is_writer = false;
|
|---|
| 253 |
|
|---|
| 254 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 255 | list_append(&wdata.link, &frw->waiters);
|
|---|
| 256 | check_for_deadlock(&frw->oi);
|
|---|
| 257 | f->waits_for = &frw->oi;
|
|---|
| 258 |
|
|---|
| 259 | futex_unlock(&fibril_synch_futex);
|
|---|
| 260 |
|
|---|
| 261 | fibril_wait_for(&wdata.event);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
|
|---|
| 265 | {
|
|---|
| 266 | fibril_t *f = (fibril_t *) fibril_get_id();
|
|---|
| 267 |
|
|---|
| 268 | futex_lock(&fibril_synch_futex);
|
|---|
| 269 |
|
|---|
| 270 | if (!frw->writers && !frw->readers) {
|
|---|
| 271 | frw->oi.owned_by = f;
|
|---|
| 272 | frw->writers++;
|
|---|
| 273 | futex_unlock(&fibril_synch_futex);
|
|---|
| 274 | return;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | f->is_writer = true;
|
|---|
| 278 |
|
|---|
| 279 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 280 | list_append(&wdata.link, &frw->waiters);
|
|---|
| 281 | check_for_deadlock(&frw->oi);
|
|---|
| 282 | f->waits_for = &frw->oi;
|
|---|
| 283 |
|
|---|
| 284 | futex_unlock(&fibril_synch_futex);
|
|---|
| 285 |
|
|---|
| 286 | fibril_wait_for(&wdata.event);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw)
|
|---|
| 290 | {
|
|---|
| 291 | if (frw->readers) {
|
|---|
| 292 | if (--frw->readers) {
|
|---|
| 293 | if (frw->oi.owned_by == (fibril_t *) fibril_get_id()) {
|
|---|
| 294 | /*
|
|---|
| 295 | * If this reader fibril was considered the
|
|---|
| 296 | * owner of this rwlock, clear the ownership
|
|---|
| 297 | * information even if there are still more
|
|---|
| 298 | * readers.
|
|---|
| 299 | *
|
|---|
| 300 | * This is the limitation of the detection
|
|---|
| 301 | * mechanism rooted in the fact that tracking
|
|---|
| 302 | * all readers would require dynamically
|
|---|
| 303 | * allocated memory for keeping linkage info.
|
|---|
| 304 | */
|
|---|
| 305 | frw->oi.owned_by = NULL;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | return;
|
|---|
| 309 | }
|
|---|
| 310 | } else {
|
|---|
| 311 | frw->writers--;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | assert(!frw->readers && !frw->writers);
|
|---|
| 315 |
|
|---|
| 316 | frw->oi.owned_by = NULL;
|
|---|
| 317 |
|
|---|
| 318 | while (!list_empty(&frw->waiters)) {
|
|---|
| 319 | link_t *tmp = list_first(&frw->waiters);
|
|---|
| 320 | awaiter_t *wdp;
|
|---|
| 321 | fibril_t *f;
|
|---|
| 322 |
|
|---|
| 323 | wdp = list_get_instance(tmp, awaiter_t, link);
|
|---|
| 324 | f = (fibril_t *) wdp->fid;
|
|---|
| 325 |
|
|---|
| 326 | if (f->is_writer) {
|
|---|
| 327 | if (frw->readers)
|
|---|
| 328 | break;
|
|---|
| 329 | frw->writers++;
|
|---|
| 330 | } else {
|
|---|
| 331 | frw->readers++;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | f->waits_for = NULL;
|
|---|
| 335 | list_remove(&wdp->link);
|
|---|
| 336 | frw->oi.owned_by = f;
|
|---|
| 337 | fibril_notify(&wdp->event);
|
|---|
| 338 |
|
|---|
| 339 | if (frw->writers)
|
|---|
| 340 | break;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
|
|---|
| 345 | {
|
|---|
| 346 | futex_lock(&fibril_synch_futex);
|
|---|
| 347 | assert(frw->readers > 0);
|
|---|
| 348 | _fibril_rwlock_common_unlock(frw);
|
|---|
| 349 | futex_unlock(&fibril_synch_futex);
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
|
|---|
| 353 | {
|
|---|
| 354 | futex_lock(&fibril_synch_futex);
|
|---|
| 355 | assert(frw->writers == 1);
|
|---|
| 356 | assert(frw->oi.owned_by == fibril_self());
|
|---|
| 357 | _fibril_rwlock_common_unlock(frw);
|
|---|
| 358 | futex_unlock(&fibril_synch_futex);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | bool fibril_rwlock_is_read_locked(fibril_rwlock_t *frw)
|
|---|
| 362 | {
|
|---|
| 363 | futex_lock(&fibril_synch_futex);
|
|---|
| 364 | bool locked = (frw->readers > 0);
|
|---|
| 365 | futex_unlock(&fibril_synch_futex);
|
|---|
| 366 | return locked;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | bool fibril_rwlock_is_write_locked(fibril_rwlock_t *frw)
|
|---|
| 370 | {
|
|---|
| 371 | futex_lock(&fibril_synch_futex);
|
|---|
| 372 | assert(frw->writers <= 1);
|
|---|
| 373 | bool locked = (frw->writers > 0) && (frw->oi.owned_by == fibril_self());
|
|---|
| 374 | futex_unlock(&fibril_synch_futex);
|
|---|
| 375 | return locked;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | bool fibril_rwlock_is_locked(fibril_rwlock_t *frw)
|
|---|
| 379 | {
|
|---|
| 380 | return fibril_rwlock_is_read_locked(frw) ||
|
|---|
| 381 | fibril_rwlock_is_write_locked(frw);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | void fibril_condvar_initialize(fibril_condvar_t *fcv)
|
|---|
| 385 | {
|
|---|
| 386 | list_initialize(&fcv->waiters);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | /**
|
|---|
| 390 | * FIXME: If `timeout` is negative, the function returns ETIMEOUT immediately,
|
|---|
| 391 | * and if `timeout` is 0, the wait never times out.
|
|---|
| 392 | * This is not consistent with other similar APIs.
|
|---|
| 393 | */
|
|---|
| 394 | errno_t
|
|---|
| 395 | fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
|
|---|
| 396 | usec_t timeout)
|
|---|
| 397 | {
|
|---|
| 398 | assert(fibril_mutex_is_locked(fm));
|
|---|
| 399 |
|
|---|
| 400 | if (timeout < 0)
|
|---|
| 401 | return ETIMEOUT;
|
|---|
| 402 |
|
|---|
| 403 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 404 | wdata.mutex = fm;
|
|---|
| 405 |
|
|---|
| 406 | struct timespec ts;
|
|---|
| 407 | struct timespec *expires = NULL;
|
|---|
| 408 | if (timeout) {
|
|---|
| 409 | getuptime(&ts);
|
|---|
| 410 | ts_add_diff(&ts, USEC2NSEC(timeout));
|
|---|
| 411 | expires = &ts;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | futex_lock(&fibril_synch_futex);
|
|---|
| 415 | _fibril_mutex_unlock_unsafe(fm);
|
|---|
| 416 | list_append(&wdata.link, &fcv->waiters);
|
|---|
| 417 | futex_unlock(&fibril_synch_futex);
|
|---|
| 418 |
|
|---|
| 419 | (void) fibril_wait_timeout(&wdata.event, expires);
|
|---|
| 420 |
|
|---|
| 421 | futex_lock(&fibril_synch_futex);
|
|---|
| 422 | bool timed_out = link_in_use(&wdata.link);
|
|---|
| 423 | list_remove(&wdata.link);
|
|---|
| 424 | futex_unlock(&fibril_synch_futex);
|
|---|
| 425 |
|
|---|
| 426 | fibril_mutex_lock(fm);
|
|---|
| 427 |
|
|---|
| 428 | return timed_out ? ETIMEOUT : EOK;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm)
|
|---|
| 432 | {
|
|---|
| 433 | (void) fibril_condvar_wait_timeout(fcv, fm, 0);
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | void fibril_condvar_signal(fibril_condvar_t *fcv)
|
|---|
| 437 | {
|
|---|
| 438 | futex_lock(&fibril_synch_futex);
|
|---|
| 439 |
|
|---|
| 440 | awaiter_t *w = list_pop(&fcv->waiters, awaiter_t, link);
|
|---|
| 441 | if (w != NULL)
|
|---|
| 442 | fibril_notify(&w->event);
|
|---|
| 443 |
|
|---|
| 444 | futex_unlock(&fibril_synch_futex);
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | void fibril_condvar_broadcast(fibril_condvar_t *fcv)
|
|---|
| 448 | {
|
|---|
| 449 | futex_lock(&fibril_synch_futex);
|
|---|
| 450 |
|
|---|
| 451 | awaiter_t *w;
|
|---|
| 452 | while ((w = list_pop(&fcv->waiters, awaiter_t, link)))
|
|---|
| 453 | fibril_notify(&w->event);
|
|---|
| 454 |
|
|---|
| 455 | futex_unlock(&fibril_synch_futex);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /** Timer fibril.
|
|---|
| 459 | *
|
|---|
| 460 | * @param arg Timer
|
|---|
| 461 | */
|
|---|
| 462 | static errno_t fibril_timer_func(void *arg)
|
|---|
| 463 | {
|
|---|
| 464 | fibril_timer_t *timer = (fibril_timer_t *) arg;
|
|---|
| 465 | errno_t rc;
|
|---|
| 466 |
|
|---|
| 467 | fibril_mutex_lock(timer->lockp);
|
|---|
| 468 |
|
|---|
| 469 | while (timer->state != fts_cleanup) {
|
|---|
| 470 | switch (timer->state) {
|
|---|
| 471 | case fts_not_set:
|
|---|
| 472 | case fts_fired:
|
|---|
| 473 | fibril_condvar_wait(&timer->cv, timer->lockp);
|
|---|
| 474 | break;
|
|---|
| 475 | case fts_active:
|
|---|
| 476 | rc = fibril_condvar_wait_timeout(&timer->cv,
|
|---|
| 477 | timer->lockp, timer->delay);
|
|---|
| 478 | if (rc == ETIMEOUT && timer->state == fts_active) {
|
|---|
| 479 | timer->state = fts_fired;
|
|---|
| 480 | timer->handler_fid = fibril_get_id();
|
|---|
| 481 | fibril_mutex_unlock(timer->lockp);
|
|---|
| 482 | timer->fun(timer->arg);
|
|---|
| 483 | fibril_mutex_lock(timer->lockp);
|
|---|
| 484 | timer->handler_fid = 0;
|
|---|
| 485 | }
|
|---|
| 486 | break;
|
|---|
| 487 | case fts_cleanup:
|
|---|
| 488 | case fts_clean:
|
|---|
| 489 | assert(false);
|
|---|
| 490 | break;
|
|---|
| 491 | }
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | /* Acknowledge timer fibril has finished cleanup. */
|
|---|
| 495 | timer->state = fts_clean;
|
|---|
| 496 | fibril_condvar_broadcast(&timer->cv);
|
|---|
| 497 | fibril_mutex_unlock(timer->lockp);
|
|---|
| 498 |
|
|---|
| 499 | return 0;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | /** Create new timer.
|
|---|
| 503 | *
|
|---|
| 504 | * @return New timer on success, @c NULL if out of memory.
|
|---|
| 505 | */
|
|---|
| 506 | fibril_timer_t *fibril_timer_create(fibril_mutex_t *lock)
|
|---|
| 507 | {
|
|---|
| 508 | fid_t fid;
|
|---|
| 509 | fibril_timer_t *timer;
|
|---|
| 510 |
|
|---|
| 511 | timer = calloc(1, sizeof(fibril_timer_t));
|
|---|
| 512 | if (timer == NULL)
|
|---|
| 513 | return NULL;
|
|---|
| 514 |
|
|---|
| 515 | fid = fibril_create(fibril_timer_func, (void *) timer);
|
|---|
| 516 | if (fid == 0) {
|
|---|
| 517 | free(timer);
|
|---|
| 518 | return NULL;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | fibril_mutex_initialize(&timer->lock);
|
|---|
| 522 | fibril_condvar_initialize(&timer->cv);
|
|---|
| 523 |
|
|---|
| 524 | timer->fibril = fid;
|
|---|
| 525 | timer->state = fts_not_set;
|
|---|
| 526 | timer->lockp = (lock != NULL) ? lock : &timer->lock;
|
|---|
| 527 |
|
|---|
| 528 | fibril_add_ready(fid);
|
|---|
| 529 | return timer;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /** Destroy timer.
|
|---|
| 533 | *
|
|---|
| 534 | * @param timer Timer, must not be active or accessed by other threads.
|
|---|
| 535 | */
|
|---|
| 536 | void fibril_timer_destroy(fibril_timer_t *timer)
|
|---|
| 537 | {
|
|---|
| 538 | fibril_mutex_lock(timer->lockp);
|
|---|
| 539 | assert(timer->state == fts_not_set || timer->state == fts_fired);
|
|---|
| 540 |
|
|---|
| 541 | /* Request timer fibril to terminate. */
|
|---|
| 542 | timer->state = fts_cleanup;
|
|---|
| 543 | fibril_condvar_broadcast(&timer->cv);
|
|---|
| 544 |
|
|---|
| 545 | /* Wait for timer fibril to terminate */
|
|---|
| 546 | while (timer->state != fts_clean)
|
|---|
| 547 | fibril_condvar_wait(&timer->cv, timer->lockp);
|
|---|
| 548 | fibril_mutex_unlock(timer->lockp);
|
|---|
| 549 |
|
|---|
| 550 | free(timer);
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /** Set timer.
|
|---|
| 554 | *
|
|---|
| 555 | * Set timer to execute a callback function after the specified
|
|---|
| 556 | * interval.
|
|---|
| 557 | *
|
|---|
| 558 | * @param timer Timer
|
|---|
| 559 | * @param delay Delay in microseconds
|
|---|
| 560 | * @param fun Callback function
|
|---|
| 561 | * @param arg Argument for @a fun
|
|---|
| 562 | */
|
|---|
| 563 | void fibril_timer_set(fibril_timer_t *timer, usec_t delay,
|
|---|
| 564 | fibril_timer_fun_t fun, void *arg)
|
|---|
| 565 | {
|
|---|
| 566 | fibril_mutex_lock(timer->lockp);
|
|---|
| 567 | fibril_timer_set_locked(timer, delay, fun, arg);
|
|---|
| 568 | fibril_mutex_unlock(timer->lockp);
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | /** Set locked timer.
|
|---|
| 572 | *
|
|---|
| 573 | * Set timer to execute a callback function after the specified
|
|---|
| 574 | * interval. Must be called when the timer is locked.
|
|---|
| 575 | *
|
|---|
| 576 | * @param timer Timer
|
|---|
| 577 | * @param delay Delay in microseconds
|
|---|
| 578 | * @param fun Callback function
|
|---|
| 579 | * @param arg Argument for @a fun
|
|---|
| 580 | */
|
|---|
| 581 | void fibril_timer_set_locked(fibril_timer_t *timer, usec_t delay,
|
|---|
| 582 | fibril_timer_fun_t fun, void *arg)
|
|---|
| 583 | {
|
|---|
| 584 | assert(fibril_mutex_is_locked(timer->lockp));
|
|---|
| 585 | assert(timer->state == fts_not_set || timer->state == fts_fired);
|
|---|
| 586 | timer->state = fts_active;
|
|---|
| 587 | timer->delay = delay;
|
|---|
| 588 | timer->fun = fun;
|
|---|
| 589 | timer->arg = arg;
|
|---|
| 590 | fibril_condvar_broadcast(&timer->cv);
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | /** Clear timer.
|
|---|
| 594 | *
|
|---|
| 595 | * Clears (cancels) timer and returns last state of the timer.
|
|---|
| 596 | * This can be one of:
|
|---|
| 597 | * - fts_not_set If the timer has not been set or has been cleared
|
|---|
| 598 | * - fts_active Timer was set but did not fire
|
|---|
| 599 | * - fts_fired Timer fired
|
|---|
| 600 | *
|
|---|
| 601 | * @param timer Timer
|
|---|
| 602 | * @return Last timer state
|
|---|
| 603 | */
|
|---|
| 604 | fibril_timer_state_t fibril_timer_clear(fibril_timer_t *timer)
|
|---|
| 605 | {
|
|---|
| 606 | fibril_timer_state_t old_state;
|
|---|
| 607 |
|
|---|
| 608 | fibril_mutex_lock(timer->lockp);
|
|---|
| 609 | old_state = fibril_timer_clear_locked(timer);
|
|---|
| 610 | fibril_mutex_unlock(timer->lockp);
|
|---|
| 611 |
|
|---|
| 612 | return old_state;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | /** Clear locked timer.
|
|---|
| 616 | *
|
|---|
| 617 | * Clears (cancels) timer and returns last state of the timer.
|
|---|
| 618 | * This can be one of:
|
|---|
| 619 | * - fts_not_set If the timer has not been set or has been cleared
|
|---|
| 620 | * - fts_active Timer was set but did not fire
|
|---|
| 621 | * - fts_fired Timer fired
|
|---|
| 622 | * Must be called when the timer is locked.
|
|---|
| 623 | *
|
|---|
| 624 | * @param timer Timer
|
|---|
| 625 | * @return Last timer state
|
|---|
| 626 | */
|
|---|
| 627 | fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *timer)
|
|---|
| 628 | {
|
|---|
| 629 | fibril_timer_state_t old_state;
|
|---|
| 630 |
|
|---|
| 631 | assert(fibril_mutex_is_locked(timer->lockp));
|
|---|
| 632 |
|
|---|
| 633 | while (timer->handler_fid != 0) {
|
|---|
| 634 | if (timer->handler_fid == fibril_get_id()) {
|
|---|
| 635 | printf("Deadlock detected.\n");
|
|---|
| 636 | stacktrace_print();
|
|---|
| 637 | printf("Fibril %p is trying to clear timer %p from "
|
|---|
| 638 | "inside its handler %p.\n",
|
|---|
| 639 | fibril_get_id(), timer, timer->fun);
|
|---|
| 640 | abort();
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | fibril_condvar_wait(&timer->cv, timer->lockp);
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | old_state = timer->state;
|
|---|
| 647 | timer->state = fts_not_set;
|
|---|
| 648 |
|
|---|
| 649 | timer->delay = 0;
|
|---|
| 650 | timer->fun = NULL;
|
|---|
| 651 | timer->arg = NULL;
|
|---|
| 652 | fibril_condvar_broadcast(&timer->cv);
|
|---|
| 653 |
|
|---|
| 654 | return old_state;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | /**
|
|---|
| 658 | * Initialize a semaphore with initial count set to the provided value.
|
|---|
| 659 | *
|
|---|
| 660 | * @param sem Semaphore to initialize.
|
|---|
| 661 | * @param count Initial count. Must not be negative.
|
|---|
| 662 | */
|
|---|
| 663 | void fibril_semaphore_initialize(fibril_semaphore_t *sem, long count)
|
|---|
| 664 | {
|
|---|
| 665 | /*
|
|---|
| 666 | * Negative count denotes the length of waitlist,
|
|---|
| 667 | * so it makes no sense as an initial value.
|
|---|
| 668 | */
|
|---|
| 669 | assert(count >= 0);
|
|---|
| 670 | sem->closed = false;
|
|---|
| 671 | sem->count = count;
|
|---|
| 672 | list_initialize(&sem->waiters);
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | /**
|
|---|
| 676 | * Produce one token.
|
|---|
| 677 | * If there are fibrils waiting for tokens, this operation satisfies
|
|---|
| 678 | * exactly one waiting `fibril_semaphore_down()`.
|
|---|
| 679 | * This operation never blocks the fibril.
|
|---|
| 680 | *
|
|---|
| 681 | * @param sem Semaphore to use.
|
|---|
| 682 | */
|
|---|
| 683 | void fibril_semaphore_up(fibril_semaphore_t *sem)
|
|---|
| 684 | {
|
|---|
| 685 | futex_lock(&fibril_synch_futex);
|
|---|
| 686 |
|
|---|
| 687 | if (sem->closed) {
|
|---|
| 688 | futex_unlock(&fibril_synch_futex);
|
|---|
| 689 | return;
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | sem->count++;
|
|---|
| 693 |
|
|---|
| 694 | if (sem->count <= 0) {
|
|---|
| 695 | awaiter_t *w = list_pop(&sem->waiters, awaiter_t, link);
|
|---|
| 696 | assert(w);
|
|---|
| 697 | fibril_notify(&w->event);
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | futex_unlock(&fibril_synch_futex);
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | /**
|
|---|
| 704 | * Consume one token.
|
|---|
| 705 | * If there are no available tokens (count <= 0), this operation blocks until
|
|---|
| 706 | * another fibril produces a token using `fibril_semaphore_up()`.
|
|---|
| 707 | *
|
|---|
| 708 | * @param sem Semaphore to use.
|
|---|
| 709 | */
|
|---|
| 710 | void fibril_semaphore_down(fibril_semaphore_t *sem)
|
|---|
| 711 | {
|
|---|
| 712 | futex_lock(&fibril_synch_futex);
|
|---|
| 713 |
|
|---|
| 714 | if (sem->closed) {
|
|---|
| 715 | futex_unlock(&fibril_synch_futex);
|
|---|
| 716 | return;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | sem->count--;
|
|---|
| 720 |
|
|---|
| 721 | if (sem->count >= 0) {
|
|---|
| 722 | futex_unlock(&fibril_synch_futex);
|
|---|
| 723 | return;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 727 | list_append(&wdata.link, &sem->waiters);
|
|---|
| 728 |
|
|---|
| 729 | futex_unlock(&fibril_synch_futex);
|
|---|
| 730 |
|
|---|
| 731 | fibril_wait_for(&wdata.event);
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *sem, usec_t timeout)
|
|---|
| 735 | {
|
|---|
| 736 | if (timeout < 0)
|
|---|
| 737 | return ETIMEOUT;
|
|---|
| 738 |
|
|---|
| 739 | futex_lock(&fibril_synch_futex);
|
|---|
| 740 | if (sem->closed) {
|
|---|
| 741 | futex_unlock(&fibril_synch_futex);
|
|---|
| 742 | return EOK;
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | sem->count--;
|
|---|
| 746 |
|
|---|
| 747 | if (sem->count >= 0) {
|
|---|
| 748 | futex_unlock(&fibril_synch_futex);
|
|---|
| 749 | return EOK;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | awaiter_t wdata = AWAITER_INIT;
|
|---|
| 753 | list_append(&wdata.link, &sem->waiters);
|
|---|
| 754 |
|
|---|
| 755 | futex_unlock(&fibril_synch_futex);
|
|---|
| 756 |
|
|---|
| 757 | struct timespec ts;
|
|---|
| 758 | struct timespec *expires = NULL;
|
|---|
| 759 | if (timeout) {
|
|---|
| 760 | getuptime(&ts);
|
|---|
| 761 | ts_add_diff(&ts, USEC2NSEC(timeout));
|
|---|
| 762 | expires = &ts;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | errno_t rc = fibril_wait_timeout(&wdata.event, expires);
|
|---|
| 766 | if (rc == EOK)
|
|---|
| 767 | return EOK;
|
|---|
| 768 |
|
|---|
| 769 | futex_lock(&fibril_synch_futex);
|
|---|
| 770 | if (!link_in_use(&wdata.link)) {
|
|---|
| 771 | futex_unlock(&fibril_synch_futex);
|
|---|
| 772 | return EOK;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | list_remove(&wdata.link);
|
|---|
| 776 | sem->count++;
|
|---|
| 777 | futex_unlock(&fibril_synch_futex);
|
|---|
| 778 |
|
|---|
| 779 | return rc;
|
|---|
| 780 | }
|
|---|
| 781 |
|
|---|
| 782 | /**
|
|---|
| 783 | * Close the semaphore.
|
|---|
| 784 | * All future down() operations return instantly.
|
|---|
| 785 | */
|
|---|
| 786 | void fibril_semaphore_close(fibril_semaphore_t *sem)
|
|---|
| 787 | {
|
|---|
| 788 | futex_lock(&fibril_synch_futex);
|
|---|
| 789 | sem->closed = true;
|
|---|
| 790 | awaiter_t *w;
|
|---|
| 791 |
|
|---|
| 792 | while ((w = list_pop(&sem->waiters, awaiter_t, link)))
|
|---|
| 793 | fibril_notify(&w->event);
|
|---|
| 794 |
|
|---|
| 795 | futex_unlock(&fibril_synch_futex);
|
|---|
| 796 | }
|
|---|
| 797 |
|
|---|
| 798 | /** @}
|
|---|
| 799 | */
|
|---|