… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (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 |
29 | * 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. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
42 | #include <stdio.h> |
42 | #include <stdio.h> |
43 | |
43 | |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
47 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
48 | #include <sys/time.h> |
50 | #include <sys/time.h> |
49 | #include <time.h> |
51 | #include <time.h> |
50 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
51 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
52 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
53 | #endif |
57 | #endif |
|
|
58 | |
|
|
59 | #ifndef EV_USE_SELECT |
|
|
60 | # define EV_USE_SELECT 1 |
|
|
61 | #endif |
|
|
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 | |
|
|
67 | #ifndef EV_USE_EPOLL |
|
|
68 | # define EV_USE_EPOLL 0 |
|
|
69 | #endif |
|
|
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 | /**/ |
54 | |
80 | |
55 | #ifndef CLOCK_MONOTONIC |
81 | #ifndef CLOCK_MONOTONIC |
56 | # undef EV_USE_MONOTONIC |
82 | # undef EV_USE_MONOTONIC |
57 | # define EV_USE_MONOTONIC 0 |
83 | # define EV_USE_MONOTONIC 0 |
58 | #endif |
84 | #endif |
59 | |
85 | |
60 | #ifndef EV_USE_SELECT |
|
|
61 | # define EV_USE_SELECT 1 |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | #ifndef EV_USE_EPOLL |
|
|
65 | # define EV_USE_EPOLL 0 |
|
|
66 | #endif |
|
|
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
69 | # define EV_USE_REALTIME 0 |
88 | # define EV_USE_REALTIME 0 |
70 | #endif |
89 | #endif |
71 | #ifndef EV_USE_REALTIME |
90 | |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
91 | /**/ |
73 | #endif |
|
|
74 | |
92 | |
75 | #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) */ |
76 | #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) */ |
77 | #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 */ |
78 | #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 */ |
79 | |
97 | |
80 | #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) |
81 | |
113 | |
82 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
83 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
84 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
85 | |
117 | |
86 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
87 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
88 | int ev_method; |
120 | int ev_method; |
89 | |
121 | |
90 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
91 | |
123 | |
… | |
… | |
111 | |
143 | |
112 | static ev_tstamp |
144 | static ev_tstamp |
113 | get_clock (void) |
145 | get_clock (void) |
114 | { |
146 | { |
115 | #if EV_USE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
116 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
117 | { |
149 | { |
118 | struct timespec ts; |
150 | struct timespec ts; |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
121 | } |
153 | } |
… | |
… | |
125 | } |
157 | } |
126 | |
158 | |
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
128 | |
160 | |
129 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
130 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
131 | { \ |
163 | { \ |
132 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
133 | do \ |
165 | do \ |
134 | { \ |
166 | { \ |
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
143 | |
175 | |
144 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
145 | |
177 | |
146 | typedef struct |
178 | typedef struct |
147 | { |
179 | { |
148 | struct ev_io *head; |
180 | struct ev_watcher_list *head; |
149 | unsigned char events; |
181 | unsigned char events; |
150 | unsigned char reify; |
182 | unsigned char reify; |
151 | } ANFD; |
183 | } ANFD; |
152 | |
184 | |
153 | static ANFD *anfds; |
185 | static ANFD *anfds; |
… | |
… | |
170 | { |
202 | { |
171 | W w; |
203 | W w; |
172 | int events; |
204 | int events; |
173 | } ANPENDING; |
205 | } ANPENDING; |
174 | |
206 | |
175 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
176 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
177 | |
209 | |
178 | static void |
210 | static void |
179 | event (W w, int events) |
211 | event (W w, int events) |
180 | { |
212 | { |
181 | if (w->pending) |
213 | if (w->pending) |
182 | { |
214 | { |
183 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
184 | return; |
216 | return; |
185 | } |
217 | } |
186 | |
218 | |
187 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
189 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
190 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
191 | } |
223 | } |
192 | |
224 | |
193 | static void |
225 | static void |
194 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
195 | { |
227 | { |
… | |
… | |
203 | fd_event (int fd, int events) |
235 | fd_event (int fd, int events) |
204 | { |
236 | { |
205 | ANFD *anfd = anfds + fd; |
237 | ANFD *anfd = anfds + fd; |
206 | struct ev_io *w; |
238 | struct ev_io *w; |
207 | |
239 | |
208 | for (w = anfd->head; w; w = w->next) |
240 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
209 | { |
241 | { |
210 | int ev = w->events & events; |
242 | int ev = w->events & events; |
211 | |
243 | |
212 | if (ev) |
244 | if (ev) |
213 | event ((W)w, ev); |
245 | event ((W)w, ev); |
… | |
… | |
230 | ANFD *anfd = anfds + fd; |
262 | ANFD *anfd = anfds + fd; |
231 | struct ev_io *w; |
263 | struct ev_io *w; |
232 | |
264 | |
233 | int events = 0; |
265 | int events = 0; |
234 | |
266 | |
235 | for (w = anfd->head; w; w = w->next) |
267 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
236 | events |= w->events; |
268 | events |= w->events; |
237 | |
269 | |
238 | anfd->reify = 0; |
270 | anfd->reify = 0; |
239 | |
271 | |
240 | if (anfd->events != events) |
272 | if (anfd->events != events) |
… | |
… | |
258 | ++fdchangecnt; |
290 | ++fdchangecnt; |
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
260 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
261 | } |
293 | } |
262 | |
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 | |
263 | /* called on EBADF to verify fds */ |
307 | /* called on EBADF to verify fds */ |
264 | static void |
308 | static void |
265 | fd_recheck (void) |
309 | fd_ebadf (void) |
266 | { |
310 | { |
267 | int fd; |
311 | int fd; |
268 | |
312 | |
269 | for (fd = 0; fd < anfdmax; ++fd) |
313 | for (fd = 0; fd < anfdmax; ++fd) |
270 | if (anfds [fd].events) |
314 | if (anfds [fd].events) |
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
315 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
272 | 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) |
273 | { |
327 | { |
274 | ev_io_stop (anfds [fd].head); |
328 | close (fd); |
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
329 | fd_kill (fd); |
|
|
330 | return; |
276 | } |
331 | } |
277 | } |
332 | } |
278 | |
333 | |
279 | /*****************************************************************************/ |
334 | /*****************************************************************************/ |
280 | |
335 | |
281 | static struct ev_timer **timers; |
336 | static struct ev_timer **timers; |
… | |
… | |
327 | |
382 | |
328 | /*****************************************************************************/ |
383 | /*****************************************************************************/ |
329 | |
384 | |
330 | typedef struct |
385 | typedef struct |
331 | { |
386 | { |
332 | struct ev_signal *head; |
387 | struct ev_watcher_list *head; |
333 | sig_atomic_t volatile gotsig; |
388 | sig_atomic_t volatile gotsig; |
334 | } ANSIG; |
389 | } ANSIG; |
335 | |
390 | |
336 | static ANSIG *signals; |
391 | static ANSIG *signals; |
337 | static int signalmax; |
392 | static int signalmax; |
… | |
… | |
357 | { |
412 | { |
358 | signals [signum - 1].gotsig = 1; |
413 | signals [signum - 1].gotsig = 1; |
359 | |
414 | |
360 | if (!gotsig) |
415 | if (!gotsig) |
361 | { |
416 | { |
|
|
417 | int old_errno = errno; |
362 | gotsig = 1; |
418 | gotsig = 1; |
363 | write (sigpipe [1], &signum, 1); |
419 | write (sigpipe [1], &signum, 1); |
|
|
420 | errno = old_errno; |
364 | } |
421 | } |
365 | } |
422 | } |
366 | |
423 | |
367 | static void |
424 | static void |
368 | sigcb (struct ev_io *iow, int revents) |
425 | sigcb (struct ev_io *iow, int revents) |
369 | { |
426 | { |
370 | struct ev_signal *w; |
427 | struct ev_watcher_list *w; |
371 | int sig; |
428 | int signum; |
372 | |
429 | |
373 | read (sigpipe [0], &revents, 1); |
430 | read (sigpipe [0], &revents, 1); |
374 | gotsig = 0; |
431 | gotsig = 0; |
375 | |
432 | |
376 | for (sig = signalmax; sig--; ) |
433 | for (signum = signalmax; signum--; ) |
377 | if (signals [sig].gotsig) |
434 | if (signals [signum].gotsig) |
378 | { |
435 | { |
379 | signals [sig].gotsig = 0; |
436 | signals [signum].gotsig = 0; |
380 | |
437 | |
381 | for (w = signals [sig].head; w; w = w->next) |
438 | for (w = signals [signum].head; w; w = w->next) |
382 | event ((W)w, EV_SIGNAL); |
439 | event ((W)w, EV_SIGNAL); |
383 | } |
440 | } |
384 | } |
441 | } |
385 | |
442 | |
386 | static void |
443 | static void |
387 | siginit (void) |
444 | siginit (void) |
388 | { |
445 | { |
|
|
446 | #ifndef WIN32 |
389 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
390 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
391 | |
449 | |
392 | /* rather than sort out wether we really need nb, set it */ |
450 | /* rather than sort out wether we really need nb, set it */ |
393 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
394 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
453 | #endif |
395 | |
454 | |
396 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
397 | ev_io_start (&sigev); |
456 | ev_io_start (&sigev); |
398 | } |
457 | } |
399 | |
458 | |
… | |
… | |
411 | /*****************************************************************************/ |
470 | /*****************************************************************************/ |
412 | |
471 | |
413 | static struct ev_child *childs [PID_HASHSIZE]; |
472 | static struct ev_child *childs [PID_HASHSIZE]; |
414 | static struct ev_signal childev; |
473 | static struct ev_signal childev; |
415 | |
474 | |
|
|
475 | #ifndef WIN32 |
|
|
476 | |
416 | #ifndef WCONTINUED |
477 | #ifndef WCONTINUED |
417 | # define WCONTINUED 0 |
478 | # define WCONTINUED 0 |
418 | #endif |
479 | #endif |
419 | |
480 | |
420 | 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 |
421 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
422 | { |
498 | { |
423 | struct ev_child *w; |
|
|
424 | int pid, status; |
499 | int pid, status; |
425 | |
500 | |
426 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
501 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
427 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
502 | { |
428 | if (w->pid == pid || w->pid == -1) |
503 | /* make sure we are called again until all childs have been reaped */ |
429 | { |
|
|
430 | w->status = status; |
|
|
431 | event ((W)w, EV_CHILD); |
504 | event ((W)sw, EV_SIGNAL); |
432 | } |
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 | } |
433 | } |
509 | } |
|
|
510 | |
|
|
511 | #endif |
434 | |
512 | |
435 | /*****************************************************************************/ |
513 | /*****************************************************************************/ |
436 | |
514 | |
|
|
515 | #if EV_USE_KQUEUE |
|
|
516 | # include "ev_kqueue.c" |
|
|
517 | #endif |
437 | #if EV_USE_EPOLL |
518 | #if EV_USE_EPOLL |
438 | # include "ev_epoll.c" |
519 | # include "ev_epoll.c" |
439 | #endif |
520 | #endif |
|
|
521 | #if EV_USE_POLL |
|
|
522 | # include "ev_poll.c" |
|
|
523 | #endif |
440 | #if EV_USE_SELECT |
524 | #if EV_USE_SELECT |
441 | # include "ev_select.c" |
525 | # include "ev_select.c" |
442 | #endif |
526 | #endif |
443 | |
527 | |
444 | int |
528 | int |
… | |
… | |
451 | ev_version_minor (void) |
535 | ev_version_minor (void) |
452 | { |
536 | { |
453 | return EV_VERSION_MINOR; |
537 | return EV_VERSION_MINOR; |
454 | } |
538 | } |
455 | |
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 | |
456 | int ev_init (int flags) |
552 | int ev_init (int methods) |
457 | { |
553 | { |
458 | if (!ev_method) |
554 | if (!ev_method) |
459 | { |
555 | { |
460 | #if EV_USE_MONOTONIC |
556 | #if EV_USE_MONOTONIC |
461 | { |
557 | { |
… | |
… | |
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
559 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
464 | have_monotonic = 1; |
560 | have_monotonic = 1; |
465 | } |
561 | } |
466 | #endif |
562 | #endif |
467 | |
563 | |
468 | ev_now = ev_time (); |
564 | ev_now = ev_time (); |
469 | now = get_clock (); |
565 | now = get_clock (); |
|
|
566 | now_floor = now; |
470 | diff = ev_now - now; |
567 | diff = ev_now - now; |
471 | |
568 | |
472 | if (pipe (sigpipe)) |
569 | if (pipe (sigpipe)) |
473 | return 0; |
570 | return 0; |
474 | |
571 | |
|
|
572 | if (methods == EVMETHOD_AUTO) |
|
|
573 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
574 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
575 | else |
475 | 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 |
476 | #if EV_USE_EPOLL |
582 | #if EV_USE_EPOLL |
477 | 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); |
478 | #endif |
587 | #endif |
479 | #if EV_USE_SELECT |
588 | #if EV_USE_SELECT |
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
589 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
481 | #endif |
590 | #endif |
482 | |
591 | |
483 | if (ev_method) |
592 | if (ev_method) |
484 | { |
593 | { |
485 | ev_watcher_init (&sigev, sigcb); |
594 | ev_watcher_init (&sigev, sigcb); |
|
|
595 | ev_set_priority (&sigev, EV_MAXPRI); |
486 | siginit (); |
596 | siginit (); |
487 | |
597 | |
|
|
598 | #ifndef WIN32 |
488 | ev_signal_init (&childev, childcb, SIGCHLD); |
599 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
600 | ev_set_priority (&childev, EV_MAXPRI); |
489 | ev_signal_start (&childev); |
601 | ev_signal_start (&childev); |
|
|
602 | #endif |
490 | } |
603 | } |
491 | } |
604 | } |
492 | |
605 | |
493 | return ev_method; |
606 | return ev_method; |
494 | } |
607 | } |
… | |
… | |
525 | /*****************************************************************************/ |
638 | /*****************************************************************************/ |
526 | |
639 | |
527 | static void |
640 | static void |
528 | call_pending (void) |
641 | call_pending (void) |
529 | { |
642 | { |
|
|
643 | int pri; |
|
|
644 | |
|
|
645 | for (pri = NUMPRI; pri--; ) |
530 | while (pendingcnt) |
646 | while (pendingcnt [pri]) |
531 | { |
647 | { |
532 | ANPENDING *p = pendings + --pendingcnt; |
648 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
533 | |
649 | |
534 | if (p->w) |
650 | if (p->w) |
535 | { |
651 | { |
536 | p->w->pending = 0; |
652 | p->w->pending = 0; |
537 | p->w->cb (p->w, p->events); |
653 | p->w->cb (p->w, p->events); |
538 | } |
654 | } |
539 | } |
655 | } |
540 | } |
656 | } |
541 | |
657 | |
542 | static void |
658 | static void |
543 | timers_reify (void) |
659 | timers_reify (void) |
544 | { |
660 | { |
… | |
… | |
604 | } |
720 | } |
605 | } |
721 | } |
606 | } |
722 | } |
607 | } |
723 | } |
608 | |
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 | |
609 | static void |
743 | static void |
610 | time_update (void) |
744 | time_update (void) |
611 | { |
745 | { |
612 | int i; |
746 | int i; |
613 | |
747 | |
614 | ev_now = ev_time (); |
748 | #if EV_USE_MONOTONIC |
615 | |
|
|
616 | if (have_monotonic) |
749 | if (expect_true (have_monotonic)) |
617 | { |
750 | { |
618 | ev_tstamp odiff = diff; |
751 | if (time_update_monotonic ()) |
619 | |
|
|
620 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
621 | { |
752 | { |
622 | now = get_clock (); |
753 | ev_tstamp odiff = diff; |
|
|
754 | |
|
|
755 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
756 | { |
623 | diff = ev_now - now; |
757 | diff = ev_now - now; |
624 | |
758 | |
625 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
759 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
626 | return; /* all is well */ |
760 | return; /* all is well */ |
627 | |
761 | |
628 | 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 */ |
629 | } |
769 | } |
630 | |
|
|
631 | periodics_reschedule (diff - odiff); |
|
|
632 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
633 | } |
770 | } |
634 | else |
771 | else |
|
|
772 | #endif |
635 | { |
773 | { |
|
|
774 | ev_now = ev_time (); |
|
|
775 | |
636 | 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)) |
637 | { |
777 | { |
638 | periodics_reschedule (ev_now - now); |
778 | periodics_reschedule (ev_now - now); |
639 | |
779 | |
640 | /* 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 */ |
641 | for (i = 0; i < timercnt; ++i) |
781 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
654 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
794 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
655 | |
795 | |
656 | do |
796 | do |
657 | { |
797 | { |
658 | /* queue check watchers (and execute them) */ |
798 | /* queue check watchers (and execute them) */ |
659 | if (preparecnt) |
799 | if (expect_false (preparecnt)) |
660 | { |
800 | { |
661 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
801 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
662 | call_pending (); |
802 | call_pending (); |
663 | } |
803 | } |
664 | |
804 | |
… | |
… | |
667 | |
807 | |
668 | /* calculate blocking time */ |
808 | /* calculate blocking time */ |
669 | |
809 | |
670 | /* 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 |
671 | 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 | { |
672 | ev_now = ev_time (); |
818 | ev_now = ev_time (); |
|
|
819 | now = ev_now; |
|
|
820 | } |
673 | |
821 | |
674 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
822 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
675 | block = 0.; |
823 | block = 0.; |
676 | else |
824 | else |
677 | { |
825 | { |
678 | block = MAX_BLOCKTIME; |
826 | block = MAX_BLOCKTIME; |
679 | |
827 | |
680 | if (timercnt) |
828 | if (timercnt) |
681 | { |
829 | { |
682 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
830 | ev_tstamp to = timers [0]->at - now + method_fudge; |
683 | if (block > to) block = to; |
831 | if (block > to) block = to; |
684 | } |
832 | } |
685 | |
833 | |
686 | if (periodiccnt) |
834 | if (periodiccnt) |
687 | { |
835 | { |
… | |
… | |
744 | static void |
892 | static void |
745 | ev_clear_pending (W w) |
893 | ev_clear_pending (W w) |
746 | { |
894 | { |
747 | if (w->pending) |
895 | if (w->pending) |
748 | { |
896 | { |
749 | pendings [w->pending - 1].w = 0; |
897 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
750 | w->pending = 0; |
898 | w->pending = 0; |
751 | } |
899 | } |
752 | } |
900 | } |
753 | |
901 | |
754 | static void |
902 | static void |
755 | ev_start (W w, int active) |
903 | ev_start (W w, int active) |
756 | { |
904 | { |
|
|
905 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
906 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
907 | |
757 | w->active = active; |
908 | w->active = active; |
758 | } |
909 | } |
759 | |
910 | |
760 | static void |
911 | static void |
761 | ev_stop (W w) |
912 | ev_stop (W w) |
… | |
… | |
878 | } |
1029 | } |
879 | |
1030 | |
880 | ev_stop ((W)w); |
1031 | ev_stop ((W)w); |
881 | } |
1032 | } |
882 | |
1033 | |
|
|
1034 | #ifndef SA_RESTART |
|
|
1035 | # define SA_RESTART 0 |
|
|
1036 | #endif |
|
|
1037 | |
883 | void |
1038 | void |
884 | ev_signal_start (struct ev_signal *w) |
1039 | ev_signal_start (struct ev_signal *w) |
885 | { |
1040 | { |
886 | if (ev_is_active (w)) |
1041 | if (ev_is_active (w)) |
887 | return; |
1042 | return; |
… | |
… | |
895 | if (!w->next) |
1050 | if (!w->next) |
896 | { |
1051 | { |
897 | struct sigaction sa; |
1052 | struct sigaction sa; |
898 | sa.sa_handler = sighandler; |
1053 | sa.sa_handler = sighandler; |
899 | sigfillset (&sa.sa_mask); |
1054 | sigfillset (&sa.sa_mask); |
900 | sa.sa_flags = 0; |
1055 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
901 | sigaction (w->signum, &sa, 0); |
1056 | sigaction (w->signum, &sa, 0); |
902 | } |
1057 | } |
903 | } |
1058 | } |
904 | |
1059 | |
905 | void |
1060 | void |