… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
130 | |
|
|
131 | #ifdef EV_H |
|
|
132 | # include EV_H |
|
|
133 | #else |
131 | #include "ev.h" |
134 | # include "ev.h" |
|
|
135 | #endif |
132 | |
136 | |
133 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
138 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
139 | # define inline inline |
136 | #else |
140 | #else |
… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
152 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
153 | typedef struct ev_watcher_time *WT; |
150 | |
154 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
155 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
156 | |
153 | #if WIN32 |
157 | #include "ev_win32.c" |
154 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
155 | /* MSDN says this is required to handle SIGFPE */ |
|
|
156 | volatile double SIGFPE_REQ = 0.0f; |
|
|
157 | |
|
|
158 | static int |
|
|
159 | ev_socketpair_tcp (int filedes [2]) |
|
|
160 | { |
|
|
161 | struct sockaddr_in addr = { 0 }; |
|
|
162 | int addr_size = sizeof (addr); |
|
|
163 | SOCKET listener; |
|
|
164 | SOCKET sock [2] = { -1, -1 }; |
|
|
165 | |
|
|
166 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
167 | return -1; |
|
|
168 | |
|
|
169 | addr.sin_family = AF_INET; |
|
|
170 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
|
|
171 | addr.sin_port = 0; |
|
|
172 | |
|
|
173 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
|
|
174 | goto fail; |
|
|
175 | |
|
|
176 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
|
|
177 | goto fail; |
|
|
178 | |
|
|
179 | if (listen (listener, 1)) |
|
|
180 | goto fail; |
|
|
181 | |
|
|
182 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
183 | goto fail; |
|
|
184 | |
|
|
185 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
|
|
186 | goto fail; |
|
|
187 | |
|
|
188 | if ((sock[1] = accept (listener, 0, 0)) < 0) |
|
|
189 | goto fail; |
|
|
190 | |
|
|
191 | closesocket (listener); |
|
|
192 | |
|
|
193 | filedes [0] = sock [0]; |
|
|
194 | filedes [1] = sock [1]; |
|
|
195 | |
|
|
196 | return 0; |
|
|
197 | |
|
|
198 | fail: |
|
|
199 | closesocket (listener); |
|
|
200 | |
|
|
201 | if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
|
|
202 | if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
|
|
203 | |
|
|
204 | return -1; |
|
|
205 | } |
|
|
206 | |
|
|
207 | # define ev_pipe(filedes) ev_socketpair_tcp (filedes) |
|
|
208 | #else |
|
|
209 | # define ev_pipe(filedes) pipe (filedes) |
|
|
210 | #endif |
|
|
211 | |
158 | |
212 | /*****************************************************************************/ |
159 | /*****************************************************************************/ |
213 | |
160 | |
214 | static void (*syserr_cb)(const char *msg); |
161 | static void (*syserr_cb)(const char *msg); |
215 | |
162 | |
… | |
… | |
272 | int events; |
219 | int events; |
273 | } ANPENDING; |
220 | } ANPENDING; |
274 | |
221 | |
275 | #if EV_MULTIPLICITY |
222 | #if EV_MULTIPLICITY |
276 | |
223 | |
277 | struct ev_loop |
224 | struct ev_loop |
278 | { |
225 | { |
279 | # define VAR(name,decl) decl; |
226 | #define VAR(name,decl) decl; |
280 | # include "ev_vars.h" |
227 | #include "ev_vars.h" |
281 | }; |
|
|
282 | # undef VAR |
228 | #undef VAR |
|
|
229 | }; |
283 | # include "ev_wrap.h" |
230 | #include "ev_wrap.h" |
|
|
231 | |
|
|
232 | struct ev_loop default_loop_struct; |
|
|
233 | static struct ev_loop *default_loop; |
284 | |
234 | |
285 | #else |
235 | #else |
286 | |
236 | |
287 | # define VAR(name,decl) static decl; |
237 | #define VAR(name,decl) static decl; |
288 | # include "ev_vars.h" |
238 | #include "ev_vars.h" |
289 | # undef VAR |
239 | #undef VAR |
|
|
240 | |
|
|
241 | static int default_loop; |
290 | |
242 | |
291 | #endif |
243 | #endif |
292 | |
244 | |
293 | /*****************************************************************************/ |
245 | /*****************************************************************************/ |
294 | |
246 | |
… | |
… | |
325 | ev_now (EV_P) |
277 | ev_now (EV_P) |
326 | { |
278 | { |
327 | return rt_now; |
279 | return rt_now; |
328 | } |
280 | } |
329 | |
281 | |
330 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
282 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
331 | |
283 | |
332 | #define array_needsize(base,cur,cnt,init) \ |
284 | #define array_needsize(type,base,cur,cnt,init) \ |
333 | if (expect_false ((cnt) > cur)) \ |
285 | if (expect_false ((cnt) > cur)) \ |
334 | { \ |
286 | { \ |
335 | int newcnt = cur; \ |
287 | int newcnt = cur; \ |
336 | do \ |
288 | do \ |
337 | { \ |
289 | { \ |
338 | newcnt = array_roundsize (base, newcnt << 1); \ |
290 | newcnt = array_roundsize (type, newcnt << 1); \ |
339 | } \ |
291 | } \ |
340 | while ((cnt) > newcnt); \ |
292 | while ((cnt) > newcnt); \ |
341 | \ |
293 | \ |
342 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
343 | init (base + cur, newcnt - cur); \ |
295 | init (base + cur, newcnt - cur); \ |
344 | cur = newcnt; \ |
296 | cur = newcnt; \ |
345 | } |
297 | } |
346 | |
298 | |
347 | #define array_slim(stem) \ |
299 | #define array_slim(type,stem) \ |
348 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
300 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
349 | { \ |
301 | { \ |
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
302 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
304 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
353 | } |
305 | } |
354 | |
306 | |
355 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
307 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
356 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
308 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
373 | |
325 | |
374 | ++base; |
326 | ++base; |
375 | } |
327 | } |
376 | } |
328 | } |
377 | |
329 | |
378 | static void |
330 | void |
379 | event (EV_P_ W w, int events) |
331 | ev_feed_event (EV_P_ void *w, int revents) |
380 | { |
332 | { |
|
|
333 | W w_ = (W)w; |
|
|
334 | |
381 | if (w->pending) |
335 | if (w_->pending) |
382 | { |
336 | { |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
337 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
384 | return; |
338 | return; |
385 | } |
339 | } |
386 | |
340 | |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
341 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
342 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
343 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
344 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
391 | } |
345 | } |
392 | |
346 | |
393 | static void |
347 | static void |
394 | queue_events (EV_P_ W *events, int eventcnt, int type) |
348 | queue_events (EV_P_ W *events, int eventcnt, int type) |
395 | { |
349 | { |
396 | int i; |
350 | int i; |
397 | |
351 | |
398 | for (i = 0; i < eventcnt; ++i) |
352 | for (i = 0; i < eventcnt; ++i) |
399 | event (EV_A_ events [i], type); |
353 | ev_feed_event (EV_A_ events [i], type); |
400 | } |
354 | } |
401 | |
355 | |
402 | static void |
356 | inline void |
403 | fd_event (EV_P_ int fd, int events) |
357 | fd_event (EV_P_ int fd, int revents) |
404 | { |
358 | { |
405 | ANFD *anfd = anfds + fd; |
359 | ANFD *anfd = anfds + fd; |
406 | struct ev_io *w; |
360 | struct ev_io *w; |
407 | |
361 | |
408 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
362 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
409 | { |
363 | { |
410 | int ev = w->events & events; |
364 | int ev = w->events & revents; |
411 | |
365 | |
412 | if (ev) |
366 | if (ev) |
413 | event (EV_A_ (W)w, ev); |
367 | ev_feed_event (EV_A_ (W)w, ev); |
414 | } |
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | void |
|
|
372 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
373 | { |
|
|
374 | fd_event (EV_A_ fd, revents); |
415 | } |
375 | } |
416 | |
376 | |
417 | /*****************************************************************************/ |
377 | /*****************************************************************************/ |
418 | |
378 | |
419 | static void |
379 | static void |
… | |
… | |
448 | return; |
408 | return; |
449 | |
409 | |
450 | anfds [fd].reify = 1; |
410 | anfds [fd].reify = 1; |
451 | |
411 | |
452 | ++fdchangecnt; |
412 | ++fdchangecnt; |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
413 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
454 | fdchanges [fdchangecnt - 1] = fd; |
414 | fdchanges [fdchangecnt - 1] = fd; |
455 | } |
415 | } |
456 | |
416 | |
457 | static void |
417 | static void |
458 | fd_kill (EV_P_ int fd) |
418 | fd_kill (EV_P_ int fd) |
… | |
… | |
460 | struct ev_io *w; |
420 | struct ev_io *w; |
461 | |
421 | |
462 | while ((w = (struct ev_io *)anfds [fd].head)) |
422 | while ((w = (struct ev_io *)anfds [fd].head)) |
463 | { |
423 | { |
464 | ev_io_stop (EV_A_ w); |
424 | ev_io_stop (EV_A_ w); |
465 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
425 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
466 | } |
426 | } |
467 | } |
427 | } |
468 | |
428 | |
469 | static int |
429 | static int |
470 | fd_valid (int fd) |
430 | fd_valid (int fd) |
… | |
… | |
598 | |
558 | |
599 | if (!gotsig) |
559 | if (!gotsig) |
600 | { |
560 | { |
601 | int old_errno = errno; |
561 | int old_errno = errno; |
602 | gotsig = 1; |
562 | gotsig = 1; |
|
|
563 | #ifdef WIN32 |
|
|
564 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
565 | #else |
603 | write (sigpipe [1], &signum, 1); |
566 | write (sigpipe [1], &signum, 1); |
|
|
567 | #endif |
604 | errno = old_errno; |
568 | errno = old_errno; |
605 | } |
569 | } |
606 | } |
570 | } |
607 | |
571 | |
|
|
572 | void |
|
|
573 | ev_feed_signal_event (EV_P_ int signum) |
|
|
574 | { |
|
|
575 | WL w; |
|
|
576 | |
|
|
577 | #if EV_MULTIPLICITY |
|
|
578 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
579 | #endif |
|
|
580 | |
|
|
581 | --signum; |
|
|
582 | |
|
|
583 | if (signum < 0 || signum >= signalmax) |
|
|
584 | return; |
|
|
585 | |
|
|
586 | signals [signum].gotsig = 0; |
|
|
587 | |
|
|
588 | for (w = signals [signum].head; w; w = w->next) |
|
|
589 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
590 | } |
|
|
591 | |
608 | static void |
592 | static void |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
593 | sigcb (EV_P_ struct ev_io *iow, int revents) |
610 | { |
594 | { |
611 | WL w; |
|
|
612 | int signum; |
595 | int signum; |
613 | |
596 | |
|
|
597 | #ifdef WIN32 |
|
|
598 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
599 | #else |
614 | read (sigpipe [0], &revents, 1); |
600 | read (sigpipe [0], &revents, 1); |
|
|
601 | #endif |
615 | gotsig = 0; |
602 | gotsig = 0; |
616 | |
603 | |
617 | for (signum = signalmax; signum--; ) |
604 | for (signum = signalmax; signum--; ) |
618 | if (signals [signum].gotsig) |
605 | if (signals [signum].gotsig) |
619 | { |
606 | ev_feed_signal_event (EV_A_ signum + 1); |
620 | signals [signum].gotsig = 0; |
|
|
621 | |
|
|
622 | for (w = signals [signum].head; w; w = w->next) |
|
|
623 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
624 | } |
|
|
625 | } |
607 | } |
626 | |
608 | |
627 | static void |
609 | static void |
628 | siginit (EV_P) |
610 | siginit (EV_P) |
629 | { |
611 | { |
… | |
… | |
662 | if (w->pid == pid || !w->pid) |
644 | if (w->pid == pid || !w->pid) |
663 | { |
645 | { |
664 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
646 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
665 | w->rpid = pid; |
647 | w->rpid = pid; |
666 | w->rstatus = status; |
648 | w->rstatus = status; |
667 | event (EV_A_ (W)w, EV_CHILD); |
649 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
668 | } |
650 | } |
669 | } |
651 | } |
670 | |
652 | |
671 | static void |
653 | static void |
672 | childcb (EV_P_ struct ev_signal *sw, int revents) |
654 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
674 | int pid, status; |
656 | int pid, status; |
675 | |
657 | |
676 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
658 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
677 | { |
659 | { |
678 | /* make sure we are called again until all childs have been reaped */ |
660 | /* make sure we are called again until all childs have been reaped */ |
679 | event (EV_A_ (W)sw, EV_SIGNAL); |
661 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
680 | |
662 | |
681 | child_reap (EV_A_ sw, pid, pid, status); |
663 | child_reap (EV_A_ sw, pid, pid, status); |
682 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
664 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
683 | } |
665 | } |
684 | } |
666 | } |
… | |
… | |
828 | ev_ref (EV_A); |
810 | ev_ref (EV_A); |
829 | ev_io_stop (EV_A_ &sigev); |
811 | ev_io_stop (EV_A_ &sigev); |
830 | close (sigpipe [0]); |
812 | close (sigpipe [0]); |
831 | close (sigpipe [1]); |
813 | close (sigpipe [1]); |
832 | |
814 | |
833 | while (ev_pipe (sigpipe)) |
815 | while (pipe (sigpipe)) |
834 | syserr ("(libev) error creating pipe"); |
816 | syserr ("(libev) error creating pipe"); |
835 | |
817 | |
836 | siginit (EV_A); |
818 | siginit (EV_A); |
837 | } |
819 | } |
838 | |
820 | |
… | |
… | |
869 | } |
851 | } |
870 | |
852 | |
871 | #endif |
853 | #endif |
872 | |
854 | |
873 | #if EV_MULTIPLICITY |
855 | #if EV_MULTIPLICITY |
874 | struct ev_loop default_loop_struct; |
|
|
875 | static struct ev_loop *default_loop; |
|
|
876 | |
|
|
877 | struct ev_loop * |
856 | struct ev_loop * |
878 | #else |
857 | #else |
879 | static int default_loop; |
|
|
880 | |
|
|
881 | int |
858 | int |
882 | #endif |
859 | #endif |
883 | ev_default_loop (int methods) |
860 | ev_default_loop (int methods) |
884 | { |
861 | { |
885 | if (sigpipe [0] == sigpipe [1]) |
862 | if (sigpipe [0] == sigpipe [1]) |
886 | if (ev_pipe (sigpipe)) |
863 | if (pipe (sigpipe)) |
887 | return 0; |
864 | return 0; |
888 | |
865 | |
889 | if (!default_loop) |
866 | if (!default_loop) |
890 | { |
867 | { |
891 | #if EV_MULTIPLICITY |
868 | #if EV_MULTIPLICITY |
… | |
… | |
946 | postfork = 1; |
923 | postfork = 1; |
947 | } |
924 | } |
948 | |
925 | |
949 | /*****************************************************************************/ |
926 | /*****************************************************************************/ |
950 | |
927 | |
|
|
928 | static int |
|
|
929 | any_pending (EV_P) |
|
|
930 | { |
|
|
931 | int pri; |
|
|
932 | |
|
|
933 | for (pri = NUMPRI; pri--; ) |
|
|
934 | if (pendingcnt [pri]) |
|
|
935 | return 1; |
|
|
936 | |
|
|
937 | return 0; |
|
|
938 | } |
|
|
939 | |
951 | static void |
940 | static void |
952 | call_pending (EV_P) |
941 | call_pending (EV_P) |
953 | { |
942 | { |
954 | int pri; |
943 | int pri; |
955 | |
944 | |
… | |
… | |
959 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
948 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
960 | |
949 | |
961 | if (p->w) |
950 | if (p->w) |
962 | { |
951 | { |
963 | p->w->pending = 0; |
952 | p->w->pending = 0; |
964 | p->w->cb (EV_A_ p->w, p->events); |
953 | EV_CB_INVOKE (p->w, p->events); |
965 | } |
954 | } |
966 | } |
955 | } |
967 | } |
956 | } |
968 | |
957 | |
969 | static void |
958 | static void |
… | |
… | |
983 | downheap ((WT *)timers, timercnt, 0); |
972 | downheap ((WT *)timers, timercnt, 0); |
984 | } |
973 | } |
985 | else |
974 | else |
986 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
975 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
987 | |
976 | |
988 | event (EV_A_ (W)w, EV_TIMEOUT); |
977 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
989 | } |
978 | } |
990 | } |
979 | } |
991 | |
980 | |
992 | static void |
981 | static void |
993 | periodics_reify (EV_P) |
982 | periodics_reify (EV_P) |
… | |
… | |
997 | struct ev_periodic *w = periodics [0]; |
986 | struct ev_periodic *w = periodics [0]; |
998 | |
987 | |
999 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
988 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1000 | |
989 | |
1001 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
|
|
991 | if (w->reschedule_cb) |
|
|
992 | { |
|
|
993 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
994 | |
|
|
995 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
996 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
997 | } |
1002 | if (w->interval) |
998 | else if (w->interval) |
1003 | { |
999 | { |
1004 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1000 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1005 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1001 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1006 | downheap ((WT *)periodics, periodiccnt, 0); |
1002 | downheap ((WT *)periodics, periodiccnt, 0); |
1007 | } |
1003 | } |
1008 | else |
1004 | else |
1009 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1005 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1010 | |
1006 | |
1011 | event (EV_A_ (W)w, EV_PERIODIC); |
1007 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1012 | } |
1008 | } |
1013 | } |
1009 | } |
1014 | |
1010 | |
1015 | static void |
1011 | static void |
1016 | periodics_reschedule (EV_P) |
1012 | periodics_reschedule (EV_P) |
… | |
… | |
1020 | /* adjust periodics after time jump */ |
1016 | /* adjust periodics after time jump */ |
1021 | for (i = 0; i < periodiccnt; ++i) |
1017 | for (i = 0; i < periodiccnt; ++i) |
1022 | { |
1018 | { |
1023 | struct ev_periodic *w = periodics [i]; |
1019 | struct ev_periodic *w = periodics [i]; |
1024 | |
1020 | |
|
|
1021 | if (w->reschedule_cb) |
|
|
1022 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1025 | if (w->interval) |
1023 | else if (w->interval) |
1026 | { |
|
|
1027 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1024 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1028 | |
|
|
1029 | if (fabs (diff) >= 1e-4) |
|
|
1030 | { |
|
|
1031 | ev_periodic_stop (EV_A_ w); |
|
|
1032 | ev_periodic_start (EV_A_ w); |
|
|
1033 | |
|
|
1034 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
1035 | } |
|
|
1036 | } |
|
|
1037 | } |
1025 | } |
|
|
1026 | |
|
|
1027 | /* now rebuild the heap */ |
|
|
1028 | for (i = periodiccnt >> 1; i--; ) |
|
|
1029 | downheap ((WT *)periodics, periodiccnt, i); |
1038 | } |
1030 | } |
1039 | |
1031 | |
1040 | inline int |
1032 | inline int |
1041 | time_update_monotonic (EV_P) |
1033 | time_update_monotonic (EV_P) |
1042 | { |
1034 | { |
… | |
… | |
1138 | /* update fd-related kernel structures */ |
1130 | /* update fd-related kernel structures */ |
1139 | fd_reify (EV_A); |
1131 | fd_reify (EV_A); |
1140 | |
1132 | |
1141 | /* calculate blocking time */ |
1133 | /* calculate blocking time */ |
1142 | |
1134 | |
1143 | /* we only need this for !monotonic clockor timers, but as we basically |
1135 | /* we only need this for !monotonic clock or timers, but as we basically |
1144 | always have timers, we just calculate it always */ |
1136 | always have timers, we just calculate it always */ |
1145 | #if EV_USE_MONOTONIC |
1137 | #if EV_USE_MONOTONIC |
1146 | if (expect_true (have_monotonic)) |
1138 | if (expect_true (have_monotonic)) |
1147 | time_update_monotonic (EV_A); |
1139 | time_update_monotonic (EV_A); |
1148 | else |
1140 | else |
… | |
… | |
1181 | /* queue pending timers and reschedule them */ |
1173 | /* queue pending timers and reschedule them */ |
1182 | timers_reify (EV_A); /* relative timers called last */ |
1174 | timers_reify (EV_A); /* relative timers called last */ |
1183 | periodics_reify (EV_A); /* absolute timers called first */ |
1175 | periodics_reify (EV_A); /* absolute timers called first */ |
1184 | |
1176 | |
1185 | /* queue idle watchers unless io or timers are pending */ |
1177 | /* queue idle watchers unless io or timers are pending */ |
1186 | if (!pendingcnt) |
1178 | if (idlecnt && !any_pending (EV_A)) |
1187 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1179 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1188 | |
1180 | |
1189 | /* queue check watchers, to be executed first */ |
1181 | /* queue check watchers, to be executed first */ |
1190 | if (checkcnt) |
1182 | if (checkcnt) |
1191 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1183 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1266 | return; |
1258 | return; |
1267 | |
1259 | |
1268 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1260 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1269 | |
1261 | |
1270 | ev_start (EV_A_ (W)w, 1); |
1262 | ev_start (EV_A_ (W)w, 1); |
1271 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1263 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1272 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1264 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1273 | |
1265 | |
1274 | fd_change (EV_A_ fd); |
1266 | fd_change (EV_A_ fd); |
1275 | } |
1267 | } |
1276 | |
1268 | |
… | |
… | |
1296 | ((WT)w)->at += mn_now; |
1288 | ((WT)w)->at += mn_now; |
1297 | |
1289 | |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1290 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1299 | |
1291 | |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1292 | ev_start (EV_A_ (W)w, ++timercnt); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1293 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1302 | timers [timercnt - 1] = w; |
1294 | timers [timercnt - 1] = w; |
1303 | upheap ((WT *)timers, timercnt - 1); |
1295 | upheap ((WT *)timers, timercnt - 1); |
1304 | |
1296 | |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1297 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1306 | } |
1298 | } |
… | |
… | |
1346 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1338 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1347 | { |
1339 | { |
1348 | if (ev_is_active (w)) |
1340 | if (ev_is_active (w)) |
1349 | return; |
1341 | return; |
1350 | |
1342 | |
|
|
1343 | if (w->reschedule_cb) |
|
|
1344 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1345 | else if (w->interval) |
|
|
1346 | { |
1351 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1347 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1352 | |
|
|
1353 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1348 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1354 | if (w->interval) |
|
|
1355 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1349 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1350 | } |
1356 | |
1351 | |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1352 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1353 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1359 | periodics [periodiccnt - 1] = w; |
1354 | periodics [periodiccnt - 1] = w; |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1355 | upheap ((WT *)periodics, periodiccnt - 1); |
1361 | |
1356 | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1357 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | } |
1358 | } |
… | |
… | |
1379 | |
1374 | |
1380 | ev_stop (EV_A_ (W)w); |
1375 | ev_stop (EV_A_ (W)w); |
1381 | } |
1376 | } |
1382 | |
1377 | |
1383 | void |
1378 | void |
|
|
1379 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1380 | { |
|
|
1381 | ev_periodic_stop (EV_A_ w); |
|
|
1382 | ev_periodic_start (EV_A_ w); |
|
|
1383 | } |
|
|
1384 | |
|
|
1385 | void |
1384 | ev_idle_start (EV_P_ struct ev_idle *w) |
1386 | ev_idle_start (EV_P_ struct ev_idle *w) |
1385 | { |
1387 | { |
1386 | if (ev_is_active (w)) |
1388 | if (ev_is_active (w)) |
1387 | return; |
1389 | return; |
1388 | |
1390 | |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1391 | ev_start (EV_A_ (W)w, ++idlecnt); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1392 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1391 | idles [idlecnt - 1] = w; |
1393 | idles [idlecnt - 1] = w; |
1392 | } |
1394 | } |
1393 | |
1395 | |
1394 | void |
1396 | void |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1397 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1407 | { |
1409 | { |
1408 | if (ev_is_active (w)) |
1410 | if (ev_is_active (w)) |
1409 | return; |
1411 | return; |
1410 | |
1412 | |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1413 | ev_start (EV_A_ (W)w, ++preparecnt); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1414 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1413 | prepares [preparecnt - 1] = w; |
1415 | prepares [preparecnt - 1] = w; |
1414 | } |
1416 | } |
1415 | |
1417 | |
1416 | void |
1418 | void |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1419 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1429 | { |
1431 | { |
1430 | if (ev_is_active (w)) |
1432 | if (ev_is_active (w)) |
1431 | return; |
1433 | return; |
1432 | |
1434 | |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1435 | ev_start (EV_A_ (W)w, ++checkcnt); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1436 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1435 | checks [checkcnt - 1] = w; |
1437 | checks [checkcnt - 1] = w; |
1436 | } |
1438 | } |
1437 | |
1439 | |
1438 | void |
1440 | void |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
1441 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1460 | return; |
1462 | return; |
1461 | |
1463 | |
1462 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1464 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1463 | |
1465 | |
1464 | ev_start (EV_A_ (W)w, 1); |
1466 | ev_start (EV_A_ (W)w, 1); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1467 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1468 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1467 | |
1469 | |
1468 | if (!((WL)w)->next) |
1470 | if (!((WL)w)->next) |
1469 | { |
1471 | { |
1470 | #if WIN32 |
1472 | #if WIN32 |
… | |
… | |
1553 | } |
1555 | } |
1554 | |
1556 | |
1555 | void |
1557 | void |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1558 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1557 | { |
1559 | { |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1560 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1559 | |
1561 | |
1560 | if (!once) |
1562 | if (!once) |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1563 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1562 | else |
1564 | else |
1563 | { |
1565 | { |