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