-
Notifications
You must be signed in to change notification settings - Fork 2
/
carrow.c
354 lines (280 loc) · 6.93 KB
/
carrow.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2023 Vahid Mardani
/*
* This file is part of Carrow.
* Carrow is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Carrow is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with Carrow. If not, see <https://www.gnu.org/licenses/>.
*
* Author: Vahid Mardani <vahid.mardani@gmail.com>
*/
#include "carrow.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
static volatile int _carrow_status = 99999;
static volatile int *_carrow_status_ptr = NULL;
static struct sigaction old_action;
static unsigned int _openmax;
static struct evbag **_evbags;
static volatile unsigned int _evbagscount;
static int _epollfd = -1;
struct evbag {
struct carrow_generic_coro coro;
void *state;
carrow_generic_corofunc handler;
};
#define EVBAG_HAS(fd) (_evbags[fd] != NULL)
#define EVBAG_ISNULL(fd) (_evbags[fd] == NULL)
static void
_sighandler(int s) {
printf("\n");
_carrow_status = EXIT_SUCCESS;
}
static int
evbag_set(int fd, struct carrow_generic_coro *coro, void *state,
carrow_generic_corofunc handler) {
if (fd >= _openmax) {
return -1;
}
struct evbag *bag = _evbags[fd];
if (bag == NULL) {
bag = malloc(sizeof(struct evbag));
if (bag == NULL) {
errno = ENOMEM;
return -1;
}
_evbags[fd] = bag;
_evbagscount++;
}
bag->coro = *coro;
bag->state = state;
bag->handler = handler;
return 0;
}
static int
evbag_delete(int fd) {
if (fd >= _openmax) {
return -1;
}
struct evbag *bag = _evbags[fd];
if (bag == NULL) {
return -1;
}
free(bag);
_evbagscount--;
_evbags[fd] = NULL;
return 0;
}
int
carrow_evbag_unpack(int fd, struct carrow_generic_coro *coro, void **state,
carrow_generic_corofunc *handler) {
if (fd >= _openmax) {
return -1;
}
struct evbag *bag = _evbags[fd];
if (bag == NULL) {
return -1;
}
if (coro != NULL) {
memcpy(coro, &bag->coro, sizeof(struct carrow_generic_coro));
}
if (state != NULL) {
*state = bag->state;
}
if (handler != NULL) {
*handler = bag->handler;
}
return 0;
}
int
carrow_trigger(int fd, int events) {
if (fd >= _openmax) {
return -1;
}
struct evbag *bag = _evbags[fd];
struct carrow_generic_coro c;
if (bag == NULL) {
/* Event already removed */
return -1;
}
memcpy(&c, &bag->coro, sizeof(struct carrow_generic_coro));
c.fd = fd;
c.events = events;
bag->handler(&c, bag->state);
return 0;
}
static int
evbags_init() {
_evbagscount = 0;
_evbags = calloc(_openmax, sizeof(struct evbag*));
if (_evbags == NULL) {
errno = ENOMEM;
return -1;
}
memset(_evbags, 0, sizeof(struct evbag*) * _openmax);
return 0;
}
static void
evbags_deinit() {
free(_evbags);
_evbags = NULL;
}
int
carrow_init(unsigned int openmax) {
if (_epollfd != -1) {
return -1;
}
/* Find maximum allowed openfiles */
if (openmax != 0) {
_openmax = openmax;
}
else {
_openmax = sysconf(_SC_OPEN_MAX);
if (_openmax == -1) {
errno = EINVAL;
return -1;
}
}
if (evbags_init()) {
return -1;
}
_epollfd = epoll_create1(0);
if (_epollfd < 0) {
return -1;
}
return 0;
}
int
carrow_evloop_register(void *coro, void *state, int fd, int events,
carrow_generic_corofunc handler) {
struct epoll_event ee;
if (EVBAG_HAS(fd)) {
return -1;
}
evbag_set(fd, (struct carrow_generic_coro*)coro, state, handler);
ee.events = events;
ee.data.fd = fd;
if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, fd, &ee)) {
evbag_delete(fd);
return -1;
}
return 0;
}
int
carrow_evloop_modify(void *coro, void *state, int fd, int events,
carrow_generic_corofunc handler) {
struct epoll_event ee;
if (EVBAG_ISNULL(fd)) {
return -1;
}
if (evbag_set(fd, coro, state, handler)) {
return -1;
}
ee.events = events;
ee.data.fd = fd;
if (epoll_ctl(_epollfd, EPOLL_CTL_MOD, fd, &ee)) {
evbag_delete(fd);
return -1;
}
return 0;
}
int
carrow_evloop_modify_or_register(void *coro, void *state, int fd, int events,
carrow_generic_corofunc handler) {
if (EVBAG_ISNULL(fd)) {
return carrow_evloop_register(coro, state, fd, events, handler);
}
return carrow_evloop_modify(coro, state, fd, events, handler);
}
int
carrow_evloop_unregister(int fd) {
epoll_ctl(_epollfd, EPOLL_CTL_DEL, fd, NULL);
return evbag_delete(fd);
}
int
carrow_sleep(struct carrow_generic_coro *self, unsigned int seconds,
int line) {
int fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
if (fd == -1) {
return -1;
}
struct timespec sec = {seconds, 0};
struct timespec zero = {0, 0};
struct itimerspec spec = {zero, sec};
if (timerfd_settime(fd, 0, &spec, NULL) == -1) {
close(fd);
return -1;
}
self->fd = fd;
self->events = CIN | CONCE;
self->line = line;
return 0;
}
int
carrow_handleinterrupts() {
struct sigaction new_action = {_sighandler, 0, 0, 0, 0};
if (sigaction(SIGINT, &new_action, &old_action) != 0) {
return -1;
}
_carrow_status_ptr = &_carrow_status;
return 0;
}
int
carrow_evloop(volatile int *status) {
int i;
int fd;
int nfds;
int ret = 0;
struct epoll_event ee;
struct epoll_event events[_openmax];
if ((status == NULL) && (_carrow_status_ptr != NULL)) {
status = _carrow_status_ptr;
}
evloop:
while (_evbagscount && ((status == NULL) || (*status > EXIT_FAILURE))) {
errno = 0;
nfds = epoll_wait(_epollfd, events, _openmax, -1);
if (nfds < 0) {
ret = -1;
break;
}
if (nfds == 0) {
ret = 0;
break;
}
for (i = 0; i < nfds; i++) {
ee = events[i];
fd = ee.data.fd;
carrow_trigger(fd, ee.events);
}
}
terminate:
for (fd = 0; fd < _openmax; fd++) {
carrow_trigger(fd, 0);
}
if (_evbagscount && ((status == NULL) || (*status > EXIT_FAILURE))) {
goto evloop;
}
return ret;
}
void
carrow_deinit() {
close(_epollfd);
_epollfd = -1;
evbags_deinit();
errno = 0;
}