[1cef26f] | 1 | /*
|
---|
[4db6eaf] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[1cef26f] | 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.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[596d65c] | 33 | */
|
---|
[1cef26f] | 34 |
|
---|
[f6372be9] | 35 | #include <assert.h>
|
---|
[508b0df1] | 36 | #include <stdatomic.h>
|
---|
[f6372be9] | 37 | #include <fibril.h>
|
---|
| 38 | #include <io/kio.h>
|
---|
| 39 |
|
---|
[6340b4d2] | 40 | #include "../private/fibril.h"
|
---|
[f787c8e] | 41 | #include "../private/futex.h"
|
---|
[f6372be9] | 42 |
|
---|
| 43 | //#define DPRINTF(...) kio_printf(__VA_ARGS__)
|
---|
[2965d18] | 44 | #define DPRINTF(...) dummy_printf(__VA_ARGS__)
|
---|
[17242c6e] | 45 |
|
---|
| 46 | /** Initialize futex counter.
|
---|
| 47 | *
|
---|
[596d65c] | 48 | * @param futex Futex.
|
---|
| 49 | * @param val Initialization value.
|
---|
| 50 | *
|
---|
[17242c6e] | 51 | */
|
---|
[4db6eaf] | 52 | void futex_initialize(futex_t *futex, int val)
|
---|
[1cef26f] | 53 | {
|
---|
[508b0df1] | 54 | atomic_store_explicit(&futex->val, val, memory_order_relaxed);
|
---|
[1cef26f] | 55 | }
|
---|
| 56 |
|
---|
[f6372be9] | 57 | #ifdef CONFIG_DEBUG_FUTEX
|
---|
| 58 |
|
---|
| 59 | void __futex_assert_is_locked(futex_t *futex, const char *name)
|
---|
| 60 | {
|
---|
[508b0df1] | 61 | void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
|
---|
[f6372be9] | 62 | fibril_t *self = (fibril_t *) fibril_get_id();
|
---|
| 63 | if (owner != self) {
|
---|
| 64 | DPRINTF("Assertion failed: %s (%p) is not locked by fibril %p (instead locked by fibril %p).\n", name, futex, self, owner);
|
---|
| 65 | }
|
---|
| 66 | assert(owner == self);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void __futex_assert_is_not_locked(futex_t *futex, const char *name)
|
---|
| 70 | {
|
---|
[508b0df1] | 71 | void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
|
---|
[f6372be9] | 72 | fibril_t *self = (fibril_t *) fibril_get_id();
|
---|
| 73 | if (owner == self) {
|
---|
| 74 | DPRINTF("Assertion failed: %s (%p) is already locked by fibril %p.\n", name, futex, self);
|
---|
| 75 | }
|
---|
| 76 | assert(owner != self);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void __futex_lock(futex_t *futex, const char *name)
|
---|
| 80 | {
|
---|
[6e569bf] | 81 | /*
|
---|
| 82 | * We use relaxed atomics to avoid violating C11 memory model.
|
---|
[f6372be9] | 83 | * They should compile to regular load/stores, but simple assignments
|
---|
| 84 | * would be UB by definition.
|
---|
[2965d18] | 85 | * The proper ordering is ensured by the surrounding futex operation.
|
---|
[f6372be9] | 86 | */
|
---|
| 87 |
|
---|
| 88 | fibril_t *self = (fibril_t *) fibril_get_id();
|
---|
| 89 | DPRINTF("Locking futex %s (%p) by fibril %p.\n", name, futex, self);
|
---|
| 90 | __futex_assert_is_not_locked(futex, name);
|
---|
| 91 | futex_down(futex);
|
---|
| 92 |
|
---|
[508b0df1] | 93 | void *prev_owner = atomic_load_explicit(&futex->owner,
|
---|
| 94 | memory_order_relaxed);
|
---|
[f6372be9] | 95 | assert(prev_owner == NULL);
|
---|
[508b0df1] | 96 | atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
|
---|
[f6372be9] | 97 | }
|
---|
| 98 |
|
---|
| 99 | void __futex_unlock(futex_t *futex, const char *name)
|
---|
| 100 | {
|
---|
| 101 | fibril_t *self = (fibril_t *) fibril_get_id();
|
---|
| 102 | DPRINTF("Unlocking futex %s (%p) by fibril %p.\n", name, futex, self);
|
---|
| 103 | __futex_assert_is_locked(futex, name);
|
---|
[508b0df1] | 104 | atomic_store_explicit(&futex->owner, NULL, memory_order_relaxed);
|
---|
[f6372be9] | 105 | futex_up(futex);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool __futex_trylock(futex_t *futex, const char *name)
|
---|
| 109 | {
|
---|
| 110 | fibril_t *self = (fibril_t *) fibril_get_id();
|
---|
| 111 | bool success = futex_trydown(futex);
|
---|
| 112 | if (success) {
|
---|
[508b0df1] | 113 | void *owner = atomic_load_explicit(&futex->owner,
|
---|
| 114 | memory_order_relaxed);
|
---|
[f6372be9] | 115 | assert(owner == NULL);
|
---|
| 116 |
|
---|
[508b0df1] | 117 | atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
|
---|
[f6372be9] | 118 |
|
---|
| 119 | DPRINTF("Trylock on futex %s (%p) by fibril %p succeeded.\n", name, futex, self);
|
---|
| 120 | } else {
|
---|
| 121 | DPRINTF("Trylock on futex %s (%p) by fibril %p failed.\n", name, futex, self);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return success;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void __futex_give_to(futex_t *futex, void *new_owner, const char *name)
|
---|
| 128 | {
|
---|
| 129 | fibril_t *self = fibril_self();
|
---|
| 130 | fibril_t *no = new_owner;
|
---|
| 131 | DPRINTF("Passing futex %s (%p) from fibril %p to fibril %p.\n", name, futex, self, no);
|
---|
| 132 |
|
---|
| 133 | __futex_assert_is_locked(futex, name);
|
---|
[508b0df1] | 134 | atomic_store_explicit(&futex->owner, new_owner, memory_order_relaxed);
|
---|
[f6372be9] | 135 | }
|
---|
| 136 |
|
---|
| 137 | #endif
|
---|
| 138 |
|
---|
[fadd381] | 139 | /** @}
|
---|
[b2951e2] | 140 | */
|
---|