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
RevLine 
[f3afd24]1/*
[d7f7a4a]2 * SPDX-FileCopyrightText: 2009 Jakub Jermar
[f3afd24]3 *
[d7f7a4a]4 * SPDX-License-Identifier: BSD-3-Clause
[f3afd24]5 */
6
7/** @addtogroup libc
8 * @{
9 */
10/** @file
11 */
12
[4805495]13#ifndef _LIBC_FIBRIL_SYNCH_H_
14#define _LIBC_FIBRIL_SYNCH_H_
[f3afd24]15
16#include <fibril.h>
17#include <adt/list.h>
[bd41ac52]18#include <time.h>
[3e6a98c5]19#include <stdbool.h>
[bc56f30]20#include <_bits/decls.h>
[c124c985]21
[bc56f30]22#ifndef __cplusplus
[f3afd24]23
[e9460aa]24#define FIBRIL_MUTEX_INITIALIZER(name) \
25 { \
[668f8cbf]26 .oi = { \
27 .owned_by = NULL \
28 }, \
[f3afd24]29 .counter = 1, \
[c3fa24e]30 .waiters = LIST_INITIALIZER((name).waiters), \
[f3afd24]31 }
[a35b458]32
[e9460aa]33#define FIBRIL_MUTEX_INITIALIZE(name) \
[1b20da0]34 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
[f3afd24]35
[e9460aa]36#define FIBRIL_RWLOCK_INITIALIZER(name) \
37 { \
[668f8cbf]38 .oi = { \
39 .owned_by = NULL \
40 }, \
[92d34f0b]41 .readers = 0, \
42 .writers = 0, \
[c3fa24e]43 .waiters = LIST_INITIALIZER((name).waiters), \
[f3afd24]44 }
45
[e9460aa]46#define FIBRIL_RWLOCK_INITIALIZE(name) \
47 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
48
49#define FIBRIL_CONDVAR_INITIALIZER(name) \
50 { \
[c3fa24e]51 .waiters = LIST_INITIALIZER((name).waiters), \
[c51a7cd]52 }
53
[e9460aa]54#define FIBRIL_CONDVAR_INITIALIZE(name) \
55 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
56
[bc56f30]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
[2a3214e]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,
[53f68fd]96 /** Timer fibril is requested to terminate */
97 fts_cleanup,
98 /** Timer fibril acknowledged termination */
99 fts_clean
[2a3214e]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;
[78192cc7]111 fibril_mutex_t *lockp;
[2a3214e]112 fibril_condvar_t cv;
113 fid_t fibril;
114 fibril_timer_state_t state;
[7c15d6f]115 /** FID of fibril executing handler or 0 if handler is not running */
116 fid_t handler_fid;
[2a3214e]117
[bd41ac52]118 usec_t delay;
[2a3214e]119 fibril_timer_fun_t fun;
120 void *arg;
121} fibril_timer_t;
122
[a55d76b1]123/** A counting semaphore for fibrils. */
124typedef struct {
[fb0ec570]125 long int count;
[a55d76b1]126 list_t waiters;
[d742db21]127 bool closed;
[a55d76b1]128} fibril_semaphore_t;
129
[45c8eea]130extern void __fibril_synch_init(void);
[25f6bddb]131extern void __fibril_synch_fini(void);
[45c8eea]132
[f3afd24]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 *);
[b0a76d5]137extern bool fibril_mutex_is_locked(fibril_mutex_t *);
[f3afd24]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 *);
[b0a76d5]144extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
145extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
[c81b6f2]146extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
[f3afd24]147
[9ae22ba]148extern void fibril_condvar_initialize(fibril_condvar_t *);
[b7fd2a0]149extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
[bd41ac52]150 usec_t);
[9ae22ba]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
[78192cc7]155extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
[2a3214e]156extern void fibril_timer_destroy(fibril_timer_t *);
[bd41ac52]157extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
[2a3214e]158 void *);
[bd41ac52]159extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
[78192cc7]160 fibril_timer_fun_t, void *);
[2a3214e]161extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
[78192cc7]162extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
[2a3214e]163
[a55d76b1]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 *);
[bd41ac52]167extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
[d742db21]168extern void fibril_semaphore_close(fibril_semaphore_t *);
[a55d76b1]169
[1de92fb0]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 *);
[bd41ac52]174extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
[1de92fb0]175extern void mpsc_close(mpsc_t *);
176
[bc56f30]177__HELENOS_DECLS_END;
178
[f3afd24]179#endif
180
181/** @}
182 */
Note: See TracBrowser for help on using the repository browser.