[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>
|
---|
[cadfa8e] | 38 | #include <async_priv.h>
|
---|
[f3afd24] | 39 | #include <adt/list.h>
|
---|
| 40 | #include <futex.h>
|
---|
[cadfa8e] | 41 | #include <sys/time.h>
|
---|
| 42 | #include <errno.h>
|
---|
[f3afd24] | 43 | #include <assert.h>
|
---|
[1fa010c] | 44 | #include <stacktrace.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
[f3afd24] | 46 |
|
---|
[8619f25] | 47 | static void optimize_execution_power(void)
|
---|
| 48 | {
|
---|
| 49 | /*
|
---|
| 50 | * When waking up a worker fibril previously blocked in fibril
|
---|
| 51 | * synchronization, chances are that there is an idle manager fibril
|
---|
| 52 | * waiting for IPC, that could start executing the awakened worker
|
---|
| 53 | * fibril right away. We try to detect this and bring the manager
|
---|
| 54 | * fibril back to fruitful work.
|
---|
| 55 | */
|
---|
| 56 | if (atomic_get(&threads_in_ipc_wait) > 0)
|
---|
| 57 | ipc_poke();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[1fa010c] | 60 | static bool check_for_deadlock(fibril_owner_info_t *oi)
|
---|
| 61 | {
|
---|
| 62 | while (oi && oi->owned_by) {
|
---|
| 63 | if (oi->owned_by == (fibril_t *) fibril_get_id())
|
---|
| 64 | return true;
|
---|
| 65 | oi = oi->owned_by->waits_for;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | static void print_deadlock(fibril_owner_info_t *oi)
|
---|
| 72 | {
|
---|
| 73 | fibril_t *f = (fibril_t *) fibril_get_id();
|
---|
| 74 |
|
---|
| 75 | printf("Deadlock detected: ");
|
---|
| 76 |
|
---|
| 77 | printf("Fibril %p waits for primitive %p.\n", f, oi);
|
---|
| 78 | stacktrace_print();
|
---|
| 79 |
|
---|
| 80 | while (oi && oi->owned_by) {
|
---|
| 81 | printf(". ");
|
---|
| 82 | printf("Primitive %p is owned by fibril %p.\n",
|
---|
| 83 | oi, oi->owned_by);
|
---|
| 84 | stacktrace_print_fp_pc(oi->owned_by->ctx.ebp,
|
---|
| 85 | oi->owned_by->ctx.pc);
|
---|
| 86 | if (oi->owned_by == f)
|
---|
| 87 | break;
|
---|
| 88 | printf("Fibril %p waits for primitive %p.\n",
|
---|
| 89 | oi->owned_by, oi->owned_by->waits_for);
|
---|
| 90 | oi = oi->owned_by->waits_for;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | abort();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[f3afd24] | 96 | void 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 |
|
---|
| 103 | void fibril_mutex_lock(fibril_mutex_t *fm)
|
---|
| 104 | {
|
---|
[525df28] | 105 | fibril_t *f = (fibril_t *) fibril_get_id();
|
---|
| 106 |
|
---|
[f3afd24] | 107 | futex_down(&async_futex);
|
---|
| 108 | if (fm->counter-- <= 0) {
|
---|
[854ad23] | 109 | awaiter_t wdata;
|
---|
| 110 |
|
---|
| 111 | wdata.fid = fibril_get_id();
|
---|
| 112 | wdata.active = false;
|
---|
| 113 | wdata.wu_event.inlist = true;
|
---|
| 114 | link_initialize(&wdata.wu_event.link);
|
---|
| 115 | list_append(&wdata.wu_event.link, &fm->waiters);
|
---|
[525df28] | 116 |
|
---|
[1fa010c] | 117 | if (check_for_deadlock(&fm->oi))
|
---|
| 118 | print_deadlock(&fm->oi);
|
---|
[525df28] | 119 | f->waits_for = &fm->oi;
|
---|
| 120 |
|
---|
[f3afd24] | 121 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
| 122 | } else {
|
---|
[525df28] | 123 | fm->oi.owned_by = f;
|
---|
[f3afd24] | 124 | futex_up(&async_futex);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | bool fibril_mutex_trylock(fibril_mutex_t *fm)
|
---|
| 129 | {
|
---|
| 130 | bool locked = false;
|
---|
| 131 |
|
---|
| 132 | futex_down(&async_futex);
|
---|
| 133 | if (fm->counter > 0) {
|
---|
| 134 | fm->counter--;
|
---|
[3e20fd48] | 135 | fm->oi.owned_by = (fibril_t *) fibril_get_id();
|
---|
[f3afd24] | 136 | locked = true;
|
---|
| 137 | }
|
---|
| 138 | futex_up(&async_futex);
|
---|
| 139 |
|
---|
| 140 | return locked;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[9ae22ba] | 143 | static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
|
---|
[f3afd24] | 144 | {
|
---|
| 145 | assert(fm->counter <= 0);
|
---|
| 146 | if (fm->counter++ < 0) {
|
---|
| 147 | link_t *tmp;
|
---|
[854ad23] | 148 | awaiter_t *wdp;
|
---|
[525df28] | 149 | fibril_t *f;
|
---|
[f3afd24] | 150 |
|
---|
| 151 | assert(!list_empty(&fm->waiters));
|
---|
| 152 | tmp = fm->waiters.next;
|
---|
[854ad23] | 153 | wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
|
---|
[bbb01b98] | 154 | wdp->active = true;
|
---|
[854ad23] | 155 | wdp->wu_event.inlist = false;
|
---|
[525df28] | 156 |
|
---|
| 157 | f = (fibril_t *) wdp->fid;
|
---|
| 158 | fm->oi.owned_by = f;
|
---|
| 159 | f->waits_for = NULL;
|
---|
| 160 |
|
---|
[854ad23] | 161 | list_remove(&wdp->wu_event.link);
|
---|
| 162 | fibril_add_ready(wdp->fid);
|
---|
[8619f25] | 163 | optimize_execution_power();
|
---|
[3e20fd48] | 164 | } else {
|
---|
| 165 | fm->oi.owned_by = NULL;
|
---|
[f3afd24] | 166 | }
|
---|
[9ae22ba] | 167 | }
|
---|
| 168 |
|
---|
| 169 | void fibril_mutex_unlock(fibril_mutex_t *fm)
|
---|
| 170 | {
|
---|
| 171 | futex_down(&async_futex);
|
---|
| 172 | _fibril_mutex_unlock_unsafe(fm);
|
---|
[f3afd24] | 173 | futex_up(&async_futex);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void fibril_rwlock_initialize(fibril_rwlock_t *frw)
|
---|
| 177 | {
|
---|
[3e20fd48] | 178 | frw->oi.owned_by = NULL;
|
---|
[92d34f0b] | 179 | frw->writers = 0;
|
---|
| 180 | frw->readers = 0;
|
---|
| 181 | list_initialize(&frw->waiters);
|
---|
[f3afd24] | 182 | }
|
---|
| 183 |
|
---|
| 184 | void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
|
---|
| 185 | {
|
---|
[92d34f0b] | 186 | futex_down(&async_futex);
|
---|
| 187 | if (frw->writers) {
|
---|
| 188 | fibril_t *f = (fibril_t *) fibril_get_id();
|
---|
[b69bec5] | 189 | awaiter_t wdata;
|
---|
| 190 |
|
---|
| 191 | wdata.fid = (fid_t) f;
|
---|
| 192 | wdata.active = false;
|
---|
| 193 | wdata.wu_event.inlist = true;
|
---|
| 194 | link_initialize(&wdata.wu_event.link);
|
---|
[92d34f0b] | 195 | f->flags &= ~FIBRIL_WRITER;
|
---|
[b69bec5] | 196 | list_append(&wdata.wu_event.link, &frw->waiters);
|
---|
[92d34f0b] | 197 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
| 198 | } else {
|
---|
| 199 | frw->readers++;
|
---|
| 200 | futex_up(&async_futex);
|
---|
| 201 | }
|
---|
[f3afd24] | 202 | }
|
---|
| 203 |
|
---|
| 204 | void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
|
---|
| 205 | {
|
---|
[92d34f0b] | 206 | futex_down(&async_futex);
|
---|
| 207 | if (frw->writers || frw->readers) {
|
---|
| 208 | fibril_t *f = (fibril_t *) fibril_get_id();
|
---|
[b69bec5] | 209 | awaiter_t wdata;
|
---|
| 210 |
|
---|
| 211 | wdata.fid = (fid_t) f;
|
---|
| 212 | wdata.active = false;
|
---|
| 213 | wdata.wu_event.inlist = true;
|
---|
| 214 | link_initialize(&wdata.wu_event.link);
|
---|
[92d34f0b] | 215 | f->flags |= FIBRIL_WRITER;
|
---|
[b69bec5] | 216 | list_append(&wdata.wu_event.link, &frw->waiters);
|
---|
[92d34f0b] | 217 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
| 218 | } else {
|
---|
| 219 | frw->writers++;
|
---|
| 220 | futex_up(&async_futex);
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw)
|
---|
| 225 | {
|
---|
| 226 | futex_down(&async_futex);
|
---|
| 227 | assert(frw->readers || (frw->writers == 1));
|
---|
| 228 | if (frw->readers) {
|
---|
| 229 | if (--frw->readers)
|
---|
| 230 | goto out;
|
---|
| 231 | } else {
|
---|
| 232 | frw->writers--;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | assert(!frw->readers && !frw->writers);
|
---|
| 236 |
|
---|
| 237 | while (!list_empty(&frw->waiters)) {
|
---|
| 238 | link_t *tmp = frw->waiters.next;
|
---|
[b69bec5] | 239 | awaiter_t *wdp;
|
---|
| 240 | fibril_t *f;
|
---|
| 241 |
|
---|
| 242 | wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
|
---|
| 243 | f = (fibril_t *) wdp->fid;
|
---|
[92d34f0b] | 244 |
|
---|
| 245 | if (f->flags & FIBRIL_WRITER) {
|
---|
| 246 | if (frw->readers)
|
---|
| 247 | break;
|
---|
[b69bec5] | 248 | wdp->active = true;
|
---|
| 249 | wdp->wu_event.inlist = false;
|
---|
| 250 | list_remove(&wdp->wu_event.link);
|
---|
| 251 | fibril_add_ready(wdp->fid);
|
---|
[92d34f0b] | 252 | frw->writers++;
|
---|
[8619f25] | 253 | optimize_execution_power();
|
---|
[92d34f0b] | 254 | break;
|
---|
| 255 | } else {
|
---|
[b69bec5] | 256 | wdp->active = true;
|
---|
| 257 | wdp->wu_event.inlist = false;
|
---|
| 258 | list_remove(&wdp->wu_event.link);
|
---|
| 259 | fibril_add_ready(wdp->fid);
|
---|
[92d34f0b] | 260 | frw->readers++;
|
---|
[8619f25] | 261 | optimize_execution_power();
|
---|
[92d34f0b] | 262 | }
|
---|
| 263 | }
|
---|
| 264 | out:
|
---|
| 265 | futex_up(&async_futex);
|
---|
[f3afd24] | 266 | }
|
---|
| 267 |
|
---|
| 268 | void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
|
---|
| 269 | {
|
---|
[92d34f0b] | 270 | _fibril_rwlock_common_unlock(frw);
|
---|
[f3afd24] | 271 | }
|
---|
| 272 |
|
---|
| 273 | void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
|
---|
| 274 | {
|
---|
[92d34f0b] | 275 | _fibril_rwlock_common_unlock(frw);
|
---|
[f3afd24] | 276 | }
|
---|
| 277 |
|
---|
[9ae22ba] | 278 | void fibril_condvar_initialize(fibril_condvar_t *fcv)
|
---|
| 279 | {
|
---|
| 280 | list_initialize(&fcv->waiters);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[cadfa8e] | 283 | int
|
---|
| 284 | fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
|
---|
| 285 | suseconds_t timeout)
|
---|
[9ae22ba] | 286 | {
|
---|
[cadfa8e] | 287 | awaiter_t wdata;
|
---|
| 288 |
|
---|
| 289 | if (timeout < 0)
|
---|
| 290 | return ETIMEOUT;
|
---|
| 291 |
|
---|
| 292 | wdata.fid = fibril_get_id();
|
---|
| 293 | wdata.active = false;
|
---|
| 294 |
|
---|
| 295 | wdata.to_event.inlist = timeout > 0;
|
---|
| 296 | wdata.to_event.occurred = false;
|
---|
| 297 | link_initialize(&wdata.to_event.link);
|
---|
| 298 |
|
---|
| 299 | wdata.wu_event.inlist = true;
|
---|
| 300 | link_initialize(&wdata.wu_event.link);
|
---|
[9ae22ba] | 301 |
|
---|
| 302 | futex_down(&async_futex);
|
---|
[cadfa8e] | 303 | if (timeout) {
|
---|
| 304 | gettimeofday(&wdata.to_event.expires, NULL);
|
---|
| 305 | tv_add(&wdata.to_event.expires, timeout);
|
---|
| 306 | async_insert_timeout(&wdata);
|
---|
| 307 | }
|
---|
| 308 | list_append(&wdata.wu_event.link, &fcv->waiters);
|
---|
[9ae22ba] | 309 | _fibril_mutex_unlock_unsafe(fm);
|
---|
| 310 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
| 311 | fibril_mutex_lock(fm);
|
---|
[cadfa8e] | 312 |
|
---|
| 313 | /* async_futex not held after fibril_switch() */
|
---|
| 314 | futex_down(&async_futex);
|
---|
| 315 | if (wdata.to_event.inlist)
|
---|
| 316 | list_remove(&wdata.to_event.link);
|
---|
| 317 | if (wdata.wu_event.inlist)
|
---|
| 318 | list_remove(&wdata.wu_event.link);
|
---|
| 319 | futex_up(&async_futex);
|
---|
| 320 |
|
---|
| 321 | return wdata.to_event.occurred ? ETIMEOUT : EOK;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm)
|
---|
| 325 | {
|
---|
| 326 | int rc;
|
---|
| 327 |
|
---|
| 328 | rc = fibril_condvar_wait_timeout(fcv, fm, 0);
|
---|
| 329 | assert(rc == EOK);
|
---|
[9ae22ba] | 330 | }
|
---|
| 331 |
|
---|
| 332 | static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once)
|
---|
| 333 | {
|
---|
| 334 | link_t *tmp;
|
---|
[cadfa8e] | 335 | awaiter_t *wdp;
|
---|
[9ae22ba] | 336 |
|
---|
| 337 | futex_down(&async_futex);
|
---|
| 338 | while (!list_empty(&fcv->waiters)) {
|
---|
| 339 | tmp = fcv->waiters.next;
|
---|
[cadfa8e] | 340 | wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
|
---|
| 341 | list_remove(&wdp->wu_event.link);
|
---|
| 342 | wdp->wu_event.inlist = false;
|
---|
| 343 | if (!wdp->active) {
|
---|
| 344 | wdp->active = true;
|
---|
| 345 | fibril_add_ready(wdp->fid);
|
---|
| 346 | optimize_execution_power();
|
---|
| 347 | if (once)
|
---|
| 348 | break;
|
---|
| 349 | }
|
---|
[9ae22ba] | 350 | }
|
---|
| 351 | futex_up(&async_futex);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | void fibril_condvar_signal(fibril_condvar_t *fcv)
|
---|
| 355 | {
|
---|
| 356 | _fibril_condvar_wakeup_common(fcv, true);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | void fibril_condvar_broadcast(fibril_condvar_t *fcv)
|
---|
| 360 | {
|
---|
| 361 | _fibril_condvar_wakeup_common(fcv, false);
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[f3afd24] | 364 | /** @}
|
---|
| 365 | */
|
---|