sema.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 TriaGnoSys GmbH
3  * 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
25 #ifndef SEMA_H
26 #define SEMA_H
27 
28 #include <stdint.h>
29 
30 #include "mutex.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
44 #define SEMA_CREATE(value) { (value), SEMA_OK, MUTEX_INIT }
45 
50 #define SEMA_CREATE_LOCKED() { (0), SEMA_OK, MUTEX_INIT_LOCKED }
51 
55 typedef enum {
56  SEMA_OK = 0,
57  SEMA_DESTROY,
58 } sema_state_t;
59 
63 typedef struct {
64  unsigned int value;
67 } sema_t;
68 
81 void sema_create(sema_t *sema, unsigned int value);
82 
98 void sema_destroy(sema_t *sema);
99 
118 int _sema_wait(sema_t *sema, int block, uint64_t timeout);
119 
134 static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
135 {
136  return _sema_wait(sema, (timeout != 0), timeout);
137 }
138 
149 static inline int sema_wait(sema_t *sema)
150 {
151  return _sema_wait(sema, 1, 0);
152 }
153 
165 static inline int sema_try_wait(sema_t *sema)
166 {
167  return _sema_wait(sema, 0, 0);
168 }
169 
180 int sema_post(sema_t *sema);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* SEMA_H */
187 
sema_t::mutex
mutex_t mutex
mutex of the semaphore
Definition: sema.h:66
sema_wait_timed
static int sema_wait_timed(sema_t *sema, uint64_t timeout)
Wait for a semaphore being posted.
Definition: sema.h:134
sema_destroy
void sema_destroy(sema_t *sema)
Destroys a semaphore.
sema_state_t
sema_state_t
A Semaphore states.
Definition: sema.h:55
sema_create
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
sema_wait
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
Definition: sema.h:149
_sema_wait
int _sema_wait(sema_t *sema, int block, uint64_t timeout)
Wait for a semaphore, blocking or non-blocking.
sema_t::state
sema_state_t state
state of the semaphore
Definition: sema.h:65
mutex.h
Mutex for thread synchronization.
sema_t
A Semaphore.
Definition: sema.h:63
sema_t::value
unsigned int value
value of the semaphore
Definition: sema.h:64
sema_post
int sema_post(sema_t *sema)
Signal semaphore.
sema_try_wait
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition: sema.h:165
mutex_t
Mutex structure.
Definition: mutex.h:120