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 |
… | |
… | |
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); \ |
… | |
… | |
139 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
140 | |
177 | |
141 | typedef struct |
178 | typedef struct |
142 | { |
179 | { |
143 | struct ev_io *head; |
180 | struct ev_io *head; |
144 | int events; |
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
145 | } ANFD; |
183 | } ANFD; |
146 | |
184 | |
147 | static ANFD *anfds; |
185 | static ANFD *anfds; |
148 | static int anfdmax; |
186 | static int anfdmax; |
149 | |
187 | |
… | |
… | |
152 | { |
190 | { |
153 | while (count--) |
191 | while (count--) |
154 | { |
192 | { |
155 | base->head = 0; |
193 | base->head = 0; |
156 | base->events = EV_NONE; |
194 | base->events = EV_NONE; |
|
|
195 | base->reify = 0; |
|
|
196 | |
157 | ++base; |
197 | ++base; |
158 | } |
198 | } |
159 | } |
199 | } |
160 | |
200 | |
161 | typedef struct |
201 | typedef struct |
162 | { |
202 | { |
163 | W w; |
203 | W w; |
164 | int events; |
204 | int events; |
165 | } ANPENDING; |
205 | } ANPENDING; |
166 | |
206 | |
167 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
168 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
169 | |
209 | |
170 | static void |
210 | static void |
171 | event (W w, int events) |
211 | event (W w, int events) |
172 | { |
212 | { |
173 | if (w->pending) |
213 | if (w->pending) |
174 | { |
214 | { |
175 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
176 | return; |
216 | return; |
177 | } |
217 | } |
178 | |
218 | |
179 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
180 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
181 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
182 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
183 | } |
223 | } |
184 | |
224 | |
185 | static void |
225 | static void |
186 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
187 | { |
227 | { |
… | |
… | |
225 | int events = 0; |
265 | int events = 0; |
226 | |
266 | |
227 | for (w = anfd->head; w; w = w->next) |
267 | for (w = anfd->head; w; w = w->next) |
228 | events |= w->events; |
268 | events |= w->events; |
229 | |
269 | |
230 | anfd->events &= ~EV_REIFY; |
270 | anfd->reify = 0; |
231 | |
271 | |
232 | if (anfd->events != events) |
272 | if (anfd->events != events) |
233 | { |
273 | { |
234 | method_modify (fd, anfd->events, events); |
274 | method_modify (fd, anfd->events, events); |
235 | anfd->events = events; |
275 | anfd->events = events; |
… | |
… | |
240 | } |
280 | } |
241 | |
281 | |
242 | static void |
282 | static void |
243 | fd_change (int fd) |
283 | fd_change (int fd) |
244 | { |
284 | { |
245 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
246 | return; |
286 | return; |
247 | |
287 | |
248 | anfds [fd].events |= EV_REIFY; |
288 | anfds [fd].reify = 1; |
249 | |
289 | |
250 | ++fdchangecnt; |
290 | ++fdchangecnt; |
251 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
252 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
253 | } |
293 | } |
254 | |
294 | |
|
|
295 | static void |
|
|
296 | fd_kill (int fd) |
|
|
297 | { |
|
|
298 | struct ev_io *w; |
|
|
299 | |
|
|
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
|
|
302 | { |
|
|
303 | ev_io_stop (w); |
|
|
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
255 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
256 | static void |
309 | static void |
257 | fd_recheck (void) |
310 | fd_ebadf (void) |
258 | { |
311 | { |
259 | int fd; |
312 | int fd; |
260 | |
313 | |
261 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
262 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
264 | while (anfds [fd].head) |
317 | fd_kill (fd); |
|
|
318 | } |
|
|
319 | |
|
|
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
321 | static void |
|
|
322 | fd_enomem (void) |
|
|
323 | { |
|
|
324 | int fd = anfdmax; |
|
|
325 | |
|
|
326 | while (fd--) |
|
|
327 | if (anfds [fd].events) |
265 | { |
328 | { |
266 | ev_io_stop (anfds [fd].head); |
329 | close (fd); |
267 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
330 | fd_kill (fd); |
|
|
331 | return; |
268 | } |
332 | } |
269 | } |
333 | } |
270 | |
334 | |
271 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
272 | |
336 | |
273 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
320 | /*****************************************************************************/ |
384 | /*****************************************************************************/ |
321 | |
385 | |
322 | typedef struct |
386 | typedef struct |
323 | { |
387 | { |
324 | struct ev_signal *head; |
388 | struct ev_signal *head; |
325 | sig_atomic_t gotsig; |
389 | sig_atomic_t volatile gotsig; |
326 | } ANSIG; |
390 | } ANSIG; |
327 | |
391 | |
328 | static ANSIG *signals; |
392 | static ANSIG *signals; |
329 | static int signalmax; |
393 | static int signalmax; |
330 | |
394 | |
331 | static int sigpipe [2]; |
395 | static int sigpipe [2]; |
332 | static sig_atomic_t gotsig; |
396 | static sig_atomic_t volatile gotsig; |
333 | static struct ev_io sigev; |
397 | static struct ev_io sigev; |
334 | |
398 | |
335 | static void |
399 | static void |
336 | signals_init (ANSIG *base, int count) |
400 | signals_init (ANSIG *base, int count) |
337 | { |
401 | { |
338 | while (count--) |
402 | while (count--) |
339 | { |
403 | { |
340 | base->head = 0; |
404 | base->head = 0; |
341 | base->gotsig = 0; |
405 | base->gotsig = 0; |
|
|
406 | |
342 | ++base; |
407 | ++base; |
343 | } |
408 | } |
344 | } |
409 | } |
345 | |
410 | |
346 | static void |
411 | static void |
… | |
… | |
349 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
350 | |
415 | |
351 | if (!gotsig) |
416 | if (!gotsig) |
352 | { |
417 | { |
353 | gotsig = 1; |
418 | gotsig = 1; |
354 | write (sigpipe [1], &gotsig, 1); |
419 | write (sigpipe [1], &signum, 1); |
355 | } |
420 | } |
356 | } |
421 | } |
357 | |
422 | |
358 | static void |
423 | static void |
359 | sigcb (struct ev_io *iow, int revents) |
424 | sigcb (struct ev_io *iow, int revents) |
360 | { |
425 | { |
361 | struct ev_signal *w; |
426 | struct ev_signal *w; |
362 | int sig; |
427 | int signum; |
363 | |
428 | |
|
|
429 | read (sigpipe [0], &revents, 1); |
364 | gotsig = 0; |
430 | gotsig = 0; |
365 | read (sigpipe [0], &revents, 1); |
|
|
366 | |
431 | |
367 | for (sig = signalmax; sig--; ) |
432 | for (signum = signalmax; signum--; ) |
368 | if (signals [sig].gotsig) |
433 | if (signals [signum].gotsig) |
369 | { |
434 | { |
370 | signals [sig].gotsig = 0; |
435 | signals [signum].gotsig = 0; |
371 | |
436 | |
372 | for (w = signals [sig].head; w; w = w->next) |
437 | for (w = signals [signum].head; w; w = w->next) |
373 | event ((W)w, EV_SIGNAL); |
438 | event ((W)w, EV_SIGNAL); |
374 | } |
439 | } |
375 | } |
440 | } |
376 | |
441 | |
377 | static void |
442 | static void |
378 | siginit (void) |
443 | siginit (void) |
379 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
380 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
381 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
382 | |
448 | |
383 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
384 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
385 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
386 | |
453 | |
387 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
388 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
389 | } |
456 | } |
390 | |
457 | |
… | |
… | |
402 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
403 | |
470 | |
404 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
405 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
406 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
407 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
408 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
409 | #endif |
478 | #endif |
410 | |
479 | |
411 | static void |
480 | static void |
|
|
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
482 | { |
|
|
483 | struct ev_child *w; |
|
|
484 | |
|
|
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
486 | if (w->pid == pid || !w->pid) |
|
|
487 | { |
|
|
488 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
489 | w->rpid = pid; |
|
|
490 | w->rstatus = status; |
|
|
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
412 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
413 | { |
498 | { |
414 | struct ev_child *w; |
|
|
415 | int pid, status; |
499 | int pid, status; |
416 | |
500 | |
|
|
501 | printf ("chld %x\n", revents);//D |
417 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
418 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
503 | { |
419 | if (w->pid == pid || w->pid == -1) |
504 | /* make sure we are called again until all childs have been reaped */ |
420 | { |
|
|
421 | w->status = status; |
|
|
422 | event ((W)w, EV_CHILD); |
505 | event ((W)sw, EV_SIGNAL); |
423 | } |
506 | |
|
|
507 | child_reap (sw, pid, pid, status); |
|
|
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
509 | } |
424 | } |
510 | } |
|
|
511 | |
|
|
512 | #endif |
425 | |
513 | |
426 | /*****************************************************************************/ |
514 | /*****************************************************************************/ |
427 | |
515 | |
|
|
516 | #if EV_USE_KQUEUE |
|
|
517 | # include "ev_kqueue.c" |
|
|
518 | #endif |
428 | #if EV_USE_EPOLL |
519 | #if EV_USE_EPOLL |
429 | # include "ev_epoll.c" |
520 | # include "ev_epoll.c" |
430 | #endif |
521 | #endif |
|
|
522 | #if EV_USE_POLL |
|
|
523 | # include "ev_poll.c" |
|
|
524 | #endif |
431 | #if EV_USE_SELECT |
525 | #if EV_USE_SELECT |
432 | # include "ev_select.c" |
526 | # include "ev_select.c" |
433 | #endif |
527 | #endif |
434 | |
528 | |
435 | int |
529 | int |
… | |
… | |
442 | ev_version_minor (void) |
536 | ev_version_minor (void) |
443 | { |
537 | { |
444 | return EV_VERSION_MINOR; |
538 | return EV_VERSION_MINOR; |
445 | } |
539 | } |
446 | |
540 | |
|
|
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
542 | static int |
|
|
543 | enable_secure () |
|
|
544 | { |
|
|
545 | return getuid () != geteuid () |
|
|
546 | || getgid () != getegid (); |
|
|
547 | } |
|
|
548 | |
447 | int ev_init (int flags) |
549 | int ev_init (int methods) |
448 | { |
550 | { |
449 | if (!ev_method) |
551 | if (!ev_method) |
450 | { |
552 | { |
451 | #if EV_USE_MONOTONIC |
553 | #if EV_USE_MONOTONIC |
452 | { |
554 | { |
… | |
… | |
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
556 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
455 | have_monotonic = 1; |
557 | have_monotonic = 1; |
456 | } |
558 | } |
457 | #endif |
559 | #endif |
458 | |
560 | |
459 | ev_now = ev_time (); |
561 | ev_now = ev_time (); |
460 | now = get_clock (); |
562 | now = get_clock (); |
|
|
563 | now_floor = now; |
461 | diff = ev_now - now; |
564 | diff = ev_now - now; |
462 | |
565 | |
463 | if (pipe (sigpipe)) |
566 | if (pipe (sigpipe)) |
464 | return 0; |
567 | return 0; |
465 | |
568 | |
|
|
569 | if (methods == EVMETHOD_AUTO) |
|
|
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
572 | else |
466 | ev_method = EVMETHOD_NONE; |
573 | methods = EVMETHOD_ANY; |
|
|
574 | |
|
|
575 | ev_method = 0; |
|
|
576 | #if EV_USE_KQUEUE |
|
|
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
578 | #endif |
467 | #if EV_USE_EPOLL |
579 | #if EV_USE_EPOLL |
468 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
581 | #endif |
|
|
582 | #if EV_USE_POLL |
|
|
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
469 | #endif |
584 | #endif |
470 | #if EV_USE_SELECT |
585 | #if EV_USE_SELECT |
471 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
472 | #endif |
587 | #endif |
473 | |
588 | |
474 | if (ev_method) |
589 | if (ev_method) |
475 | { |
590 | { |
476 | ev_watcher_init (&sigev, sigcb); |
591 | ev_watcher_init (&sigev, sigcb); |
|
|
592 | ev_set_priority (&sigev, EV_MAXPRI); |
477 | siginit (); |
593 | siginit (); |
478 | |
594 | |
|
|
595 | #ifndef WIN32 |
479 | ev_signal_init (&childev, childcb, SIGCHLD); |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
597 | ev_set_priority (&childev, EV_MAXPRI); |
480 | ev_signal_start (&childev); |
598 | ev_signal_start (&childev); |
|
|
599 | #endif |
481 | } |
600 | } |
482 | } |
601 | } |
483 | |
602 | |
484 | return ev_method; |
603 | return ev_method; |
485 | } |
604 | } |
486 | |
605 | |
487 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
488 | |
607 | |
489 | void |
608 | void |
490 | ev_prefork (void) |
609 | ev_fork_prepare (void) |
491 | { |
610 | { |
492 | /* nop */ |
611 | /* nop */ |
493 | } |
612 | } |
494 | |
613 | |
495 | void |
614 | void |
496 | ev_postfork_parent (void) |
615 | ev_fork_parent (void) |
497 | { |
616 | { |
498 | /* nop */ |
617 | /* nop */ |
499 | } |
618 | } |
500 | |
619 | |
501 | void |
620 | void |
502 | ev_postfork_child (void) |
621 | ev_fork_child (void) |
503 | { |
622 | { |
504 | #if EV_USE_EPOLL |
623 | #if EV_USE_EPOLL |
505 | if (ev_method == EVMETHOD_EPOLL) |
624 | if (ev_method == EVMETHOD_EPOLL) |
506 | epoll_postfork_child (); |
625 | epoll_postfork_child (); |
507 | #endif |
626 | #endif |
… | |
… | |
516 | /*****************************************************************************/ |
635 | /*****************************************************************************/ |
517 | |
636 | |
518 | static void |
637 | static void |
519 | call_pending (void) |
638 | call_pending (void) |
520 | { |
639 | { |
|
|
640 | int pri; |
|
|
641 | |
|
|
642 | for (pri = NUMPRI; pri--; ) |
521 | while (pendingcnt) |
643 | while (pendingcnt [pri]) |
522 | { |
644 | { |
523 | ANPENDING *p = pendings + --pendingcnt; |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
524 | |
646 | |
525 | if (p->w) |
647 | if (p->w) |
526 | { |
648 | { |
527 | p->w->pending = 0; |
649 | p->w->pending = 0; |
528 | p->w->cb (p->w, p->events); |
650 | p->w->cb (p->w, p->events); |
529 | } |
651 | } |
530 | } |
652 | } |
531 | } |
653 | } |
532 | |
654 | |
533 | static void |
655 | static void |
534 | timers_reify (void) |
656 | timers_reify (void) |
535 | { |
657 | { |
… | |
… | |
538 | struct ev_timer *w = timers [0]; |
660 | struct ev_timer *w = timers [0]; |
539 | |
661 | |
540 | /* first reschedule or stop timer */ |
662 | /* first reschedule or stop timer */ |
541 | if (w->repeat) |
663 | if (w->repeat) |
542 | { |
664 | { |
|
|
665 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
543 | w->at = now + w->repeat; |
666 | w->at = now + w->repeat; |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
545 | downheap ((WT *)timers, timercnt, 0); |
667 | downheap ((WT *)timers, timercnt, 0); |
546 | } |
668 | } |
547 | else |
669 | else |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
670 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
549 | |
671 | |
… | |
… | |
560 | |
682 | |
561 | /* first reschedule or stop timer */ |
683 | /* first reschedule or stop timer */ |
562 | if (w->interval) |
684 | if (w->interval) |
563 | { |
685 | { |
564 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
686 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
565 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
687 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
688 | downheap ((WT *)periodics, periodiccnt, 0); |
567 | } |
689 | } |
568 | else |
690 | else |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
691 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
570 | |
692 | |
571 | event ((W)w, EV_TIMEOUT); |
693 | event ((W)w, EV_PERIODIC); |
572 | } |
694 | } |
573 | } |
695 | } |
574 | |
696 | |
575 | static void |
697 | static void |
576 | periodics_reschedule (ev_tstamp diff) |
698 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
595 | } |
717 | } |
596 | } |
718 | } |
597 | } |
719 | } |
598 | } |
720 | } |
599 | |
721 | |
|
|
722 | static int |
|
|
723 | time_update_monotonic (void) |
|
|
724 | { |
|
|
725 | now = get_clock (); |
|
|
726 | |
|
|
727 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
728 | { |
|
|
729 | ev_now = now + diff; |
|
|
730 | return 0; |
|
|
731 | } |
|
|
732 | else |
|
|
733 | { |
|
|
734 | now_floor = now; |
|
|
735 | ev_now = ev_time (); |
|
|
736 | return 1; |
|
|
737 | } |
|
|
738 | } |
|
|
739 | |
600 | static void |
740 | static void |
601 | time_update (void) |
741 | time_update (void) |
602 | { |
742 | { |
603 | int i; |
743 | int i; |
604 | |
744 | |
605 | ev_now = ev_time (); |
745 | #if EV_USE_MONOTONIC |
606 | |
|
|
607 | if (have_monotonic) |
746 | if (expect_true (have_monotonic)) |
608 | { |
747 | { |
609 | ev_tstamp odiff = diff; |
748 | if (time_update_monotonic ()) |
610 | |
|
|
611 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
612 | { |
749 | { |
613 | now = get_clock (); |
750 | ev_tstamp odiff = diff; |
|
|
751 | |
|
|
752 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
753 | { |
614 | diff = ev_now - now; |
754 | diff = ev_now - now; |
615 | |
755 | |
616 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
756 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
617 | return; /* all is well */ |
757 | return; /* all is well */ |
618 | |
758 | |
619 | ev_now = ev_time (); |
759 | ev_now = ev_time (); |
|
|
760 | now = get_clock (); |
|
|
761 | now_floor = now; |
|
|
762 | } |
|
|
763 | |
|
|
764 | periodics_reschedule (diff - odiff); |
|
|
765 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
620 | } |
766 | } |
621 | |
|
|
622 | periodics_reschedule (diff - odiff); |
|
|
623 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
624 | } |
767 | } |
625 | else |
768 | else |
|
|
769 | #endif |
626 | { |
770 | { |
|
|
771 | ev_now = ev_time (); |
|
|
772 | |
627 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
773 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
628 | { |
774 | { |
629 | periodics_reschedule (ev_now - now); |
775 | periodics_reschedule (ev_now - now); |
630 | |
776 | |
631 | /* adjust timers. this is easy, as the offset is the same for all */ |
777 | /* adjust timers. this is easy, as the offset is the same for all */ |
632 | for (i = 0; i < timercnt; ++i) |
778 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
645 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
791 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
646 | |
792 | |
647 | do |
793 | do |
648 | { |
794 | { |
649 | /* queue check watchers (and execute them) */ |
795 | /* queue check watchers (and execute them) */ |
650 | if (preparecnt) |
796 | if (expect_false (preparecnt)) |
651 | { |
797 | { |
652 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
798 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
653 | call_pending (); |
799 | call_pending (); |
654 | } |
800 | } |
655 | |
801 | |
… | |
… | |
658 | |
804 | |
659 | /* calculate blocking time */ |
805 | /* calculate blocking time */ |
660 | |
806 | |
661 | /* we only need this for !monotonic clockor timers, but as we basically |
807 | /* we only need this for !monotonic clockor timers, but as we basically |
662 | always have timers, we just calculate it always */ |
808 | always have timers, we just calculate it always */ |
|
|
809 | #if EV_USE_MONOTONIC |
|
|
810 | if (expect_true (have_monotonic)) |
|
|
811 | time_update_monotonic (); |
|
|
812 | else |
|
|
813 | #endif |
|
|
814 | { |
663 | ev_now = ev_time (); |
815 | ev_now = ev_time (); |
|
|
816 | now = ev_now; |
|
|
817 | } |
664 | |
818 | |
665 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
819 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
666 | block = 0.; |
820 | block = 0.; |
667 | else |
821 | else |
668 | { |
822 | { |
669 | block = MAX_BLOCKTIME; |
823 | block = MAX_BLOCKTIME; |
670 | |
824 | |
671 | if (timercnt) |
825 | if (timercnt) |
672 | { |
826 | { |
673 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
827 | ev_tstamp to = timers [0]->at - now + method_fudge; |
674 | if (block > to) block = to; |
828 | if (block > to) block = to; |
675 | } |
829 | } |
676 | |
830 | |
677 | if (periodiccnt) |
831 | if (periodiccnt) |
678 | { |
832 | { |
… | |
… | |
731 | head = &(*head)->next; |
885 | head = &(*head)->next; |
732 | } |
886 | } |
733 | } |
887 | } |
734 | |
888 | |
735 | static void |
889 | static void |
736 | ev_clear (W w) |
890 | ev_clear_pending (W w) |
737 | { |
891 | { |
738 | if (w->pending) |
892 | if (w->pending) |
739 | { |
893 | { |
740 | pendings [w->pending - 1].w = 0; |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
741 | w->pending = 0; |
895 | w->pending = 0; |
742 | } |
896 | } |
743 | } |
897 | } |
744 | |
898 | |
745 | static void |
899 | static void |
746 | ev_start (W w, int active) |
900 | ev_start (W w, int active) |
747 | { |
901 | { |
|
|
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
904 | |
748 | w->active = active; |
905 | w->active = active; |
749 | } |
906 | } |
750 | |
907 | |
751 | static void |
908 | static void |
752 | ev_stop (W w) |
909 | ev_stop (W w) |
… | |
… | |
757 | /*****************************************************************************/ |
914 | /*****************************************************************************/ |
758 | |
915 | |
759 | void |
916 | void |
760 | ev_io_start (struct ev_io *w) |
917 | ev_io_start (struct ev_io *w) |
761 | { |
918 | { |
|
|
919 | int fd = w->fd; |
|
|
920 | |
762 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
763 | return; |
922 | return; |
764 | |
923 | |
765 | int fd = w->fd; |
924 | assert (("ev_io_start called with negative fd", fd >= 0)); |
766 | |
925 | |
767 | ev_start ((W)w, 1); |
926 | ev_start ((W)w, 1); |
768 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
927 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
769 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
928 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
770 | |
929 | |
… | |
… | |
772 | } |
931 | } |
773 | |
932 | |
774 | void |
933 | void |
775 | ev_io_stop (struct ev_io *w) |
934 | ev_io_stop (struct ev_io *w) |
776 | { |
935 | { |
777 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
778 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
779 | return; |
938 | return; |
780 | |
939 | |
781 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
940 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
782 | ev_stop ((W)w); |
941 | ev_stop ((W)w); |
… | |
… | |
790 | if (ev_is_active (w)) |
949 | if (ev_is_active (w)) |
791 | return; |
950 | return; |
792 | |
951 | |
793 | w->at += now; |
952 | w->at += now; |
794 | |
953 | |
795 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
954 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
796 | |
955 | |
797 | ev_start ((W)w, ++timercnt); |
956 | ev_start ((W)w, ++timercnt); |
798 | array_needsize (timers, timermax, timercnt, ); |
957 | array_needsize (timers, timermax, timercnt, ); |
799 | timers [timercnt - 1] = w; |
958 | timers [timercnt - 1] = w; |
800 | upheap ((WT *)timers, timercnt - 1); |
959 | upheap ((WT *)timers, timercnt - 1); |
801 | } |
960 | } |
802 | |
961 | |
803 | void |
962 | void |
804 | ev_timer_stop (struct ev_timer *w) |
963 | ev_timer_stop (struct ev_timer *w) |
805 | { |
964 | { |
806 | ev_clear ((W)w); |
965 | ev_clear_pending ((W)w); |
807 | if (!ev_is_active (w)) |
966 | if (!ev_is_active (w)) |
808 | return; |
967 | return; |
809 | |
968 | |
810 | if (w->active < timercnt--) |
969 | if (w->active < timercnt--) |
811 | { |
970 | { |
… | |
… | |
839 | ev_periodic_start (struct ev_periodic *w) |
998 | ev_periodic_start (struct ev_periodic *w) |
840 | { |
999 | { |
841 | if (ev_is_active (w)) |
1000 | if (ev_is_active (w)) |
842 | return; |
1001 | return; |
843 | |
1002 | |
844 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
1003 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
845 | |
1004 | |
846 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1005 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
847 | if (w->interval) |
1006 | if (w->interval) |
848 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1007 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
849 | |
1008 | |
… | |
… | |
854 | } |
1013 | } |
855 | |
1014 | |
856 | void |
1015 | void |
857 | ev_periodic_stop (struct ev_periodic *w) |
1016 | ev_periodic_stop (struct ev_periodic *w) |
858 | { |
1017 | { |
859 | ev_clear ((W)w); |
1018 | ev_clear_pending ((W)w); |
860 | if (!ev_is_active (w)) |
1019 | if (!ev_is_active (w)) |
861 | return; |
1020 | return; |
862 | |
1021 | |
863 | if (w->active < periodiccnt--) |
1022 | if (w->active < periodiccnt--) |
864 | { |
1023 | { |
… | |
… | |
867 | } |
1026 | } |
868 | |
1027 | |
869 | ev_stop ((W)w); |
1028 | ev_stop ((W)w); |
870 | } |
1029 | } |
871 | |
1030 | |
|
|
1031 | #ifndef SA_RESTART |
|
|
1032 | # define SA_RESTART 0 |
|
|
1033 | #endif |
|
|
1034 | |
872 | void |
1035 | void |
873 | ev_signal_start (struct ev_signal *w) |
1036 | ev_signal_start (struct ev_signal *w) |
874 | { |
1037 | { |
875 | if (ev_is_active (w)) |
1038 | if (ev_is_active (w)) |
876 | return; |
1039 | return; |
|
|
1040 | |
|
|
1041 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
877 | |
1042 | |
878 | ev_start ((W)w, 1); |
1043 | ev_start ((W)w, 1); |
879 | array_needsize (signals, signalmax, w->signum, signals_init); |
1044 | array_needsize (signals, signalmax, w->signum, signals_init); |
880 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1045 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
881 | |
1046 | |
882 | if (!w->next) |
1047 | if (!w->next) |
883 | { |
1048 | { |
884 | struct sigaction sa; |
1049 | struct sigaction sa; |
885 | sa.sa_handler = sighandler; |
1050 | sa.sa_handler = sighandler; |
886 | sigfillset (&sa.sa_mask); |
1051 | sigfillset (&sa.sa_mask); |
887 | sa.sa_flags = 0; |
1052 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
888 | sigaction (w->signum, &sa, 0); |
1053 | sigaction (w->signum, &sa, 0); |
889 | } |
1054 | } |
890 | } |
1055 | } |
891 | |
1056 | |
892 | void |
1057 | void |
893 | ev_signal_stop (struct ev_signal *w) |
1058 | ev_signal_stop (struct ev_signal *w) |
894 | { |
1059 | { |
895 | ev_clear ((W)w); |
1060 | ev_clear_pending ((W)w); |
896 | if (!ev_is_active (w)) |
1061 | if (!ev_is_active (w)) |
897 | return; |
1062 | return; |
898 | |
1063 | |
899 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1064 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
900 | ev_stop ((W)w); |
1065 | ev_stop ((W)w); |
… | |
… | |
915 | } |
1080 | } |
916 | |
1081 | |
917 | void |
1082 | void |
918 | ev_idle_stop (struct ev_idle *w) |
1083 | ev_idle_stop (struct ev_idle *w) |
919 | { |
1084 | { |
920 | ev_clear ((W)w); |
1085 | ev_clear_pending ((W)w); |
921 | if (ev_is_active (w)) |
1086 | if (ev_is_active (w)) |
922 | return; |
1087 | return; |
923 | |
1088 | |
924 | idles [w->active - 1] = idles [--idlecnt]; |
1089 | idles [w->active - 1] = idles [--idlecnt]; |
925 | ev_stop ((W)w); |
1090 | ev_stop ((W)w); |
… | |
… | |
937 | } |
1102 | } |
938 | |
1103 | |
939 | void |
1104 | void |
940 | ev_prepare_stop (struct ev_prepare *w) |
1105 | ev_prepare_stop (struct ev_prepare *w) |
941 | { |
1106 | { |
942 | ev_clear ((W)w); |
1107 | ev_clear_pending ((W)w); |
943 | if (ev_is_active (w)) |
1108 | if (ev_is_active (w)) |
944 | return; |
1109 | return; |
945 | |
1110 | |
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
1111 | prepares [w->active - 1] = prepares [--preparecnt]; |
947 | ev_stop ((W)w); |
1112 | ev_stop ((W)w); |
… | |
… | |
959 | } |
1124 | } |
960 | |
1125 | |
961 | void |
1126 | void |
962 | ev_check_stop (struct ev_check *w) |
1127 | ev_check_stop (struct ev_check *w) |
963 | { |
1128 | { |
964 | ev_clear ((W)w); |
1129 | ev_clear_pending ((W)w); |
965 | if (ev_is_active (w)) |
1130 | if (ev_is_active (w)) |
966 | return; |
1131 | return; |
967 | |
1132 | |
968 | checks [w->active - 1] = checks [--checkcnt]; |
1133 | checks [w->active - 1] = checks [--checkcnt]; |
969 | ev_stop ((W)w); |
1134 | ev_stop ((W)w); |
… | |
… | |
980 | } |
1145 | } |
981 | |
1146 | |
982 | void |
1147 | void |
983 | ev_child_stop (struct ev_child *w) |
1148 | ev_child_stop (struct ev_child *w) |
984 | { |
1149 | { |
985 | ev_clear ((W)w); |
1150 | ev_clear_pending ((W)w); |
986 | if (ev_is_active (w)) |
1151 | if (ev_is_active (w)) |
987 | return; |
1152 | return; |
988 | |
1153 | |
989 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1154 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
990 | ev_stop ((W)w); |
1155 | ev_stop ((W)w); |