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