… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
150 | |
150 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
152 | |
153 | #if WIN32 |
153 | #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 | |
154 | |
212 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
213 | |
156 | |
214 | static void (*syserr_cb)(const char *msg); |
157 | static void (*syserr_cb)(const char *msg); |
215 | |
158 | |
… | |
… | |
325 | ev_now (EV_P) |
268 | ev_now (EV_P) |
326 | { |
269 | { |
327 | return rt_now; |
270 | return rt_now; |
328 | } |
271 | } |
329 | |
272 | |
330 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
331 | |
274 | |
332 | #define array_needsize(base,cur,cnt,init) \ |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
333 | if (expect_false ((cnt) > cur)) \ |
276 | if (expect_false ((cnt) > cur)) \ |
334 | { \ |
277 | { \ |
335 | int newcnt = cur; \ |
278 | int newcnt = cur; \ |
336 | do \ |
279 | do \ |
337 | { \ |
280 | { \ |
338 | newcnt = array_roundsize (base, newcnt << 1); \ |
281 | newcnt = array_roundsize (type, newcnt << 1); \ |
339 | } \ |
282 | } \ |
340 | while ((cnt) > newcnt); \ |
283 | while ((cnt) > newcnt); \ |
341 | \ |
284 | \ |
342 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
285 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
343 | init (base + cur, newcnt - cur); \ |
286 | init (base + cur, newcnt - cur); \ |
344 | cur = newcnt; \ |
287 | cur = newcnt; \ |
345 | } |
288 | } |
346 | |
289 | |
347 | #define array_slim(stem) \ |
290 | #define array_slim(type,stem) \ |
348 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
291 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
349 | { \ |
292 | { \ |
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
353 | } |
296 | } |
354 | |
297 | |
355 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
298 | /* 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 */ |
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
373 | |
316 | |
374 | ++base; |
317 | ++base; |
375 | } |
318 | } |
376 | } |
319 | } |
377 | |
320 | |
378 | static void |
321 | void |
379 | event (EV_P_ W w, int events) |
322 | ev_feed_event (EV_P_ void *w, int revents) |
380 | { |
323 | { |
|
|
324 | W w_ = (W)w; |
|
|
325 | |
381 | if (w->pending) |
326 | if (w_->pending) |
382 | { |
327 | { |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
328 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
384 | return; |
329 | return; |
385 | } |
330 | } |
386 | |
331 | |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
332 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
333 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
334 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
335 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
391 | } |
336 | } |
392 | |
337 | |
393 | static void |
338 | static void |
394 | queue_events (EV_P_ W *events, int eventcnt, int type) |
339 | queue_events (EV_P_ W *events, int eventcnt, int type) |
395 | { |
340 | { |
396 | int i; |
341 | int i; |
397 | |
342 | |
398 | for (i = 0; i < eventcnt; ++i) |
343 | for (i = 0; i < eventcnt; ++i) |
399 | event (EV_A_ events [i], type); |
344 | ev_feed_event (EV_A_ events [i], type); |
400 | } |
345 | } |
401 | |
346 | |
402 | static void |
347 | static void |
403 | fd_event (EV_P_ int fd, int events) |
348 | fd_event (EV_P_ int fd, int events) |
404 | { |
349 | { |
… | |
… | |
408 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
353 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
409 | { |
354 | { |
410 | int ev = w->events & events; |
355 | int ev = w->events & events; |
411 | |
356 | |
412 | if (ev) |
357 | if (ev) |
413 | event (EV_A_ (W)w, ev); |
358 | ev_feed_event (EV_A_ (W)w, ev); |
414 | } |
359 | } |
415 | } |
360 | } |
416 | |
361 | |
417 | /*****************************************************************************/ |
362 | /*****************************************************************************/ |
418 | |
363 | |
… | |
… | |
448 | return; |
393 | return; |
449 | |
394 | |
450 | anfds [fd].reify = 1; |
395 | anfds [fd].reify = 1; |
451 | |
396 | |
452 | ++fdchangecnt; |
397 | ++fdchangecnt; |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
398 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
454 | fdchanges [fdchangecnt - 1] = fd; |
399 | fdchanges [fdchangecnt - 1] = fd; |
455 | } |
400 | } |
456 | |
401 | |
457 | static void |
402 | static void |
458 | fd_kill (EV_P_ int fd) |
403 | fd_kill (EV_P_ int fd) |
… | |
… | |
460 | struct ev_io *w; |
405 | struct ev_io *w; |
461 | |
406 | |
462 | while ((w = (struct ev_io *)anfds [fd].head)) |
407 | while ((w = (struct ev_io *)anfds [fd].head)) |
463 | { |
408 | { |
464 | ev_io_stop (EV_A_ w); |
409 | ev_io_stop (EV_A_ w); |
465 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
410 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
466 | } |
411 | } |
467 | } |
412 | } |
468 | |
413 | |
469 | static int |
414 | static int |
470 | fd_valid (int fd) |
415 | fd_valid (int fd) |
… | |
… | |
598 | |
543 | |
599 | if (!gotsig) |
544 | if (!gotsig) |
600 | { |
545 | { |
601 | int old_errno = errno; |
546 | int old_errno = errno; |
602 | gotsig = 1; |
547 | gotsig = 1; |
|
|
548 | #ifdef WIN32 |
|
|
549 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
550 | #else |
603 | write (sigpipe [1], &signum, 1); |
551 | write (sigpipe [1], &signum, 1); |
|
|
552 | #endif |
604 | errno = old_errno; |
553 | errno = old_errno; |
605 | } |
554 | } |
606 | } |
555 | } |
607 | |
556 | |
608 | static void |
557 | static void |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
610 | { |
559 | { |
611 | WL w; |
560 | WL w; |
612 | int signum; |
561 | int signum; |
613 | |
562 | |
|
|
563 | #ifdef WIN32 |
|
|
564 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
565 | #else |
614 | read (sigpipe [0], &revents, 1); |
566 | read (sigpipe [0], &revents, 1); |
|
|
567 | #endif |
615 | gotsig = 0; |
568 | gotsig = 0; |
616 | |
569 | |
617 | for (signum = signalmax; signum--; ) |
570 | for (signum = signalmax; signum--; ) |
618 | if (signals [signum].gotsig) |
571 | if (signals [signum].gotsig) |
619 | { |
572 | { |
620 | signals [signum].gotsig = 0; |
573 | signals [signum].gotsig = 0; |
621 | |
574 | |
622 | for (w = signals [signum].head; w; w = w->next) |
575 | for (w = signals [signum].head; w; w = w->next) |
623 | event (EV_A_ (W)w, EV_SIGNAL); |
576 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
624 | } |
577 | } |
625 | } |
578 | } |
626 | |
579 | |
627 | static void |
580 | static void |
628 | siginit (EV_P) |
581 | siginit (EV_P) |
… | |
… | |
662 | if (w->pid == pid || !w->pid) |
615 | if (w->pid == pid || !w->pid) |
663 | { |
616 | { |
664 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
617 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
665 | w->rpid = pid; |
618 | w->rpid = pid; |
666 | w->rstatus = status; |
619 | w->rstatus = status; |
667 | event (EV_A_ (W)w, EV_CHILD); |
620 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
668 | } |
621 | } |
669 | } |
622 | } |
670 | |
623 | |
671 | static void |
624 | static void |
672 | childcb (EV_P_ struct ev_signal *sw, int revents) |
625 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
674 | int pid, status; |
627 | int pid, status; |
675 | |
628 | |
676 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
629 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
677 | { |
630 | { |
678 | /* make sure we are called again until all childs have been reaped */ |
631 | /* make sure we are called again until all childs have been reaped */ |
679 | event (EV_A_ (W)sw, EV_SIGNAL); |
632 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
680 | |
633 | |
681 | child_reap (EV_A_ sw, pid, pid, status); |
634 | 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 */ |
635 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
683 | } |
636 | } |
684 | } |
637 | } |
… | |
… | |
828 | ev_ref (EV_A); |
781 | ev_ref (EV_A); |
829 | ev_io_stop (EV_A_ &sigev); |
782 | ev_io_stop (EV_A_ &sigev); |
830 | close (sigpipe [0]); |
783 | close (sigpipe [0]); |
831 | close (sigpipe [1]); |
784 | close (sigpipe [1]); |
832 | |
785 | |
833 | while (ev_pipe (sigpipe)) |
786 | while (pipe (sigpipe)) |
834 | syserr ("(libev) error creating pipe"); |
787 | syserr ("(libev) error creating pipe"); |
835 | |
788 | |
836 | siginit (EV_A); |
789 | siginit (EV_A); |
837 | } |
790 | } |
838 | |
791 | |
… | |
… | |
881 | int |
834 | int |
882 | #endif |
835 | #endif |
883 | ev_default_loop (int methods) |
836 | ev_default_loop (int methods) |
884 | { |
837 | { |
885 | if (sigpipe [0] == sigpipe [1]) |
838 | if (sigpipe [0] == sigpipe [1]) |
886 | if (ev_pipe (sigpipe)) |
839 | if (pipe (sigpipe)) |
887 | return 0; |
840 | return 0; |
888 | |
841 | |
889 | if (!default_loop) |
842 | if (!default_loop) |
890 | { |
843 | { |
891 | #if EV_MULTIPLICITY |
844 | #if EV_MULTIPLICITY |
… | |
… | |
945 | if (method) |
898 | if (method) |
946 | postfork = 1; |
899 | postfork = 1; |
947 | } |
900 | } |
948 | |
901 | |
949 | /*****************************************************************************/ |
902 | /*****************************************************************************/ |
|
|
903 | |
|
|
904 | static int |
|
|
905 | any_pending (EV_P) |
|
|
906 | { |
|
|
907 | int pri; |
|
|
908 | |
|
|
909 | for (pri = NUMPRI; pri--; ) |
|
|
910 | if (pendingcnt [pri]) |
|
|
911 | return 1; |
|
|
912 | |
|
|
913 | return 0; |
|
|
914 | } |
950 | |
915 | |
951 | static void |
916 | static void |
952 | call_pending (EV_P) |
917 | call_pending (EV_P) |
953 | { |
918 | { |
954 | int pri; |
919 | int pri; |
… | |
… | |
983 | downheap ((WT *)timers, timercnt, 0); |
948 | downheap ((WT *)timers, timercnt, 0); |
984 | } |
949 | } |
985 | else |
950 | else |
986 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
951 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
987 | |
952 | |
988 | event (EV_A_ (W)w, EV_TIMEOUT); |
953 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
989 | } |
954 | } |
990 | } |
955 | } |
991 | |
956 | |
992 | static void |
957 | static void |
993 | periodics_reify (EV_P) |
958 | periodics_reify (EV_P) |
… | |
… | |
997 | struct ev_periodic *w = periodics [0]; |
962 | struct ev_periodic *w = periodics [0]; |
998 | |
963 | |
999 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
964 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1000 | |
965 | |
1001 | /* first reschedule or stop timer */ |
966 | /* first reschedule or stop timer */ |
|
|
967 | if (w->reschedule_cb) |
|
|
968 | { |
|
|
969 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
970 | |
|
|
971 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
972 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
973 | } |
1002 | if (w->interval) |
974 | else if (w->interval) |
1003 | { |
975 | { |
1004 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
976 | ((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)); |
977 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1006 | downheap ((WT *)periodics, periodiccnt, 0); |
978 | downheap ((WT *)periodics, periodiccnt, 0); |
1007 | } |
979 | } |
1008 | else |
980 | else |
1009 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
981 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1010 | |
982 | |
1011 | event (EV_A_ (W)w, EV_PERIODIC); |
983 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1012 | } |
984 | } |
1013 | } |
985 | } |
1014 | |
986 | |
1015 | static void |
987 | static void |
1016 | periodics_reschedule (EV_P) |
988 | periodics_reschedule (EV_P) |
… | |
… | |
1020 | /* adjust periodics after time jump */ |
992 | /* adjust periodics after time jump */ |
1021 | for (i = 0; i < periodiccnt; ++i) |
993 | for (i = 0; i < periodiccnt; ++i) |
1022 | { |
994 | { |
1023 | struct ev_periodic *w = periodics [i]; |
995 | struct ev_periodic *w = periodics [i]; |
1024 | |
996 | |
|
|
997 | if (w->reschedule_cb) |
|
|
998 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1025 | if (w->interval) |
999 | else if (w->interval) |
1026 | { |
|
|
1027 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1000 | ((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 | } |
1001 | } |
|
|
1002 | |
|
|
1003 | /* now rebuild the heap */ |
|
|
1004 | for (i = periodiccnt >> 1; i--; ) |
|
|
1005 | downheap ((WT *)periodics, periodiccnt, i); |
1038 | } |
1006 | } |
1039 | |
1007 | |
1040 | inline int |
1008 | inline int |
1041 | time_update_monotonic (EV_P) |
1009 | time_update_monotonic (EV_P) |
1042 | { |
1010 | { |
… | |
… | |
1138 | /* update fd-related kernel structures */ |
1106 | /* update fd-related kernel structures */ |
1139 | fd_reify (EV_A); |
1107 | fd_reify (EV_A); |
1140 | |
1108 | |
1141 | /* calculate blocking time */ |
1109 | /* calculate blocking time */ |
1142 | |
1110 | |
1143 | /* we only need this for !monotonic clockor timers, but as we basically |
1111 | /* we only need this for !monotonic clock or timers, but as we basically |
1144 | always have timers, we just calculate it always */ |
1112 | always have timers, we just calculate it always */ |
1145 | #if EV_USE_MONOTONIC |
1113 | #if EV_USE_MONOTONIC |
1146 | if (expect_true (have_monotonic)) |
1114 | if (expect_true (have_monotonic)) |
1147 | time_update_monotonic (EV_A); |
1115 | time_update_monotonic (EV_A); |
1148 | else |
1116 | else |
… | |
… | |
1181 | /* queue pending timers and reschedule them */ |
1149 | /* queue pending timers and reschedule them */ |
1182 | timers_reify (EV_A); /* relative timers called last */ |
1150 | timers_reify (EV_A); /* relative timers called last */ |
1183 | periodics_reify (EV_A); /* absolute timers called first */ |
1151 | periodics_reify (EV_A); /* absolute timers called first */ |
1184 | |
1152 | |
1185 | /* queue idle watchers unless io or timers are pending */ |
1153 | /* queue idle watchers unless io or timers are pending */ |
1186 | if (!pendingcnt) |
1154 | if (idlecnt && !any_pending (EV_A)) |
1187 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1155 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1188 | |
1156 | |
1189 | /* queue check watchers, to be executed first */ |
1157 | /* queue check watchers, to be executed first */ |
1190 | if (checkcnt) |
1158 | if (checkcnt) |
1191 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1159 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1266 | return; |
1234 | return; |
1267 | |
1235 | |
1268 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1236 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1269 | |
1237 | |
1270 | ev_start (EV_A_ (W)w, 1); |
1238 | ev_start (EV_A_ (W)w, 1); |
1271 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1239 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1272 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1240 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1273 | |
1241 | |
1274 | fd_change (EV_A_ fd); |
1242 | fd_change (EV_A_ fd); |
1275 | } |
1243 | } |
1276 | |
1244 | |
… | |
… | |
1296 | ((WT)w)->at += mn_now; |
1264 | ((WT)w)->at += mn_now; |
1297 | |
1265 | |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1266 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1299 | |
1267 | |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1268 | ev_start (EV_A_ (W)w, ++timercnt); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1269 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1302 | timers [timercnt - 1] = w; |
1270 | timers [timercnt - 1] = w; |
1303 | upheap ((WT *)timers, timercnt - 1); |
1271 | upheap ((WT *)timers, timercnt - 1); |
1304 | |
1272 | |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1273 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1306 | } |
1274 | } |
… | |
… | |
1346 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1314 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1347 | { |
1315 | { |
1348 | if (ev_is_active (w)) |
1316 | if (ev_is_active (w)) |
1349 | return; |
1317 | return; |
1350 | |
1318 | |
|
|
1319 | if (w->reschedule_cb) |
|
|
1320 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1321 | else if (w->interval) |
|
|
1322 | { |
1351 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1323 | 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 */ |
1324 | /* 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; |
1325 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1326 | } |
1356 | |
1327 | |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1328 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1329 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1359 | periodics [periodiccnt - 1] = w; |
1330 | periodics [periodiccnt - 1] = w; |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1331 | upheap ((WT *)periodics, periodiccnt - 1); |
1361 | |
1332 | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1333 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | } |
1334 | } |
… | |
… | |
1379 | |
1350 | |
1380 | ev_stop (EV_A_ (W)w); |
1351 | ev_stop (EV_A_ (W)w); |
1381 | } |
1352 | } |
1382 | |
1353 | |
1383 | void |
1354 | void |
|
|
1355 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1356 | { |
|
|
1357 | ev_periodic_stop (EV_A_ w); |
|
|
1358 | ev_periodic_start (EV_A_ w); |
|
|
1359 | } |
|
|
1360 | |
|
|
1361 | void |
1384 | ev_idle_start (EV_P_ struct ev_idle *w) |
1362 | ev_idle_start (EV_P_ struct ev_idle *w) |
1385 | { |
1363 | { |
1386 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1387 | return; |
1365 | return; |
1388 | |
1366 | |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1367 | ev_start (EV_A_ (W)w, ++idlecnt); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1368 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1391 | idles [idlecnt - 1] = w; |
1369 | idles [idlecnt - 1] = w; |
1392 | } |
1370 | } |
1393 | |
1371 | |
1394 | void |
1372 | void |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1373 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1407 | { |
1385 | { |
1408 | if (ev_is_active (w)) |
1386 | if (ev_is_active (w)) |
1409 | return; |
1387 | return; |
1410 | |
1388 | |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1389 | ev_start (EV_A_ (W)w, ++preparecnt); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1390 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1413 | prepares [preparecnt - 1] = w; |
1391 | prepares [preparecnt - 1] = w; |
1414 | } |
1392 | } |
1415 | |
1393 | |
1416 | void |
1394 | void |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1395 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1429 | { |
1407 | { |
1430 | if (ev_is_active (w)) |
1408 | if (ev_is_active (w)) |
1431 | return; |
1409 | return; |
1432 | |
1410 | |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1411 | ev_start (EV_A_ (W)w, ++checkcnt); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1412 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1435 | checks [checkcnt - 1] = w; |
1413 | checks [checkcnt - 1] = w; |
1436 | } |
1414 | } |
1437 | |
1415 | |
1438 | void |
1416 | void |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
1417 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1460 | return; |
1438 | return; |
1461 | |
1439 | |
1462 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1440 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1463 | |
1441 | |
1464 | ev_start (EV_A_ (W)w, 1); |
1442 | ev_start (EV_A_ (W)w, 1); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1443 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1444 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1467 | |
1445 | |
1468 | if (!((WL)w)->next) |
1446 | if (!((WL)w)->next) |
1469 | { |
1447 | { |
1470 | #if WIN32 |
1448 | #if WIN32 |
… | |
… | |
1553 | } |
1531 | } |
1554 | |
1532 | |
1555 | void |
1533 | void |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1534 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1557 | { |
1535 | { |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1536 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1559 | |
1537 | |
1560 | if (!once) |
1538 | if (!once) |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1539 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1562 | else |
1540 | else |
1563 | { |
1541 | { |