| 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 <time.h>
|
|---|
| 41 | #include <stdbool.h>
|
|---|
| 42 |
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | fibril_owner_info_t oi; /**< Keep this the first thing. */
|
|---|
| 45 | int counter;
|
|---|
| 46 | list_t waiters;
|
|---|
| 47 | } fibril_mutex_t;
|
|---|
| 48 |
|
|---|
| 49 | #define FIBRIL_MUTEX_INITIALIZER(name) \
|
|---|
| 50 | { \
|
|---|
| 51 | .oi = { \
|
|---|
| 52 | .owned_by = NULL \
|
|---|
| 53 | }, \
|
|---|
| 54 | .counter = 1, \
|
|---|
| 55 | .waiters = LIST_INITIALIZER((name).waiters), \
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | #define FIBRIL_MUTEX_INITIALIZE(name) \
|
|---|
| 59 | fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
|
|---|
| 60 |
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | fibril_owner_info_t oi; /**< Keep this the first thing. */
|
|---|
| 63 | unsigned int writers;
|
|---|
| 64 | unsigned int readers;
|
|---|
| 65 | list_t waiters;
|
|---|
| 66 | } fibril_rwlock_t;
|
|---|
| 67 |
|
|---|
| 68 | #define FIBRIL_RWLOCK_INITIALIZER(name) \
|
|---|
| 69 | { \
|
|---|
| 70 | .oi = { \
|
|---|
| 71 | .owned_by = NULL \
|
|---|
| 72 | }, \
|
|---|
| 73 | .readers = 0, \
|
|---|
| 74 | .writers = 0, \
|
|---|
| 75 | .waiters = LIST_INITIALIZER((name).waiters), \
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | #define FIBRIL_RWLOCK_INITIALIZE(name) \
|
|---|
| 79 | fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
|
|---|
| 80 |
|
|---|
| 81 | typedef struct {
|
|---|
| 82 | list_t waiters;
|
|---|
| 83 | } fibril_condvar_t;
|
|---|
| 84 |
|
|---|
| 85 | #define FIBRIL_CONDVAR_INITIALIZER(name) \
|
|---|
| 86 | { \
|
|---|
| 87 | .waiters = LIST_INITIALIZER((name).waiters), \
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | #define FIBRIL_CONDVAR_INITIALIZE(name) \
|
|---|
| 91 | fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
|
|---|
| 92 |
|
|---|
| 93 | typedef void (*fibril_timer_fun_t)(void *);
|
|---|
| 94 |
|
|---|
| 95 | typedef enum {
|
|---|
| 96 | /** Timer has not been set or has been cleared */
|
|---|
| 97 | fts_not_set,
|
|---|
| 98 | /** Timer was set but did not fire yet */
|
|---|
| 99 | fts_active,
|
|---|
| 100 | /** Timer has fired and has not been cleared since */
|
|---|
| 101 | fts_fired,
|
|---|
| 102 | /** Timer fibril is requested to terminate */
|
|---|
| 103 | fts_cleanup,
|
|---|
| 104 | /** Timer fibril acknowledged termination */
|
|---|
| 105 | fts_clean
|
|---|
| 106 | } fibril_timer_state_t;
|
|---|
| 107 |
|
|---|
| 108 | /** Fibril timer.
|
|---|
| 109 | *
|
|---|
| 110 | * When a timer is set it executes a callback function (in a separate
|
|---|
| 111 | * fibril) after a specified time interval. The timer can be cleared
|
|---|
| 112 | * (canceled) before that. From the return value of fibril_timer_clear()
|
|---|
| 113 | * one can tell whether the timer fired or not.
|
|---|
| 114 | */
|
|---|
| 115 | typedef struct {
|
|---|
| 116 | fibril_mutex_t lock;
|
|---|
| 117 | fibril_mutex_t *lockp;
|
|---|
| 118 | fibril_condvar_t cv;
|
|---|
| 119 | fid_t fibril;
|
|---|
| 120 | fibril_timer_state_t state;
|
|---|
| 121 | /** FID of fibril executing handler or 0 if handler is not running */
|
|---|
| 122 | fid_t handler_fid;
|
|---|
| 123 |
|
|---|
| 124 | usec_t delay;
|
|---|
| 125 | fibril_timer_fun_t fun;
|
|---|
| 126 | void *arg;
|
|---|
| 127 | } fibril_timer_t;
|
|---|
| 128 |
|
|---|
| 129 | /** A counting semaphore for fibrils. */
|
|---|
| 130 | typedef struct {
|
|---|
| 131 | long int count;
|
|---|
| 132 | list_t waiters;
|
|---|
| 133 | bool closed;
|
|---|
| 134 | } fibril_semaphore_t;
|
|---|
| 135 |
|
|---|
| 136 | #define FIBRIL_SEMAPHORE_INITIALIZER(name, cnt) \
|
|---|
| 137 | { \
|
|---|
| 138 | .count = (cnt), \
|
|---|
| 139 | .waiters = LIST_INITIALIZER((name).waiters), \
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | #define FIBRIL_SEMAPHORE_INITIALIZE(name, cnt) \
|
|---|
| 143 | fibril_semaphore_t name = FIBRIL_SEMAPHORE_INITIALIZER(name, cnt)
|
|---|
| 144 |
|
|---|
| 145 | extern void __fibril_synch_init(void);
|
|---|
| 146 | extern void __fibril_synch_fini(void);
|
|---|
| 147 |
|
|---|
| 148 | extern void fibril_mutex_initialize(fibril_mutex_t *);
|
|---|
| 149 | extern void fibril_mutex_lock(fibril_mutex_t *);
|
|---|
| 150 | extern bool fibril_mutex_trylock(fibril_mutex_t *);
|
|---|
| 151 | extern void fibril_mutex_unlock(fibril_mutex_t *);
|
|---|
| 152 | extern bool fibril_mutex_is_locked(fibril_mutex_t *);
|
|---|
| 153 |
|
|---|
| 154 | extern void fibril_rwlock_initialize(fibril_rwlock_t *);
|
|---|
| 155 | extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
|
|---|
| 156 | extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
|
|---|
| 157 | extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
|
|---|
| 158 | extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
|
|---|
| 159 | extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
|
|---|
| 160 | extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
|
|---|
| 161 | extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
|
|---|
| 162 |
|
|---|
| 163 | extern void fibril_condvar_initialize(fibril_condvar_t *);
|
|---|
| 164 | extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
|
|---|
| 165 | usec_t);
|
|---|
| 166 | extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
|
|---|
| 167 | extern void fibril_condvar_signal(fibril_condvar_t *);
|
|---|
| 168 | extern void fibril_condvar_broadcast(fibril_condvar_t *);
|
|---|
| 169 |
|
|---|
| 170 | extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
|
|---|
| 171 | extern void fibril_timer_destroy(fibril_timer_t *);
|
|---|
| 172 | extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
|
|---|
| 173 | void *);
|
|---|
| 174 | extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
|
|---|
| 175 | fibril_timer_fun_t, void *);
|
|---|
| 176 | extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
|
|---|
| 177 | extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
|
|---|
| 178 |
|
|---|
| 179 | extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
|
|---|
| 180 | extern void fibril_semaphore_up(fibril_semaphore_t *);
|
|---|
| 181 | extern void fibril_semaphore_down(fibril_semaphore_t *);
|
|---|
| 182 | extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
|
|---|
| 183 | extern void fibril_semaphore_close(fibril_semaphore_t *);
|
|---|
| 184 |
|
|---|
| 185 | typedef struct mpsc mpsc_t;
|
|---|
| 186 | extern mpsc_t *mpsc_create(size_t);
|
|---|
| 187 | extern void mpsc_destroy(mpsc_t *);
|
|---|
| 188 | extern errno_t mpsc_send(mpsc_t *, const void *);
|
|---|
| 189 | extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
|
|---|
| 190 | extern void mpsc_close(mpsc_t *);
|
|---|
| 191 |
|
|---|
| 192 | #endif
|
|---|
| 193 |
|
|---|
| 194 | /** @}
|
|---|
| 195 | */
|
|---|