-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgl_chan.h
281 lines (245 loc) · 7.27 KB
/
hgl_chan.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* LICENSE:
*
* MIT License
*
* Copyright (c) 2023 Henrik A. Glass
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* MIT License
*
*
* ABOUT:
*
* hgl_chan.h implements a thread-safe message queue similar to go channels.
*
*
* USAGE:
*
* Include hgl_chan.h file like this:
*
* #define HGL_CHAN_IMPLEMENTATION
* #include "hgl_chan.h"
*
*
* EXAMPLE:
*
* See the examples directory.
*
*
* AUTHOR: Henrik A. Glass
*
*/
#ifndef HGL_CHAN_H
#define HGL_CHAN_H
/*--- Include files ---------------------------------------------------------------------*/
#include <stdbool.h>
#include <pthread.h>
#include <stdarg.h>
/*--- Public type definitions -----------------------------------------------------------*/
typedef struct
{
void *item;
pthread_mutex_t mutex;
pthread_cond_t cvar_writable;
pthread_cond_t cvar_readable;
bool readable;
int efd;
} HglChan;
/**
* Creates a channel.
*/
HglChan hgl_chan_make(void);
/**
* Destroys the channel `c`.
*/
void hgl_chan_destroy(HglChan *c);
/**
* Sends item `item` on the channel `c`. Will block if there already
* is an item in `c`.
*/
void hgl_chan_send(HglChan *c, void *item);
/**
* Tries to send the item `item` on the channel `c`. If there already is
* an item on `c`, `item` will not be sent and -1 will be returned, otherwise,
* if `item` was successfully sent, 0 will be returned.
*/
int hgl_chan_try_send(HglChan *c, void *item);
/**
* Recieve an item from the channel `c`. Will block until there is an item
* on `c`.
*/
void *hgl_chan_recv(HglChan *c);
/**
* Recieve an item from the channel `c`. Will return immediately return NULL
* if there is no item on `c`. If there is an item on `c`, it will be returned.
*/
void *hgl_chan_try_recv(HglChan *c);
/**
* Wait on any number (<= 128) of channels simultaneously, until at least one
* of them becomes readable (i.e. something was sent on the channel). Returns
* a pointer to the first readable channel in the variadic arguments list.
*/
HglChan *hgl_chan_select(int n_args, ...);
/**
* Returns a pointer to the first readable channel in the variadic arguments
* list. If there are no readable channels NULL is returned.
*/
HglChan *hgl_chan_try_select(int n_args, ...);
#endif /* HGL_CHAN_H */
#ifdef HGL_CHAN_IMPLEMENTATION
#include <sys/eventfd.h>
#include <unistd.h>
#include <poll.h>
#include <stdlib.h>
HglChan hgl_chan_make()
{
int err;
HglChan c;
c.readable = false;
err = pthread_mutex_init(&c.mutex, NULL);
err |= pthread_cond_init(&c.cvar_writable, NULL);
err |= pthread_cond_init(&c.cvar_readable, NULL);
c.efd = eventfd(0, 0);
if (err != 0) {
fprintf(stderr, "[hgl_chan.h]: Failed to create channel.\n");
}
return c;
}
void hgl_chan_destroy(HglChan *c)
{
pthread_mutex_destroy(&c->mutex);
pthread_cond_destroy(&c->cvar_writable);
pthread_cond_destroy(&c->cvar_readable);
close(c->efd);
}
void hgl_chan_send(HglChan *c, void *item)
{
pthread_mutex_lock(&c->mutex);
while (c->readable /* implies not writable */) {
pthread_cond_wait(&c->cvar_writable, &c->mutex);
}
c->item = item;
c->readable = true;
uint64_t efd_inc = 1;
ssize_t n_bytes_written = write(c->efd, &efd_inc, sizeof(uint64_t)); // increment counter
if (n_bytes_written != sizeof(uint64_t)) {
fprintf(stderr, "[hgl_chan.h]: Aborted <%s, %d>\n", __FILE__, __LINE__);
abort(); // Temporary, TODO handle this properly
}
pthread_cond_signal(&c->cvar_readable);
pthread_mutex_unlock(&c->mutex);
}
int hgl_chan_try_send(HglChan *c, void *item)
{
pthread_mutex_lock(&c->mutex);
if (c->readable /* impl. not writable */) {
pthread_mutex_unlock(&c->mutex);
return -1;
}
c->item = item;
c->readable = true;
uint64_t efd_inc = 1;
ssize_t n_bytes_written = write(c->efd, &efd_inc, sizeof(uint64_t)); // increment counter
if (n_bytes_written != sizeof(uint64_t)) {
fprintf(stderr, "[hgl_chan.h]: Aborted <%s, %d>\n", __FILE__, __LINE__);
abort(); // Temporary, TODO handle this properly
}
pthread_cond_signal(&c->cvar_readable);
pthread_mutex_unlock(&c->mutex);
return 0;
}
void *hgl_chan_recv(HglChan *c)
{
pthread_mutex_lock(&c->mutex);
while (!c->readable) {
pthread_cond_wait(&c->cvar_readable, &c->mutex);
}
void *item = c->item;
c->readable = false;
uint64_t efd_counter;
read(c->efd, &efd_counter, sizeof(uint64_t)); // resets counter to zero
pthread_cond_signal(&c->cvar_writable);
pthread_mutex_unlock(&c->mutex);
return item;
}
void *hgl_chan_try_recv(HglChan *c)
{
pthread_mutex_lock(&c->mutex);
if (!c->readable) {
pthread_mutex_unlock(&c->mutex);
return NULL;
}
void *item = c->item;
c->readable = false;
uint64_t efd_counter;
read(c->efd, &efd_counter, sizeof(uint64_t)); // resets counter to zero
pthread_cond_signal(&c->cvar_writable);
pthread_mutex_unlock(&c->mutex);
return item;
}
HglChan *hgl_chan_select(int n_args, ...)
{
HglChan *ret = NULL;
static struct pollfd pfds[128]; // 128 should be more than enough
if (n_args > 128) {
fprintf(stderr, "[hgl_chan.h] Error: too many (> 128) channels passed to `hgl_chan_select`.\n");
return NULL;
}
/* setup va_list */
va_list args1, args2;
va_start(args1, n_args);
va_copy(args2, args1);
/* populate pfds */
for (int i = 0; i < n_args; i++) {
HglChan *c = va_arg(args1, HglChan *);
pfds[i].fd = c->efd;
pfds[i].events = POLLIN;
}
/* poll: wait (forever) till one of the channels becomes readable */
poll(pfds, (nfds_t) n_args, -1);
/* find the first readable channel */
for (int i = 0; i < n_args; i++) {
HglChan *c = va_arg(args2, HglChan *);
if (c->readable) {
ret = c;
break;
}
}
va_end(args1);
va_end(args2);
return ret;
}
HglChan *hgl_chan_try_select(int n_args, ...)
{
HglChan *ret = NULL;
/* setup va_list */
va_list args;
va_start(args, n_args);
for (int i = 0; i < n_args; i++) {
HglChan *c = va_arg(args, HglChan *);
if (c->readable) {
ret = c;
break;
}
}
va_end(args);
return ret;
}
#endif