… | |
… | |
32 | #ifdef __cplusplus |
32 | #ifdef __cplusplus |
33 | extern "C" { |
33 | extern "C" { |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
|
|
37 | # ifdef EV_CONFIG_H |
|
|
38 | # include EV_CONFIG_H |
|
|
39 | # else |
37 | # include "config.h" |
40 | # include "config.h" |
|
|
41 | # endif |
38 | |
42 | |
39 | # if HAVE_CLOCK_GETTIME |
43 | # if HAVE_CLOCK_GETTIME |
40 | # ifndef EV_USE_MONOTONIC |
44 | # ifndef EV_USE_MONOTONIC |
41 | # define EV_USE_MONOTONIC 1 |
45 | # define EV_USE_MONOTONIC 1 |
42 | # endif |
46 | # endif |
43 | # ifndef EV_USE_REALTIME |
47 | # ifndef EV_USE_REALTIME |
44 | # define EV_USE_REALTIME 1 |
48 | # define EV_USE_REALTIME 1 |
45 | # endif |
49 | # endif |
|
|
50 | # else |
|
|
51 | # ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 0 |
|
|
53 | # endif |
|
|
54 | # ifndef EV_USE_REALTIME |
|
|
55 | # define EV_USE_REALTIME 0 |
|
|
56 | # endif |
46 | # endif |
57 | # endif |
47 | |
58 | |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
59 | # ifndef EV_USE_SELECT |
|
|
60 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
49 | # define EV_USE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
|
|
62 | # else |
|
|
63 | # define EV_USE_SELECT 0 |
|
|
64 | # endif |
50 | # endif |
65 | # endif |
51 | |
66 | |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
67 | # ifndef EV_USE_POLL |
|
|
68 | # if HAVE_POLL && HAVE_POLL_H |
53 | # define EV_USE_POLL 1 |
69 | # define EV_USE_POLL 1 |
|
|
70 | # else |
|
|
71 | # define EV_USE_POLL 0 |
|
|
72 | # endif |
54 | # endif |
73 | # endif |
55 | |
74 | |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
75 | # ifndef EV_USE_EPOLL |
|
|
76 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
57 | # define EV_USE_EPOLL 1 |
77 | # define EV_USE_EPOLL 1 |
|
|
78 | # else |
|
|
79 | # define EV_USE_EPOLL 0 |
|
|
80 | # endif |
58 | # endif |
81 | # endif |
59 | |
82 | |
|
|
83 | # ifndef EV_USE_KQUEUE |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
84 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
61 | # define EV_USE_KQUEUE 1 |
85 | # define EV_USE_KQUEUE 1 |
|
|
86 | # else |
|
|
87 | # define EV_USE_KQUEUE 0 |
|
|
88 | # endif |
62 | # endif |
89 | # endif |
63 | |
90 | |
64 | # if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) |
91 | # ifndef EV_USE_PORT |
|
|
92 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
65 | # define EV_USE_PORT 1 |
93 | # define EV_USE_PORT 1 |
|
|
94 | # else |
|
|
95 | # define EV_USE_PORT 0 |
|
|
96 | # endif |
66 | # endif |
97 | # endif |
67 | |
98 | |
68 | #endif |
99 | #endif |
69 | |
100 | |
70 | #include <math.h> |
101 | #include <math.h> |
… | |
… | |
80 | #include <time.h> |
111 | #include <time.h> |
81 | |
112 | |
82 | #include <signal.h> |
113 | #include <signal.h> |
83 | |
114 | |
84 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
85 | # include <unistd.h> |
|
|
86 | # include <sys/time.h> |
116 | # include <sys/time.h> |
87 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
88 | #else |
119 | #else |
89 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
90 | # include <windows.h> |
121 | # include <windows.h> |
91 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
92 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
127 | # define EV_USE_PORT 0 |
158 | # define EV_USE_PORT 0 |
128 | #endif |
159 | #endif |
129 | |
160 | |
130 | /**/ |
161 | /**/ |
131 | |
162 | |
132 | /* darwin simply cannot be helped */ |
|
|
133 | #ifdef __APPLE__ |
|
|
134 | # undef EV_USE_POLL |
|
|
135 | # undef EV_USE_KQUEUE |
|
|
136 | #endif |
|
|
137 | |
|
|
138 | #ifndef CLOCK_MONOTONIC |
163 | #ifndef CLOCK_MONOTONIC |
139 | # undef EV_USE_MONOTONIC |
164 | # undef EV_USE_MONOTONIC |
140 | # define EV_USE_MONOTONIC 0 |
165 | # define EV_USE_MONOTONIC 0 |
141 | #endif |
166 | #endif |
142 | |
167 | |
… | |
… | |
162 | # include "ev.h" |
187 | # include "ev.h" |
163 | #endif |
188 | #endif |
164 | |
189 | |
165 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
166 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
192 | # define inline_size static inline /* inline for codesize */ |
|
|
193 | # if EV_MINIMAL |
167 | # define inline inline |
194 | # define noinline __attribute__ ((noinline)) |
|
|
195 | # define inline_speed static noinline |
|
|
196 | # else |
|
|
197 | # define noinline |
|
|
198 | # define inline_speed static inline |
|
|
199 | # endif |
168 | #else |
200 | #else |
169 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
170 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
171 | #endif |
205 | #endif |
172 | |
206 | |
173 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
174 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
175 | |
209 | |
… | |
… | |
177 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
211 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
178 | |
212 | |
179 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
213 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
180 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
214 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
181 | |
215 | |
182 | typedef struct ev_watcher *W; |
216 | typedef ev_watcher *W; |
183 | typedef struct ev_watcher_list *WL; |
217 | typedef ev_watcher_list *WL; |
184 | typedef struct ev_watcher_time *WT; |
218 | typedef ev_watcher_time *WT; |
185 | |
219 | |
186 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
220 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
187 | |
221 | |
188 | #ifdef _WIN32 |
222 | #ifdef _WIN32 |
189 | # include "ev_win32.c" |
223 | # include "ev_win32.c" |
… | |
… | |
191 | |
225 | |
192 | /*****************************************************************************/ |
226 | /*****************************************************************************/ |
193 | |
227 | |
194 | static void (*syserr_cb)(const char *msg); |
228 | static void (*syserr_cb)(const char *msg); |
195 | |
229 | |
|
|
230 | void |
196 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
231 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
197 | { |
232 | { |
198 | syserr_cb = cb; |
233 | syserr_cb = cb; |
199 | } |
234 | } |
200 | |
235 | |
201 | static void |
236 | static void noinline |
202 | syserr (const char *msg) |
237 | syserr (const char *msg) |
203 | { |
238 | { |
204 | if (!msg) |
239 | if (!msg) |
205 | msg = "(libev) system error"; |
240 | msg = "(libev) system error"; |
206 | |
241 | |
… | |
… | |
213 | } |
248 | } |
214 | } |
249 | } |
215 | |
250 | |
216 | static void *(*alloc)(void *ptr, long size); |
251 | static void *(*alloc)(void *ptr, long size); |
217 | |
252 | |
|
|
253 | void |
218 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
254 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
219 | { |
255 | { |
220 | alloc = cb; |
256 | alloc = cb; |
221 | } |
257 | } |
222 | |
258 | |
223 | static void * |
259 | static void * |
… | |
… | |
295 | gettimeofday (&tv, 0); |
331 | gettimeofday (&tv, 0); |
296 | return tv.tv_sec + tv.tv_usec * 1e-6; |
332 | return tv.tv_sec + tv.tv_usec * 1e-6; |
297 | #endif |
333 | #endif |
298 | } |
334 | } |
299 | |
335 | |
300 | inline ev_tstamp |
336 | ev_tstamp inline_size |
301 | get_clock (void) |
337 | get_clock (void) |
302 | { |
338 | { |
303 | #if EV_USE_MONOTONIC |
339 | #if EV_USE_MONOTONIC |
304 | if (expect_true (have_monotonic)) |
340 | if (expect_true (have_monotonic)) |
305 | { |
341 | { |
… | |
… | |
348 | #define array_free(stem, idx) \ |
384 | #define array_free(stem, idx) \ |
349 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
385 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
350 | |
386 | |
351 | /*****************************************************************************/ |
387 | /*****************************************************************************/ |
352 | |
388 | |
353 | static void |
389 | void noinline |
354 | anfds_init (ANFD *base, int count) |
|
|
355 | { |
|
|
356 | while (count--) |
|
|
357 | { |
|
|
358 | base->head = 0; |
|
|
359 | base->events = EV_NONE; |
|
|
360 | base->reify = 0; |
|
|
361 | |
|
|
362 | ++base; |
|
|
363 | } |
|
|
364 | } |
|
|
365 | |
|
|
366 | void |
|
|
367 | ev_feed_event (EV_P_ void *w, int revents) |
390 | ev_feed_event (EV_P_ void *w, int revents) |
368 | { |
391 | { |
369 | W w_ = (W)w; |
392 | W w_ = (W)w; |
370 | |
393 | |
371 | if (w_->pending) |
394 | if (expect_false (w_->pending)) |
372 | { |
395 | { |
373 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
396 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
374 | return; |
397 | return; |
375 | } |
398 | } |
376 | |
399 | |
… | |
… | |
378 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
401 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
379 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
402 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
380 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
403 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
381 | } |
404 | } |
382 | |
405 | |
383 | static void |
406 | void inline_size |
384 | queue_events (EV_P_ W *events, int eventcnt, int type) |
407 | queue_events (EV_P_ W *events, int eventcnt, int type) |
385 | { |
408 | { |
386 | int i; |
409 | int i; |
387 | |
410 | |
388 | for (i = 0; i < eventcnt; ++i) |
411 | for (i = 0; i < eventcnt; ++i) |
389 | ev_feed_event (EV_A_ events [i], type); |
412 | ev_feed_event (EV_A_ events [i], type); |
390 | } |
413 | } |
391 | |
414 | |
392 | inline void |
415 | /*****************************************************************************/ |
|
|
416 | |
|
|
417 | void inline_size |
|
|
418 | anfds_init (ANFD *base, int count) |
|
|
419 | { |
|
|
420 | while (count--) |
|
|
421 | { |
|
|
422 | base->head = 0; |
|
|
423 | base->events = EV_NONE; |
|
|
424 | base->reify = 0; |
|
|
425 | |
|
|
426 | ++base; |
|
|
427 | } |
|
|
428 | } |
|
|
429 | |
|
|
430 | void inline_speed |
393 | fd_event (EV_P_ int fd, int revents) |
431 | fd_event (EV_P_ int fd, int revents) |
394 | { |
432 | { |
395 | ANFD *anfd = anfds + fd; |
433 | ANFD *anfd = anfds + fd; |
396 | struct ev_io *w; |
434 | ev_io *w; |
397 | |
435 | |
398 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
436 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
399 | { |
437 | { |
400 | int ev = w->events & revents; |
438 | int ev = w->events & revents; |
401 | |
439 | |
402 | if (ev) |
440 | if (ev) |
403 | ev_feed_event (EV_A_ (W)w, ev); |
441 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
408 | ev_feed_fd_event (EV_P_ int fd, int revents) |
446 | ev_feed_fd_event (EV_P_ int fd, int revents) |
409 | { |
447 | { |
410 | fd_event (EV_A_ fd, revents); |
448 | fd_event (EV_A_ fd, revents); |
411 | } |
449 | } |
412 | |
450 | |
413 | /*****************************************************************************/ |
451 | void inline_size |
414 | |
|
|
415 | static void |
|
|
416 | fd_reify (EV_P) |
452 | fd_reify (EV_P) |
417 | { |
453 | { |
418 | int i; |
454 | int i; |
419 | |
455 | |
420 | for (i = 0; i < fdchangecnt; ++i) |
456 | for (i = 0; i < fdchangecnt; ++i) |
421 | { |
457 | { |
422 | int fd = fdchanges [i]; |
458 | int fd = fdchanges [i]; |
423 | ANFD *anfd = anfds + fd; |
459 | ANFD *anfd = anfds + fd; |
424 | struct ev_io *w; |
460 | ev_io *w; |
425 | |
461 | |
426 | int events = 0; |
462 | int events = 0; |
427 | |
463 | |
428 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
464 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
429 | events |= w->events; |
465 | events |= w->events; |
430 | |
466 | |
431 | #if EV_SELECT_IS_WINSOCKET |
467 | #if EV_SELECT_IS_WINSOCKET |
432 | if (events) |
468 | if (events) |
433 | { |
469 | { |
… | |
… | |
437 | } |
473 | } |
438 | #endif |
474 | #endif |
439 | |
475 | |
440 | anfd->reify = 0; |
476 | anfd->reify = 0; |
441 | |
477 | |
442 | method_modify (EV_A_ fd, anfd->events, events); |
478 | backend_modify (EV_A_ fd, anfd->events, events); |
443 | anfd->events = events; |
479 | anfd->events = events; |
444 | } |
480 | } |
445 | |
481 | |
446 | fdchangecnt = 0; |
482 | fdchangecnt = 0; |
447 | } |
483 | } |
448 | |
484 | |
449 | static void |
485 | void inline_size |
450 | fd_change (EV_P_ int fd) |
486 | fd_change (EV_P_ int fd) |
451 | { |
487 | { |
452 | if (anfds [fd].reify) |
488 | if (expect_false (anfds [fd].reify)) |
453 | return; |
489 | return; |
454 | |
490 | |
455 | anfds [fd].reify = 1; |
491 | anfds [fd].reify = 1; |
456 | |
492 | |
457 | ++fdchangecnt; |
493 | ++fdchangecnt; |
458 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
494 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
459 | fdchanges [fdchangecnt - 1] = fd; |
495 | fdchanges [fdchangecnt - 1] = fd; |
460 | } |
496 | } |
461 | |
497 | |
462 | static void |
498 | void inline_speed |
463 | fd_kill (EV_P_ int fd) |
499 | fd_kill (EV_P_ int fd) |
464 | { |
500 | { |
465 | struct ev_io *w; |
501 | ev_io *w; |
466 | |
502 | |
467 | while ((w = (struct ev_io *)anfds [fd].head)) |
503 | while ((w = (ev_io *)anfds [fd].head)) |
468 | { |
504 | { |
469 | ev_io_stop (EV_A_ w); |
505 | ev_io_stop (EV_A_ w); |
470 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
506 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
471 | } |
507 | } |
472 | } |
508 | } |
473 | |
509 | |
474 | static int |
510 | int inline_size |
475 | fd_valid (int fd) |
511 | fd_valid (int fd) |
476 | { |
512 | { |
477 | #ifdef _WIN32 |
513 | #ifdef _WIN32 |
478 | return _get_osfhandle (fd) != -1; |
514 | return _get_osfhandle (fd) != -1; |
479 | #else |
515 | #else |
480 | return fcntl (fd, F_GETFD) != -1; |
516 | return fcntl (fd, F_GETFD) != -1; |
481 | #endif |
517 | #endif |
482 | } |
518 | } |
483 | |
519 | |
484 | /* called on EBADF to verify fds */ |
520 | /* called on EBADF to verify fds */ |
485 | static void |
521 | static void noinline |
486 | fd_ebadf (EV_P) |
522 | fd_ebadf (EV_P) |
487 | { |
523 | { |
488 | int fd; |
524 | int fd; |
489 | |
525 | |
490 | for (fd = 0; fd < anfdmax; ++fd) |
526 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
492 | if (!fd_valid (fd) == -1 && errno == EBADF) |
528 | if (!fd_valid (fd) == -1 && errno == EBADF) |
493 | fd_kill (EV_A_ fd); |
529 | fd_kill (EV_A_ fd); |
494 | } |
530 | } |
495 | |
531 | |
496 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
532 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
497 | static void |
533 | static void noinline |
498 | fd_enomem (EV_P) |
534 | fd_enomem (EV_P) |
499 | { |
535 | { |
500 | int fd; |
536 | int fd; |
501 | |
537 | |
502 | for (fd = anfdmax; fd--; ) |
538 | for (fd = anfdmax; fd--; ) |
… | |
… | |
505 | fd_kill (EV_A_ fd); |
541 | fd_kill (EV_A_ fd); |
506 | return; |
542 | return; |
507 | } |
543 | } |
508 | } |
544 | } |
509 | |
545 | |
510 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
546 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
511 | static void |
547 | static void noinline |
512 | fd_rearm_all (EV_P) |
548 | fd_rearm_all (EV_P) |
513 | { |
549 | { |
514 | int fd; |
550 | int fd; |
515 | |
551 | |
516 | /* this should be highly optimised to not do anything but set a flag */ |
552 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
522 | } |
558 | } |
523 | } |
559 | } |
524 | |
560 | |
525 | /*****************************************************************************/ |
561 | /*****************************************************************************/ |
526 | |
562 | |
527 | static void |
563 | void inline_speed |
528 | upheap (WT *heap, int k) |
564 | upheap (WT *heap, int k) |
529 | { |
565 | { |
530 | WT w = heap [k]; |
566 | WT w = heap [k]; |
531 | |
567 | |
532 | while (k && heap [k >> 1]->at > w->at) |
568 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
539 | heap [k] = w; |
575 | heap [k] = w; |
540 | ((W)heap [k])->active = k + 1; |
576 | ((W)heap [k])->active = k + 1; |
541 | |
577 | |
542 | } |
578 | } |
543 | |
579 | |
544 | static void |
580 | void inline_speed |
545 | downheap (WT *heap, int N, int k) |
581 | downheap (WT *heap, int N, int k) |
546 | { |
582 | { |
547 | WT w = heap [k]; |
583 | WT w = heap [k]; |
548 | |
584 | |
549 | while (k < (N >> 1)) |
585 | while (k < (N >> 1)) |
… | |
… | |
563 | |
599 | |
564 | heap [k] = w; |
600 | heap [k] = w; |
565 | ((W)heap [k])->active = k + 1; |
601 | ((W)heap [k])->active = k + 1; |
566 | } |
602 | } |
567 | |
603 | |
568 | inline void |
604 | void inline_size |
569 | adjustheap (WT *heap, int N, int k) |
605 | adjustheap (WT *heap, int N, int k) |
570 | { |
606 | { |
571 | upheap (heap, k); |
607 | upheap (heap, k); |
572 | downheap (heap, N, k); |
608 | downheap (heap, N, k); |
573 | } |
609 | } |
… | |
… | |
583 | static ANSIG *signals; |
619 | static ANSIG *signals; |
584 | static int signalmax; |
620 | static int signalmax; |
585 | |
621 | |
586 | static int sigpipe [2]; |
622 | static int sigpipe [2]; |
587 | static sig_atomic_t volatile gotsig; |
623 | static sig_atomic_t volatile gotsig; |
588 | static struct ev_io sigev; |
624 | static ev_io sigev; |
589 | |
625 | |
590 | static void |
626 | void inline_size |
591 | signals_init (ANSIG *base, int count) |
627 | signals_init (ANSIG *base, int count) |
592 | { |
628 | { |
593 | while (count--) |
629 | while (count--) |
594 | { |
630 | { |
595 | base->head = 0; |
631 | base->head = 0; |
… | |
… | |
615 | write (sigpipe [1], &signum, 1); |
651 | write (sigpipe [1], &signum, 1); |
616 | errno = old_errno; |
652 | errno = old_errno; |
617 | } |
653 | } |
618 | } |
654 | } |
619 | |
655 | |
620 | void |
656 | void noinline |
621 | ev_feed_signal_event (EV_P_ int signum) |
657 | ev_feed_signal_event (EV_P_ int signum) |
622 | { |
658 | { |
623 | WL w; |
659 | WL w; |
624 | |
660 | |
625 | #if EV_MULTIPLICITY |
661 | #if EV_MULTIPLICITY |
… | |
… | |
636 | for (w = signals [signum].head; w; w = w->next) |
672 | for (w = signals [signum].head; w; w = w->next) |
637 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
673 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
638 | } |
674 | } |
639 | |
675 | |
640 | static void |
676 | static void |
641 | sigcb (EV_P_ struct ev_io *iow, int revents) |
677 | sigcb (EV_P_ ev_io *iow, int revents) |
642 | { |
678 | { |
643 | int signum; |
679 | int signum; |
644 | |
680 | |
645 | read (sigpipe [0], &revents, 1); |
681 | read (sigpipe [0], &revents, 1); |
646 | gotsig = 0; |
682 | gotsig = 0; |
… | |
… | |
648 | for (signum = signalmax; signum--; ) |
684 | for (signum = signalmax; signum--; ) |
649 | if (signals [signum].gotsig) |
685 | if (signals [signum].gotsig) |
650 | ev_feed_signal_event (EV_A_ signum + 1); |
686 | ev_feed_signal_event (EV_A_ signum + 1); |
651 | } |
687 | } |
652 | |
688 | |
653 | inline void |
689 | void inline_size |
654 | fd_intern (int fd) |
690 | fd_intern (int fd) |
655 | { |
691 | { |
656 | #ifdef _WIN32 |
692 | #ifdef _WIN32 |
657 | int arg = 1; |
693 | int arg = 1; |
658 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
694 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
660 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
696 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
661 | fcntl (fd, F_SETFL, O_NONBLOCK); |
697 | fcntl (fd, F_SETFL, O_NONBLOCK); |
662 | #endif |
698 | #endif |
663 | } |
699 | } |
664 | |
700 | |
665 | static void |
701 | static void noinline |
666 | siginit (EV_P) |
702 | siginit (EV_P) |
667 | { |
703 | { |
668 | fd_intern (sigpipe [0]); |
704 | fd_intern (sigpipe [0]); |
669 | fd_intern (sigpipe [1]); |
705 | fd_intern (sigpipe [1]); |
670 | |
706 | |
… | |
… | |
673 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
709 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
674 | } |
710 | } |
675 | |
711 | |
676 | /*****************************************************************************/ |
712 | /*****************************************************************************/ |
677 | |
713 | |
678 | static struct ev_child *childs [PID_HASHSIZE]; |
714 | static ev_child *childs [PID_HASHSIZE]; |
679 | |
715 | |
680 | #ifndef _WIN32 |
716 | #ifndef _WIN32 |
681 | |
717 | |
682 | static struct ev_signal childev; |
718 | static ev_signal childev; |
683 | |
719 | |
684 | #ifndef WCONTINUED |
720 | #ifndef WCONTINUED |
685 | # define WCONTINUED 0 |
721 | # define WCONTINUED 0 |
686 | #endif |
722 | #endif |
687 | |
723 | |
688 | static void |
724 | void inline_speed |
689 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
725 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
690 | { |
726 | { |
691 | struct ev_child *w; |
727 | ev_child *w; |
692 | |
728 | |
693 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
729 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
694 | if (w->pid == pid || !w->pid) |
730 | if (w->pid == pid || !w->pid) |
695 | { |
731 | { |
696 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
732 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
697 | w->rpid = pid; |
733 | w->rpid = pid; |
698 | w->rstatus = status; |
734 | w->rstatus = status; |
699 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
735 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
700 | } |
736 | } |
701 | } |
737 | } |
702 | |
738 | |
703 | static void |
739 | static void |
704 | childcb (EV_P_ struct ev_signal *sw, int revents) |
740 | childcb (EV_P_ ev_signal *sw, int revents) |
705 | { |
741 | { |
706 | int pid, status; |
742 | int pid, status; |
707 | |
743 | |
708 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
744 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
709 | { |
745 | { |
710 | /* make sure we are called again until all childs have been reaped */ |
746 | /* make sure we are called again until all childs have been reaped */ |
|
|
747 | /* we need to do it this way so that the callback gets called before we continue */ |
711 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
748 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
712 | |
749 | |
713 | child_reap (EV_A_ sw, pid, pid, status); |
750 | child_reap (EV_A_ sw, pid, pid, status); |
714 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
751 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
715 | } |
752 | } |
716 | } |
753 | } |
717 | |
754 | |
718 | #endif |
755 | #endif |
719 | |
756 | |
… | |
… | |
746 | { |
783 | { |
747 | return EV_VERSION_MINOR; |
784 | return EV_VERSION_MINOR; |
748 | } |
785 | } |
749 | |
786 | |
750 | /* return true if we are running with elevated privileges and should ignore env variables */ |
787 | /* return true if we are running with elevated privileges and should ignore env variables */ |
751 | static int |
788 | int inline_size |
752 | enable_secure (void) |
789 | enable_secure (void) |
753 | { |
790 | { |
754 | #ifdef _WIN32 |
791 | #ifdef _WIN32 |
755 | return 0; |
792 | return 0; |
756 | #else |
793 | #else |
… | |
… | |
758 | || getgid () != getegid (); |
795 | || getgid () != getegid (); |
759 | #endif |
796 | #endif |
760 | } |
797 | } |
761 | |
798 | |
762 | unsigned int |
799 | unsigned int |
763 | ev_method (EV_P) |
800 | ev_supported_backends (void) |
764 | { |
801 | { |
765 | return method; |
802 | unsigned int flags = 0; |
|
|
803 | |
|
|
804 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
|
|
805 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
|
|
806 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
|
|
807 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
|
|
808 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
|
|
809 | |
|
|
810 | return flags; |
|
|
811 | } |
|
|
812 | |
|
|
813 | unsigned int |
|
|
814 | ev_recommended_backends (void) |
|
|
815 | { |
|
|
816 | unsigned int flags = ev_supported_backends (); |
|
|
817 | |
|
|
818 | #ifndef __NetBSD__ |
|
|
819 | /* kqueue is borked on everything but netbsd apparently */ |
|
|
820 | /* it usually doesn't work correctly on anything but sockets and pipes */ |
|
|
821 | flags &= ~EVBACKEND_KQUEUE; |
|
|
822 | #endif |
|
|
823 | #ifdef __APPLE__ |
|
|
824 | // flags &= ~EVBACKEND_KQUEUE; for documentation |
|
|
825 | flags &= ~EVBACKEND_POLL; |
|
|
826 | #endif |
|
|
827 | |
|
|
828 | return flags; |
|
|
829 | } |
|
|
830 | |
|
|
831 | unsigned int |
|
|
832 | ev_embeddable_backends (void) |
|
|
833 | { |
|
|
834 | return EVBACKEND_EPOLL |
|
|
835 | | EVBACKEND_KQUEUE |
|
|
836 | | EVBACKEND_PORT; |
|
|
837 | } |
|
|
838 | |
|
|
839 | unsigned int |
|
|
840 | ev_backend (EV_P) |
|
|
841 | { |
|
|
842 | return backend; |
766 | } |
843 | } |
767 | |
844 | |
768 | static void |
845 | static void |
769 | loop_init (EV_P_ unsigned int flags) |
846 | loop_init (EV_P_ unsigned int flags) |
770 | { |
847 | { |
771 | if (!method) |
848 | if (!backend) |
772 | { |
849 | { |
773 | #if EV_USE_MONOTONIC |
850 | #if EV_USE_MONOTONIC |
774 | { |
851 | { |
775 | struct timespec ts; |
852 | struct timespec ts; |
776 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
853 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
… | |
… | |
781 | ev_rt_now = ev_time (); |
858 | ev_rt_now = ev_time (); |
782 | mn_now = get_clock (); |
859 | mn_now = get_clock (); |
783 | now_floor = mn_now; |
860 | now_floor = mn_now; |
784 | rtmn_diff = ev_rt_now - mn_now; |
861 | rtmn_diff = ev_rt_now - mn_now; |
785 | |
862 | |
786 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
863 | if (!(flags & EVFLAG_NOENV) |
|
|
864 | && !enable_secure () |
|
|
865 | && getenv ("LIBEV_FLAGS")) |
787 | flags = atoi (getenv ("LIBEV_FLAGS")); |
866 | flags = atoi (getenv ("LIBEV_FLAGS")); |
788 | |
867 | |
789 | if (!(flags & 0x0000ffff)) |
868 | if (!(flags & 0x0000ffffUL)) |
790 | flags |= 0x0000ffff; |
869 | flags |= ev_recommended_backends (); |
791 | |
870 | |
792 | method = 0; |
871 | backend = 0; |
793 | #if EV_USE_PORT |
872 | #if EV_USE_PORT |
794 | if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); |
873 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
795 | #endif |
874 | #endif |
796 | #if EV_USE_KQUEUE |
875 | #if EV_USE_KQUEUE |
797 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
876 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
798 | #endif |
877 | #endif |
799 | #if EV_USE_EPOLL |
878 | #if EV_USE_EPOLL |
800 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
879 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
801 | #endif |
880 | #endif |
802 | #if EV_USE_POLL |
881 | #if EV_USE_POLL |
803 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
882 | if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); |
804 | #endif |
883 | #endif |
805 | #if EV_USE_SELECT |
884 | #if EV_USE_SELECT |
806 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
885 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
807 | #endif |
886 | #endif |
808 | |
887 | |
809 | ev_init (&sigev, sigcb); |
888 | ev_init (&sigev, sigcb); |
810 | ev_set_priority (&sigev, EV_MAXPRI); |
889 | ev_set_priority (&sigev, EV_MAXPRI); |
811 | } |
890 | } |
812 | } |
891 | } |
813 | |
892 | |
814 | void |
893 | static void |
815 | loop_destroy (EV_P) |
894 | loop_destroy (EV_P) |
816 | { |
895 | { |
817 | int i; |
896 | int i; |
818 | |
897 | |
819 | #if EV_USE_PORT |
898 | #if EV_USE_PORT |
820 | if (method == EVMETHOD_PORT ) port_destroy (EV_A); |
899 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
821 | #endif |
900 | #endif |
822 | #if EV_USE_KQUEUE |
901 | #if EV_USE_KQUEUE |
823 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
902 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
824 | #endif |
903 | #endif |
825 | #if EV_USE_EPOLL |
904 | #if EV_USE_EPOLL |
826 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
905 | if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); |
827 | #endif |
906 | #endif |
828 | #if EV_USE_POLL |
907 | #if EV_USE_POLL |
829 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
908 | if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); |
830 | #endif |
909 | #endif |
831 | #if EV_USE_SELECT |
910 | #if EV_USE_SELECT |
832 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
911 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
833 | #endif |
912 | #endif |
834 | |
913 | |
835 | for (i = NUMPRI; i--; ) |
914 | for (i = NUMPRI; i--; ) |
836 | array_free (pending, [i]); |
915 | array_free (pending, [i]); |
837 | |
916 | |
838 | /* have to use the microsoft-never-gets-it-right macro */ |
917 | /* have to use the microsoft-never-gets-it-right macro */ |
839 | array_free (fdchange, EMPTY0); |
918 | array_free (fdchange, EMPTY0); |
840 | array_free (timer, EMPTY0); |
919 | array_free (timer, EMPTY0); |
841 | #if EV_PERIODICS |
920 | #if EV_PERIODIC_ENABLE |
842 | array_free (periodic, EMPTY0); |
921 | array_free (periodic, EMPTY0); |
843 | #endif |
922 | #endif |
844 | array_free (idle, EMPTY0); |
923 | array_free (idle, EMPTY0); |
845 | array_free (prepare, EMPTY0); |
924 | array_free (prepare, EMPTY0); |
846 | array_free (check, EMPTY0); |
925 | array_free (check, EMPTY0); |
847 | |
926 | |
848 | method = 0; |
927 | backend = 0; |
849 | } |
928 | } |
850 | |
929 | |
851 | static void |
930 | static void |
852 | loop_fork (EV_P) |
931 | loop_fork (EV_P) |
853 | { |
932 | { |
854 | #if EV_USE_PORT |
933 | #if EV_USE_PORT |
855 | if (method == EVMETHOD_PORT ) port_fork (EV_A); |
934 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
856 | #endif |
935 | #endif |
857 | #if EV_USE_KQUEUE |
936 | #if EV_USE_KQUEUE |
858 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
937 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
859 | #endif |
938 | #endif |
860 | #if EV_USE_EPOLL |
939 | #if EV_USE_EPOLL |
861 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
940 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
862 | #endif |
941 | #endif |
863 | |
942 | |
864 | if (ev_is_active (&sigev)) |
943 | if (ev_is_active (&sigev)) |
865 | { |
944 | { |
866 | /* default loop */ |
945 | /* default loop */ |
… | |
… | |
887 | |
966 | |
888 | memset (loop, 0, sizeof (struct ev_loop)); |
967 | memset (loop, 0, sizeof (struct ev_loop)); |
889 | |
968 | |
890 | loop_init (EV_A_ flags); |
969 | loop_init (EV_A_ flags); |
891 | |
970 | |
892 | if (ev_method (EV_A)) |
971 | if (ev_backend (EV_A)) |
893 | return loop; |
972 | return loop; |
894 | |
973 | |
895 | return 0; |
974 | return 0; |
896 | } |
975 | } |
897 | |
976 | |
… | |
… | |
910 | |
989 | |
911 | #endif |
990 | #endif |
912 | |
991 | |
913 | #if EV_MULTIPLICITY |
992 | #if EV_MULTIPLICITY |
914 | struct ev_loop * |
993 | struct ev_loop * |
915 | ev_default_loop_ (unsigned int flags) |
994 | ev_default_loop_init (unsigned int flags) |
916 | #else |
995 | #else |
917 | int |
996 | int |
918 | ev_default_loop (unsigned int flags) |
997 | ev_default_loop (unsigned int flags) |
919 | #endif |
998 | #endif |
920 | { |
999 | { |
… | |
… | |
930 | ev_default_loop_ptr = 1; |
1009 | ev_default_loop_ptr = 1; |
931 | #endif |
1010 | #endif |
932 | |
1011 | |
933 | loop_init (EV_A_ flags); |
1012 | loop_init (EV_A_ flags); |
934 | |
1013 | |
935 | if (ev_method (EV_A)) |
1014 | if (ev_backend (EV_A)) |
936 | { |
1015 | { |
937 | siginit (EV_A); |
1016 | siginit (EV_A); |
938 | |
1017 | |
939 | #ifndef _WIN32 |
1018 | #ifndef _WIN32 |
940 | ev_signal_init (&childev, childcb, SIGCHLD); |
1019 | ev_signal_init (&childev, childcb, SIGCHLD); |
… | |
… | |
976 | { |
1055 | { |
977 | #if EV_MULTIPLICITY |
1056 | #if EV_MULTIPLICITY |
978 | struct ev_loop *loop = ev_default_loop_ptr; |
1057 | struct ev_loop *loop = ev_default_loop_ptr; |
979 | #endif |
1058 | #endif |
980 | |
1059 | |
981 | if (method) |
1060 | if (backend) |
982 | postfork = 1; |
1061 | postfork = 1; |
983 | } |
1062 | } |
984 | |
1063 | |
985 | /*****************************************************************************/ |
1064 | /*****************************************************************************/ |
986 | |
1065 | |
987 | static int |
1066 | int inline_size |
988 | any_pending (EV_P) |
1067 | any_pending (EV_P) |
989 | { |
1068 | { |
990 | int pri; |
1069 | int pri; |
991 | |
1070 | |
992 | for (pri = NUMPRI; pri--; ) |
1071 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
994 | return 1; |
1073 | return 1; |
995 | |
1074 | |
996 | return 0; |
1075 | return 0; |
997 | } |
1076 | } |
998 | |
1077 | |
999 | inline void |
1078 | void inline_speed |
1000 | call_pending (EV_P) |
1079 | call_pending (EV_P) |
1001 | { |
1080 | { |
1002 | int pri; |
1081 | int pri; |
1003 | |
1082 | |
1004 | for (pri = NUMPRI; pri--; ) |
1083 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1006 | { |
1085 | { |
1007 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1086 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1008 | |
1087 | |
1009 | if (expect_true (p->w)) |
1088 | if (expect_true (p->w)) |
1010 | { |
1089 | { |
|
|
1090 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1091 | |
1011 | p->w->pending = 0; |
1092 | p->w->pending = 0; |
1012 | EV_CB_INVOKE (p->w, p->events); |
1093 | EV_CB_INVOKE (p->w, p->events); |
1013 | } |
1094 | } |
1014 | } |
1095 | } |
1015 | } |
1096 | } |
1016 | |
1097 | |
1017 | static void |
1098 | void inline_size |
1018 | timers_reify (EV_P) |
1099 | timers_reify (EV_P) |
1019 | { |
1100 | { |
1020 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1101 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1021 | { |
1102 | { |
1022 | struct ev_timer *w = timers [0]; |
1103 | ev_timer *w = timers [0]; |
1023 | |
1104 | |
1024 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1105 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1025 | |
1106 | |
1026 | /* first reschedule or stop timer */ |
1107 | /* first reschedule or stop timer */ |
1027 | if (w->repeat) |
1108 | if (w->repeat) |
… | |
… | |
1039 | |
1120 | |
1040 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1121 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1041 | } |
1122 | } |
1042 | } |
1123 | } |
1043 | |
1124 | |
1044 | #if EV_PERIODICS |
1125 | #if EV_PERIODIC_ENABLE |
1045 | static void |
1126 | void inline_size |
1046 | periodics_reify (EV_P) |
1127 | periodics_reify (EV_P) |
1047 | { |
1128 | { |
1048 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1129 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1049 | { |
1130 | { |
1050 | struct ev_periodic *w = periodics [0]; |
1131 | ev_periodic *w = periodics [0]; |
1051 | |
1132 | |
1052 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1133 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1053 | |
1134 | |
1054 | /* first reschedule or stop timer */ |
1135 | /* first reschedule or stop timer */ |
1055 | if (w->reschedule_cb) |
1136 | if (w->reschedule_cb) |
… | |
… | |
1069 | |
1150 | |
1070 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1151 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1071 | } |
1152 | } |
1072 | } |
1153 | } |
1073 | |
1154 | |
1074 | static void |
1155 | static void noinline |
1075 | periodics_reschedule (EV_P) |
1156 | periodics_reschedule (EV_P) |
1076 | { |
1157 | { |
1077 | int i; |
1158 | int i; |
1078 | |
1159 | |
1079 | /* adjust periodics after time jump */ |
1160 | /* adjust periodics after time jump */ |
1080 | for (i = 0; i < periodiccnt; ++i) |
1161 | for (i = 0; i < periodiccnt; ++i) |
1081 | { |
1162 | { |
1082 | struct ev_periodic *w = periodics [i]; |
1163 | ev_periodic *w = periodics [i]; |
1083 | |
1164 | |
1084 | if (w->reschedule_cb) |
1165 | if (w->reschedule_cb) |
1085 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1166 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1086 | else if (w->interval) |
1167 | else if (w->interval) |
1087 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1168 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1091 | for (i = periodiccnt >> 1; i--; ) |
1172 | for (i = periodiccnt >> 1; i--; ) |
1092 | downheap ((WT *)periodics, periodiccnt, i); |
1173 | downheap ((WT *)periodics, periodiccnt, i); |
1093 | } |
1174 | } |
1094 | #endif |
1175 | #endif |
1095 | |
1176 | |
1096 | inline int |
1177 | int inline_size |
1097 | time_update_monotonic (EV_P) |
1178 | time_update_monotonic (EV_P) |
1098 | { |
1179 | { |
1099 | mn_now = get_clock (); |
1180 | mn_now = get_clock (); |
1100 | |
1181 | |
1101 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1182 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1109 | ev_rt_now = ev_time (); |
1190 | ev_rt_now = ev_time (); |
1110 | return 1; |
1191 | return 1; |
1111 | } |
1192 | } |
1112 | } |
1193 | } |
1113 | |
1194 | |
1114 | static void |
1195 | void inline_size |
1115 | time_update (EV_P) |
1196 | time_update (EV_P) |
1116 | { |
1197 | { |
1117 | int i; |
1198 | int i; |
1118 | |
1199 | |
1119 | #if EV_USE_MONOTONIC |
1200 | #if EV_USE_MONOTONIC |
… | |
… | |
1121 | { |
1202 | { |
1122 | if (time_update_monotonic (EV_A)) |
1203 | if (time_update_monotonic (EV_A)) |
1123 | { |
1204 | { |
1124 | ev_tstamp odiff = rtmn_diff; |
1205 | ev_tstamp odiff = rtmn_diff; |
1125 | |
1206 | |
1126 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1207 | /* loop a few times, before making important decisions. |
|
|
1208 | * on the choice of "4": one iteration isn't enough, |
|
|
1209 | * in case we get preempted during the calls to |
|
|
1210 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1211 | * to succeed in that case, though. and looping a few more times |
|
|
1212 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1213 | * in the unlikely event of getting preempted here. |
|
|
1214 | */ |
|
|
1215 | for (i = 4; --i; ) |
1127 | { |
1216 | { |
1128 | rtmn_diff = ev_rt_now - mn_now; |
1217 | rtmn_diff = ev_rt_now - mn_now; |
1129 | |
1218 | |
1130 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1219 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1131 | return; /* all is well */ |
1220 | return; /* all is well */ |
… | |
… | |
1133 | ev_rt_now = ev_time (); |
1222 | ev_rt_now = ev_time (); |
1134 | mn_now = get_clock (); |
1223 | mn_now = get_clock (); |
1135 | now_floor = mn_now; |
1224 | now_floor = mn_now; |
1136 | } |
1225 | } |
1137 | |
1226 | |
1138 | # if EV_PERIODICS |
1227 | # if EV_PERIODIC_ENABLE |
1139 | periodics_reschedule (EV_A); |
1228 | periodics_reschedule (EV_A); |
1140 | # endif |
1229 | # endif |
1141 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1230 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1142 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1231 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1143 | } |
1232 | } |
… | |
… | |
1147 | { |
1236 | { |
1148 | ev_rt_now = ev_time (); |
1237 | ev_rt_now = ev_time (); |
1149 | |
1238 | |
1150 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1239 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1151 | { |
1240 | { |
1152 | #if EV_PERIODICS |
1241 | #if EV_PERIODIC_ENABLE |
1153 | periodics_reschedule (EV_A); |
1242 | periodics_reschedule (EV_A); |
1154 | #endif |
1243 | #endif |
1155 | |
1244 | |
1156 | /* adjust timers. this is easy, as the offset is the same for all */ |
1245 | /* adjust timers. this is easy, as the offset is the same for all */ |
1157 | for (i = 0; i < timercnt; ++i) |
1246 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1177 | static int loop_done; |
1266 | static int loop_done; |
1178 | |
1267 | |
1179 | void |
1268 | void |
1180 | ev_loop (EV_P_ int flags) |
1269 | ev_loop (EV_P_ int flags) |
1181 | { |
1270 | { |
1182 | double block; |
|
|
1183 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1271 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1272 | ? EVUNLOOP_ONE |
|
|
1273 | : EVUNLOOP_CANCEL; |
1184 | |
1274 | |
1185 | while (activecnt) |
1275 | while (activecnt) |
1186 | { |
1276 | { |
1187 | /* queue check watchers (and execute them) */ |
1277 | /* queue check watchers (and execute them) */ |
1188 | if (expect_false (preparecnt)) |
1278 | if (expect_false (preparecnt)) |
… | |
… | |
1197 | |
1287 | |
1198 | /* update fd-related kernel structures */ |
1288 | /* update fd-related kernel structures */ |
1199 | fd_reify (EV_A); |
1289 | fd_reify (EV_A); |
1200 | |
1290 | |
1201 | /* calculate blocking time */ |
1291 | /* calculate blocking time */ |
|
|
1292 | { |
|
|
1293 | double block; |
1202 | |
1294 | |
1203 | /* we only need this for !monotonic clock or timers, but as we basically |
1295 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1204 | always have timers, we just calculate it always */ |
1296 | block = 0.; /* do not block at all */ |
|
|
1297 | else |
|
|
1298 | { |
|
|
1299 | /* update time to cancel out callback processing overhead */ |
1205 | #if EV_USE_MONOTONIC |
1300 | #if EV_USE_MONOTONIC |
1206 | if (expect_true (have_monotonic)) |
1301 | if (expect_true (have_monotonic)) |
1207 | time_update_monotonic (EV_A); |
1302 | time_update_monotonic (EV_A); |
1208 | else |
1303 | else |
1209 | #endif |
1304 | #endif |
1210 | { |
1305 | { |
1211 | ev_rt_now = ev_time (); |
1306 | ev_rt_now = ev_time (); |
1212 | mn_now = ev_rt_now; |
1307 | mn_now = ev_rt_now; |
1213 | } |
1308 | } |
1214 | |
1309 | |
1215 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1216 | block = 0.; |
|
|
1217 | else |
|
|
1218 | { |
|
|
1219 | block = MAX_BLOCKTIME; |
1310 | block = MAX_BLOCKTIME; |
1220 | |
1311 | |
1221 | if (timercnt) |
1312 | if (timercnt) |
1222 | { |
1313 | { |
1223 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1314 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1224 | if (block > to) block = to; |
1315 | if (block > to) block = to; |
1225 | } |
1316 | } |
1226 | |
1317 | |
1227 | #if EV_PERIODICS |
1318 | #if EV_PERIODIC_ENABLE |
1228 | if (periodiccnt) |
1319 | if (periodiccnt) |
1229 | { |
1320 | { |
1230 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1321 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1231 | if (block > to) block = to; |
1322 | if (block > to) block = to; |
1232 | } |
1323 | } |
1233 | #endif |
1324 | #endif |
1234 | |
1325 | |
1235 | if (block < 0.) block = 0.; |
1326 | if (expect_false (block < 0.)) block = 0.; |
1236 | } |
1327 | } |
1237 | |
1328 | |
1238 | method_poll (EV_A_ block); |
1329 | backend_poll (EV_A_ block); |
|
|
1330 | } |
1239 | |
1331 | |
1240 | /* update ev_rt_now, do magic */ |
1332 | /* update ev_rt_now, do magic */ |
1241 | time_update (EV_A); |
1333 | time_update (EV_A); |
1242 | |
1334 | |
1243 | /* queue pending timers and reschedule them */ |
1335 | /* queue pending timers and reschedule them */ |
1244 | timers_reify (EV_A); /* relative timers called last */ |
1336 | timers_reify (EV_A); /* relative timers called last */ |
1245 | #if EV_PERIODICS |
1337 | #if EV_PERIODIC_ENABLE |
1246 | periodics_reify (EV_A); /* absolute timers called first */ |
1338 | periodics_reify (EV_A); /* absolute timers called first */ |
1247 | #endif |
1339 | #endif |
1248 | |
1340 | |
1249 | /* queue idle watchers unless io or timers are pending */ |
1341 | /* queue idle watchers unless other events are pending */ |
1250 | if (idlecnt && !any_pending (EV_A)) |
1342 | if (idlecnt && !any_pending (EV_A)) |
1251 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1343 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1252 | |
1344 | |
1253 | /* queue check watchers, to be executed first */ |
1345 | /* queue check watchers, to be executed first */ |
1254 | if (checkcnt) |
1346 | if (expect_false (checkcnt)) |
1255 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1347 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1256 | |
1348 | |
1257 | call_pending (EV_A); |
1349 | call_pending (EV_A); |
1258 | |
1350 | |
1259 | if (loop_done) |
1351 | if (expect_false (loop_done)) |
1260 | break; |
1352 | break; |
1261 | } |
1353 | } |
1262 | |
1354 | |
1263 | if (loop_done != 2) |
1355 | if (loop_done == EVUNLOOP_ONE) |
1264 | loop_done = 0; |
1356 | loop_done = EVUNLOOP_CANCEL; |
1265 | } |
1357 | } |
1266 | |
1358 | |
1267 | void |
1359 | void |
1268 | ev_unloop (EV_P_ int how) |
1360 | ev_unloop (EV_P_ int how) |
1269 | { |
1361 | { |
1270 | loop_done = how; |
1362 | loop_done = how; |
1271 | } |
1363 | } |
1272 | |
1364 | |
1273 | /*****************************************************************************/ |
1365 | /*****************************************************************************/ |
1274 | |
1366 | |
1275 | inline void |
1367 | void inline_size |
1276 | wlist_add (WL *head, WL elem) |
1368 | wlist_add (WL *head, WL elem) |
1277 | { |
1369 | { |
1278 | elem->next = *head; |
1370 | elem->next = *head; |
1279 | *head = elem; |
1371 | *head = elem; |
1280 | } |
1372 | } |
1281 | |
1373 | |
1282 | inline void |
1374 | void inline_size |
1283 | wlist_del (WL *head, WL elem) |
1375 | wlist_del (WL *head, WL elem) |
1284 | { |
1376 | { |
1285 | while (*head) |
1377 | while (*head) |
1286 | { |
1378 | { |
1287 | if (*head == elem) |
1379 | if (*head == elem) |
… | |
… | |
1292 | |
1384 | |
1293 | head = &(*head)->next; |
1385 | head = &(*head)->next; |
1294 | } |
1386 | } |
1295 | } |
1387 | } |
1296 | |
1388 | |
1297 | inline void |
1389 | void inline_speed |
1298 | ev_clear_pending (EV_P_ W w) |
1390 | ev_clear_pending (EV_P_ W w) |
1299 | { |
1391 | { |
1300 | if (w->pending) |
1392 | if (w->pending) |
1301 | { |
1393 | { |
1302 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1394 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1303 | w->pending = 0; |
1395 | w->pending = 0; |
1304 | } |
1396 | } |
1305 | } |
1397 | } |
1306 | |
1398 | |
1307 | inline void |
1399 | void inline_speed |
1308 | ev_start (EV_P_ W w, int active) |
1400 | ev_start (EV_P_ W w, int active) |
1309 | { |
1401 | { |
1310 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1402 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1311 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1403 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1312 | |
1404 | |
1313 | w->active = active; |
1405 | w->active = active; |
1314 | ev_ref (EV_A); |
1406 | ev_ref (EV_A); |
1315 | } |
1407 | } |
1316 | |
1408 | |
1317 | inline void |
1409 | void inline_size |
1318 | ev_stop (EV_P_ W w) |
1410 | ev_stop (EV_P_ W w) |
1319 | { |
1411 | { |
1320 | ev_unref (EV_A); |
1412 | ev_unref (EV_A); |
1321 | w->active = 0; |
1413 | w->active = 0; |
1322 | } |
1414 | } |
1323 | |
1415 | |
1324 | /*****************************************************************************/ |
1416 | /*****************************************************************************/ |
1325 | |
1417 | |
1326 | void |
1418 | void |
1327 | ev_io_start (EV_P_ struct ev_io *w) |
1419 | ev_io_start (EV_P_ ev_io *w) |
1328 | { |
1420 | { |
1329 | int fd = w->fd; |
1421 | int fd = w->fd; |
1330 | |
1422 | |
1331 | if (ev_is_active (w)) |
1423 | if (expect_false (ev_is_active (w))) |
1332 | return; |
1424 | return; |
1333 | |
1425 | |
1334 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1426 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1335 | |
1427 | |
1336 | ev_start (EV_A_ (W)w, 1); |
1428 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1339 | |
1431 | |
1340 | fd_change (EV_A_ fd); |
1432 | fd_change (EV_A_ fd); |
1341 | } |
1433 | } |
1342 | |
1434 | |
1343 | void |
1435 | void |
1344 | ev_io_stop (EV_P_ struct ev_io *w) |
1436 | ev_io_stop (EV_P_ ev_io *w) |
1345 | { |
1437 | { |
1346 | ev_clear_pending (EV_A_ (W)w); |
1438 | ev_clear_pending (EV_A_ (W)w); |
1347 | if (!ev_is_active (w)) |
1439 | if (expect_false (!ev_is_active (w))) |
1348 | return; |
1440 | return; |
1349 | |
1441 | |
1350 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1442 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1351 | |
1443 | |
1352 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1444 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
… | |
… | |
1354 | |
1446 | |
1355 | fd_change (EV_A_ w->fd); |
1447 | fd_change (EV_A_ w->fd); |
1356 | } |
1448 | } |
1357 | |
1449 | |
1358 | void |
1450 | void |
1359 | ev_timer_start (EV_P_ struct ev_timer *w) |
1451 | ev_timer_start (EV_P_ ev_timer *w) |
1360 | { |
1452 | { |
1361 | if (ev_is_active (w)) |
1453 | if (expect_false (ev_is_active (w))) |
1362 | return; |
1454 | return; |
1363 | |
1455 | |
1364 | ((WT)w)->at += mn_now; |
1456 | ((WT)w)->at += mn_now; |
1365 | |
1457 | |
1366 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1458 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1367 | |
1459 | |
1368 | ev_start (EV_A_ (W)w, ++timercnt); |
1460 | ev_start (EV_A_ (W)w, ++timercnt); |
1369 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1461 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1370 | timers [timercnt - 1] = w; |
1462 | timers [timercnt - 1] = w; |
1371 | upheap ((WT *)timers, timercnt - 1); |
1463 | upheap ((WT *)timers, timercnt - 1); |
1372 | |
1464 | |
1373 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1465 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1374 | } |
1466 | } |
1375 | |
1467 | |
1376 | void |
1468 | void |
1377 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1469 | ev_timer_stop (EV_P_ ev_timer *w) |
1378 | { |
1470 | { |
1379 | ev_clear_pending (EV_A_ (W)w); |
1471 | ev_clear_pending (EV_A_ (W)w); |
1380 | if (!ev_is_active (w)) |
1472 | if (expect_false (!ev_is_active (w))) |
1381 | return; |
1473 | return; |
1382 | |
1474 | |
1383 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1475 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1384 | |
1476 | |
1385 | if (((W)w)->active < timercnt--) |
1477 | if (expect_true (((W)w)->active < timercnt--)) |
1386 | { |
1478 | { |
1387 | timers [((W)w)->active - 1] = timers [timercnt]; |
1479 | timers [((W)w)->active - 1] = timers [timercnt]; |
1388 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1480 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1389 | } |
1481 | } |
1390 | |
1482 | |
… | |
… | |
1392 | |
1484 | |
1393 | ev_stop (EV_A_ (W)w); |
1485 | ev_stop (EV_A_ (W)w); |
1394 | } |
1486 | } |
1395 | |
1487 | |
1396 | void |
1488 | void |
1397 | ev_timer_again (EV_P_ struct ev_timer *w) |
1489 | ev_timer_again (EV_P_ ev_timer *w) |
1398 | { |
1490 | { |
1399 | if (ev_is_active (w)) |
1491 | if (ev_is_active (w)) |
1400 | { |
1492 | { |
1401 | if (w->repeat) |
1493 | if (w->repeat) |
1402 | { |
1494 | { |
… | |
… | |
1411 | w->at = w->repeat; |
1503 | w->at = w->repeat; |
1412 | ev_timer_start (EV_A_ w); |
1504 | ev_timer_start (EV_A_ w); |
1413 | } |
1505 | } |
1414 | } |
1506 | } |
1415 | |
1507 | |
1416 | #if EV_PERIODICS |
1508 | #if EV_PERIODIC_ENABLE |
1417 | void |
1509 | void |
1418 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1510 | ev_periodic_start (EV_P_ ev_periodic *w) |
1419 | { |
1511 | { |
1420 | if (ev_is_active (w)) |
1512 | if (expect_false (ev_is_active (w))) |
1421 | return; |
1513 | return; |
1422 | |
1514 | |
1423 | if (w->reschedule_cb) |
1515 | if (w->reschedule_cb) |
1424 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1516 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1425 | else if (w->interval) |
1517 | else if (w->interval) |
… | |
… | |
1428 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1520 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1429 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1521 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1430 | } |
1522 | } |
1431 | |
1523 | |
1432 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1524 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1433 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1525 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1434 | periodics [periodiccnt - 1] = w; |
1526 | periodics [periodiccnt - 1] = w; |
1435 | upheap ((WT *)periodics, periodiccnt - 1); |
1527 | upheap ((WT *)periodics, periodiccnt - 1); |
1436 | |
1528 | |
1437 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1529 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1438 | } |
1530 | } |
1439 | |
1531 | |
1440 | void |
1532 | void |
1441 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1533 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1442 | { |
1534 | { |
1443 | ev_clear_pending (EV_A_ (W)w); |
1535 | ev_clear_pending (EV_A_ (W)w); |
1444 | if (!ev_is_active (w)) |
1536 | if (expect_false (!ev_is_active (w))) |
1445 | return; |
1537 | return; |
1446 | |
1538 | |
1447 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1539 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1448 | |
1540 | |
1449 | if (((W)w)->active < periodiccnt--) |
1541 | if (expect_true (((W)w)->active < periodiccnt--)) |
1450 | { |
1542 | { |
1451 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1543 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1452 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1544 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1453 | } |
1545 | } |
1454 | |
1546 | |
1455 | ev_stop (EV_A_ (W)w); |
1547 | ev_stop (EV_A_ (W)w); |
1456 | } |
1548 | } |
1457 | |
1549 | |
1458 | void |
1550 | void |
1459 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1551 | ev_periodic_again (EV_P_ ev_periodic *w) |
1460 | { |
1552 | { |
1461 | /* TODO: use adjustheap and recalculation */ |
1553 | /* TODO: use adjustheap and recalculation */ |
1462 | ev_periodic_stop (EV_A_ w); |
1554 | ev_periodic_stop (EV_A_ w); |
1463 | ev_periodic_start (EV_A_ w); |
1555 | ev_periodic_start (EV_A_ w); |
1464 | } |
1556 | } |
1465 | #endif |
1557 | #endif |
1466 | |
1558 | |
1467 | void |
1559 | void |
1468 | ev_idle_start (EV_P_ struct ev_idle *w) |
1560 | ev_idle_start (EV_P_ ev_idle *w) |
1469 | { |
1561 | { |
1470 | if (ev_is_active (w)) |
1562 | if (expect_false (ev_is_active (w))) |
1471 | return; |
1563 | return; |
1472 | |
1564 | |
1473 | ev_start (EV_A_ (W)w, ++idlecnt); |
1565 | ev_start (EV_A_ (W)w, ++idlecnt); |
1474 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1566 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1475 | idles [idlecnt - 1] = w; |
1567 | idles [idlecnt - 1] = w; |
1476 | } |
1568 | } |
1477 | |
1569 | |
1478 | void |
1570 | void |
1479 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1571 | ev_idle_stop (EV_P_ ev_idle *w) |
1480 | { |
1572 | { |
1481 | ev_clear_pending (EV_A_ (W)w); |
1573 | ev_clear_pending (EV_A_ (W)w); |
1482 | if (!ev_is_active (w)) |
1574 | if (expect_false (!ev_is_active (w))) |
1483 | return; |
1575 | return; |
1484 | |
1576 | |
|
|
1577 | { |
|
|
1578 | int active = ((W)w)->active; |
1485 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1579 | idles [active - 1] = idles [--idlecnt]; |
|
|
1580 | ((W)idles [active - 1])->active = active; |
|
|
1581 | } |
|
|
1582 | |
1486 | ev_stop (EV_A_ (W)w); |
1583 | ev_stop (EV_A_ (W)w); |
1487 | } |
1584 | } |
1488 | |
1585 | |
1489 | void |
1586 | void |
1490 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1587 | ev_prepare_start (EV_P_ ev_prepare *w) |
1491 | { |
1588 | { |
1492 | if (ev_is_active (w)) |
1589 | if (expect_false (ev_is_active (w))) |
1493 | return; |
1590 | return; |
1494 | |
1591 | |
1495 | ev_start (EV_A_ (W)w, ++preparecnt); |
1592 | ev_start (EV_A_ (W)w, ++preparecnt); |
1496 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1593 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1497 | prepares [preparecnt - 1] = w; |
1594 | prepares [preparecnt - 1] = w; |
1498 | } |
1595 | } |
1499 | |
1596 | |
1500 | void |
1597 | void |
1501 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1598 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1502 | { |
1599 | { |
1503 | ev_clear_pending (EV_A_ (W)w); |
1600 | ev_clear_pending (EV_A_ (W)w); |
1504 | if (!ev_is_active (w)) |
1601 | if (expect_false (!ev_is_active (w))) |
1505 | return; |
1602 | return; |
1506 | |
1603 | |
|
|
1604 | { |
|
|
1605 | int active = ((W)w)->active; |
1507 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1606 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1607 | ((W)prepares [active - 1])->active = active; |
|
|
1608 | } |
|
|
1609 | |
1508 | ev_stop (EV_A_ (W)w); |
1610 | ev_stop (EV_A_ (W)w); |
1509 | } |
1611 | } |
1510 | |
1612 | |
1511 | void |
1613 | void |
1512 | ev_check_start (EV_P_ struct ev_check *w) |
1614 | ev_check_start (EV_P_ ev_check *w) |
1513 | { |
1615 | { |
1514 | if (ev_is_active (w)) |
1616 | if (expect_false (ev_is_active (w))) |
1515 | return; |
1617 | return; |
1516 | |
1618 | |
1517 | ev_start (EV_A_ (W)w, ++checkcnt); |
1619 | ev_start (EV_A_ (W)w, ++checkcnt); |
1518 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1620 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1519 | checks [checkcnt - 1] = w; |
1621 | checks [checkcnt - 1] = w; |
1520 | } |
1622 | } |
1521 | |
1623 | |
1522 | void |
1624 | void |
1523 | ev_check_stop (EV_P_ struct ev_check *w) |
1625 | ev_check_stop (EV_P_ ev_check *w) |
1524 | { |
1626 | { |
1525 | ev_clear_pending (EV_A_ (W)w); |
1627 | ev_clear_pending (EV_A_ (W)w); |
1526 | if (!ev_is_active (w)) |
1628 | if (expect_false (!ev_is_active (w))) |
1527 | return; |
1629 | return; |
1528 | |
1630 | |
|
|
1631 | { |
|
|
1632 | int active = ((W)w)->active; |
1529 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1633 | checks [active - 1] = checks [--checkcnt]; |
|
|
1634 | ((W)checks [active - 1])->active = active; |
|
|
1635 | } |
|
|
1636 | |
1530 | ev_stop (EV_A_ (W)w); |
1637 | ev_stop (EV_A_ (W)w); |
1531 | } |
1638 | } |
1532 | |
1639 | |
1533 | #ifndef SA_RESTART |
1640 | #ifndef SA_RESTART |
1534 | # define SA_RESTART 0 |
1641 | # define SA_RESTART 0 |
1535 | #endif |
1642 | #endif |
1536 | |
1643 | |
1537 | void |
1644 | void |
1538 | ev_signal_start (EV_P_ struct ev_signal *w) |
1645 | ev_signal_start (EV_P_ ev_signal *w) |
1539 | { |
1646 | { |
1540 | #if EV_MULTIPLICITY |
1647 | #if EV_MULTIPLICITY |
1541 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1648 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1542 | #endif |
1649 | #endif |
1543 | if (ev_is_active (w)) |
1650 | if (expect_false (ev_is_active (w))) |
1544 | return; |
1651 | return; |
1545 | |
1652 | |
1546 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1653 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1547 | |
1654 | |
1548 | ev_start (EV_A_ (W)w, 1); |
1655 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1562 | #endif |
1669 | #endif |
1563 | } |
1670 | } |
1564 | } |
1671 | } |
1565 | |
1672 | |
1566 | void |
1673 | void |
1567 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1674 | ev_signal_stop (EV_P_ ev_signal *w) |
1568 | { |
1675 | { |
1569 | ev_clear_pending (EV_A_ (W)w); |
1676 | ev_clear_pending (EV_A_ (W)w); |
1570 | if (!ev_is_active (w)) |
1677 | if (expect_false (!ev_is_active (w))) |
1571 | return; |
1678 | return; |
1572 | |
1679 | |
1573 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1680 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1574 | ev_stop (EV_A_ (W)w); |
1681 | ev_stop (EV_A_ (W)w); |
1575 | |
1682 | |
1576 | if (!signals [w->signum - 1].head) |
1683 | if (!signals [w->signum - 1].head) |
1577 | signal (w->signum, SIG_DFL); |
1684 | signal (w->signum, SIG_DFL); |
1578 | } |
1685 | } |
1579 | |
1686 | |
1580 | void |
1687 | void |
1581 | ev_child_start (EV_P_ struct ev_child *w) |
1688 | ev_child_start (EV_P_ ev_child *w) |
1582 | { |
1689 | { |
1583 | #if EV_MULTIPLICITY |
1690 | #if EV_MULTIPLICITY |
1584 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1691 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1585 | #endif |
1692 | #endif |
1586 | if (ev_is_active (w)) |
1693 | if (expect_false (ev_is_active (w))) |
1587 | return; |
1694 | return; |
1588 | |
1695 | |
1589 | ev_start (EV_A_ (W)w, 1); |
1696 | ev_start (EV_A_ (W)w, 1); |
1590 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1697 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1591 | } |
1698 | } |
1592 | |
1699 | |
1593 | void |
1700 | void |
1594 | ev_child_stop (EV_P_ struct ev_child *w) |
1701 | ev_child_stop (EV_P_ ev_child *w) |
1595 | { |
1702 | { |
1596 | ev_clear_pending (EV_A_ (W)w); |
1703 | ev_clear_pending (EV_A_ (W)w); |
1597 | if (!ev_is_active (w)) |
1704 | if (expect_false (!ev_is_active (w))) |
1598 | return; |
1705 | return; |
1599 | |
1706 | |
1600 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1707 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1601 | ev_stop (EV_A_ (W)w); |
1708 | ev_stop (EV_A_ (W)w); |
1602 | } |
1709 | } |
1603 | |
1710 | |
|
|
1711 | #if EV_EMBED_ENABLE |
|
|
1712 | void noinline |
|
|
1713 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1714 | { |
|
|
1715 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1716 | } |
|
|
1717 | |
|
|
1718 | static void |
|
|
1719 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1720 | { |
|
|
1721 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1722 | |
|
|
1723 | if (ev_cb (w)) |
|
|
1724 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1725 | else |
|
|
1726 | ev_embed_sweep (loop, w); |
|
|
1727 | } |
|
|
1728 | |
|
|
1729 | void |
|
|
1730 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1731 | { |
|
|
1732 | if (expect_false (ev_is_active (w))) |
|
|
1733 | return; |
|
|
1734 | |
|
|
1735 | { |
|
|
1736 | struct ev_loop *loop = w->loop; |
|
|
1737 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1738 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1742 | ev_io_start (EV_A_ &w->io); |
|
|
1743 | |
|
|
1744 | ev_start (EV_A_ (W)w, 1); |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | void |
|
|
1748 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1749 | { |
|
|
1750 | ev_clear_pending (EV_A_ (W)w); |
|
|
1751 | if (expect_false (!ev_is_active (w))) |
|
|
1752 | return; |
|
|
1753 | |
|
|
1754 | ev_io_stop (EV_A_ &w->io); |
|
|
1755 | |
|
|
1756 | ev_stop (EV_A_ (W)w); |
|
|
1757 | } |
|
|
1758 | #endif |
|
|
1759 | |
|
|
1760 | #if EV_STAT_ENABLE |
|
|
1761 | |
|
|
1762 | # ifdef _WIN32 |
|
|
1763 | # define lstat(a,b) stat(a,b) |
|
|
1764 | # endif |
|
|
1765 | |
|
|
1766 | void |
|
|
1767 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1768 | { |
|
|
1769 | if (lstat (w->path, &w->attr) < 0) |
|
|
1770 | w->attr.st_nlink = 0; |
|
|
1771 | else if (!w->attr.st_nlink) |
|
|
1772 | w->attr.st_nlink = 1; |
|
|
1773 | } |
|
|
1774 | |
|
|
1775 | static void |
|
|
1776 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1777 | { |
|
|
1778 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1779 | |
|
|
1780 | /* we copy this here each the time so that */ |
|
|
1781 | /* prev has the old value when the callback gets invoked */ |
|
|
1782 | w->prev = w->attr; |
|
|
1783 | ev_stat_stat (EV_A_ w); |
|
|
1784 | |
|
|
1785 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1786 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | void |
|
|
1790 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1791 | { |
|
|
1792 | if (expect_false (ev_is_active (w))) |
|
|
1793 | return; |
|
|
1794 | |
|
|
1795 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1796 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1797 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1798 | |
|
|
1799 | ev_stat_stat (EV_A_ w); |
|
|
1800 | |
|
|
1801 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1802 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1803 | ev_timer_start (EV_A_ &w->timer); |
|
|
1804 | |
|
|
1805 | ev_start (EV_A_ (W)w, 1); |
|
|
1806 | } |
|
|
1807 | |
|
|
1808 | void |
|
|
1809 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1810 | { |
|
|
1811 | ev_clear_pending (EV_A_ (W)w); |
|
|
1812 | if (expect_false (!ev_is_active (w))) |
|
|
1813 | return; |
|
|
1814 | |
|
|
1815 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1816 | |
|
|
1817 | ev_stop (EV_A_ (W)w); |
|
|
1818 | } |
|
|
1819 | #endif |
|
|
1820 | |
1604 | /*****************************************************************************/ |
1821 | /*****************************************************************************/ |
1605 | |
1822 | |
1606 | struct ev_once |
1823 | struct ev_once |
1607 | { |
1824 | { |
1608 | struct ev_io io; |
1825 | ev_io io; |
1609 | struct ev_timer to; |
1826 | ev_timer to; |
1610 | void (*cb)(int revents, void *arg); |
1827 | void (*cb)(int revents, void *arg); |
1611 | void *arg; |
1828 | void *arg; |
1612 | }; |
1829 | }; |
1613 | |
1830 | |
1614 | static void |
1831 | static void |
… | |
… | |
1623 | |
1840 | |
1624 | cb (revents, arg); |
1841 | cb (revents, arg); |
1625 | } |
1842 | } |
1626 | |
1843 | |
1627 | static void |
1844 | static void |
1628 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1845 | once_cb_io (EV_P_ ev_io *w, int revents) |
1629 | { |
1846 | { |
1630 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1847 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1631 | } |
1848 | } |
1632 | |
1849 | |
1633 | static void |
1850 | static void |
1634 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1851 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1635 | { |
1852 | { |
1636 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1853 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1637 | } |
1854 | } |
1638 | |
1855 | |
1639 | void |
1856 | void |
1640 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1857 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1641 | { |
1858 | { |
1642 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1859 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1643 | |
1860 | |
1644 | if (!once) |
1861 | if (expect_false (!once)) |
|
|
1862 | { |
1645 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1863 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1646 | else |
1864 | return; |
1647 | { |
1865 | } |
|
|
1866 | |
1648 | once->cb = cb; |
1867 | once->cb = cb; |
1649 | once->arg = arg; |
1868 | once->arg = arg; |
1650 | |
1869 | |
1651 | ev_init (&once->io, once_cb_io); |
1870 | ev_init (&once->io, once_cb_io); |
1652 | if (fd >= 0) |
1871 | if (fd >= 0) |
1653 | { |
1872 | { |
1654 | ev_io_set (&once->io, fd, events); |
1873 | ev_io_set (&once->io, fd, events); |
1655 | ev_io_start (EV_A_ &once->io); |
1874 | ev_io_start (EV_A_ &once->io); |
1656 | } |
1875 | } |
1657 | |
1876 | |
1658 | ev_init (&once->to, once_cb_to); |
1877 | ev_init (&once->to, once_cb_to); |
1659 | if (timeout >= 0.) |
1878 | if (timeout >= 0.) |
1660 | { |
1879 | { |
1661 | ev_timer_set (&once->to, timeout, 0.); |
1880 | ev_timer_set (&once->to, timeout, 0.); |
1662 | ev_timer_start (EV_A_ &once->to); |
1881 | ev_timer_start (EV_A_ &once->to); |
1663 | } |
|
|
1664 | } |
1882 | } |
1665 | } |
1883 | } |
1666 | |
1884 | |
1667 | #ifdef __cplusplus |
1885 | #ifdef __cplusplus |
1668 | } |
1886 | } |