… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
|
|
29 | #if EV_USE_CONFIG_H |
|
|
30 | # include "config.h" |
|
|
31 | #endif |
29 | |
32 | |
30 | #include <math.h> |
33 | #include <math.h> |
31 | #include <stdlib.h> |
34 | #include <stdlib.h> |
32 | #include <unistd.h> |
35 | #include <unistd.h> |
33 | #include <fcntl.h> |
36 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
44 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
46 | #include <sys/time.h> |
44 | #include <time.h> |
47 | #include <time.h> |
45 | |
48 | |
46 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
49 | # endif |
52 | # endif |
50 | #endif |
53 | #endif |
51 | |
54 | |
52 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
53 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
54 | #endif |
57 | #endif |
55 | |
58 | |
56 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
57 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
58 | #endif |
61 | #endif |
59 | |
62 | |
|
|
63 | #ifndef CLOCK_REALTIME |
|
|
64 | # define EV_USE_REALTIME 0 |
|
|
65 | #endif |
60 | #ifndef HAVE_REALTIME |
66 | #ifndef EV_USE_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
62 | #endif |
68 | #endif |
63 | |
69 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
67 | |
74 | |
68 | #include "ev.h" |
75 | #include "ev.h" |
69 | |
76 | |
70 | typedef struct ev_watcher *W; |
77 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
78 | typedef struct ev_watcher_list *WL; |
… | |
… | |
84 | /*****************************************************************************/ |
91 | /*****************************************************************************/ |
85 | |
92 | |
86 | ev_tstamp |
93 | ev_tstamp |
87 | ev_time (void) |
94 | ev_time (void) |
88 | { |
95 | { |
89 | #if HAVE_REALTIME |
96 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
97 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
98 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
99 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
100 | #else |
94 | struct timeval tv; |
101 | struct timeval tv; |
… | |
… | |
98 | } |
105 | } |
99 | |
106 | |
100 | static ev_tstamp |
107 | static ev_tstamp |
101 | get_clock (void) |
108 | get_clock (void) |
102 | { |
109 | { |
103 | #if HAVE_MONOTONIC |
110 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
111 | if (have_monotonic) |
105 | { |
112 | { |
106 | struct timespec ts; |
113 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
116 | } |
110 | #endif |
117 | #endif |
111 | |
118 | |
112 | return ev_time (); |
119 | return ev_time (); |
113 | } |
120 | } |
|
|
121 | |
|
|
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
114 | |
123 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
124 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
125 | if ((cnt) > cur) \ |
117 | { \ |
126 | { \ |
118 | int newcnt = cur; \ |
127 | int newcnt = cur; \ |
119 | do \ |
128 | do \ |
120 | { \ |
129 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
131 | } \ |
123 | while ((cnt) > newcnt); \ |
132 | while ((cnt) > newcnt); \ |
124 | \ |
133 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
134 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
135 | init (base + cur, newcnt - cur); \ |
… | |
… | |
159 | static int pendingmax, pendingcnt; |
168 | static int pendingmax, pendingcnt; |
160 | |
169 | |
161 | static void |
170 | static void |
162 | event (W w, int events) |
171 | event (W w, int events) |
163 | { |
172 | { |
164 | if (w->active) |
|
|
165 | { |
|
|
166 | w->pending = ++pendingcnt; |
173 | w->pending = ++pendingcnt; |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
174 | array_needsize (pendings, pendingmax, pendingcnt, ); |
168 | pendings [pendingcnt - 1].w = w; |
175 | pendings [pendingcnt - 1].w = w; |
169 | pendings [pendingcnt - 1].events = events; |
176 | pendings [pendingcnt - 1].events = events; |
170 | } |
|
|
171 | } |
177 | } |
172 | |
178 | |
173 | static void |
179 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
180 | queue_events (W *events, int eventcnt, int type) |
175 | { |
181 | { |
… | |
… | |
228 | } |
234 | } |
229 | |
235 | |
230 | static void |
236 | static void |
231 | fd_change (int fd) |
237 | fd_change (int fd) |
232 | { |
238 | { |
233 | if (anfds [fd].events & EV_REIFY) |
239 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
234 | return; |
240 | return; |
235 | |
241 | |
236 | anfds [fd].events |= EV_REIFY; |
242 | anfds [fd].events |= EV_REIFY; |
237 | |
243 | |
238 | ++fdchangecnt; |
244 | ++fdchangecnt; |
… | |
… | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
255 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
256 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
257 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
258 | while (anfds [fd].head) |
253 | { |
259 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
260 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
255 | ev_io_stop (anfds [fd].head); |
261 | ev_io_stop (anfds [fd].head); |
256 | } |
262 | } |
257 | } |
263 | } |
258 | |
264 | |
259 | /*****************************************************************************/ |
265 | /*****************************************************************************/ |
… | |
… | |
411 | } |
417 | } |
412 | } |
418 | } |
413 | |
419 | |
414 | /*****************************************************************************/ |
420 | /*****************************************************************************/ |
415 | |
421 | |
416 | #if HAVE_EPOLL |
422 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
423 | # include "ev_epoll.c" |
418 | #endif |
424 | #endif |
419 | #if HAVE_SELECT |
425 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
426 | # include "ev_select.c" |
421 | #endif |
427 | #endif |
422 | |
428 | |
423 | int |
429 | int |
424 | ev_version_major (void) |
430 | ev_version_major (void) |
… | |
… | |
434 | |
440 | |
435 | int ev_init (int flags) |
441 | int ev_init (int flags) |
436 | { |
442 | { |
437 | if (!ev_method) |
443 | if (!ev_method) |
438 | { |
444 | { |
439 | #if HAVE_MONOTONIC |
445 | #if EV_USE_MONOTONIC |
440 | { |
446 | { |
441 | struct timespec ts; |
447 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
448 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
449 | have_monotonic = 1; |
444 | } |
450 | } |
… | |
… | |
450 | |
456 | |
451 | if (pipe (sigpipe)) |
457 | if (pipe (sigpipe)) |
452 | return 0; |
458 | return 0; |
453 | |
459 | |
454 | ev_method = EVMETHOD_NONE; |
460 | ev_method = EVMETHOD_NONE; |
455 | #if HAVE_EPOLL |
461 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
462 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
457 | #endif |
463 | #endif |
458 | #if HAVE_SELECT |
464 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
465 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
460 | #endif |
466 | #endif |
461 | |
467 | |
462 | if (ev_method) |
468 | if (ev_method) |
463 | { |
469 | { |
… | |
… | |
487 | } |
493 | } |
488 | |
494 | |
489 | void |
495 | void |
490 | ev_postfork_child (void) |
496 | ev_postfork_child (void) |
491 | { |
497 | { |
492 | #if HAVE_EPOLL |
498 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
499 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
500 | epoll_postfork_child (); |
495 | #endif |
501 | #endif |
496 | |
502 | |
497 | ev_io_stop (&sigev); |
503 | ev_io_stop (&sigev); |
… | |
… | |
523 | { |
529 | { |
524 | while (timercnt && timers [0]->at <= now) |
530 | while (timercnt && timers [0]->at <= now) |
525 | { |
531 | { |
526 | struct ev_timer *w = timers [0]; |
532 | struct ev_timer *w = timers [0]; |
527 | |
533 | |
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
|
|
530 | /* first reschedule or stop timer */ |
534 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
535 | if (w->repeat) |
532 | { |
536 | { |
533 | w->at = now + w->repeat; |
537 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
538 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
535 | downheap ((WT *)timers, timercnt, 0); |
539 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
540 | } |
537 | else |
541 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
542 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
543 | |
|
|
544 | event ((W)w, EV_TIMEOUT); |
539 | } |
545 | } |
540 | } |
546 | } |
541 | |
547 | |
542 | static void |
548 | static void |
543 | periodics_reify (void) |
549 | periodics_reify (void) |
… | |
… | |
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1023 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1018 | { |
1024 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1025 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1026 | |
1021 | if (!once) |
1027 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1028 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1029 | else |
1024 | { |
1030 | { |
1025 | once->cb = cb; |
1031 | once->cb = cb; |
1026 | once->arg = arg; |
1032 | once->arg = arg; |
1027 | |
1033 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1034 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1035 | if (fd >= 0) |
1031 | { |
1036 | { |
1032 | ev_io_set (&once->io, fd, events); |
1037 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1038 | ev_io_start (&once->io); |
1034 | } |
1039 | } |
1035 | |
1040 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1041 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1042 | if (timeout >= 0.) |
1039 | { |
1043 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1044 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1045 | ev_timer_start (&once->to); |
1042 | } |
1046 | } |