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

Last change on this file since cd1e3fc0 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

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