| 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 |
|
|---|
| 35 | #ifndef LIBC_FIBRIL_SYNCH_H_
|
|---|
| 36 | #define LIBC_FIBRIL_SYNCH_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <fibril.h>
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 | #include <tls.h>
|
|---|
| 41 | #include <sys/time.h>
|
|---|
| 42 | #include <stdbool.h>
|
|---|
| 43 | #include <futex.h>
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * "Restricted" fibril mutex.
|
|---|
| 47 | *
|
|---|
| 48 | * Similar to `fibril_mutex_t`, but has a set of restrictions placed on its
|
|---|
| 49 | * use. Within a rmutex critical section, you
|
|---|
| 50 | * - may not use any other synchronization primitive,
|
|---|
| 51 | * save for another `fibril_rmutex_t`. This includes nonblocking
|
|---|
| 52 | * operations like cvar signal and mutex unlock.
|
|---|
| 53 | * - may not read IPC messages
|
|---|
| 54 | * - may not start a new thread/fibril
|
|---|
| 55 | * (creating fibril without starting is fine)
|
|---|
| 56 | *
|
|---|
| 57 | * Additionally, locking with a timeout is not possible on this mutex,
|
|---|
| 58 | * and there is no associated condition variable type.
|
|---|
| 59 | * This is a design constraint, not a lack of implementation effort.
|
|---|
| 60 | */
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | // TODO: At this point, this is just silly handwaving to hide current
|
|---|
| 63 | // futex use behind a fibril based abstraction. Later, the imple-
|
|---|
| 64 | // mentation will change, but the restrictions placed on this type
|
|---|
| 65 | // will allow it to be simpler and faster than a regular mutex.
|
|---|
| 66 | // There might also be optional debug checking of the assumptions.
|
|---|
| 67 | //
|
|---|
| 68 | // Note that a consequence of the restrictions is that if we are
|
|---|
| 69 | // running on a single thread, no other fibril can ever get to run
|
|---|
| 70 | // while a fibril has a rmutex locked. That means that for
|
|---|
| 71 | // single-threaded programs, we can reduce all rmutex locks and
|
|---|
| 72 | // unlocks to simple branches on a global bool variable.
|
|---|
| 73 |
|
|---|
| 74 | futex_t futex;
|
|---|
| 75 | } fibril_rmutex_t;
|
|---|
| 76 |
|
|---|
| 77 | #define FIBRIL_RMUTEX_INITIALIZER(name) \
|
|---|
| 78 | { .futex = FUTEX_INITIALIZE(1) }
|
|---|
| 79 |
|
|---|
| 80 | #define FIBRIL_RMUTEX_INITIALIZE(name) \
|
|---|
| 81 | fibril_rmutex_t name = FIBRIL_RMUTEX_INITIALIZER(name)
|
|---|
| 82 |
|
|---|
| 83 | static inline void fibril_rmutex_initialize(fibril_rmutex_t *m)
|
|---|
| 84 | {
|
|---|
| 85 | futex_initialize(&m->futex, 1);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | static inline void fibril_rmutex_lock(fibril_rmutex_t *m)
|
|---|
| 89 | {
|
|---|
| 90 | futex_lock(&m->futex);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static inline bool fibril_rmutex_trylock(fibril_rmutex_t *m)
|
|---|
| 94 | {
|
|---|
| 95 | return futex_trylock(&m->futex);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static inline void fibril_rmutex_unlock(fibril_rmutex_t *m)
|
|---|
| 99 | {
|
|---|
| 100 | futex_unlock(&m->futex);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | typedef struct {
|
|---|
| 104 | fibril_owner_info_t oi; /**< Keep this the first thing. */
|
|---|
| 105 | int counter;
|
|---|
| 106 | list_t waiters;
|
|---|
| 107 | } fibril_mutex_t;
|
|---|
| 108 |
|
|---|
| 109 | #define FIBRIL_MUTEX_INITIALIZER(name) \
|
|---|
| 110 | { \
|
|---|
| 111 | .oi = { \
|
|---|
| 112 | .owned_by = NULL \
|
|---|
| 113 | }, \
|
|---|
| 114 | .counter = 1, \
|
|---|
| 115 | .waiters = { \
|
|---|
| 116 | .head = { \
|
|---|
| 117 | .prev = &(name).waiters.head, \
|
|---|
| 118 | .next = &(name).waiters.head, \
|
|---|
| 119 | } \
|
|---|
| 120 | } \
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | #define FIBRIL_MUTEX_INITIALIZE(name) \
|
|---|
| 124 | fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
|
|---|
| 125 |
|
|---|
| 126 | typedef struct {
|
|---|
| 127 | fibril_owner_info_t oi; /**< Keep this the first thing. */
|
|---|
| 128 | unsigned int writers;
|
|---|
| 129 | unsigned int readers;
|
|---|
| 130 | list_t waiters;
|
|---|
| 131 | } fibril_rwlock_t;
|
|---|
| 132 |
|
|---|
| 133 | #define FIBRIL_RWLOCK_INITIALIZER(name) \
|
|---|
| 134 | { \
|
|---|
| 135 | .oi = { \
|
|---|
| 136 | .owned_by = NULL \
|
|---|
| 137 | }, \
|
|---|
| 138 | .readers = 0, \
|
|---|
| 139 | .writers = 0, \
|
|---|
| 140 | .waiters = { \
|
|---|
| 141 | .head = { \
|
|---|
| 142 | .prev = &(name).waiters.head, \
|
|---|
| 143 | .next = &(name).waiters.head, \
|
|---|
| 144 | } \
|
|---|
| 145 | } \
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | #define FIBRIL_RWLOCK_INITIALIZE(name) \
|
|---|
| 149 | fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
|
|---|
| 150 |
|
|---|
| 151 | typedef struct {
|
|---|
| 152 | list_t waiters;
|
|---|
| 153 | } fibril_condvar_t;
|
|---|
| 154 |
|
|---|
| 155 | #define FIBRIL_CONDVAR_INITIALIZER(name) \
|
|---|
| 156 | { \
|
|---|
| 157 | .waiters = { \
|
|---|
| 158 | .head = { \
|
|---|
| 159 | .next = &(name).waiters.head, \
|
|---|
| 160 | .prev = &(name).waiters.head, \
|
|---|
| 161 | } \
|
|---|
| 162 | } \
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | #define FIBRIL_CONDVAR_INITIALIZE(name) \
|
|---|
| 166 | fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
|
|---|
| 167 |
|
|---|
| 168 | typedef void (*fibril_timer_fun_t)(void *);
|
|---|
| 169 |
|
|---|
| 170 | typedef enum {
|
|---|
| 171 | /** Timer has not been set or has been cleared */
|
|---|
| 172 | fts_not_set,
|
|---|
| 173 | /** Timer was set but did not fire yet */
|
|---|
| 174 | fts_active,
|
|---|
| 175 | /** Timer has fired and has not been cleared since */
|
|---|
| 176 | fts_fired,
|
|---|
| 177 | /** Timer fibril is requested to terminate */
|
|---|
| 178 | fts_cleanup,
|
|---|
| 179 | /** Timer fibril acknowledged termination */
|
|---|
| 180 | fts_clean
|
|---|
| 181 | } fibril_timer_state_t;
|
|---|
| 182 |
|
|---|
| 183 | /** Fibril timer.
|
|---|
| 184 | *
|
|---|
| 185 | * When a timer is set it executes a callback function (in a separate
|
|---|
| 186 | * fibril) after a specified time interval. The timer can be cleared
|
|---|
| 187 | * (canceled) before that. From the return value of fibril_timer_clear()
|
|---|
| 188 | * one can tell whether the timer fired or not.
|
|---|
| 189 | */
|
|---|
| 190 | typedef struct {
|
|---|
| 191 | fibril_mutex_t lock;
|
|---|
| 192 | fibril_mutex_t *lockp;
|
|---|
| 193 | fibril_condvar_t cv;
|
|---|
| 194 | fid_t fibril;
|
|---|
| 195 | fibril_timer_state_t state;
|
|---|
| 196 | /** FID of fibril executing handler or 0 if handler is not running */
|
|---|
| 197 | fid_t handler_fid;
|
|---|
| 198 |
|
|---|
| 199 | suseconds_t delay;
|
|---|
| 200 | fibril_timer_fun_t fun;
|
|---|
| 201 | void *arg;
|
|---|
| 202 | } fibril_timer_t;
|
|---|
| 203 |
|
|---|
| 204 | /** A counting semaphore for fibrils. */
|
|---|
| 205 | typedef struct {
|
|---|
| 206 | long int count;
|
|---|
| 207 | list_t waiters;
|
|---|
| 208 | } fibril_semaphore_t;
|
|---|
| 209 |
|
|---|
| 210 | #define FIBRIL_SEMAPHORE_INITIALIZER(name, cnt) \
|
|---|
| 211 | { \
|
|---|
| 212 | .count = (cnt), \
|
|---|
| 213 | .waiters = { \
|
|---|
| 214 | .head = { \
|
|---|
| 215 | .next = &(name).waiters.head, \
|
|---|
| 216 | .prev = &(name).waiters.head, \
|
|---|
| 217 | } \
|
|---|
| 218 | } \
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | #define FIBRIL_SEMAPHORE_INITIALIZE(name, cnt) \
|
|---|
| 222 | fibril_semaphore_t name = FIBRIL_SEMAPHORE_INITIALIZER(name, cnt)
|
|---|
| 223 |
|
|---|
| 224 | extern void fibril_mutex_initialize(fibril_mutex_t *);
|
|---|
| 225 | extern void fibril_mutex_lock(fibril_mutex_t *);
|
|---|
| 226 | extern bool fibril_mutex_trylock(fibril_mutex_t *);
|
|---|
| 227 | extern void fibril_mutex_unlock(fibril_mutex_t *);
|
|---|
| 228 | extern bool fibril_mutex_is_locked(fibril_mutex_t *);
|
|---|
| 229 |
|
|---|
| 230 | extern void fibril_rwlock_initialize(fibril_rwlock_t *);
|
|---|
| 231 | extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
|
|---|
| 232 | extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
|
|---|
| 233 | extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
|
|---|
| 234 | extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
|
|---|
| 235 | extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
|
|---|
| 236 | extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
|
|---|
| 237 | extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
|
|---|
| 238 |
|
|---|
| 239 | extern void fibril_condvar_initialize(fibril_condvar_t *);
|
|---|
| 240 | extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
|
|---|
| 241 | suseconds_t);
|
|---|
| 242 | extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
|
|---|
| 243 | extern void fibril_condvar_signal(fibril_condvar_t *);
|
|---|
| 244 | extern void fibril_condvar_broadcast(fibril_condvar_t *);
|
|---|
| 245 |
|
|---|
| 246 | extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
|
|---|
| 247 | extern void fibril_timer_destroy(fibril_timer_t *);
|
|---|
| 248 | extern void fibril_timer_set(fibril_timer_t *, suseconds_t, fibril_timer_fun_t,
|
|---|
| 249 | void *);
|
|---|
| 250 | extern void fibril_timer_set_locked(fibril_timer_t *, suseconds_t,
|
|---|
| 251 | fibril_timer_fun_t, void *);
|
|---|
| 252 | extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
|
|---|
| 253 | extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
|
|---|
| 254 |
|
|---|
| 255 | extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
|
|---|
| 256 | extern void fibril_semaphore_up(fibril_semaphore_t *);
|
|---|
| 257 | extern void fibril_semaphore_down(fibril_semaphore_t *);
|
|---|
| 258 |
|
|---|
| 259 | #endif
|
|---|
| 260 |
|
|---|
| 261 | /** @}
|
|---|
| 262 | */
|
|---|