-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsem.c
183 lines (144 loc) · 2.43 KB
/
sem.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <sys/types.h>
#include <stdint.h>
#include "flowd.h"
#ifdef WITH_THREADS
#include <pthread.h>
static pthread_cond_t cond;
static pthread_mutex_t mutex;
int flow_sem_init(void)
{
pthread_mutex_init(&mutex, NULL);
return pthread_cond_init(&cond, NULL);
}
int flow_sem_init_poster(void)
{
return 0;
}
int flow_sem_init_waiter(void)
{
return 0;
}
int flow_sem_post(void)
{
return pthread_cond_signal(&cond);
}
int flow_sem_wait(void)
{
int r;
r = pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
return r;
}
int flow_sem_zero(void)
{
return pthread_mutex_lock(&mutex);
}
int flow_sem_lock(void)
{
return pthread_mutex_lock(&mutex);
}
int flow_sem_unlock(void)
{
return pthread_mutex_unlock(&mutex);
}
void flow_sem_destroy(void)
{
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
#elif defined(POSIX_SEM) && defined(WITH_RECEIVER) /* POSIX SEMAPHORES */
#include <semaphore.h>
#include <errno.h>
static sem_t sem;
int flow_sem_init(void)
{
return sem_init(&sem, 1, 1);
}
int flow_sem_init_poster(void)
{
return 0;
}
int flow_sem_init_waiter(void)
{
return 0;
}
int flow_sem_post(void)
{
return sem_post(&sem);
}
int flow_sem_wait(void)
{
return sem_wait(&sem);
}
int flow_sem_zero(void)
{
while (sem_trywait(&sem) == 0);
return (errno == EAGAIN) ? 0 : -1;
}
int flow_sem_lock(void)
{
return 0; /* sem_wait(&sem2); */
}
int flow_sem_unlock(void)
{
return 0; /* sem_post(&sem2); */
}
void flow_sem_destroy(void)
{
sem_destroy(&sem);
}
#elif defined(WITH_RECEIVER) /* signaling via unnamed pipe */
#include <unistd.h>
#include <fcntl.h>
static int hpipe[2];
static char buf[16384];
int flow_sem_init(void)
{
return pipe(hpipe);
}
int flow_sem_init_poster(void)
{
close(hpipe[0]);
hpipe[0] = -1;
return 0;
}
int flow_sem_init_waiter(void)
{
close(hpipe[1]);
hpipe[1] = -1;
return 0;
}
int flow_sem_post(void)
{
fcntl(hpipe[1], F_SETFL, O_NONBLOCK);
write(hpipe[1], "", 1);
return 0;
}
int flow_sem_wait(void)
{
fcntl(hpipe[0], F_SETFL, 0);
read(hpipe[0], buf, sizeof(buf));
return 0;
}
int flow_sem_zero(void)
{
fcntl(hpipe[0], F_SETFL, O_NONBLOCK);
while (read(hpipe[0], buf, sizeof(buf)) > 0);
return 0;
}
int flow_sem_lock(void)
{
return 0; /* sem_wait(&sem2); */
}
int flow_sem_unlock(void)
{
return 0; /* sem_post(&sem2); */
}
void flow_sem_destroy(void)
{
if (hpipe[1] != -1)
{ close(hpipe[1]);
hpipe[1] = -1;
}
}
#endif