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

Last change on this file since eec201d was bd41ac52, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

  • Property mode set to 100644
File size: 6.3 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>
[bd41ac52]41#include <time.h>
[3e6a98c5]42#include <stdbool.h>
[c124c985]43
[668f8cbf]44typedef struct {
[d161715]45 fibril_owner_info_t oi; /**< Keep this the first thing. */
[f3afd24]46 int counter;
[b72efe8]47 list_t waiters;
[f3afd24]48} fibril_mutex_t;
49
[e9460aa]50#define FIBRIL_MUTEX_INITIALIZER(name) \
51 { \
[668f8cbf]52 .oi = { \
53 .owned_by = NULL \
54 }, \
[f3afd24]55 .counter = 1, \
56 .waiters = { \
[b72efe8]57 .head = { \
58 .prev = &(name).waiters.head, \
59 .next = &(name).waiters.head, \
60 } \
[f3afd24]61 } \
62 }
[a35b458]63
[e9460aa]64#define FIBRIL_MUTEX_INITIALIZE(name) \
[1b20da0]65 fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
[f3afd24]66
67typedef struct {
[d161715]68 fibril_owner_info_t oi; /**< Keep this the first thing. */
[fb0ec570]69 unsigned int writers;
70 unsigned int readers;
[b72efe8]71 list_t waiters;
[f3afd24]72} fibril_rwlock_t;
73
[e9460aa]74#define FIBRIL_RWLOCK_INITIALIZER(name) \
75 { \
[668f8cbf]76 .oi = { \
77 .owned_by = NULL \
78 }, \
[92d34f0b]79 .readers = 0, \
80 .writers = 0, \
81 .waiters = { \
[b72efe8]82 .head = { \
83 .prev = &(name).waiters.head, \
84 .next = &(name).waiters.head, \
85 } \
[f3afd24]86 } \
87 }
88
[e9460aa]89#define FIBRIL_RWLOCK_INITIALIZE(name) \
90 fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
91
[9ae22ba]92typedef struct {
[b72efe8]93 list_t waiters;
[9ae22ba]94} fibril_condvar_t;
95
[e9460aa]96#define FIBRIL_CONDVAR_INITIALIZER(name) \
97 { \
[c51a7cd]98 .waiters = { \
[b72efe8]99 .head = { \
100 .next = &(name).waiters.head, \
101 .prev = &(name).waiters.head, \
102 } \
[c51a7cd]103 } \
104 }
105
[e9460aa]106#define FIBRIL_CONDVAR_INITIALIZE(name) \
107 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
108
[2a3214e]109typedef void (*fibril_timer_fun_t)(void *);
110
111typedef 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,
[53f68fd]118 /** Timer fibril is requested to terminate */
119 fts_cleanup,
120 /** Timer fibril acknowledged termination */
121 fts_clean
[2a3214e]122} fibril_timer_state_t;
123
124/** Fibril timer.
125 *
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.
130 */
131typedef struct {
132 fibril_mutex_t lock;
[78192cc7]133 fibril_mutex_t *lockp;
[2a3214e]134 fibril_condvar_t cv;
135 fid_t fibril;
136 fibril_timer_state_t state;
[7c15d6f]137 /** FID of fibril executing handler or 0 if handler is not running */
138 fid_t handler_fid;
[2a3214e]139
[bd41ac52]140 usec_t delay;
[2a3214e]141 fibril_timer_fun_t fun;
142 void *arg;
143} fibril_timer_t;
144
[a55d76b1]145/** A counting semaphore for fibrils. */
146typedef struct {
[fb0ec570]147 long int count;
[a55d76b1]148 list_t waiters;
[d742db21]149 bool closed;
[a55d76b1]150} fibril_semaphore_t;
151
152#define FIBRIL_SEMAPHORE_INITIALIZER(name, cnt) \
153 { \
154 .count = (cnt), \
155 .waiters = { \
156 .head = { \
157 .next = &(name).waiters.head, \
158 .prev = &(name).waiters.head, \
159 } \
160 } \
161 }
162
163#define FIBRIL_SEMAPHORE_INITIALIZE(name, cnt) \
164 fibril_semaphore_t name = FIBRIL_SEMAPHORE_INITIALIZER(name, cnt)
165
[f3afd24]166extern void fibril_mutex_initialize(fibril_mutex_t *);
167extern void fibril_mutex_lock(fibril_mutex_t *);
168extern bool fibril_mutex_trylock(fibril_mutex_t *);
169extern void fibril_mutex_unlock(fibril_mutex_t *);
[b0a76d5]170extern bool fibril_mutex_is_locked(fibril_mutex_t *);
[f3afd24]171
172extern void fibril_rwlock_initialize(fibril_rwlock_t *);
173extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
174extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
175extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
176extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
[b0a76d5]177extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
178extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
[c81b6f2]179extern bool fibril_rwlock_is_locked(fibril_rwlock_t *);
[f3afd24]180
[9ae22ba]181extern void fibril_condvar_initialize(fibril_condvar_t *);
[b7fd2a0]182extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
[bd41ac52]183 usec_t);
[9ae22ba]184extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
185extern void fibril_condvar_signal(fibril_condvar_t *);
186extern void fibril_condvar_broadcast(fibril_condvar_t *);
187
[78192cc7]188extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
[2a3214e]189extern void fibril_timer_destroy(fibril_timer_t *);
[bd41ac52]190extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
[2a3214e]191 void *);
[bd41ac52]192extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
[78192cc7]193 fibril_timer_fun_t, void *);
[2a3214e]194extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
[78192cc7]195extern fibril_timer_state_t fibril_timer_clear_locked(fibril_timer_t *);
[2a3214e]196
[a55d76b1]197extern void fibril_semaphore_initialize(fibril_semaphore_t *, long);
198extern void fibril_semaphore_up(fibril_semaphore_t *);
199extern void fibril_semaphore_down(fibril_semaphore_t *);
[bd41ac52]200extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
[d742db21]201extern void fibril_semaphore_close(fibril_semaphore_t *);
[a55d76b1]202
[1de92fb0]203typedef struct mpsc mpsc_t;
204extern mpsc_t *mpsc_create(size_t);
205extern void mpsc_destroy(mpsc_t *);
206extern errno_t mpsc_send(mpsc_t *, const void *);
[bd41ac52]207extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
[1de92fb0]208extern void mpsc_close(mpsc_t *);
209
[f3afd24]210#endif
211
212/** @}
213 */
Note: See TracBrowser for help on using the repository browser.