C++: mutex::init should be constexpr
[helenos.git] / uspace / lib / c / include / fibril_synch.h
blobfc32aa13d60bf3b458a2ff9ba1a1841f991fb966
1 /*
2 * Copyright (c) 2009 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup libc
30 * @{
32 /** @file
35 #ifndef _LIBC_FIBRIL_SYNCH_H_
36 #define _LIBC_FIBRIL_SYNCH_H_
38 #include <fibril.h>
39 #include <adt/list.h>
40 #include <time.h>
41 #include <stdbool.h>
42 #include <_bits/decls.h>
44 #ifndef __cplusplus
46 #define FIBRIL_MUTEX_INITIALIZER(name) \
47 { \
48 .oi = { \
49 .owned_by = NULL \
50 }, \
51 .counter = 1, \
52 .waiters = LIST_INITIALIZER((name).waiters), \
55 #define FIBRIL_MUTEX_INITIALIZE(name) \
56 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
58 #define FIBRIL_RWLOCK_INITIALIZER(name) \
59 { \
60 .oi = { \
61 .owned_by = NULL \
62 }, \
63 .readers = 0, \
64 .writers = 0, \
65 .waiters = LIST_INITIALIZER((name).waiters), \
68 #define FIBRIL_RWLOCK_INITIALIZE(name) \
69 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
71 #define FIBRIL_CONDVAR_INITIALIZER(name) \
72 { \
73 .waiters = LIST_INITIALIZER((name).waiters), \
76 #define FIBRIL_CONDVAR_INITIALIZE(name) \
77 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
79 #define FIBRIL_SEMAPHORE_INITIALIZER(name, cnt) \
80 { \
81 .count = (cnt), \
82 .waiters = LIST_INITIALIZER((name).waiters), \
85 #define FIBRIL_SEMAPHORE_INITIALIZE(name, cnt) \
86 fibril_semaphore_t name = FIBRIL_SEMAPHORE_INITIALIZER(name, cnt)
88 #endif
90 __HELENOS_DECLS_BEGIN;
92 typedef struct {
93 fibril_owner_info_t oi; /**< Keep this the first thing. */
94 int counter;
95 list_t waiters;
96 } fibril_mutex_t;
98 typedef struct {
99 fibril_owner_info_t oi; /**< Keep this the first thing. */
100 unsigned int writers;
101 unsigned int readers;
102 list_t waiters;
103 } fibril_rwlock_t;
105 typedef struct {
106 list_t waiters;
107 } fibril_condvar_t;
109 typedef void (*fibril_timer_fun_t)(void *);
111 typedef enum {
112 /** Timer has not been set or has been cleared */
113 fts_not_set,
114 /** Timer was set but did not fire yet */
115 fts_active,
116 /** Timer has fired and has not been cleared since */
117 fts_fired,
118 /** Timer fibril is requested to terminate */
119 fts_cleanup,
120 /** Timer fibril acknowledged termination */
121 fts_clean
122 } fibril_timer_state_t;
124 /** Fibril timer.
126 * When a timer is set it executes a callback function (in a separate
127 * fibril) after a specified time interval. The timer can be cleared
128 * (canceled) before that. From the return value of fibril_timer_clear()
129 * one can tell whether the timer fired or not.
131 typedef struct {
132 fibril_mutex_t lock;
133 fibril_mutex_t *lockp;
134 fibril_condvar_t cv;
135 fid_t fibril;
136 fibril_timer_state_t state;
137 /** FID of fibril executing handler or 0 if handler is not running */
138 fid_t handler_fid;
140 usec_t delay;
141 fibril_timer_fun_t fun;
142 void *arg;
143 } fibril_timer_t;
145 /** A counting semaphore for fibrils. */
146 typedef struct {
147 long int count;
148 list_t waiters;
149 bool closed;
150 } fibril_semaphore_t;
152 extern void __fibril_synch_init(void);
153 extern void __fibril_synch_fini(void);
155 /** Initialize fibril mutex.
157 * Kept as in-line to allow constexpr marker for C++ library where this
158 * is used by C++ mutex type (list initialization are two assignments
159 * so it is actually reasonable to have this inlined).
161 static inline __CONSTEXPR void fibril_mutex_initialize(fibril_mutex_t *fm)
163 fm->oi.owned_by = NULL;
164 fm->counter = 1;
165 list_initialize(&fm->waiters);
168 extern void fibril_mutex_lock(fibril_mutex_t *);
169 extern bool fibril_mutex_trylock(fibril_mutex_t *);
170 extern void fibril_mutex_unlock(fibril_mutex_t *);
171 extern bool fibril_mutex_is_locked(fibril_mutex_t *);
173 extern void fibril_rwlock_initialize(fibril_rwlock_t *);
174 extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
175 extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
176 extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
177 extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
178 extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
179 extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
180 extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
182 extern void fibril_condvar_initialize(fibril_condvar_t *);
183 extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
184 usec_t);
185 extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
186 extern void fibril_condvar_signal(fibril_condvar_t *);
187 extern void fibril_condvar_broadcast(fibril_condvar_t *);
189 extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
190 extern void fibril_timer_destroy(fibril_timer_t *);
191 extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
192 void *);
193 extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
194 fibril_timer_fun_t, void *);
195 extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
196 extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
198 extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
199 extern void fibril_semaphore_up(fibril_semaphore_t *);
200 extern void fibril_semaphore_down(fibril_semaphore_t *);
201 extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
202 extern void fibril_semaphore_close(fibril_semaphore_t *);
204 typedef struct mpsc mpsc_t;
205 extern mpsc_t *mpsc_create(size_t);
206 extern void mpsc_destroy(mpsc_t *);
207 extern errno_t mpsc_send(mpsc_t *, const void *);
208 extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
209 extern void mpsc_close(mpsc_t *);
211 __HELENOS_DECLS_END;
213 #endif
215 /** @}