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

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

Implement MPSC FIFO channel and use it to pass inbound IPC calls to the connection fibril.

Technically, a SPSC channel would be sufficient for this, and might be added
in the future but MPSC is more widely useful.

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