Loading...
Searching...
No Matches
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
34extern "C" {
35#endif
36
40typedef sema_t sem_t;
41
45#define SEM_FAILED ((sem_t *) 0)
46
68static 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
96static inline int sem_destroy(sem_t *sem)
97{
98 sema_destroy((sema_t *)sem);
99 return 0;
100}
101
124static 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
152static 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
177static 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
198static inline int sem_close(sem_t *sem)
199{
200 (void)sem;
201 errno = EINVAL;
202 return -1;
203}
204
218static inline int sem_unlink(const char *name)
219{
220 (void)name;
221 errno = ENOENT;
222 return -1;
223}
224
245int sem_timedwait(sem_t *sem, const struct timespec *abstime);
246
259static 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
283static inline int sem_getvalue(sem_t *sem, int *sval)
284{
285 if (sem != NULL) {
286 *sval = (int)sema_get_value((sema_t *)sem);
287 return 0;
288 }
289 errno = EINVAL;
290 return -1;
291}
292
293#ifdef __cplusplus
294}
295#endif
296
297#endif /* SEMAPHORE_H */
#define ENOENT
No such file or directory.
Definition errno.h:114
#define EINVAL
Invalid argument.
Definition errno.h:97
#define ENOMEM
Not enough space.
Definition errno.h:118
int errno
number of last error
#define SEM_FAILED
Value returned if ‘sem_open’ failed.
Definition semaphore.h:45
static int sem_unlink(const char *name)
Remove named semaphore name.
Definition semaphore.h:218
static int sem_getvalue(sem_t *sem, int *sval)
Get current value of sem and store it in sval.
Definition semaphore.h:283
sema_t sem_t
POSIX-specific semaphore type.
Definition semaphore.h:40
static int sem_init(sem_t *sem, int pshared, unsigned value)
Initialize an unnamed semaphore.
Definition semaphore.h:68
static int sem_trywait(sem_t *sem)
Test whether sem is posted.
Definition semaphore.h:259
static sem_t * sem_open(const char *name, int oflag,...)
Open a named semaphore name with open flags oflag.
Definition semaphore.h:177
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
Similar to ‘sem_wait’ but wait only until abstime.
static int sem_destroy(sem_t *sem)
destroy an unnamed semaphore
Definition semaphore.h:96
static int sem_post(sem_t *sem)
Unlock a semaphore.
Definition semaphore.h:124
static int sem_wait(sem_t *sem)
Lock a semaphore.
Definition semaphore.h:152
static int sem_close(sem_t *sem)
Close descriptor for named semaphore sem.
Definition semaphore.h:198
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
Definition sema.h:173
void sema_destroy(sema_t *sema)
Destroys a semaphore.
static unsigned sema_get_value(const sema_t *sema)
Get a semaphore's current value.
Definition sema.h:112
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition sema.h:189
int sema_post(sema_t *sema)
Signal semaphore.
Semaphore definitions.
A Semaphore.
Definition sema.h:68