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 POSIX_SEMAPHORE_H
26 #define POSIX_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 /* POSIX_SEMAPHORE_H */
298 
static int sem_unlink(const char *name)
Remove named semaphore name.
Definition: semaphore.h:218
Semaphore definitions.
static int sema_try_wait(sema_t *sema)
Test if the semaphore is posted.
Definition: sema.h:165
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
unsigned int value
value of the semaphore
Definition: sema.h:64
static int sem_post(sem_t *sem)
Unlock a semaphore.
Definition: semaphore.h:124
static int sema_wait(sema_t *sema)
Wait for a semaphore being posted (without timeout).
Definition: sema.h:149
static int sem_close(sem_t *sem)
Close descriptor for named semaphore sem.
Definition: semaphore.h:198
#define EINVAL
Invalid argument.
Definition: errno.h:97
void sema_destroy(sema_t *sema)
Destroys a semaphore.
A Semaphore.
Definition: sema.h:63
int errno
number of last error
static int sem_destroy(sem_t *sem)
destroy an unnamed semaphore
Definition: semaphore.h:96
int sema_post(sema_t *sema)
Signal semaphore.
#define SEM_FAILED
Value returned if `sem_open' failed.
Definition: semaphore.h:45
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
Similar to `sem_wait' but wait only until abstime.
static int sem_wait(sem_t *sem)
Lock a semaphore.
Definition: semaphore.h:152
static sem_t * sem_open(const char *name, int oflag,...)
Open a named semaphore name with open flags oflag.
Definition: semaphore.h:177
void sema_create(sema_t *sema, unsigned int value)
Creates semaphore dynamically.
#define ENOMEM
Not enough space.
Definition: errno.h:118
Definition of struct timespec for the MSP430.
Definition: msp430_types.h:50
sema_t sem_t
POSIX-specific semaphore type.
Definition: semaphore.h:40
#define ENOENT
No such file or directory.
Definition: errno.h:114
static int sem_getvalue(sem_t *sem, int *sval)
Get current value of sem and store it in sval.
Definition: semaphore.h:283