]> git.meshlink.io Git - meshlink/blob - src/event.c
Remove global variable "now".
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "dropin.h"
23 #include "event.h"
24 #include "net.h"
25 #include "splay_tree.h"
26 #include "utils.h"
27 #include "xalloc.h"
28
29 event_loop_t *loop;
30
31 static int io_compare(const io_t *a, const io_t *b) {
32         return a->fd - b->fd;
33 }
34
35 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
36         struct timeval diff;
37         timersub(&a->tv, &b->tv, &diff);
38         if(diff.tv_sec < 0)
39                 return -1;
40         if(diff.tv_sec > 0)
41                 return 1;
42         if(diff.tv_usec < 0)
43                 return -1;
44         if(diff.tv_usec > 0)
45                 return 1;
46         if(a < b)
47                 return -1;
48         if(a > b)
49                 return 1;
50         return 0;
51 }
52
53 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
54         if(io->cb)
55                 return;
56
57         io->fd = fd;
58         io->cb = cb;
59         io->data = data;
60         io->node.data = io;
61
62         io_set(loop, io, flags);
63
64         if(!splay_insert_node(&loop->ios, &io->node))
65                 abort();
66 }
67
68 void io_set(event_loop_t *loop, io_t *io, int flags) {
69         io->flags = flags;
70
71         if(flags & IO_READ)
72                 FD_SET(io->fd, &loop->readfds);
73         else
74                 FD_CLR(io->fd, &loop->readfds);
75
76         if(flags & IO_WRITE)
77                 FD_SET(io->fd, &loop->writefds);
78         else
79                 FD_CLR(io->fd, &loop->writefds);
80 }
81
82 void io_del(event_loop_t *loop, io_t *io) {
83         if(!io->cb)
84                 return;
85
86         io_set(loop, io, 0);
87
88         splay_unlink_node(&loop->ios, &io->node);
89         io->cb = NULL;
90 }
91
92 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
93         timeout->cb = cb;
94         timeout->data = data;
95         timeout->node.data = timeout;
96
97         timeout_set(loop, timeout, tv);
98 }
99
100 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
101         if(timerisset(&timeout->tv))
102                 splay_unlink_node(&loop->timeouts, &timeout->node);
103
104         if(!loop->now.tv_sec)
105                 gettimeofday(&loop->now, NULL);
106
107         timeradd(&loop->now, tv, &timeout->tv);
108
109         if(!splay_insert_node(&loop->timeouts, &timeout->node))
110                 abort();
111 }
112
113 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
114         if(!timeout->cb)
115                 return;
116
117         splay_unlink_node(&loop->timeouts, &timeout->node);
118         timeout->cb = 0;
119         timeout->tv = (struct timeval){0, 0};
120 }
121
122 static int signal_compare(const signal_t *a, const signal_t *b) {
123         return (int)a->signum - (int)b->signum;
124 }
125
126 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
127         unsigned char signum;
128         if(read(loop->pipefd[0], &signum, 1) != 1)
129                 return;
130
131         signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
132         if(sig)
133                 sig->cb(loop, sig->data);
134 }
135
136 static void pipe_init(event_loop_t *loop) {
137         if(!pipe(loop->pipefd))
138                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
139 }
140
141 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
142         if(sig->cb)
143                 return;
144
145         sig->cb = cb;
146         sig->data = data;
147         sig->signum = signum;
148         sig->node.data = sig;
149
150         if(loop->pipefd[0] == -1)
151                 pipe_init(loop);
152
153         if(!splay_insert_node(&loop->signals, &sig->node))
154                 abort();
155 }
156
157 void signal_del(event_loop_t *loop, signal_t *sig) {
158         if(!sig->cb)
159                 return;
160
161         splay_unlink_node(&loop->signals, &sig->node);
162         sig->cb = NULL;
163 }
164
165 bool event_loop_run(event_loop_t *loop) {
166         loop->running = true;
167
168         fd_set readable;
169         fd_set writable;
170
171         while(loop->running) {
172                 gettimeofday(&loop->now, NULL);
173                 struct timeval diff, *tv = NULL;
174
175                 while(loop->timeouts.head) {
176                         timeout_t *timeout = loop->timeouts.head->data;
177                         timersub(&timeout->tv, &loop->now, &diff);
178
179                         if(diff.tv_sec < 0) {
180                                 timeout->cb(loop, timeout->data);
181                                 if(timercmp(&timeout->tv, &loop->now, <))
182                                         timeout_del(loop, timeout);
183                         } else {
184                                 tv = &diff;
185                                 break;
186                         }
187                 }
188
189                 memcpy(&readable, &loop->readfds, sizeof readable);
190                 memcpy(&writable, &loop->writefds, sizeof writable);
191
192                 int fds = 0;
193
194                 if(loop->ios.tail) {
195                         io_t *last = loop->ios.tail->data;
196                         fds = last->fd + 1;
197                 }
198
199                 int n = select(fds, &readable, &writable, NULL, tv);
200
201                 if(n < 0) {
202                         if(sockwouldblock(errno))
203                                 continue;
204                         else
205                                 return false;
206                 }
207
208                 if(!n)
209                         continue;
210
211                 for splay_each(io_t, io, &loop->ios) {
212                         if(FD_ISSET(io->fd, &writable))
213                                 io->cb(loop, io->data, IO_WRITE);
214                         else if(FD_ISSET(io->fd, &readable))
215                                 io->cb(loop, io->data, IO_READ);
216                 }
217         }
218
219         return true;
220 }
221
222 void event_flush_output(event_loop_t *loop) {
223         for splay_each(io_t, io, &loop->ios)
224                 if(FD_ISSET(io->fd, &loop->writefds))
225                         io->cb(loop, io->data, IO_WRITE);
226 }
227
228 void event_loop_stop(event_loop_t *loop) {
229         loop->running = false;
230 }
231
232 void event_loop_init(event_loop_t *loop) {
233         loop->ios.compare = (splay_compare_t)io_compare;
234         loop->timeouts.compare = (splay_compare_t)timeout_compare;
235         loop->signals.compare = (splay_compare_t)signal_compare;
236         loop->pipefd[0] = -1;
237         loop->pipefd[1] = -1;
238         gettimeofday(&loop->now, NULL);
239 }
240
241 void event_loop_exit(event_loop_t *loop) {
242         for splay_each(io_t, io, &loop->ios)
243                 splay_free_node(&loop->ios, node);
244         for splay_each(timeout_t, timeout, &loop->timeouts)
245                 splay_free_node(&loop->timeouts, node);
246         for splay_each(signal_t, signal, &loop->signals)
247                 splay_free_node(&loop->signals, node);
248 }