source: mainline/uspace/lib/c/include/fibril_synch.h@ c124c985

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c124c985 was c124c985, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Add fibril_rmutex_t type and fibril multithreading enablement.

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[f3afd24]1/*
[1b20da0]2 * Copyright (c) 2009 Jakub Jermar
[f3afd24]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
[1e4cada]35#ifndef LIBC_FIBRIL_SYNCH_H_
36#define LIBC_FIBRIL_SYNCH_H_
[f3afd24]37
38#include <fibril.h>
39#include <adt/list.h>
40#include <libarch/tls.h>
[cadfa8e]41#include <sys/time.h>
[3e6a98c5]42#include <stdbool.h>
[c124c985]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 */
61typedef 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
83static inline void fibril_rmutex_initialize(fibril_rmutex_t *m)
84{
85 futex_initialize(&m->futex, 1);
86}
87
88static inline void fibril_rmutex_lock(fibril_rmutex_t *m)
89{
90 futex_lock(&m->futex);
91}
92
93static inline bool fibril_rmutex_trylock(fibril_rmutex_t *m)
94{
95 return futex_trylock(&m->futex);
96}
97
98static inline void fibril_rmutex_unlock(fibril_rmutex_t *m)
99{
100 futex_unlock(&m->futex);
101}
[f3afd24]102
[668f8cbf]103typedef struct {
[d161715]104 fibril_owner_info_t oi; /**< Keep this the first thing. */
[f3afd24]105 int counter;
[b72efe8]106 list_t waiters;
[f3afd24]107} fibril_mutex_t;
108
[e9460aa]109#define FIBRIL_MUTEX_INITIALIZER(name) \
110 { \
[668f8cbf]111 .oi = { \
112 .owned_by = NULL \
113 }, \
[f3afd24]114 .counter = 1, \
115 .waiters = { \
[b72efe8]116 .head = { \
117 .prev = &(name).waiters.head, \
118 .next = &(name).waiters.head, \
119 } \
[f3afd24]120 } \
121 }
[a35b458]122
[e9460aa]123#define FIBRIL_MUTEX_INITIALIZE(name) \
[1b20da0]124 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
[f3afd24]125
126typedef struct {
[d161715]127 fibril_owner_info_t oi; /**< Keep this the first thing. */
[fb0ec570]128 unsigned int writers;
129 unsigned int readers;
[b72efe8]130 list_t waiters;
[f3afd24]131} fibril_rwlock_t;
132
[e9460aa]133#define FIBRIL_RWLOCK_INITIALIZER(name) \
134 { \
[668f8cbf]135 .oi = { \
136 .owned_by = NULL \
137 }, \
[92d34f0b]138 .readers = 0, \
139 .writers = 0, \
140 .waiters = { \
[b72efe8]141 .head = { \
142 .prev = &(name).waiters.head, \
143 .next = &(name).waiters.head, \
144 } \
[f3afd24]145 } \
146 }
147
[e9460aa]148#define FIBRIL_RWLOCK_INITIALIZE(name) \
149 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
150
[9ae22ba]151typedef struct {
[b72efe8]152 list_t waiters;
[9ae22ba]153} fibril_condvar_t;
154
[e9460aa]155#define FIBRIL_CONDVAR_INITIALIZER(name) \
156 { \
[c51a7cd]157 .waiters = { \
[b72efe8]158 .head = { \
159 .next = &(name).waiters.head, \
160 .prev = &(name).waiters.head, \
161 } \
[c51a7cd]162 } \
163 }
164
[e9460aa]165#define FIBRIL_CONDVAR_INITIALIZE(name) \
166 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
167
[2a3214e]168typedef void (*fibril_timer_fun_t)(void *);
169
170typedef 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,
[53f68fd]177 /** Timer fibril is requested to terminate */
178 fts_cleanup,
179 /** Timer fibril acknowledged termination */
180 fts_clean
[2a3214e]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 */
190typedef struct {
191 fibril_mutex_t lock;
[78192cc7]192 fibril_mutex_t *lockp;
[2a3214e]193 fibril_condvar_t cv;
194 fid_t fibril;
195 fibril_timer_state_t state;
[7c15d6f]196 /** FID of fibril executing handler or 0 if handler is not running */
197 fid_t handler_fid;
[2a3214e]198
199 suseconds_t delay;
200 fibril_timer_fun_t fun;
201 void *arg;
202} fibril_timer_t;
203
[a55d76b1]204/** A counting semaphore for fibrils. */
205typedef struct {
[fb0ec570]206 long int count;
[a55d76b1]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
[f3afd24]224extern void fibril_mutex_initialize(fibril_mutex_t *);
225extern void fibril_mutex_lock(fibril_mutex_t *);
226extern bool fibril_mutex_trylock(fibril_mutex_t *);
227extern void fibril_mutex_unlock(fibril_mutex_t *);
[b0a76d5]228extern bool fibril_mutex_is_locked(fibril_mutex_t *);
[f3afd24]229
230extern void fibril_rwlock_initialize(fibril_rwlock_t *);
231extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
232extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
233extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
234extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
[b0a76d5]235extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
236extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
[c81b6f2]237extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
[f3afd24]238
[9ae22ba]239extern void fibril_condvar_initialize(fibril_condvar_t *);
[b7fd2a0]240extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
[cadfa8e]241 suseconds_t);
[9ae22ba]242extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
243extern void fibril_condvar_signal(fibril_condvar_t *);
244extern void fibril_condvar_broadcast(fibril_condvar_t *);
245
[78192cc7]246extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
[2a3214e]247extern void fibril_timer_destroy(fibril_timer_t *);
248extern void fibril_timer_set(fibril_timer_t *, suseconds_t, fibril_timer_fun_t,
249 void *);
[78192cc7]250extern void fibril_timer_set_locked(fibril_timer_t *, suseconds_t,
251 fibril_timer_fun_t, void *);
[2a3214e]252extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
[78192cc7]253extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
[2a3214e]254
[a55d76b1]255extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
256extern void fibril_semaphore_up(fibril_semaphore_t *);
257extern void fibril_semaphore_down(fibril_semaphore_t *);
258
[f3afd24]259#endif
260
261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.