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