blob: b8f9a39508763e05a9906d0b645ef5248ab3f62b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/**
* @file sem.h
*/
#ifndef STASIS_SEMAPHORE_H
#define STASIS_SEMAPHORE_H
#include "core.h"
#include <semaphore.h>
#if defined(STASIS_OS_DARWIN)
// Darwin's sem_open() limits the path length to PSEMNAMLEN
// even though it isn't used directly.
#include <sys/posix_sem.h> // PSEMNAMLEN
#endif
struct Semaphore {
sem_t *sem;
char name[STASIS_NAME_MAX];
};
/**
* Initialize a cross-platform semaphore (Linux/Darwin)
*
* @code c
* #include "sem.h"
*
* int main(int argc, char *argv[]) {
* struct Semaphore s;
* if (semaphore_init(&s, "mysem", 1)) {
* perror("semaphore_init failed");
* exit(1);
* }
* if (semaphore_wait(&s)) {
* perror("semaphore_wait failed");
* exit(1);
* }
*
* //
* // Critical section
* // CODE HERE
* //
*
* if (semaphore_post(&s)) {
* perror("semaphore_post failed");
* exit(1);
* }
*
* semaphore_destroy(&s);
* }
* @endcode
*
* @param s a pointer to `Semaphore`
* @param name of the semaphore
* @param value initial value of the semaphore
* @return -1 on error
* @return 0 on success
*/
int semaphore_init(struct Semaphore *s, const char *name, int value);
int semaphore_wait(struct Semaphore *s);
int semaphore_post(struct Semaphore *s);
void semaphore_destroy(struct Semaphore *s);
#endif //STASIS_SEMAPHORE_H
|