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

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

Use LIST_INITIALIZER() macro

  • Property mode set to 100644
File size: 6.1 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 <time.h>
42#include <stdbool.h>
43
44typedef struct {
45 fibril_owner_info_t oi; /**< Keep this the first thing. */
46 int counter;
47 list_t waiters;
48} fibril_mutex_t;
49
50#define FIBRIL_MUTEX_INITIALIZER(name) \
51 { \
52 .oi = { \
53 .owned_by = NULL \
54 }, \
55 .counter = 1, \
56 .waiters = LIST_INITIALIZER((name).waiters), \
57 }
58
59#define FIBRIL_MUTEX_INITIALIZE(name) \
60 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
61
62typedef struct {
63 fibril_owner_info_t oi; /**< Keep this the first thing. */
64 unsigned int writers;
65 unsigned int readers;
66 list_t waiters;
67} fibril_rwlock_t;
68
69#define FIBRIL_RWLOCK_INITIALIZER(name) \
70 { \
71 .oi = { \
72 .owned_by = NULL \
73 }, \
74 .readers = 0, \
75 .writers = 0, \
76 .waiters = LIST_INITIALIZER((name).waiters), \
77 }
78
79#define FIBRIL_RWLOCK_INITIALIZE(name) \
80 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
81
82typedef struct {
83 list_t waiters;
84} fibril_condvar_t;
85
86#define FIBRIL_CONDVAR_INITIALIZER(name) \
87 { \
88 .waiters = LIST_INITIALIZER((name).waiters), \
89 }
90
91#define FIBRIL_CONDVAR_INITIALIZE(name) \
92 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
93
94typedef void (*fibril_timer_fun_t)(void *);
95
96typedef enum {
97 /** Timer has not been set or has been cleared */
98 fts_not_set,
99 /** Timer was set but did not fire yet */
100 fts_active,
101 /** Timer has fired and has not been cleared since */
102 fts_fired,
103 /** Timer fibril is requested to terminate */
104 fts_cleanup,
105 /** Timer fibril acknowledged termination */
106 fts_clean
107} fibril_timer_state_t;
108
109/** Fibril timer.
110 *
111 * When a timer is set it executes a callback function (in a separate
112 * fibril) after a specified time interval. The timer can be cleared
113 * (canceled) before that. From the return value of fibril_timer_clear()
114 * one can tell whether the timer fired or not.
115 */
116typedef struct {
117 fibril_mutex_t lock;
118 fibril_mutex_t *lockp;
119 fibril_condvar_t cv;
120 fid_t fibril;
121 fibril_timer_state_t state;
122 /** FID of fibril executing handler or 0 if handler is not running */
123 fid_t handler_fid;
124
125 usec_t delay;
126 fibril_timer_fun_t fun;
127 void *arg;
128} fibril_timer_t;
129
130/** A counting semaphore for fibrils. */
131typedef struct {
132 long int count;
133 list_t waiters;
134 bool closed;
135} fibril_semaphore_t;
136
137#define FIBRIL_SEMAPHORE_INITIALIZER(name, cnt) \
138 { \
139 .count = (cnt), \
140 .waiters = LIST_INITIALIZER((name).waiters), \
141 }
142
143#define FIBRIL_SEMAPHORE_INITIALIZE(name, cnt) \
144 fibril_semaphore_t name = FIBRIL_SEMAPHORE_INITIALIZER(name, cnt)
145
146extern void __fibril_synch_init(void);
147extern void __fibril_synch_fini(void);
148
149extern void fibril_mutex_initialize(fibril_mutex_t *);
150extern void fibril_mutex_lock(fibril_mutex_t *);
151extern bool fibril_mutex_trylock(fibril_mutex_t *);
152extern void fibril_mutex_unlock(fibril_mutex_t *);
153extern bool fibril_mutex_is_locked(fibril_mutex_t *);
154
155extern void fibril_rwlock_initialize(fibril_rwlock_t *);
156extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
157extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
158extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
159extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
160extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
161extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
162extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
163
164extern void fibril_condvar_initialize(fibril_condvar_t *);
165extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
166 usec_t);
167extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
168extern void fibril_condvar_signal(fibril_condvar_t *);
169extern void fibril_condvar_broadcast(fibril_condvar_t *);
170
171extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
172extern void fibril_timer_destroy(fibril_timer_t *);
173extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
174 void *);
175extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
176 fibril_timer_fun_t, void *);
177extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
178extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
179
180extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
181extern void fibril_semaphore_up(fibril_semaphore_t *);
182extern void fibril_semaphore_down(fibril_semaphore_t *);
183extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
184extern void fibril_semaphore_close(fibril_semaphore_t *);
185
186typedef struct mpsc mpsc_t;
187extern mpsc_t *mpsc_create(size_t);
188extern void mpsc_destroy(mpsc_t *);
189extern errno_t mpsc_send(mpsc_t *, const void *);
190extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
191extern void mpsc_close(mpsc_t *);
192
193#endif
194
195/** @}
196 */
Note: See TracBrowser for help on using the repository browser.