1 |
root |
1.17 |
/* |
2 |
|
|
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 |
|
|
* All rights reserved. |
4 |
|
|
* |
5 |
|
|
* Redistribution and use in source and binary forms, with or without |
6 |
|
|
* modification, are permitted provided that the following conditions are |
7 |
|
|
* met: |
8 |
|
|
* |
9 |
|
|
* * Redistributions of source code must retain the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
|
|
* * Redistributions in binary form must reproduce the above |
13 |
|
|
* copyright notice, this list of conditions and the following |
14 |
|
|
* disclaimer in the documentation and/or other materials provided |
15 |
|
|
* with the distribution. |
16 |
|
|
* |
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
21 |
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
22 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
23 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
|
|
*/ |
29 |
|
|
|
30 |
root |
1.1 |
#include <math.h> |
31 |
|
|
#include <stdlib.h> |
32 |
root |
1.7 |
#include <unistd.h> |
33 |
|
|
#include <fcntl.h> |
34 |
|
|
#include <signal.h> |
35 |
root |
1.16 |
#include <stddef.h> |
36 |
root |
1.1 |
|
37 |
|
|
#include <stdio.h> |
38 |
|
|
|
39 |
root |
1.4 |
#include <assert.h> |
40 |
root |
1.1 |
#include <errno.h> |
41 |
|
|
#include <sys/time.h> |
42 |
|
|
#include <time.h> |
43 |
|
|
|
44 |
root |
1.10 |
#ifndef HAVE_MONOTONIC |
45 |
|
|
# ifdef CLOCK_MONOTONIC |
46 |
|
|
# define HAVE_MONOTONIC 1 |
47 |
|
|
# endif |
48 |
root |
1.1 |
#endif |
49 |
|
|
|
50 |
root |
1.10 |
#ifndef HAVE_SELECT |
51 |
|
|
# define HAVE_SELECT 1 |
52 |
|
|
#endif |
53 |
|
|
|
54 |
|
|
#ifndef HAVE_EPOLL |
55 |
|
|
# define HAVE_EPOLL 0 |
56 |
|
|
#endif |
57 |
|
|
|
58 |
|
|
#ifndef HAVE_REALTIME |
59 |
|
|
# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
60 |
|
|
#endif |
61 |
root |
1.1 |
|
62 |
root |
1.4 |
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 |
root |
1.1 |
#define MAX_BLOCKTIME 60. |
64 |
|
|
|
65 |
|
|
#include "ev.h" |
66 |
|
|
|
67 |
root |
1.10 |
typedef struct ev_watcher *W; |
68 |
|
|
typedef struct ev_watcher_list *WL; |
69 |
root |
1.12 |
typedef struct ev_watcher_time *WT; |
70 |
root |
1.10 |
|
71 |
root |
1.4 |
static ev_tstamp now, diff; /* monotonic clock */ |
72 |
root |
1.1 |
ev_tstamp ev_now; |
73 |
|
|
int ev_method; |
74 |
|
|
|
75 |
|
|
static int have_monotonic; /* runtime */ |
76 |
|
|
|
77 |
|
|
static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
78 |
root |
1.5 |
static void (*method_modify)(int fd, int oev, int nev); |
79 |
root |
1.1 |
static void (*method_poll)(ev_tstamp timeout); |
80 |
|
|
|
81 |
root |
1.8 |
/*****************************************************************************/ |
82 |
|
|
|
83 |
root |
1.1 |
ev_tstamp |
84 |
|
|
ev_time (void) |
85 |
|
|
{ |
86 |
|
|
#if HAVE_REALTIME |
87 |
|
|
struct timespec ts; |
88 |
|
|
clock_gettime (CLOCK_REALTIME, &ts); |
89 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
90 |
|
|
#else |
91 |
|
|
struct timeval tv; |
92 |
|
|
gettimeofday (&tv, 0); |
93 |
|
|
return tv.tv_sec + tv.tv_usec * 1e-6; |
94 |
|
|
#endif |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
static ev_tstamp |
98 |
|
|
get_clock (void) |
99 |
|
|
{ |
100 |
|
|
#if HAVE_MONOTONIC |
101 |
|
|
if (have_monotonic) |
102 |
|
|
{ |
103 |
|
|
struct timespec ts; |
104 |
|
|
clock_gettime (CLOCK_MONOTONIC, &ts); |
105 |
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9; |
106 |
|
|
} |
107 |
|
|
#endif |
108 |
|
|
|
109 |
|
|
return ev_time (); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
#define array_needsize(base,cur,cnt,init) \ |
113 |
|
|
if ((cnt) > cur) \ |
114 |
|
|
{ \ |
115 |
root |
1.2 |
int newcnt = cur ? cur << 1 : 16; \ |
116 |
root |
1.1 |
base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 |
|
|
init (base + cur, newcnt - cur); \ |
118 |
|
|
cur = newcnt; \ |
119 |
|
|
} |
120 |
|
|
|
121 |
root |
1.8 |
/*****************************************************************************/ |
122 |
|
|
|
123 |
root |
1.1 |
typedef struct |
124 |
|
|
{ |
125 |
|
|
struct ev_io *head; |
126 |
|
|
unsigned char wev, rev; /* want, received event set */ |
127 |
|
|
} ANFD; |
128 |
|
|
|
129 |
|
|
static ANFD *anfds; |
130 |
|
|
static int anfdmax; |
131 |
|
|
|
132 |
|
|
static int *fdchanges; |
133 |
|
|
static int fdchangemax, fdchangecnt; |
134 |
|
|
|
135 |
|
|
static void |
136 |
|
|
anfds_init (ANFD *base, int count) |
137 |
|
|
{ |
138 |
|
|
while (count--) |
139 |
|
|
{ |
140 |
|
|
base->head = 0; |
141 |
|
|
base->wev = base->rev = EV_NONE; |
142 |
|
|
++base; |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
typedef struct |
147 |
|
|
{ |
148 |
root |
1.10 |
W w; |
149 |
root |
1.1 |
int events; |
150 |
|
|
} ANPENDING; |
151 |
|
|
|
152 |
|
|
static ANPENDING *pendings; |
153 |
|
|
static int pendingmax, pendingcnt; |
154 |
|
|
|
155 |
|
|
static void |
156 |
root |
1.10 |
event (W w, int events) |
157 |
root |
1.1 |
{ |
158 |
root |
1.16 |
if (w->active) |
159 |
|
|
{ |
160 |
|
|
w->pending = ++pendingcnt; |
161 |
|
|
array_needsize (pendings, pendingmax, pendingcnt, ); |
162 |
|
|
pendings [pendingcnt - 1].w = w; |
163 |
|
|
pendings [pendingcnt - 1].events = events; |
164 |
|
|
} |
165 |
root |
1.1 |
} |
166 |
|
|
|
167 |
|
|
static void |
168 |
|
|
fd_event (int fd, int events) |
169 |
|
|
{ |
170 |
|
|
ANFD *anfd = anfds + fd; |
171 |
|
|
struct ev_io *w; |
172 |
|
|
|
173 |
|
|
for (w = anfd->head; w; w = w->next) |
174 |
|
|
{ |
175 |
|
|
int ev = w->events & events; |
176 |
|
|
|
177 |
|
|
if (ev) |
178 |
root |
1.10 |
event ((W)w, ev); |
179 |
root |
1.1 |
} |
180 |
|
|
} |
181 |
|
|
|
182 |
root |
1.9 |
static void |
183 |
root |
1.10 |
queue_events (W *events, int eventcnt, int type) |
184 |
root |
1.9 |
{ |
185 |
|
|
int i; |
186 |
|
|
|
187 |
|
|
for (i = 0; i < eventcnt; ++i) |
188 |
|
|
event (events [i], type); |
189 |
|
|
} |
190 |
|
|
|
191 |
root |
1.19 |
/* called on EBADF to verify fds */ |
192 |
|
|
static void |
193 |
|
|
fd_recheck () |
194 |
|
|
{ |
195 |
|
|
int fd; |
196 |
|
|
|
197 |
|
|
for (fd = 0; fd < anfdmax; ++fd) |
198 |
|
|
if (anfds [fd].wev) |
199 |
|
|
if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 |
|
|
while (anfds [fd].head) |
201 |
|
|
evio_stop (anfds [fd].head); |
202 |
|
|
} |
203 |
|
|
|
204 |
root |
1.8 |
/*****************************************************************************/ |
205 |
|
|
|
206 |
root |
1.12 |
static struct ev_timer **timers; |
207 |
|
|
static int timermax, timercnt; |
208 |
root |
1.4 |
|
209 |
root |
1.12 |
static struct ev_periodic **periodics; |
210 |
|
|
static int periodicmax, periodiccnt; |
211 |
root |
1.1 |
|
212 |
|
|
static void |
213 |
root |
1.12 |
upheap (WT *timers, int k) |
214 |
root |
1.1 |
{ |
215 |
root |
1.12 |
WT w = timers [k]; |
216 |
root |
1.1 |
|
217 |
|
|
while (k && timers [k >> 1]->at > w->at) |
218 |
|
|
{ |
219 |
|
|
timers [k] = timers [k >> 1]; |
220 |
|
|
timers [k]->active = k + 1; |
221 |
|
|
k >>= 1; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
timers [k] = w; |
225 |
|
|
timers [k]->active = k + 1; |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
static void |
230 |
root |
1.12 |
downheap (WT *timers, int N, int k) |
231 |
root |
1.1 |
{ |
232 |
root |
1.12 |
WT w = timers [k]; |
233 |
root |
1.1 |
|
234 |
root |
1.4 |
while (k < (N >> 1)) |
235 |
root |
1.1 |
{ |
236 |
|
|
int j = k << 1; |
237 |
|
|
|
238 |
root |
1.4 |
if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
239 |
root |
1.1 |
++j; |
240 |
|
|
|
241 |
|
|
if (w->at <= timers [j]->at) |
242 |
|
|
break; |
243 |
|
|
|
244 |
|
|
timers [k] = timers [j]; |
245 |
root |
1.2 |
timers [k]->active = k + 1; |
246 |
root |
1.1 |
k = j; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
timers [k] = w; |
250 |
|
|
timers [k]->active = k + 1; |
251 |
|
|
} |
252 |
|
|
|
253 |
root |
1.8 |
/*****************************************************************************/ |
254 |
|
|
|
255 |
root |
1.7 |
typedef struct |
256 |
|
|
{ |
257 |
|
|
struct ev_signal *head; |
258 |
|
|
sig_atomic_t gotsig; |
259 |
|
|
} ANSIG; |
260 |
|
|
|
261 |
|
|
static ANSIG *signals; |
262 |
root |
1.4 |
static int signalmax; |
263 |
root |
1.1 |
|
264 |
root |
1.7 |
static int sigpipe [2]; |
265 |
|
|
static sig_atomic_t gotsig; |
266 |
|
|
static struct ev_io sigev; |
267 |
|
|
|
268 |
root |
1.1 |
static void |
269 |
root |
1.7 |
signals_init (ANSIG *base, int count) |
270 |
root |
1.1 |
{ |
271 |
|
|
while (count--) |
272 |
root |
1.7 |
{ |
273 |
|
|
base->head = 0; |
274 |
|
|
base->gotsig = 0; |
275 |
|
|
++base; |
276 |
|
|
} |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
static void |
280 |
|
|
sighandler (int signum) |
281 |
|
|
{ |
282 |
|
|
signals [signum - 1].gotsig = 1; |
283 |
|
|
|
284 |
|
|
if (!gotsig) |
285 |
|
|
{ |
286 |
|
|
gotsig = 1; |
287 |
|
|
write (sigpipe [1], &gotsig, 1); |
288 |
|
|
} |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
static void |
292 |
|
|
sigcb (struct ev_io *iow, int revents) |
293 |
|
|
{ |
294 |
|
|
struct ev_signal *w; |
295 |
|
|
int sig; |
296 |
|
|
|
297 |
|
|
gotsig = 0; |
298 |
|
|
read (sigpipe [0], &revents, 1); |
299 |
|
|
|
300 |
|
|
for (sig = signalmax; sig--; ) |
301 |
|
|
if (signals [sig].gotsig) |
302 |
|
|
{ |
303 |
|
|
signals [sig].gotsig = 0; |
304 |
|
|
|
305 |
|
|
for (w = signals [sig].head; w; w = w->next) |
306 |
root |
1.10 |
event ((W)w, EV_SIGNAL); |
307 |
root |
1.7 |
} |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
static void |
311 |
|
|
siginit (void) |
312 |
|
|
{ |
313 |
|
|
fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
314 |
|
|
fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
315 |
|
|
|
316 |
|
|
/* rather than sort out wether we really need nb, set it */ |
317 |
|
|
fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
318 |
|
|
fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
319 |
|
|
|
320 |
|
|
evio_set (&sigev, sigpipe [0], EV_READ); |
321 |
|
|
evio_start (&sigev); |
322 |
root |
1.1 |
} |
323 |
|
|
|
324 |
root |
1.8 |
/*****************************************************************************/ |
325 |
|
|
|
326 |
root |
1.9 |
static struct ev_idle **idles; |
327 |
|
|
static int idlemax, idlecnt; |
328 |
|
|
|
329 |
root |
1.20 |
static struct ev_prepare **prepares; |
330 |
|
|
static int preparemax, preparecnt; |
331 |
|
|
|
332 |
root |
1.9 |
static struct ev_check **checks; |
333 |
|
|
static int checkmax, checkcnt; |
334 |
|
|
|
335 |
|
|
/*****************************************************************************/ |
336 |
|
|
|
337 |
root |
1.1 |
#if HAVE_EPOLL |
338 |
|
|
# include "ev_epoll.c" |
339 |
|
|
#endif |
340 |
|
|
#if HAVE_SELECT |
341 |
|
|
# include "ev_select.c" |
342 |
|
|
#endif |
343 |
|
|
|
344 |
|
|
int ev_init (int flags) |
345 |
|
|
{ |
346 |
|
|
#if HAVE_MONOTONIC |
347 |
|
|
{ |
348 |
|
|
struct timespec ts; |
349 |
|
|
if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
350 |
|
|
have_monotonic = 1; |
351 |
|
|
} |
352 |
|
|
#endif |
353 |
|
|
|
354 |
|
|
ev_now = ev_time (); |
355 |
root |
1.4 |
now = get_clock (); |
356 |
|
|
diff = ev_now - now; |
357 |
root |
1.1 |
|
358 |
root |
1.7 |
if (pipe (sigpipe)) |
359 |
|
|
return 0; |
360 |
|
|
|
361 |
|
|
ev_method = EVMETHOD_NONE; |
362 |
root |
1.1 |
#if HAVE_EPOLL |
363 |
root |
1.7 |
if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
364 |
root |
1.1 |
#endif |
365 |
|
|
#if HAVE_SELECT |
366 |
root |
1.7 |
if (ev_method == EVMETHOD_NONE) select_init (flags); |
367 |
root |
1.1 |
#endif |
368 |
|
|
|
369 |
root |
1.7 |
if (ev_method) |
370 |
|
|
{ |
371 |
root |
1.12 |
evw_init (&sigev, sigcb); |
372 |
root |
1.7 |
siginit (); |
373 |
|
|
} |
374 |
|
|
|
375 |
root |
1.1 |
return ev_method; |
376 |
|
|
} |
377 |
|
|
|
378 |
root |
1.8 |
/*****************************************************************************/ |
379 |
|
|
|
380 |
root |
1.1 |
void ev_prefork (void) |
381 |
|
|
{ |
382 |
root |
1.11 |
/* nop */ |
383 |
root |
1.1 |
} |
384 |
|
|
|
385 |
|
|
void ev_postfork_parent (void) |
386 |
|
|
{ |
387 |
root |
1.11 |
/* nop */ |
388 |
root |
1.1 |
} |
389 |
|
|
|
390 |
|
|
void ev_postfork_child (void) |
391 |
|
|
{ |
392 |
|
|
#if HAVE_EPOLL |
393 |
root |
1.5 |
if (ev_method == EVMETHOD_EPOLL) |
394 |
|
|
epoll_postfork_child (); |
395 |
root |
1.1 |
#endif |
396 |
root |
1.7 |
|
397 |
|
|
evio_stop (&sigev); |
398 |
|
|
close (sigpipe [0]); |
399 |
|
|
close (sigpipe [1]); |
400 |
|
|
pipe (sigpipe); |
401 |
|
|
siginit (); |
402 |
root |
1.1 |
} |
403 |
|
|
|
404 |
root |
1.8 |
/*****************************************************************************/ |
405 |
|
|
|
406 |
root |
1.1 |
static void |
407 |
root |
1.5 |
fd_reify (void) |
408 |
|
|
{ |
409 |
|
|
int i; |
410 |
|
|
|
411 |
|
|
for (i = 0; i < fdchangecnt; ++i) |
412 |
|
|
{ |
413 |
|
|
int fd = fdchanges [i]; |
414 |
|
|
ANFD *anfd = anfds + fd; |
415 |
|
|
struct ev_io *w; |
416 |
|
|
|
417 |
|
|
int wev = 0; |
418 |
|
|
|
419 |
|
|
for (w = anfd->head; w; w = w->next) |
420 |
|
|
wev |= w->events; |
421 |
|
|
|
422 |
|
|
if (anfd->wev != wev) |
423 |
|
|
{ |
424 |
|
|
method_modify (fd, anfd->wev, wev); |
425 |
|
|
anfd->wev = wev; |
426 |
|
|
} |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
fdchangecnt = 0; |
430 |
|
|
} |
431 |
|
|
|
432 |
|
|
static void |
433 |
root |
1.1 |
call_pending () |
434 |
|
|
{ |
435 |
root |
1.18 |
while (pendingcnt) |
436 |
root |
1.1 |
{ |
437 |
root |
1.18 |
ANPENDING *p = pendings + --pendingcnt; |
438 |
root |
1.1 |
|
439 |
|
|
if (p->w) |
440 |
|
|
{ |
441 |
|
|
p->w->pending = 0; |
442 |
|
|
p->w->cb (p->w, p->events); |
443 |
|
|
} |
444 |
|
|
} |
445 |
|
|
} |
446 |
|
|
|
447 |
|
|
static void |
448 |
root |
1.12 |
timers_reify () |
449 |
root |
1.1 |
{ |
450 |
root |
1.4 |
while (timercnt && timers [0]->at <= now) |
451 |
root |
1.1 |
{ |
452 |
|
|
struct ev_timer *w = timers [0]; |
453 |
|
|
|
454 |
root |
1.16 |
event ((W)w, EV_TIMEOUT); |
455 |
|
|
|
456 |
root |
1.4 |
/* first reschedule or stop timer */ |
457 |
root |
1.1 |
if (w->repeat) |
458 |
|
|
{ |
459 |
root |
1.12 |
w->at = now + w->repeat; |
460 |
|
|
assert (("timer timeout in the past, negative repeat?", w->at > now)); |
461 |
|
|
downheap ((WT *)timers, timercnt, 0); |
462 |
|
|
} |
463 |
|
|
else |
464 |
|
|
evtimer_stop (w); /* nonrepeating: stop timer */ |
465 |
|
|
} |
466 |
|
|
} |
467 |
root |
1.4 |
|
468 |
root |
1.12 |
static void |
469 |
|
|
periodics_reify () |
470 |
|
|
{ |
471 |
|
|
while (periodiccnt && periodics [0]->at <= ev_now) |
472 |
|
|
{ |
473 |
|
|
struct ev_periodic *w = periodics [0]; |
474 |
root |
1.1 |
|
475 |
root |
1.12 |
/* first reschedule or stop timer */ |
476 |
|
|
if (w->interval) |
477 |
|
|
{ |
478 |
|
|
w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
479 |
|
|
assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
480 |
|
|
downheap ((WT *)periodics, periodiccnt, 0); |
481 |
root |
1.1 |
} |
482 |
|
|
else |
483 |
root |
1.12 |
evperiodic_stop (w); /* nonrepeating: stop timer */ |
484 |
|
|
|
485 |
|
|
event ((W)w, EV_TIMEOUT); |
486 |
|
|
} |
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
static void |
490 |
root |
1.13 |
periodics_reschedule (ev_tstamp diff) |
491 |
root |
1.12 |
{ |
492 |
|
|
int i; |
493 |
|
|
|
494 |
root |
1.13 |
/* adjust periodics after time jump */ |
495 |
root |
1.12 |
for (i = 0; i < periodiccnt; ++i) |
496 |
|
|
{ |
497 |
|
|
struct ev_periodic *w = periodics [i]; |
498 |
|
|
|
499 |
|
|
if (w->interval) |
500 |
root |
1.4 |
{ |
501 |
root |
1.12 |
ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
502 |
|
|
|
503 |
|
|
if (fabs (diff) >= 1e-4) |
504 |
|
|
{ |
505 |
|
|
evperiodic_stop (w); |
506 |
|
|
evperiodic_start (w); |
507 |
|
|
|
508 |
|
|
i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
509 |
|
|
} |
510 |
root |
1.4 |
} |
511 |
root |
1.12 |
} |
512 |
root |
1.1 |
} |
513 |
|
|
|
514 |
root |
1.4 |
static void |
515 |
|
|
time_update () |
516 |
|
|
{ |
517 |
|
|
int i; |
518 |
root |
1.12 |
|
519 |
root |
1.4 |
ev_now = ev_time (); |
520 |
|
|
|
521 |
|
|
if (have_monotonic) |
522 |
|
|
{ |
523 |
|
|
ev_tstamp odiff = diff; |
524 |
|
|
|
525 |
root |
1.12 |
for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
526 |
root |
1.4 |
{ |
527 |
|
|
now = get_clock (); |
528 |
|
|
diff = ev_now - now; |
529 |
|
|
|
530 |
|
|
if (fabs (odiff - diff) < MIN_TIMEJUMP) |
531 |
|
|
return; /* all is well */ |
532 |
|
|
|
533 |
|
|
ev_now = ev_time (); |
534 |
|
|
} |
535 |
|
|
|
536 |
root |
1.13 |
periodics_reschedule (diff - odiff); |
537 |
|
|
/* no timer adjustment, as the monotonic clock doesn't jump */ |
538 |
root |
1.4 |
} |
539 |
|
|
else |
540 |
|
|
{ |
541 |
|
|
if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
542 |
root |
1.13 |
{ |
543 |
|
|
periodics_reschedule (ev_now - now); |
544 |
|
|
|
545 |
|
|
/* adjust timers. this is easy, as the offset is the same for all */ |
546 |
|
|
for (i = 0; i < timercnt; ++i) |
547 |
|
|
timers [i]->at += diff; |
548 |
|
|
} |
549 |
root |
1.4 |
|
550 |
|
|
now = ev_now; |
551 |
|
|
} |
552 |
|
|
} |
553 |
|
|
|
554 |
root |
1.1 |
int ev_loop_done; |
555 |
|
|
|
556 |
root |
1.4 |
void ev_loop (int flags) |
557 |
root |
1.1 |
{ |
558 |
|
|
double block; |
559 |
root |
1.13 |
ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
560 |
root |
1.1 |
|
561 |
root |
1.20 |
do |
562 |
root |
1.9 |
{ |
563 |
root |
1.20 |
/* queue check watchers (and execute them) */ |
564 |
root |
1.21 |
if (preparecnt) |
565 |
root |
1.20 |
{ |
566 |
|
|
queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
567 |
|
|
call_pending (); |
568 |
|
|
} |
569 |
root |
1.9 |
|
570 |
root |
1.1 |
/* update fd-related kernel structures */ |
571 |
root |
1.5 |
fd_reify (); |
572 |
root |
1.1 |
|
573 |
|
|
/* calculate blocking time */ |
574 |
root |
1.12 |
|
575 |
root |
1.21 |
/* we only need this for !monotonic clockor timers, but as we basically |
576 |
|
|
always have timers, we just calculate it always */ |
577 |
root |
1.12 |
ev_now = ev_time (); |
578 |
|
|
|
579 |
root |
1.9 |
if (flags & EVLOOP_NONBLOCK || idlecnt) |
580 |
root |
1.1 |
block = 0.; |
581 |
|
|
else |
582 |
|
|
{ |
583 |
root |
1.4 |
block = MAX_BLOCKTIME; |
584 |
|
|
|
585 |
root |
1.12 |
if (timercnt) |
586 |
root |
1.4 |
{ |
587 |
root |
1.14 |
ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
588 |
root |
1.4 |
if (block > to) block = to; |
589 |
|
|
} |
590 |
|
|
|
591 |
root |
1.12 |
if (periodiccnt) |
592 |
root |
1.4 |
{ |
593 |
root |
1.12 |
ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
594 |
root |
1.4 |
if (block > to) block = to; |
595 |
|
|
} |
596 |
|
|
|
597 |
root |
1.1 |
if (block < 0.) block = 0.; |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
method_poll (block); |
601 |
|
|
|
602 |
root |
1.4 |
/* update ev_now, do magic */ |
603 |
|
|
time_update (); |
604 |
|
|
|
605 |
root |
1.9 |
/* queue pending timers and reschedule them */ |
606 |
root |
1.20 |
timers_reify (); /* relative timers called last */ |
607 |
|
|
periodics_reify (); /* absolute timers called first */ |
608 |
root |
1.1 |
|
609 |
root |
1.9 |
/* queue idle watchers unless io or timers are pending */ |
610 |
|
|
if (!pendingcnt) |
611 |
root |
1.10 |
queue_events ((W *)idles, idlecnt, EV_IDLE); |
612 |
root |
1.9 |
|
613 |
root |
1.20 |
/* queue check watchers, to be executed first */ |
614 |
|
|
if (checkcnt) |
615 |
|
|
queue_events ((W *)checks, checkcnt, EV_CHECK); |
616 |
root |
1.9 |
|
617 |
root |
1.1 |
call_pending (); |
618 |
|
|
} |
619 |
|
|
while (!ev_loop_done); |
620 |
root |
1.13 |
|
621 |
|
|
if (ev_loop_done != 2) |
622 |
|
|
ev_loop_done = 0; |
623 |
root |
1.1 |
} |
624 |
|
|
|
625 |
root |
1.8 |
/*****************************************************************************/ |
626 |
|
|
|
627 |
root |
1.1 |
static void |
628 |
root |
1.10 |
wlist_add (WL *head, WL elem) |
629 |
root |
1.1 |
{ |
630 |
|
|
elem->next = *head; |
631 |
|
|
*head = elem; |
632 |
|
|
} |
633 |
|
|
|
634 |
|
|
static void |
635 |
root |
1.10 |
wlist_del (WL *head, WL elem) |
636 |
root |
1.1 |
{ |
637 |
|
|
while (*head) |
638 |
|
|
{ |
639 |
|
|
if (*head == elem) |
640 |
|
|
{ |
641 |
|
|
*head = elem->next; |
642 |
|
|
return; |
643 |
|
|
} |
644 |
|
|
|
645 |
|
|
head = &(*head)->next; |
646 |
|
|
} |
647 |
|
|
} |
648 |
|
|
|
649 |
|
|
static void |
650 |
root |
1.16 |
ev_clear (W w) |
651 |
|
|
{ |
652 |
|
|
if (w->pending) |
653 |
|
|
{ |
654 |
|
|
pendings [w->pending - 1].w = 0; |
655 |
|
|
w->pending = 0; |
656 |
|
|
} |
657 |
|
|
} |
658 |
|
|
|
659 |
|
|
static void |
660 |
root |
1.10 |
ev_start (W w, int active) |
661 |
root |
1.1 |
{ |
662 |
|
|
w->active = active; |
663 |
|
|
} |
664 |
|
|
|
665 |
|
|
static void |
666 |
root |
1.10 |
ev_stop (W w) |
667 |
root |
1.1 |
{ |
668 |
|
|
w->active = 0; |
669 |
|
|
} |
670 |
|
|
|
671 |
root |
1.8 |
/*****************************************************************************/ |
672 |
|
|
|
673 |
root |
1.1 |
void |
674 |
|
|
evio_start (struct ev_io *w) |
675 |
|
|
{ |
676 |
|
|
if (ev_is_active (w)) |
677 |
|
|
return; |
678 |
|
|
|
679 |
|
|
int fd = w->fd; |
680 |
|
|
|
681 |
root |
1.10 |
ev_start ((W)w, 1); |
682 |
root |
1.1 |
array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
683 |
root |
1.10 |
wlist_add ((WL *)&anfds[fd].head, (WL)w); |
684 |
root |
1.1 |
|
685 |
|
|
++fdchangecnt; |
686 |
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
687 |
|
|
fdchanges [fdchangecnt - 1] = fd; |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
void |
691 |
|
|
evio_stop (struct ev_io *w) |
692 |
|
|
{ |
693 |
root |
1.16 |
ev_clear ((W)w); |
694 |
root |
1.1 |
if (!ev_is_active (w)) |
695 |
|
|
return; |
696 |
|
|
|
697 |
root |
1.10 |
wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
698 |
|
|
ev_stop ((W)w); |
699 |
root |
1.1 |
|
700 |
|
|
++fdchangecnt; |
701 |
|
|
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
702 |
|
|
fdchanges [fdchangecnt - 1] = w->fd; |
703 |
|
|
} |
704 |
|
|
|
705 |
|
|
void |
706 |
|
|
evtimer_start (struct ev_timer *w) |
707 |
|
|
{ |
708 |
|
|
if (ev_is_active (w)) |
709 |
|
|
return; |
710 |
|
|
|
711 |
root |
1.12 |
w->at += now; |
712 |
|
|
|
713 |
root |
1.13 |
assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
714 |
|
|
|
715 |
root |
1.12 |
ev_start ((W)w, ++timercnt); |
716 |
|
|
array_needsize (timers, timermax, timercnt, ); |
717 |
|
|
timers [timercnt - 1] = w; |
718 |
|
|
upheap ((WT *)timers, timercnt - 1); |
719 |
|
|
} |
720 |
|
|
|
721 |
|
|
void |
722 |
|
|
evtimer_stop (struct ev_timer *w) |
723 |
|
|
{ |
724 |
root |
1.16 |
ev_clear ((W)w); |
725 |
root |
1.12 |
if (!ev_is_active (w)) |
726 |
|
|
return; |
727 |
|
|
|
728 |
|
|
if (w->active < timercnt--) |
729 |
root |
1.1 |
{ |
730 |
root |
1.12 |
timers [w->active - 1] = timers [timercnt]; |
731 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
732 |
|
|
} |
733 |
root |
1.4 |
|
734 |
root |
1.14 |
w->at = w->repeat; |
735 |
|
|
|
736 |
root |
1.12 |
ev_stop ((W)w); |
737 |
|
|
} |
738 |
root |
1.4 |
|
739 |
root |
1.12 |
void |
740 |
root |
1.14 |
evtimer_again (struct ev_timer *w) |
741 |
|
|
{ |
742 |
|
|
if (ev_is_active (w)) |
743 |
|
|
{ |
744 |
|
|
if (w->repeat) |
745 |
|
|
{ |
746 |
|
|
w->at = now + w->repeat; |
747 |
|
|
downheap ((WT *)timers, timercnt, w->active - 1); |
748 |
|
|
} |
749 |
|
|
else |
750 |
|
|
evtimer_stop (w); |
751 |
|
|
} |
752 |
|
|
else if (w->repeat) |
753 |
|
|
evtimer_start (w); |
754 |
|
|
} |
755 |
|
|
|
756 |
|
|
void |
757 |
root |
1.12 |
evperiodic_start (struct ev_periodic *w) |
758 |
|
|
{ |
759 |
|
|
if (ev_is_active (w)) |
760 |
|
|
return; |
761 |
root |
1.1 |
|
762 |
root |
1.13 |
assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
763 |
|
|
|
764 |
root |
1.12 |
/* this formula differs from the one in periodic_reify because we do not always round up */ |
765 |
|
|
if (w->interval) |
766 |
|
|
w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
767 |
|
|
|
768 |
|
|
ev_start ((W)w, ++periodiccnt); |
769 |
|
|
array_needsize (periodics, periodicmax, periodiccnt, ); |
770 |
|
|
periodics [periodiccnt - 1] = w; |
771 |
|
|
upheap ((WT *)periodics, periodiccnt - 1); |
772 |
root |
1.1 |
} |
773 |
|
|
|
774 |
|
|
void |
775 |
root |
1.12 |
evperiodic_stop (struct ev_periodic *w) |
776 |
root |
1.1 |
{ |
777 |
root |
1.16 |
ev_clear ((W)w); |
778 |
root |
1.1 |
if (!ev_is_active (w)) |
779 |
|
|
return; |
780 |
|
|
|
781 |
root |
1.12 |
if (w->active < periodiccnt--) |
782 |
root |
1.2 |
{ |
783 |
root |
1.12 |
periodics [w->active - 1] = periodics [periodiccnt]; |
784 |
|
|
downheap ((WT *)periodics, periodiccnt, w->active - 1); |
785 |
root |
1.2 |
} |
786 |
|
|
|
787 |
root |
1.10 |
ev_stop ((W)w); |
788 |
root |
1.1 |
} |
789 |
|
|
|
790 |
|
|
void |
791 |
|
|
evsignal_start (struct ev_signal *w) |
792 |
|
|
{ |
793 |
|
|
if (ev_is_active (w)) |
794 |
|
|
return; |
795 |
|
|
|
796 |
root |
1.10 |
ev_start ((W)w, 1); |
797 |
root |
1.1 |
array_needsize (signals, signalmax, w->signum, signals_init); |
798 |
root |
1.10 |
wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
799 |
root |
1.7 |
|
800 |
|
|
if (!w->next) |
801 |
|
|
{ |
802 |
|
|
struct sigaction sa; |
803 |
|
|
sa.sa_handler = sighandler; |
804 |
|
|
sigfillset (&sa.sa_mask); |
805 |
|
|
sa.sa_flags = 0; |
806 |
|
|
sigaction (w->signum, &sa, 0); |
807 |
|
|
} |
808 |
root |
1.1 |
} |
809 |
|
|
|
810 |
|
|
void |
811 |
|
|
evsignal_stop (struct ev_signal *w) |
812 |
|
|
{ |
813 |
root |
1.16 |
ev_clear ((W)w); |
814 |
root |
1.1 |
if (!ev_is_active (w)) |
815 |
|
|
return; |
816 |
|
|
|
817 |
root |
1.10 |
wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
818 |
|
|
ev_stop ((W)w); |
819 |
root |
1.7 |
|
820 |
|
|
if (!signals [w->signum - 1].head) |
821 |
|
|
signal (w->signum, SIG_DFL); |
822 |
root |
1.1 |
} |
823 |
|
|
|
824 |
root |
1.9 |
void evidle_start (struct ev_idle *w) |
825 |
|
|
{ |
826 |
|
|
if (ev_is_active (w)) |
827 |
|
|
return; |
828 |
|
|
|
829 |
root |
1.10 |
ev_start ((W)w, ++idlecnt); |
830 |
root |
1.9 |
array_needsize (idles, idlemax, idlecnt, ); |
831 |
|
|
idles [idlecnt - 1] = w; |
832 |
|
|
} |
833 |
|
|
|
834 |
|
|
void evidle_stop (struct ev_idle *w) |
835 |
|
|
{ |
836 |
root |
1.16 |
ev_clear ((W)w); |
837 |
|
|
if (ev_is_active (w)) |
838 |
|
|
return; |
839 |
|
|
|
840 |
root |
1.9 |
idles [w->active - 1] = idles [--idlecnt]; |
841 |
root |
1.10 |
ev_stop ((W)w); |
842 |
root |
1.9 |
} |
843 |
|
|
|
844 |
root |
1.20 |
void evprepare_start (struct ev_prepare *w) |
845 |
|
|
{ |
846 |
|
|
if (ev_is_active (w)) |
847 |
|
|
return; |
848 |
|
|
|
849 |
|
|
ev_start ((W)w, ++preparecnt); |
850 |
|
|
array_needsize (prepares, preparemax, preparecnt, ); |
851 |
|
|
prepares [preparecnt - 1] = w; |
852 |
|
|
} |
853 |
|
|
|
854 |
|
|
void evprepare_stop (struct ev_prepare *w) |
855 |
|
|
{ |
856 |
|
|
ev_clear ((W)w); |
857 |
|
|
if (ev_is_active (w)) |
858 |
|
|
return; |
859 |
|
|
|
860 |
|
|
prepares [w->active - 1] = prepares [--preparecnt]; |
861 |
|
|
ev_stop ((W)w); |
862 |
|
|
} |
863 |
|
|
|
864 |
root |
1.9 |
void evcheck_start (struct ev_check *w) |
865 |
|
|
{ |
866 |
|
|
if (ev_is_active (w)) |
867 |
|
|
return; |
868 |
|
|
|
869 |
root |
1.10 |
ev_start ((W)w, ++checkcnt); |
870 |
root |
1.9 |
array_needsize (checks, checkmax, checkcnt, ); |
871 |
|
|
checks [checkcnt - 1] = w; |
872 |
|
|
} |
873 |
|
|
|
874 |
|
|
void evcheck_stop (struct ev_check *w) |
875 |
|
|
{ |
876 |
root |
1.16 |
ev_clear ((W)w); |
877 |
|
|
if (ev_is_active (w)) |
878 |
|
|
return; |
879 |
|
|
|
880 |
root |
1.9 |
checks [w->active - 1] = checks [--checkcnt]; |
881 |
root |
1.10 |
ev_stop ((W)w); |
882 |
root |
1.9 |
} |
883 |
|
|
|
884 |
root |
1.1 |
/*****************************************************************************/ |
885 |
root |
1.10 |
|
886 |
root |
1.16 |
struct ev_once |
887 |
|
|
{ |
888 |
|
|
struct ev_io io; |
889 |
|
|
struct ev_timer to; |
890 |
|
|
void (*cb)(int revents, void *arg); |
891 |
|
|
void *arg; |
892 |
|
|
}; |
893 |
|
|
|
894 |
|
|
static void |
895 |
|
|
once_cb (struct ev_once *once, int revents) |
896 |
|
|
{ |
897 |
|
|
void (*cb)(int revents, void *arg) = once->cb; |
898 |
|
|
void *arg = once->arg; |
899 |
|
|
|
900 |
|
|
evio_stop (&once->io); |
901 |
|
|
evtimer_stop (&once->to); |
902 |
|
|
free (once); |
903 |
|
|
|
904 |
|
|
cb (revents, arg); |
905 |
|
|
} |
906 |
|
|
|
907 |
|
|
static void |
908 |
|
|
once_cb_io (struct ev_io *w, int revents) |
909 |
|
|
{ |
910 |
|
|
once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
911 |
|
|
} |
912 |
|
|
|
913 |
|
|
static void |
914 |
|
|
once_cb_to (struct ev_timer *w, int revents) |
915 |
|
|
{ |
916 |
|
|
once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
917 |
|
|
} |
918 |
|
|
|
919 |
|
|
void |
920 |
|
|
ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
921 |
|
|
{ |
922 |
|
|
struct ev_once *once = malloc (sizeof (struct ev_once)); |
923 |
|
|
|
924 |
|
|
if (!once) |
925 |
|
|
cb (EV_ERROR, arg); |
926 |
|
|
else |
927 |
|
|
{ |
928 |
|
|
once->cb = cb; |
929 |
|
|
once->arg = arg; |
930 |
|
|
|
931 |
|
|
evw_init (&once->io, once_cb_io); |
932 |
|
|
|
933 |
|
|
if (fd >= 0) |
934 |
|
|
{ |
935 |
|
|
evio_set (&once->io, fd, events); |
936 |
|
|
evio_start (&once->io); |
937 |
|
|
} |
938 |
|
|
|
939 |
|
|
evw_init (&once->to, once_cb_to); |
940 |
|
|
|
941 |
|
|
if (timeout >= 0.) |
942 |
|
|
{ |
943 |
|
|
evtimer_set (&once->to, timeout, 0.); |
944 |
|
|
evtimer_start (&once->to); |
945 |
|
|
} |
946 |
|
|
} |
947 |
|
|
} |
948 |
|
|
|
949 |
|
|
/*****************************************************************************/ |
950 |
|
|
|
951 |
root |
1.13 |
#if 0 |
952 |
root |
1.12 |
|
953 |
|
|
struct ev_io wio; |
954 |
root |
1.1 |
|
955 |
|
|
static void |
956 |
|
|
sin_cb (struct ev_io *w, int revents) |
957 |
|
|
{ |
958 |
|
|
fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
959 |
|
|
} |
960 |
|
|
|
961 |
|
|
static void |
962 |
|
|
ocb (struct ev_timer *w, int revents) |
963 |
|
|
{ |
964 |
root |
1.4 |
//fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
965 |
|
|
evtimer_stop (w); |
966 |
|
|
evtimer_start (w); |
967 |
root |
1.1 |
} |
968 |
|
|
|
969 |
root |
1.7 |
static void |
970 |
|
|
scb (struct ev_signal *w, int revents) |
971 |
|
|
{ |
972 |
|
|
fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
973 |
root |
1.12 |
evio_stop (&wio); |
974 |
|
|
evio_start (&wio); |
975 |
root |
1.7 |
} |
976 |
|
|
|
977 |
root |
1.9 |
static void |
978 |
|
|
gcb (struct ev_signal *w, int revents) |
979 |
|
|
{ |
980 |
|
|
fprintf (stderr, "generic %x\n", revents); |
981 |
root |
1.12 |
|
982 |
root |
1.9 |
} |
983 |
|
|
|
984 |
root |
1.1 |
int main (void) |
985 |
|
|
{ |
986 |
|
|
ev_init (0); |
987 |
|
|
|
988 |
root |
1.12 |
evio_init (&wio, sin_cb, 0, EV_READ); |
989 |
|
|
evio_start (&wio); |
990 |
root |
1.1 |
|
991 |
root |
1.4 |
struct ev_timer t[10000]; |
992 |
root |
1.2 |
|
993 |
root |
1.9 |
#if 0 |
994 |
root |
1.2 |
int i; |
995 |
root |
1.4 |
for (i = 0; i < 10000; ++i) |
996 |
root |
1.2 |
{ |
997 |
|
|
struct ev_timer *w = t + i; |
998 |
|
|
evw_init (w, ocb, i); |
999 |
root |
1.12 |
evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1000 |
root |
1.2 |
evtimer_start (w); |
1001 |
|
|
if (drand48 () < 0.5) |
1002 |
|
|
evtimer_stop (w); |
1003 |
|
|
} |
1004 |
root |
1.4 |
#endif |
1005 |
|
|
|
1006 |
|
|
struct ev_timer t1; |
1007 |
root |
1.12 |
evtimer_init (&t1, ocb, 5, 10); |
1008 |
root |
1.4 |
evtimer_start (&t1); |
1009 |
root |
1.1 |
|
1010 |
root |
1.7 |
struct ev_signal sig; |
1011 |
root |
1.12 |
evsignal_init (&sig, scb, SIGQUIT); |
1012 |
root |
1.7 |
evsignal_start (&sig); |
1013 |
|
|
|
1014 |
root |
1.9 |
struct ev_check cw; |
1015 |
root |
1.12 |
evcheck_init (&cw, gcb); |
1016 |
root |
1.9 |
evcheck_start (&cw); |
1017 |
|
|
|
1018 |
|
|
struct ev_idle iw; |
1019 |
root |
1.12 |
evidle_init (&iw, gcb); |
1020 |
root |
1.9 |
evidle_start (&iw); |
1021 |
|
|
|
1022 |
root |
1.1 |
ev_loop (0); |
1023 |
|
|
|
1024 |
|
|
return 0; |
1025 |
|
|
} |
1026 |
|
|
|
1027 |
|
|
#endif |
1028 |
|
|
|
1029 |
|
|
|
1030 |
|
|
|
1031 |
|
|
|