… | |
… | |
36 | |
36 | |
37 | #include <stdio.h> |
37 | #include <stdio.h> |
38 | |
38 | |
39 | #include <assert.h> |
39 | #include <assert.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
43 | #include <sys/time.h> |
42 | #include <time.h> |
44 | #include <time.h> |
43 | |
45 | |
44 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
… | |
… | |
59 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
60 | #endif |
62 | #endif |
61 | |
63 | |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
64 | |
67 | |
65 | #include "ev.h" |
68 | #include "ev.h" |
66 | |
69 | |
67 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
68 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
110 | } |
113 | } |
111 | |
114 | |
112 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
113 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
114 | { \ |
117 | { \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
118 | cur = newcnt; \ |
127 | cur = newcnt; \ |
119 | } |
128 | } |
120 | |
129 | |
… | |
… | |
188 | event (events [i], type); |
197 | event (events [i], type); |
189 | } |
198 | } |
190 | |
199 | |
191 | /* called on EBADF to verify fds */ |
200 | /* called on EBADF to verify fds */ |
192 | static void |
201 | static void |
193 | fd_recheck () |
202 | fd_recheck (void) |
194 | { |
203 | { |
195 | int fd; |
204 | int fd; |
196 | |
205 | |
197 | for (fd = 0; fd < anfdmax; ++fd) |
206 | for (fd = 0; fd < anfdmax; ++fd) |
198 | if (anfds [fd].wev) |
207 | if (anfds [fd].wev) |
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 | while (anfds [fd].head) |
209 | while (anfds [fd].head) |
|
|
210 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
201 | evio_stop (anfds [fd].head); |
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
202 | } |
214 | } |
203 | |
215 | |
204 | /*****************************************************************************/ |
216 | /*****************************************************************************/ |
205 | |
217 | |
206 | static struct ev_timer **timers; |
218 | static struct ev_timer **timers; |
… | |
… | |
332 | static struct ev_check **checks; |
344 | static struct ev_check **checks; |
333 | static int checkmax, checkcnt; |
345 | static int checkmax, checkcnt; |
334 | |
346 | |
335 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
336 | |
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
|
|
372 | |
337 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
338 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
339 | #endif |
375 | #endif |
340 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
341 | # include "ev_select.c" |
377 | # include "ev_select.c" |
342 | #endif |
378 | #endif |
343 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
344 | int ev_init (int flags) |
392 | int ev_init (int flags) |
345 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
346 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
347 | { |
397 | { |
348 | struct timespec ts; |
398 | struct timespec ts; |
349 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
350 | have_monotonic = 1; |
400 | have_monotonic = 1; |
351 | } |
401 | } |
352 | #endif |
402 | #endif |
353 | |
403 | |
354 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
355 | now = get_clock (); |
405 | now = get_clock (); |
356 | diff = ev_now - now; |
406 | diff = ev_now - now; |
357 | |
407 | |
358 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
359 | return 0; |
409 | return 0; |
360 | |
410 | |
361 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
362 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
363 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
364 | #endif |
414 | #endif |
365 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
366 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
367 | #endif |
417 | #endif |
368 | |
418 | |
369 | if (ev_method) |
419 | if (ev_method) |
370 | { |
420 | { |
371 | evw_init (&sigev, sigcb); |
421 | evw_init (&sigev, sigcb); |
372 | siginit (); |
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
373 | } |
427 | } |
374 | |
428 | |
375 | return ev_method; |
429 | return ev_method; |
376 | } |
430 | } |
377 | |
431 | |
378 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
379 | |
433 | |
|
|
434 | void |
380 | void ev_prefork (void) |
435 | ev_prefork (void) |
381 | { |
436 | { |
382 | /* nop */ |
437 | /* nop */ |
383 | } |
438 | } |
384 | |
439 | |
|
|
440 | void |
385 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
386 | { |
442 | { |
387 | /* nop */ |
443 | /* nop */ |
388 | } |
444 | } |
389 | |
445 | |
|
|
446 | void |
390 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
391 | { |
448 | { |
392 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
393 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
394 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
395 | #endif |
452 | #endif |
… | |
… | |
428 | |
485 | |
429 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
430 | } |
487 | } |
431 | |
488 | |
432 | static void |
489 | static void |
433 | call_pending () |
490 | call_pending (void) |
434 | { |
491 | { |
435 | while (pendingcnt) |
492 | while (pendingcnt) |
436 | { |
493 | { |
437 | ANPENDING *p = pendings + --pendingcnt; |
494 | ANPENDING *p = pendings + --pendingcnt; |
438 | |
495 | |
… | |
… | |
443 | } |
500 | } |
444 | } |
501 | } |
445 | } |
502 | } |
446 | |
503 | |
447 | static void |
504 | static void |
448 | timers_reify () |
505 | timers_reify (void) |
449 | { |
506 | { |
450 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
451 | { |
508 | { |
452 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
453 | |
510 | |
… | |
… | |
464 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
465 | } |
522 | } |
466 | } |
523 | } |
467 | |
524 | |
468 | static void |
525 | static void |
469 | periodics_reify () |
526 | periodics_reify (void) |
470 | { |
527 | { |
471 | while (periodiccnt && periodics [0]->at <= ev_now) |
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
472 | { |
529 | { |
473 | struct ev_periodic *w = periodics [0]; |
530 | struct ev_periodic *w = periodics [0]; |
474 | |
531 | |
… | |
… | |
510 | } |
567 | } |
511 | } |
568 | } |
512 | } |
569 | } |
513 | |
570 | |
514 | static void |
571 | static void |
515 | time_update () |
572 | time_update (void) |
516 | { |
573 | { |
517 | int i; |
574 | int i; |
518 | |
575 | |
519 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
520 | |
577 | |
… | |
… | |
683 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
684 | |
741 | |
685 | ++fdchangecnt; |
742 | ++fdchangecnt; |
686 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
687 | fdchanges [fdchangecnt - 1] = fd; |
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
688 | } |
748 | } |
689 | |
749 | |
690 | void |
750 | void |
691 | evio_stop (struct ev_io *w) |
751 | evio_stop (struct ev_io *w) |
692 | { |
752 | { |
|
|
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
693 | ev_clear ((W)w); |
755 | ev_clear ((W)w); |
694 | if (!ev_is_active (w)) |
756 | if (!ev_is_active (w)) |
695 | return; |
757 | return; |
696 | |
758 | |
697 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
… | |
… | |
876 | ev_clear ((W)w); |
938 | ev_clear ((W)w); |
877 | if (ev_is_active (w)) |
939 | if (ev_is_active (w)) |
878 | return; |
940 | return; |
879 | |
941 | |
880 | checks [w->active - 1] = checks [--checkcnt]; |
942 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
943 | ev_stop ((W)w); |
|
|
944 | } |
|
|
945 | |
|
|
946 | void evchild_start (struct ev_child *w) |
|
|
947 | { |
|
|
948 | if (ev_is_active (w)) |
|
|
949 | return; |
|
|
950 | |
|
|
951 | ev_start ((W)w, 1); |
|
|
952 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
953 | } |
|
|
954 | |
|
|
955 | void evchild_stop (struct ev_child *w) |
|
|
956 | { |
|
|
957 | ev_clear ((W)w); |
|
|
958 | if (ev_is_active (w)) |
|
|
959 | return; |
|
|
960 | |
|
|
961 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
881 | ev_stop ((W)w); |
962 | ev_stop ((W)w); |
882 | } |
963 | } |
883 | |
964 | |
884 | /*****************************************************************************/ |
965 | /*****************************************************************************/ |
885 | |
966 | |