]> git.meshlink.io Git - meshlink/blob - src/event.c
Remove global variables from the event loop code.
[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 struct timeval now;
31
32 static int io_compare(const io_t *a, const io_t *b) {
33         return a->fd - b->fd;
34 }
35
36 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
37         struct timeval diff;
38         timersub(&a->tv, &b->tv, &diff);
39         if(diff.tv_sec < 0)
40                 return -1;
41         if(diff.tv_sec > 0)
42                 return 1;
43         if(diff.tv_usec < 0)
44                 return -1;
45         if(diff.tv_usec > 0)
46                 return 1;
47         if(a < b)
48                 return -1;
49         if(a > b)
50                 return 1;
51         return 0;
52 }
53
54 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
55         if(io->cb)
56                 return;
57
58         io->fd = fd;
59         io->cb = cb;
60         io->data = data;
61         io->node.data = io;
62
63         io_set(loop, io, flags);
64
65         if(!splay_insert_node(&loop->ios, &io->node))
66                 abort();
67 }
68
69 void io_set(event_loop_t *loop, io_t *io, int flags) {
70         io->flags = flags;
71
72         if(flags & IO_READ)
73                 FD_SET(io->fd, &loop->readfds);
74         else
75                 FD_CLR(io->fd, &loop->readfds);
76
77         if(flags & IO_WRITE)
78                 FD_SET(io->fd, &loop->writefds);
79         else
80                 FD_CLR(io->fd, &loop->writefds);
81 }
82
83 void io_del(event_loop_t *loop, io_t *io) {
84         if(!io->cb)
85                 return;
86
87         io_set(loop, io, 0);
88
89         splay_unlink_node(&loop->ios, &io->node);
90         io->cb = NULL;
91 }
92
93 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
94         timeout->cb = cb;
95         timeout->data = data;
96         timeout->node.data = timeout;
97
98         timeout_set(loop, timeout, tv);
99 }
100
101 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
102         if(timerisset(&timeout->tv))
103                 splay_unlink_node(&loop->timeouts, &timeout->node);
104
105         if(!loop->now.tv_sec)
106                 gettimeofday(&loop->now, NULL);
107
108         timeradd(&loop->now, tv, &timeout->tv);
109
110         if(!splay_insert_node(&loop->timeouts, &timeout->node))
111                 abort();
112 }
113
114 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
115         if(!timeout->cb)
116                 return;
117
118         splay_unlink_node(&loop->timeouts, &timeout->node);
119         timeout->cb = 0;
120         timeout->tv = (struct timeval){0, 0};
121 }
122
123 static int signal_compare(const signal_t *a, const signal_t *b) {
124         return (int)a->signum - (int)b->signum;
125 }
126
127 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
128         unsigned char signum;
129         if(read(loop->pipefd[0], &signum, 1) != 1)
130                 return;
131
132         signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
133         if(sig)
134                 sig->cb(loop, sig->data);
135 }
136
137 static void pipe_init(event_loop_t *loop) {
138         if(!pipe(loop->pipefd))
139                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
140 }
141
142 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
143         if(sig->cb)
144                 return;
145
146         sig->cb = cb;
147         sig->data = data;
148         sig->signum = signum;
149         sig->node.data = sig;
150
151         if(loop->pipefd[0] == -1)
152                 pipe_init(loop);
153
154         if(!splay_insert_node(&loop->signals, &sig->node))
155                 abort();
156 }
157
158 void signal_del(event_loop_t *loop, signal_t *sig) {
159         if(!sig->cb)
160                 return;
161
162         splay_unlink_node(&loop->signals, &sig->node);
163         sig->cb = NULL;
164 }
165
166 bool event_loop_run(event_loop_t *loop) {
167         loop->running = true;
168
169         fd_set readable;
170         fd_set writable;
171
172         while(loop->running) {
173                 gettimeofday(&loop->now, NULL);
174                 now = loop->now;
175                 struct timeval diff, *tv = NULL;
176
177                 while(loop->timeouts.head) {
178                         timeout_t *timeout = loop->timeouts.head->data;
179                         timersub(&timeout->tv, &loop->now, &diff);
180
181                         if(diff.tv_sec < 0) {
182                                 timeout->cb(loop, timeout->data);
183                                 if(timercmp(&timeout->tv, &loop->now, <))
184                                         timeout_del(loop, timeout);
185                         } else {
186                                 tv = &diff;
187                                 break;
188                         }
189                 }
190
191                 memcpy(&readable, &loop->readfds, sizeof readable);
192                 memcpy(&writable, &loop->writefds, sizeof writable);
193
194                 int fds = 0;
195
196                 if(loop->ios.tail) {
197                         io_t *last = loop->ios.tail->data;
198                         fds = last->fd + 1;
199                 }
200
201                 int n = select(fds, &readable, &writable, NULL, tv);
202
203                 if(n < 0) {
204                         if(sockwouldblock(errno))
205                                 continue;
206                         else
207                                 return false;
208                 }
209
210                 if(!n)
211                         continue;
212
213                 for splay_each(io_t, io, &loop->ios) {
214                         if(FD_ISSET(io->fd, &writable))
215                                 io->cb(loop, io->data, IO_WRITE);
216                         else if(FD_ISSET(io->fd, &readable))
217                                 io->cb(loop, io->data, IO_READ);
218                 }
219         }
220
221         return true;
222 }
223
224 void event_flush_output(event_loop_t *loop) {
225         for splay_each(io_t, io, &loop->ios)
226                 if(FD_ISSET(io->fd, &loop->writefds))
227                         io->cb(loop, io->data, IO_WRITE);
228 }
229
230 void event_loop_stop(event_loop_t *loop) {
231         loop->running = false;
232 }
233
234 void event_loop_init(event_loop_t *loop) {
235         loop->ios.compare = (splay_compare_t)io_compare;
236         loop->timeouts.compare = (splay_compare_t)timeout_compare;
237         loop->signals.compare = (splay_compare_t)signal_compare;
238         loop->pipefd[0] = -1;
239         loop->pipefd[1] = -1;
240 }
241
242 void event_loop_exit(event_loop_t *loop) {
243         for splay_each(io_t, io, &loop->ios)
244                 splay_free_node(&loop->ios, node);
245         for splay_each(timeout_t, timeout, &loop->timeouts)
246                 splay_free_node(&loop->timeouts, node);
247         for splay_each(signal_t, signal, &loop->signals)
248                 splay_free_node(&loop->signals, node);
249 }