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@…>, 8 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
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>
[0b05082]40#include <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
[1de92fb0]52 * operations like cvar signal and mutex unlock, unless otherwise
53 * specified.
[c124c985]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
[668f8cbf]84typedef struct {
[d161715]85 fibril_owner_info_t oi; /**< Keep this the first thing. */
[f3afd24]86 int counter;
[b72efe8]87 list_t waiters;
[f3afd24]88} fibril_mutex_t;
89
[e9460aa]90#define FIBRIL_MUTEX_INITIALIZER(name) \
91 { \
[668f8cbf]92 .oi = { \
93 .owned_by = NULL \
94 }, \
[f3afd24]95 .counter = 1, \
96 .waiters = { \
[b72efe8]97 .head = { \
98 .prev = &(name).waiters.head, \
99 .next = &(name).waiters.head, \
100 } \
[f3afd24]101 } \
102 }
[a35b458]103
[e9460aa]104#define FIBRIL_MUTEX_INITIALIZE(name) \
[1b20da0]105 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
[f3afd24]106
107typedef struct {
[d161715]108 fibril_owner_info_t oi; /**< Keep this the first thing. */
[fb0ec570]109 unsigned int writers;
110 unsigned int readers;
[b72efe8]111 list_t waiters;
[f3afd24]112} fibril_rwlock_t;
113
[e9460aa]114#define FIBRIL_RWLOCK_INITIALIZER(name) \
115 { \
[668f8cbf]116 .oi = { \
117 .owned_by = NULL \
118 }, \
[92d34f0b]119 .readers = 0, \
120 .writers = 0, \
121 .waiters = { \
[b72efe8]122 .head = { \
123 .prev = &(name).waiters.head, \
124 .next = &(name).waiters.head, \
125 } \
[f3afd24]126 } \
127 }
128
[e9460aa]129#define FIBRIL_RWLOCK_INITIALIZE(name) \
130 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
131
[9ae22ba]132typedef struct {
[b72efe8]133 list_t waiters;
[9ae22ba]134} fibril_condvar_t;
135
[e9460aa]136#define FIBRIL_CONDVAR_INITIALIZER(name) \
137 { \
[c51a7cd]138 .waiters = { \
[b72efe8]139 .head = { \
140 .next = &(name).waiters.head, \
141 .prev = &(name).waiters.head, \
142 } \
[c51a7cd]143 } \
144 }
145
[e9460aa]146#define FIBRIL_CONDVAR_INITIALIZE(name) \
147 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
148
[2a3214e]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,
[53f68fd]158 /** Timer fibril is requested to terminate */
159 fts_cleanup,
160 /** Timer fibril acknowledged termination */
161 fts_clean
[2a3214e]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;
[78192cc7]173 fibril_mutex_t *lockp;
[2a3214e]174 fibril_condvar_t cv;
175 fid_t fibril;
176 fibril_timer_state_t state;
[7c15d6f]177 /** FID of fibril executing handler or 0 if handler is not running */
178 fid_t handler_fid;
[2a3214e]179
180 suseconds_t delay;
181 fibril_timer_fun_t fun;
182 void *arg;
183} fibril_timer_t;
184
[a55d76b1]185/** A counting semaphore for fibrils. */
186typedef struct {
[fb0ec570]187 long int count;
[a55d76b1]188 list_t waiters;
[d742db21]189 bool closed;
[a55d76b1]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
[2965d18]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
[f3afd24]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 *);
[b0a76d5]215extern bool fibril_mutex_is_locked(fibril_mutex_t *);
[f3afd24]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 *);
[b0a76d5]222extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
223extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
[c81b6f2]224extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
[f3afd24]225
[9ae22ba]226extern void fibril_condvar_initialize(fibril_condvar_t *);
[b7fd2a0]227extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
[cadfa8e]228 suseconds_t);
[9ae22ba]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
[78192cc7]233extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
[2a3214e]234extern void fibril_timer_destroy(fibril_timer_t *);
235extern void fibril_timer_set(fibril_timer_t *, suseconds_t, fibril_timer_fun_t,
236 void *);
[78192cc7]237extern void fibril_timer_set_locked(fibril_timer_t *, suseconds_t,
238 fibril_timer_fun_t, void *);
[2a3214e]239extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
[78192cc7]240extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
[2a3214e]241
[a55d76b1]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 *);
[d742db21]245extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, suseconds_t);
246extern void fibril_semaphore_close(fibril_semaphore_t *);
[a55d76b1]247
[1de92fb0]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
[f3afd24]255#endif
256
257/** @}
258 */
Note: See TracBrowser for help on using the repository browser.