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;
65  sema_state_t state;
67 } sema_t;
68 
81 void sema_create(sema_t *sema, unsigned int value);
82 
98 void sema_destroy(sema_t *sema);
99 
107 static inline unsigned sema_get_value(const sema_t *sema)
108 {
109  return sema->value;
110 }
111 
130 int _sema_wait(sema_t *sema, int block, uint64_t timeout);
131 
146 static inline int sema_wait_timed(sema_t *sema, uint64_t timeout)
147 {
148  return _sema_wait(sema, (timeout != 0), timeout);
149 }
150 
161 static inline int sema_wait(sema_t *sema)
162 {
163  return _sema_wait(sema, 1, 0);
164 }
165 
177 static inline int sema_try_wait(sema_t *sema)
178 {
179  return _sema_wait(sema, 0, 0);
180 }
181 
192 int sema_post(sema_t *sema);
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* SEMA_H */
199 
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition: sema.h:177
mutex_t mutex
mutex of the semaphore
Definition: sema.h:66
sema_state_t state
state of the semaphore
Definition: sema.h:65
unsigned int value
value of the semaphore
Definition: sema.h:64
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
Definition: sema.h:161
sema_state_t
A Semaphore states.
Definition: sema.h:55
void sema_destroy(sema_t *sema)
Destroys a semaphore.
static int sema_wait_timed(sema_t *sema, uint64_t timeout)
Wait for a semaphore being posted.
Definition: sema.h:146
A Semaphore.
Definition: sema.h:63
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore&#39;s current value.
Definition: sema.h:107
Mutex for thread synchronization.
int sema_post(sema_t *sema)
Signal semaphore.
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
Mutex structure.
Definition: mutex.h:123
int _sema_wait(sema_t *sema, int block, uint64_t timeout)
Wait for a semaphore, blocking or non-blocking.