semaphore.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 TriaGnoSys GmbH
3  * 2013 Freie Universität Berlin
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 SEMAPHORE_H
26 #define SEMAPHORE_H
27 
28 #include <errno.h>
29 #include <time.h>
30 
31 #include "sema.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 typedef sema_t sem_t;
41 
45 #define SEM_FAILED ((sem_t *) 0)
46 
68 static inline int sem_init(sem_t *sem, int pshared, unsigned value)
69 {
70  sema_create((sema_t *)sem, value);
71  (void)pshared;
72  return 0;
73 }
74 
96 static inline int sem_destroy(sem_t *sem)
97 {
98  sema_destroy((sema_t *)sem);
99  return 0;
100 }
101 
124 static inline int sem_post(sem_t *sem)
125 {
126  int res = sema_post((sema_t *)sem);
127 
128  if (res < 0) {
129  errno = -res;
130  return -1;
131  }
132  return 0;
133 }
134 
152 static inline int sem_wait(sem_t *sem)
153 {
154  int res = sema_wait((sema_t *)sem);
155 
156  if (res < 0) {
157  errno = -res;
158  return -1;
159  }
160  return 0;
161 }
162 
177 static inline sem_t *sem_open(const char *name, int oflag, ...)
178 {
179  (void)name;
180  (void)oflag;
181  errno = ENOMEM;
182  return SEM_FAILED;
183 }
184 
198 static inline int sem_close(sem_t *sem)
199 {
200  (void)sem;
201  errno = EINVAL;
202  return -1;
203 }
204 
218 static inline int sem_unlink(const char *name)
219 {
220  (void)name;
221  errno = ENOENT;
222  return -1;
223 }
224 
245 int sem_timedwait(sem_t *sem, const struct timespec *abstime);
246 
259 static inline int sem_trywait(sem_t *sem)
260 {
261  int res = sema_try_wait((sema_t *)sem);
262 
263  if (res < 0) {
264  errno = -res;
265  return -1;
266  }
267  return 0;
268 }
269 
283 static inline int sem_getvalue(sem_t *sem, int *sval)
284 {
285  if (sem != NULL) {
286  *sval = (int)sem->value;
287  return 0;
288  }
289  errno = EINVAL;
290  return -1;
291 }
292 
293 #ifdef __cplusplus
294 }
295 #endif
296 
297 #endif /* SEMAPHORE_H */
298 
EINVAL
#define EINVAL
Invalid argument.
Definition: errno.h:97
sem_destroy
static int sem_destroy(sem_t *sem)
destroy an unnamed semaphore
Definition: semaphore.h:96
SEM_FAILED
#define SEM_FAILED
Value returned if ‘sem_open’ failed.
Definition: semaphore.h:45
sem_timedwait
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
Similar to ‘sem_wait’ but wait only until abstime.
errno
int errno
number of last error
sem_wait
static int sem_wait(sem_t *sem)
Lock a semaphore.
Definition: semaphore.h:152
ENOMEM
#define ENOMEM
Not enough space.
Definition: errno.h:118
sem_t
sema_t sem_t
POSIX-specific semaphore type.
Definition: semaphore.h:40
sem_close
static int sem_close(sem_t *sem)
Close descriptor for named semaphore sem.
Definition: semaphore.h:198
sema_destroy
void sema_destroy(sema_t *sema)
Destroys a semaphore.
sema.h
Semaphore definitions.
ENOENT
#define ENOENT
No such file or directory.
Definition: errno.h:114
sem_getvalue
static int sem_getvalue(sem_t *sem, int *sval)
Get current value of sem and store it in sval.
Definition: semaphore.h:283
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
sem_post
static int sem_post(sem_t *sem)
Unlock a semaphore.
Definition: semaphore.h:124
sem_init
static int sem_init(sem_t *sem, int pshared, unsigned value)
Initialize an unnamed semaphore.
Definition: semaphore.h:68
sem_open
static sem_t * sem_open(const char *name, int oflag,...)
Open a named semaphore name with open flags oflag.
Definition: semaphore.h:177
sema_t
A Semaphore.
Definition: sema.h:63
sem_unlink
static int sem_unlink(const char *name)
Remove named semaphore name.
Definition: semaphore.h:218
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
sem_trywait
static int sem_trywait(sem_t *sem)
Test whether sem is posted.
Definition: semaphore.h:259
errno.h