1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (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. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
30 | */ |
29 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
30 | # include "config.h" |
32 | # include "config.h" |
31 | #endif |
33 | #endif |
32 | |
34 | |
33 | #include <math.h> |
35 | #include <math.h> |
34 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
40 | #include <stdio.h> |
42 | #include <stdio.h> |
41 | |
43 | |
42 | #include <assert.h> |
44 | #include <assert.h> |
43 | #include <errno.h> |
45 | #include <errno.h> |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
45 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
46 | #include <sys/time.h> |
50 | #include <sys/time.h> |
47 | #include <time.h> |
51 | #include <time.h> |
48 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
49 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
57 | #endif |
54 | |
58 | |
55 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
57 | #endif |
61 | #endif |
58 | |
62 | |
|
|
63 | #ifndef EV_USE_POLL |
|
|
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
65 | #endif |
|
|
66 | |
59 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
61 | #endif |
69 | #endif |
62 | |
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #ifndef EV_USE_REALTIME |
|
|
76 | # define EV_USE_REALTIME 1 |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | /**/ |
|
|
80 | |
|
|
81 | #ifndef CLOCK_MONOTONIC |
|
|
82 | # undef EV_USE_MONOTONIC |
|
|
83 | # define EV_USE_MONOTONIC 0 |
|
|
84 | #endif |
|
|
85 | |
63 | #ifndef CLOCK_REALTIME |
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
88 | # define EV_USE_REALTIME 0 |
65 | #endif |
89 | #endif |
66 | #ifndef EV_USE_REALTIME |
90 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
91 | /**/ |
68 | #endif |
|
|
69 | |
92 | |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
74 | |
97 | |
75 | #include "ev.h" |
98 | #include "ev.h" |
|
|
99 | |
|
|
100 | #if __GNUC__ >= 3 |
|
|
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
102 | # define inline inline |
|
|
103 | #else |
|
|
104 | # define expect(expr,value) (expr) |
|
|
105 | # define inline static |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
76 | |
113 | |
77 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
80 | |
117 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
83 | int ev_method; |
120 | int ev_method; |
84 | |
121 | |
85 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
86 | |
123 | |
… | |
… | |
106 | |
143 | |
107 | static ev_tstamp |
144 | static ev_tstamp |
108 | get_clock (void) |
145 | get_clock (void) |
109 | { |
146 | { |
110 | #if EV_USE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
112 | { |
149 | { |
113 | struct timespec ts; |
150 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
153 | } |
… | |
… | |
120 | } |
157 | } |
121 | |
158 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
160 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
163 | { \ |
127 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
128 | do \ |
165 | do \ |
129 | { \ |
166 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
138 | |
175 | |
139 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
140 | |
177 | |
141 | typedef struct |
178 | typedef struct |
142 | { |
179 | { |
143 | struct ev_io *head; |
180 | struct ev_watcher_list *head; |
144 | unsigned char events; |
181 | unsigned char events; |
145 | unsigned char reify; |
182 | unsigned char reify; |
146 | } ANFD; |
183 | } ANFD; |
147 | |
184 | |
148 | static ANFD *anfds; |
185 | static ANFD *anfds; |
… | |
… | |
165 | { |
202 | { |
166 | W w; |
203 | W w; |
167 | int events; |
204 | int events; |
168 | } ANPENDING; |
205 | } ANPENDING; |
169 | |
206 | |
170 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
171 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
172 | |
209 | |
173 | static void |
210 | static void |
174 | event (W w, int events) |
211 | event (W w, int events) |
175 | { |
212 | { |
176 | if (w->pending) |
213 | if (w->pending) |
177 | { |
214 | { |
178 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
179 | return; |
216 | return; |
180 | } |
217 | } |
181 | |
218 | |
182 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
184 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
185 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
186 | } |
223 | } |
187 | |
224 | |
188 | static void |
225 | static void |
189 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
190 | { |
227 | { |
… | |
… | |
198 | fd_event (int fd, int events) |
235 | fd_event (int fd, int events) |
199 | { |
236 | { |
200 | ANFD *anfd = anfds + fd; |
237 | ANFD *anfd = anfds + fd; |
201 | struct ev_io *w; |
238 | struct ev_io *w; |
202 | |
239 | |
203 | for (w = anfd->head; w; w = w->next) |
240 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
204 | { |
241 | { |
205 | int ev = w->events & events; |
242 | int ev = w->events & events; |
206 | |
243 | |
207 | if (ev) |
244 | if (ev) |
208 | event ((W)w, ev); |
245 | event ((W)w, ev); |
… | |
… | |
225 | ANFD *anfd = anfds + fd; |
262 | ANFD *anfd = anfds + fd; |
226 | struct ev_io *w; |
263 | struct ev_io *w; |
227 | |
264 | |
228 | int events = 0; |
265 | int events = 0; |
229 | |
266 | |
230 | for (w = anfd->head; w; w = w->next) |
267 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
231 | events |= w->events; |
268 | events |= w->events; |
232 | |
269 | |
233 | anfd->reify = 0; |
270 | anfd->reify = 0; |
234 | |
271 | |
235 | if (anfd->events != events) |
272 | if (anfd->events != events) |
… | |
… | |
253 | ++fdchangecnt; |
290 | ++fdchangecnt; |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
255 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
256 | } |
293 | } |
257 | |
294 | |
|
|
295 | static void |
|
|
296 | fd_kill (int fd) |
|
|
297 | { |
|
|
298 | struct ev_io *w; |
|
|
299 | |
|
|
300 | while ((w = (struct ev_io *)anfds [fd].head)) |
|
|
301 | { |
|
|
302 | ev_io_stop (w); |
|
|
303 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
304 | } |
|
|
305 | } |
|
|
306 | |
258 | /* called on EBADF to verify fds */ |
307 | /* called on EBADF to verify fds */ |
259 | static void |
308 | static void |
260 | fd_recheck (void) |
309 | fd_ebadf (void) |
261 | { |
310 | { |
262 | int fd; |
311 | int fd; |
263 | |
312 | |
264 | for (fd = 0; fd < anfdmax; ++fd) |
313 | for (fd = 0; fd < anfdmax; ++fd) |
265 | if (anfds [fd].events) |
314 | if (anfds [fd].events) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
315 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
267 | while (anfds [fd].head) |
316 | fd_kill (fd); |
|
|
317 | } |
|
|
318 | |
|
|
319 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
320 | static void |
|
|
321 | fd_enomem (void) |
|
|
322 | { |
|
|
323 | int fd = anfdmax; |
|
|
324 | |
|
|
325 | while (fd--) |
|
|
326 | if (anfds [fd].events) |
268 | { |
327 | { |
269 | ev_io_stop (anfds [fd].head); |
328 | close (fd); |
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
329 | fd_kill (fd); |
|
|
330 | return; |
271 | } |
331 | } |
272 | } |
332 | } |
273 | |
333 | |
274 | /*****************************************************************************/ |
334 | /*****************************************************************************/ |
275 | |
335 | |
276 | static struct ev_timer **timers; |
336 | static struct ev_timer **timers; |
… | |
… | |
322 | |
382 | |
323 | /*****************************************************************************/ |
383 | /*****************************************************************************/ |
324 | |
384 | |
325 | typedef struct |
385 | typedef struct |
326 | { |
386 | { |
327 | struct ev_signal *head; |
387 | struct ev_watcher_list *head; |
328 | sig_atomic_t gotsig; |
388 | sig_atomic_t volatile gotsig; |
329 | } ANSIG; |
389 | } ANSIG; |
330 | |
390 | |
331 | static ANSIG *signals; |
391 | static ANSIG *signals; |
332 | static int signalmax; |
392 | static int signalmax; |
333 | |
393 | |
334 | static int sigpipe [2]; |
394 | static int sigpipe [2]; |
335 | static sig_atomic_t gotsig; |
395 | static sig_atomic_t volatile gotsig; |
336 | static struct ev_io sigev; |
396 | static struct ev_io sigev; |
337 | |
397 | |
338 | static void |
398 | static void |
339 | signals_init (ANSIG *base, int count) |
399 | signals_init (ANSIG *base, int count) |
340 | { |
400 | { |
… | |
… | |
352 | { |
412 | { |
353 | signals [signum - 1].gotsig = 1; |
413 | signals [signum - 1].gotsig = 1; |
354 | |
414 | |
355 | if (!gotsig) |
415 | if (!gotsig) |
356 | { |
416 | { |
|
|
417 | int old_errno = errno; |
357 | gotsig = 1; |
418 | gotsig = 1; |
358 | write (sigpipe [1], &gotsig, 1); |
419 | write (sigpipe [1], &signum, 1); |
|
|
420 | errno = old_errno; |
359 | } |
421 | } |
360 | } |
422 | } |
361 | |
423 | |
362 | static void |
424 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
425 | sigcb (struct ev_io *iow, int revents) |
364 | { |
426 | { |
365 | struct ev_signal *w; |
427 | struct ev_watcher_list *w; |
366 | int sig; |
428 | int signum; |
367 | |
429 | |
|
|
430 | read (sigpipe [0], &revents, 1); |
368 | gotsig = 0; |
431 | gotsig = 0; |
369 | read (sigpipe [0], &revents, 1); |
|
|
370 | |
432 | |
371 | for (sig = signalmax; sig--; ) |
433 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
434 | if (signals [signum].gotsig) |
373 | { |
435 | { |
374 | signals [sig].gotsig = 0; |
436 | signals [signum].gotsig = 0; |
375 | |
437 | |
376 | for (w = signals [sig].head; w; w = w->next) |
438 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
439 | event ((W)w, EV_SIGNAL); |
378 | } |
440 | } |
379 | } |
441 | } |
380 | |
442 | |
381 | static void |
443 | static void |
382 | siginit (void) |
444 | siginit (void) |
383 | { |
445 | { |
|
|
446 | #ifndef WIN32 |
384 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
385 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
386 | |
449 | |
387 | /* rather than sort out wether we really need nb, set it */ |
450 | /* rather than sort out wether we really need nb, set it */ |
388 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
389 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
453 | #endif |
390 | |
454 | |
391 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
392 | ev_io_start (&sigev); |
456 | ev_io_start (&sigev); |
393 | } |
457 | } |
394 | |
458 | |
… | |
… | |
406 | /*****************************************************************************/ |
470 | /*****************************************************************************/ |
407 | |
471 | |
408 | static struct ev_child *childs [PID_HASHSIZE]; |
472 | static struct ev_child *childs [PID_HASHSIZE]; |
409 | static struct ev_signal childev; |
473 | static struct ev_signal childev; |
410 | |
474 | |
|
|
475 | #ifndef WIN32 |
|
|
476 | |
411 | #ifndef WCONTINUED |
477 | #ifndef WCONTINUED |
412 | # define WCONTINUED 0 |
478 | # define WCONTINUED 0 |
413 | #endif |
479 | #endif |
414 | |
480 | |
415 | static void |
481 | static void |
|
|
482 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
483 | { |
|
|
484 | struct ev_child *w; |
|
|
485 | |
|
|
486 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
487 | if (w->pid == pid || !w->pid) |
|
|
488 | { |
|
|
489 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
490 | w->rpid = pid; |
|
|
491 | w->rstatus = status; |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
416 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
417 | { |
498 | { |
418 | struct ev_child *w; |
|
|
419 | int pid, status; |
499 | int pid, status; |
420 | |
500 | |
421 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
501 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
422 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
502 | { |
423 | if (w->pid == pid || w->pid == -1) |
503 | /* make sure we are called again until all childs have been reaped */ |
424 | { |
|
|
425 | w->status = status; |
|
|
426 | event ((W)w, EV_CHILD); |
504 | event ((W)sw, EV_SIGNAL); |
427 | } |
505 | |
|
|
506 | child_reap (sw, pid, pid, status); |
|
|
507 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
508 | } |
428 | } |
509 | } |
|
|
510 | |
|
|
511 | #endif |
429 | |
512 | |
430 | /*****************************************************************************/ |
513 | /*****************************************************************************/ |
431 | |
514 | |
|
|
515 | #if EV_USE_KQUEUE |
|
|
516 | # include "ev_kqueue.c" |
|
|
517 | #endif |
432 | #if EV_USE_EPOLL |
518 | #if EV_USE_EPOLL |
433 | # include "ev_epoll.c" |
519 | # include "ev_epoll.c" |
434 | #endif |
520 | #endif |
|
|
521 | #if EV_USE_POLL |
|
|
522 | # include "ev_poll.c" |
|
|
523 | #endif |
435 | #if EV_USE_SELECT |
524 | #if EV_USE_SELECT |
436 | # include "ev_select.c" |
525 | # include "ev_select.c" |
437 | #endif |
526 | #endif |
438 | |
527 | |
439 | int |
528 | int |
… | |
… | |
446 | ev_version_minor (void) |
535 | ev_version_minor (void) |
447 | { |
536 | { |
448 | return EV_VERSION_MINOR; |
537 | return EV_VERSION_MINOR; |
449 | } |
538 | } |
450 | |
539 | |
|
|
540 | /* return true if we are running with elevated privileges and should ignore env variables */ |
|
|
541 | static int |
|
|
542 | enable_secure () |
|
|
543 | { |
|
|
544 | #ifdef WIN32 |
|
|
545 | return 0; |
|
|
546 | #else |
|
|
547 | return getuid () != geteuid () |
|
|
548 | || getgid () != getegid (); |
|
|
549 | #endif |
|
|
550 | } |
|
|
551 | |
451 | int ev_init (int flags) |
552 | int ev_init (int methods) |
452 | { |
553 | { |
453 | if (!ev_method) |
554 | if (!ev_method) |
454 | { |
555 | { |
455 | #if EV_USE_MONOTONIC |
556 | #if EV_USE_MONOTONIC |
456 | { |
557 | { |
… | |
… | |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
559 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
459 | have_monotonic = 1; |
560 | have_monotonic = 1; |
460 | } |
561 | } |
461 | #endif |
562 | #endif |
462 | |
563 | |
463 | ev_now = ev_time (); |
564 | ev_now = ev_time (); |
464 | now = get_clock (); |
565 | now = get_clock (); |
|
|
566 | now_floor = now; |
465 | diff = ev_now - now; |
567 | diff = ev_now - now; |
466 | |
568 | |
467 | if (pipe (sigpipe)) |
569 | if (pipe (sigpipe)) |
468 | return 0; |
570 | return 0; |
469 | |
571 | |
|
|
572 | if (methods == EVMETHOD_AUTO) |
|
|
573 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
574 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
575 | else |
470 | ev_method = EVMETHOD_NONE; |
576 | methods = EVMETHOD_ANY; |
|
|
577 | |
|
|
578 | ev_method = 0; |
|
|
579 | #if EV_USE_KQUEUE |
|
|
580 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
581 | #endif |
471 | #if EV_USE_EPOLL |
582 | #if EV_USE_EPOLL |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
583 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
584 | #endif |
|
|
585 | #if EV_USE_POLL |
|
|
586 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
473 | #endif |
587 | #endif |
474 | #if EV_USE_SELECT |
588 | #if EV_USE_SELECT |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
589 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
476 | #endif |
590 | #endif |
477 | |
591 | |
478 | if (ev_method) |
592 | if (ev_method) |
479 | { |
593 | { |
480 | ev_watcher_init (&sigev, sigcb); |
594 | ev_watcher_init (&sigev, sigcb); |
|
|
595 | ev_set_priority (&sigev, EV_MAXPRI); |
481 | siginit (); |
596 | siginit (); |
482 | |
597 | |
|
|
598 | #ifndef WIN32 |
483 | ev_signal_init (&childev, childcb, SIGCHLD); |
599 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
600 | ev_set_priority (&childev, EV_MAXPRI); |
484 | ev_signal_start (&childev); |
601 | ev_signal_start (&childev); |
|
|
602 | #endif |
485 | } |
603 | } |
486 | } |
604 | } |
487 | |
605 | |
488 | return ev_method; |
606 | return ev_method; |
489 | } |
607 | } |
490 | |
608 | |
491 | /*****************************************************************************/ |
609 | /*****************************************************************************/ |
492 | |
610 | |
493 | void |
611 | void |
494 | ev_prefork (void) |
612 | ev_fork_prepare (void) |
495 | { |
613 | { |
496 | /* nop */ |
614 | /* nop */ |
497 | } |
615 | } |
498 | |
616 | |
499 | void |
617 | void |
500 | ev_postfork_parent (void) |
618 | ev_fork_parent (void) |
501 | { |
619 | { |
502 | /* nop */ |
620 | /* nop */ |
503 | } |
621 | } |
504 | |
622 | |
505 | void |
623 | void |
506 | ev_postfork_child (void) |
624 | ev_fork_child (void) |
507 | { |
625 | { |
508 | #if EV_USE_EPOLL |
626 | #if EV_USE_EPOLL |
509 | if (ev_method == EVMETHOD_EPOLL) |
627 | if (ev_method == EVMETHOD_EPOLL) |
510 | epoll_postfork_child (); |
628 | epoll_postfork_child (); |
511 | #endif |
629 | #endif |
… | |
… | |
520 | /*****************************************************************************/ |
638 | /*****************************************************************************/ |
521 | |
639 | |
522 | static void |
640 | static void |
523 | call_pending (void) |
641 | call_pending (void) |
524 | { |
642 | { |
|
|
643 | int pri; |
|
|
644 | |
|
|
645 | for (pri = NUMPRI; pri--; ) |
525 | while (pendingcnt) |
646 | while (pendingcnt [pri]) |
526 | { |
647 | { |
527 | ANPENDING *p = pendings + --pendingcnt; |
648 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
528 | |
649 | |
529 | if (p->w) |
650 | if (p->w) |
530 | { |
651 | { |
531 | p->w->pending = 0; |
652 | p->w->pending = 0; |
532 | p->w->cb (p->w, p->events); |
653 | p->w->cb (p->w, p->events); |
533 | } |
654 | } |
534 | } |
655 | } |
535 | } |
656 | } |
536 | |
657 | |
537 | static void |
658 | static void |
538 | timers_reify (void) |
659 | timers_reify (void) |
539 | { |
660 | { |
… | |
… | |
599 | } |
720 | } |
600 | } |
721 | } |
601 | } |
722 | } |
602 | } |
723 | } |
603 | |
724 | |
|
|
725 | static int |
|
|
726 | time_update_monotonic (void) |
|
|
727 | { |
|
|
728 | now = get_clock (); |
|
|
729 | |
|
|
730 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
731 | { |
|
|
732 | ev_now = now + diff; |
|
|
733 | return 0; |
|
|
734 | } |
|
|
735 | else |
|
|
736 | { |
|
|
737 | now_floor = now; |
|
|
738 | ev_now = ev_time (); |
|
|
739 | return 1; |
|
|
740 | } |
|
|
741 | } |
|
|
742 | |
604 | static void |
743 | static void |
605 | time_update (void) |
744 | time_update (void) |
606 | { |
745 | { |
607 | int i; |
746 | int i; |
608 | |
747 | |
609 | ev_now = ev_time (); |
748 | #if EV_USE_MONOTONIC |
610 | |
|
|
611 | if (have_monotonic) |
749 | if (expect_true (have_monotonic)) |
612 | { |
750 | { |
613 | ev_tstamp odiff = diff; |
751 | if (time_update_monotonic ()) |
614 | |
|
|
615 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
616 | { |
752 | { |
617 | now = get_clock (); |
753 | ev_tstamp odiff = diff; |
|
|
754 | |
|
|
755 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
756 | { |
618 | diff = ev_now - now; |
757 | diff = ev_now - now; |
619 | |
758 | |
620 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
759 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
621 | return; /* all is well */ |
760 | return; /* all is well */ |
622 | |
761 | |
623 | ev_now = ev_time (); |
762 | ev_now = ev_time (); |
|
|
763 | now = get_clock (); |
|
|
764 | now_floor = now; |
|
|
765 | } |
|
|
766 | |
|
|
767 | periodics_reschedule (diff - odiff); |
|
|
768 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
624 | } |
769 | } |
625 | |
|
|
626 | periodics_reschedule (diff - odiff); |
|
|
627 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
628 | } |
770 | } |
629 | else |
771 | else |
|
|
772 | #endif |
630 | { |
773 | { |
|
|
774 | ev_now = ev_time (); |
|
|
775 | |
631 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
776 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
632 | { |
777 | { |
633 | periodics_reschedule (ev_now - now); |
778 | periodics_reschedule (ev_now - now); |
634 | |
779 | |
635 | /* adjust timers. this is easy, as the offset is the same for all */ |
780 | /* adjust timers. this is easy, as the offset is the same for all */ |
636 | for (i = 0; i < timercnt; ++i) |
781 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
794 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
650 | |
795 | |
651 | do |
796 | do |
652 | { |
797 | { |
653 | /* queue check watchers (and execute them) */ |
798 | /* queue check watchers (and execute them) */ |
654 | if (preparecnt) |
799 | if (expect_false (preparecnt)) |
655 | { |
800 | { |
656 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
801 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
657 | call_pending (); |
802 | call_pending (); |
658 | } |
803 | } |
659 | |
804 | |
… | |
… | |
662 | |
807 | |
663 | /* calculate blocking time */ |
808 | /* calculate blocking time */ |
664 | |
809 | |
665 | /* we only need this for !monotonic clockor timers, but as we basically |
810 | /* we only need this for !monotonic clockor timers, but as we basically |
666 | always have timers, we just calculate it always */ |
811 | always have timers, we just calculate it always */ |
|
|
812 | #if EV_USE_MONOTONIC |
|
|
813 | if (expect_true (have_monotonic)) |
|
|
814 | time_update_monotonic (); |
|
|
815 | else |
|
|
816 | #endif |
|
|
817 | { |
667 | ev_now = ev_time (); |
818 | ev_now = ev_time (); |
|
|
819 | now = ev_now; |
|
|
820 | } |
668 | |
821 | |
669 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
822 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
670 | block = 0.; |
823 | block = 0.; |
671 | else |
824 | else |
672 | { |
825 | { |
673 | block = MAX_BLOCKTIME; |
826 | block = MAX_BLOCKTIME; |
674 | |
827 | |
675 | if (timercnt) |
828 | if (timercnt) |
676 | { |
829 | { |
677 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
830 | ev_tstamp to = timers [0]->at - now + method_fudge; |
678 | if (block > to) block = to; |
831 | if (block > to) block = to; |
679 | } |
832 | } |
680 | |
833 | |
681 | if (periodiccnt) |
834 | if (periodiccnt) |
682 | { |
835 | { |
… | |
… | |
739 | static void |
892 | static void |
740 | ev_clear_pending (W w) |
893 | ev_clear_pending (W w) |
741 | { |
894 | { |
742 | if (w->pending) |
895 | if (w->pending) |
743 | { |
896 | { |
744 | pendings [w->pending - 1].w = 0; |
897 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
745 | w->pending = 0; |
898 | w->pending = 0; |
746 | } |
899 | } |
747 | } |
900 | } |
748 | |
901 | |
749 | static void |
902 | static void |
750 | ev_start (W w, int active) |
903 | ev_start (W w, int active) |
751 | { |
904 | { |
|
|
905 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
906 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
907 | |
752 | w->active = active; |
908 | w->active = active; |
753 | } |
909 | } |
754 | |
910 | |
755 | static void |
911 | static void |
756 | ev_stop (W w) |
912 | ev_stop (W w) |
… | |
… | |
761 | /*****************************************************************************/ |
917 | /*****************************************************************************/ |
762 | |
918 | |
763 | void |
919 | void |
764 | ev_io_start (struct ev_io *w) |
920 | ev_io_start (struct ev_io *w) |
765 | { |
921 | { |
|
|
922 | int fd = w->fd; |
|
|
923 | |
766 | if (ev_is_active (w)) |
924 | if (ev_is_active (w)) |
767 | return; |
925 | return; |
768 | |
|
|
769 | int fd = w->fd; |
|
|
770 | |
926 | |
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
927 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
928 | |
773 | ev_start ((W)w, 1); |
929 | ev_start ((W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
930 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
… | |
… | |
873 | } |
1029 | } |
874 | |
1030 | |
875 | ev_stop ((W)w); |
1031 | ev_stop ((W)w); |
876 | } |
1032 | } |
877 | |
1033 | |
|
|
1034 | #ifndef SA_RESTART |
|
|
1035 | # define SA_RESTART 0 |
|
|
1036 | #endif |
|
|
1037 | |
878 | void |
1038 | void |
879 | ev_signal_start (struct ev_signal *w) |
1039 | ev_signal_start (struct ev_signal *w) |
880 | { |
1040 | { |
881 | if (ev_is_active (w)) |
1041 | if (ev_is_active (w)) |
882 | return; |
1042 | return; |
… | |
… | |
890 | if (!w->next) |
1050 | if (!w->next) |
891 | { |
1051 | { |
892 | struct sigaction sa; |
1052 | struct sigaction sa; |
893 | sa.sa_handler = sighandler; |
1053 | sa.sa_handler = sighandler; |
894 | sigfillset (&sa.sa_mask); |
1054 | sigfillset (&sa.sa_mask); |
895 | sa.sa_flags = 0; |
1055 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
896 | sigaction (w->signum, &sa, 0); |
1056 | sigaction (w->signum, &sa, 0); |
897 | } |
1057 | } |
898 | } |
1058 | } |
899 | |
1059 | |
900 | void |
1060 | void |