[7bd34e5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jan Vesely
|
---|
| 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 | #include <assert.h>
|
---|
| 29 | #include <async.h>
|
---|
| 30 | #include <async_priv.h>
|
---|
| 31 | #include <futex.h>
|
---|
| 32 |
|
---|
| 33 | #include "fibril_semaphore.h"
|
---|
| 34 | static void optimize_execution_power(void)
|
---|
| 35 | {
|
---|
| 36 | /*
|
---|
| 37 | * When waking up a worker fibril previously blocked in fibril
|
---|
| 38 | * synchronization, chances are that there is an idle manager fibril
|
---|
| 39 | * waiting for IPC, that could start executing the awakened worker
|
---|
| 40 | * fibril right away. We try to detect this and bring the manager
|
---|
| 41 | * fibril back to fruitful work.
|
---|
| 42 | */
|
---|
| 43 | if (atomic_get(&threads_in_ipc_wait) > 0)
|
---|
| 44 | ipc_poke();
|
---|
| 45 | }
|
---|
| 46 | /*----------------------------------------------------------------------------*/
|
---|
| 47 | void fibril_semaphore_initialize(fibril_semaphore_t *fs, int value)
|
---|
| 48 | {
|
---|
| 49 | assert( fs );
|
---|
| 50 | fs->oi.owned_by = NULL;
|
---|
| 51 | fs->counter = 1;
|
---|
| 52 | fs->value = value;
|
---|
| 53 | list_initialize(&fs->waiters);
|
---|
| 54 | }
|
---|
| 55 | /*----------------------------------------------------------------------------*/
|
---|
| 56 | void fibril_semaphore_down(fibril_semaphore_t *fs)
|
---|
| 57 | {
|
---|
| 58 | assert( fs );
|
---|
| 59 | fibril_t *f = (fibril_t *) fibril_get_id();
|
---|
| 60 |
|
---|
| 61 | futex_down(&async_futex);
|
---|
| 62 | if (--fs->value < 0) {
|
---|
| 63 | awaiter_t wdata;
|
---|
| 64 | wdata.fid = fibril_get_id();
|
---|
| 65 | wdata.active = false;
|
---|
| 66 | wdata.wu_event.inlist = true;
|
---|
| 67 | link_initialize(&wdata.wu_event.link);
|
---|
| 68 | list_append(&wdata.wu_event.link, &fs->waiters);
|
---|
| 69 | // check_for_deadlock(&fm->oi);
|
---|
| 70 | f->waits_for = &fs->oi;
|
---|
| 71 | ++fs->counter;
|
---|
| 72 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
| 73 | } else {
|
---|
| 74 | fs->oi.owned_by = f;
|
---|
| 75 | futex_up(&async_futex);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | /*----------------------------------------------------------------------------*/
|
---|
| 79 | bool fibril_semaphore_trydown(fibril_semaphore_t *fs)
|
---|
| 80 | {
|
---|
| 81 | bool locked = false;
|
---|
| 82 |
|
---|
| 83 | futex_down(&async_futex);
|
---|
| 84 | if (fs->value > 0) {
|
---|
| 85 | --fs->value;
|
---|
| 86 | fs->oi.owned_by = (fibril_t *) fibril_get_id();
|
---|
| 87 | locked = true;
|
---|
| 88 | }
|
---|
| 89 | futex_up(&async_futex);
|
---|
| 90 |
|
---|
| 91 | return locked;
|
---|
| 92 | }
|
---|
| 93 | /*----------------------------------------------------------------------------*/
|
---|
| 94 | void fibril_semaphore_up(fibril_semaphore_t *fs)
|
---|
| 95 | {
|
---|
| 96 | assert(fs);
|
---|
| 97 | futex_down(&async_futex);
|
---|
| 98 | if (++fs->value <= 0) {
|
---|
| 99 | link_t *tmp;
|
---|
| 100 | awaiter_t *wdp;
|
---|
| 101 | fibril_t *f;
|
---|
| 102 |
|
---|
| 103 | assert(!list_empty(&fs->waiters));
|
---|
| 104 |
|
---|
| 105 | tmp = fs->waiters.next;
|
---|
| 106 | wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
|
---|
| 107 | wdp->active = true;
|
---|
| 108 | wdp->wu_event.inlist = false;
|
---|
| 109 |
|
---|
| 110 | f = (fibril_t *) wdp->fid;
|
---|
| 111 | fs->oi.owned_by = f;
|
---|
| 112 | f->waits_for = NULL;
|
---|
| 113 |
|
---|
| 114 | list_remove(&wdp->wu_event.link);
|
---|
| 115 | fibril_add_ready(wdp->fid);
|
---|
| 116 | optimize_execution_power();
|
---|
| 117 | } else {
|
---|
| 118 | fs->oi.owned_by = NULL;
|
---|
| 119 | }
|
---|
| 120 | futex_up(&async_futex);
|
---|
| 121 | }
|
---|