-
Notifications
You must be signed in to change notification settings - Fork 2
/
carrow_generic.c
114 lines (90 loc) · 2.76 KB
/
carrow_generic.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
// 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" // NOLINT
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
CARROW_NAME(coro)
CARROW_NAME(coro_create) (CARROW_NAME(corofunc) f) {
return (CARROW_NAME(coro)){f, 0, -1, -1}; // NOLINT
}
CARROW_NAME(coro)
CARROW_NAME(coro_stop) () {
return (CARROW_NAME(coro)){NULL, -1, -1, -1}; // NOLINT
}
void
CARROW_NAME(coro_run) (CARROW_NAME(coro) *self, CARROW_ENTITY *state) {
if (self->run == NULL) {
return;
}
run:
self->run(self, state);
if (self->run == NULL) {
return;
}
if (self->fd == -1) {
goto run;
}
if (CARROW_NAME(evloop_modify_or_register)(self, state, self->fd,
self->events)) {
self->events = 0;
goto run;
}
}
void
CARROW_NAME(coro_create_and_run) (CARROW_NAME(corofunc) f,
CARROW_ENTITY *state) {
CARROW_NAME(coro) c = CARROW_NAME(coro_create)(f);
CARROW_NAME(coro_run)(&c, state);
}
int
CARROW_NAME(evloop_register) (CARROW_NAME(coro) *c, CARROW_ENTITY *s, int fd,
int events) {
return carrow_evloop_register(c, s, fd, events,
(carrow_generic_corofunc)CARROW_NAME(coro_run));
}
int
CARROW_NAME(evloop_modify) (CARROW_NAME(coro) *c, CARROW_ENTITY *s, int fd,
int events) {
return carrow_evloop_modify(c, s, fd, events,
(carrow_generic_corofunc)CARROW_NAME(coro_run));
}
int
CARROW_NAME(evloop_modify_or_register) (CARROW_NAME(coro) *c,
CARROW_ENTITY *s, int fd, int events) {
return carrow_evloop_modify_or_register(c, s, fd, events,
(carrow_generic_corofunc)CARROW_NAME(coro_run));
}
int
CARROW_NAME(evloop_unregister) (int fd) {
return carrow_evloop_unregister(fd);
}
int
CARROW_NAME(forever) (CARROW_NAME(corofunc) f, CARROW_ENTITY *state,
volatile int *status) {
int ret = EXIT_SUCCESS;
carrow_init(0);
CARROW_NAME(coro_create_and_run) (f, state);
if (carrow_evloop(status)) {
ret = EXIT_FAILURE;
}
carrow_deinit();
return ret;
}