… | |
… | |
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> |
… | |
… | |
36 | |
39 | |
37 | #include <stdio.h> |
40 | #include <stdio.h> |
38 | |
41 | |
39 | #include <assert.h> |
42 | #include <assert.h> |
40 | #include <errno.h> |
43 | #include <errno.h> |
|
|
44 | #include <sys/types.h> |
|
|
45 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
46 | #include <sys/time.h> |
42 | #include <time.h> |
47 | #include <time.h> |
43 | |
48 | |
44 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
46 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
47 | # endif |
52 | # endif |
48 | #endif |
53 | #endif |
49 | |
54 | |
50 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
51 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
52 | #endif |
57 | #endif |
53 | |
58 | |
54 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
55 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
56 | #endif |
61 | #endif |
57 | |
62 | |
58 | #ifndef HAVE_REALTIME |
63 | #ifndef EV_USE_REALTIME |
59 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
64 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
60 | #endif |
65 | #endif |
61 | |
66 | |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
67 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 | #define MAX_BLOCKTIME 60. |
68 | #define MAX_BLOCKTIME 59.731 |
|
|
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
64 | |
70 | |
65 | #include "ev.h" |
71 | #include "ev.h" |
66 | |
72 | |
67 | typedef struct ev_watcher *W; |
73 | typedef struct ev_watcher *W; |
68 | typedef struct ev_watcher_list *WL; |
74 | typedef struct ev_watcher_list *WL; |
… | |
… | |
81 | /*****************************************************************************/ |
87 | /*****************************************************************************/ |
82 | |
88 | |
83 | ev_tstamp |
89 | ev_tstamp |
84 | ev_time (void) |
90 | ev_time (void) |
85 | { |
91 | { |
86 | #if HAVE_REALTIME |
92 | #if EV_USE_REALTIME |
87 | struct timespec ts; |
93 | struct timespec ts; |
88 | clock_gettime (CLOCK_REALTIME, &ts); |
94 | clock_gettime (CLOCK_REALTIME, &ts); |
89 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
95 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
90 | #else |
96 | #else |
91 | struct timeval tv; |
97 | struct timeval tv; |
… | |
… | |
95 | } |
101 | } |
96 | |
102 | |
97 | static ev_tstamp |
103 | static ev_tstamp |
98 | get_clock (void) |
104 | get_clock (void) |
99 | { |
105 | { |
100 | #if HAVE_MONOTONIC |
106 | #if EV_USE_MONOTONIC |
101 | if (have_monotonic) |
107 | if (have_monotonic) |
102 | { |
108 | { |
103 | struct timespec ts; |
109 | struct timespec ts; |
104 | clock_gettime (CLOCK_MONOTONIC, &ts); |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
105 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
107 | #endif |
113 | #endif |
108 | |
114 | |
109 | return ev_time (); |
115 | return ev_time (); |
110 | } |
116 | } |
111 | |
117 | |
|
|
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
119 | |
112 | #define array_needsize(base,cur,cnt,init) \ |
120 | #define array_needsize(base,cur,cnt,init) \ |
113 | if ((cnt) > cur) \ |
121 | if ((cnt) > cur) \ |
114 | { \ |
122 | { \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
123 | int newcnt = cur; \ |
|
|
124 | do \ |
|
|
125 | { \ |
|
|
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
|
|
127 | } \ |
|
|
128 | while ((cnt) > newcnt); \ |
|
|
129 | \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
130 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 | init (base + cur, newcnt - cur); \ |
131 | init (base + cur, newcnt - cur); \ |
118 | cur = newcnt; \ |
132 | cur = newcnt; \ |
119 | } |
133 | } |
120 | |
134 | |
121 | /*****************************************************************************/ |
135 | /*****************************************************************************/ |
122 | |
136 | |
123 | typedef struct |
137 | typedef struct |
124 | { |
138 | { |
125 | struct ev_io *head; |
139 | struct ev_io *head; |
126 | unsigned char wev, rev; /* want, received event set */ |
140 | int events; |
127 | } ANFD; |
141 | } ANFD; |
128 | |
142 | |
129 | static ANFD *anfds; |
143 | static ANFD *anfds; |
130 | static int anfdmax; |
144 | static int anfdmax; |
131 | |
145 | |
132 | static int *fdchanges; |
|
|
133 | static int fdchangemax, fdchangecnt; |
|
|
134 | |
|
|
135 | static void |
146 | static void |
136 | anfds_init (ANFD *base, int count) |
147 | anfds_init (ANFD *base, int count) |
137 | { |
148 | { |
138 | while (count--) |
149 | while (count--) |
139 | { |
150 | { |
140 | base->head = 0; |
151 | base->head = 0; |
141 | base->wev = base->rev = EV_NONE; |
152 | base->events = EV_NONE; |
142 | ++base; |
153 | ++base; |
143 | } |
154 | } |
144 | } |
155 | } |
145 | |
156 | |
146 | typedef struct |
157 | typedef struct |
… | |
… | |
153 | static int pendingmax, pendingcnt; |
164 | static int pendingmax, pendingcnt; |
154 | |
165 | |
155 | static void |
166 | static void |
156 | event (W w, int events) |
167 | event (W w, int events) |
157 | { |
168 | { |
158 | if (w->active) |
|
|
159 | { |
|
|
160 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
162 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
163 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
164 | } |
173 | } |
|
|
174 | |
|
|
175 | static void |
|
|
176 | queue_events (W *events, int eventcnt, int type) |
|
|
177 | { |
|
|
178 | int i; |
|
|
179 | |
|
|
180 | for (i = 0; i < eventcnt; ++i) |
|
|
181 | event (events [i], type); |
165 | } |
182 | } |
166 | |
183 | |
167 | static void |
184 | static void |
168 | fd_event (int fd, int events) |
185 | fd_event (int fd, int events) |
169 | { |
186 | { |
… | |
… | |
177 | if (ev) |
194 | if (ev) |
178 | event ((W)w, ev); |
195 | event ((W)w, ev); |
179 | } |
196 | } |
180 | } |
197 | } |
181 | |
198 | |
|
|
199 | /*****************************************************************************/ |
|
|
200 | |
|
|
201 | static int *fdchanges; |
|
|
202 | static int fdchangemax, fdchangecnt; |
|
|
203 | |
182 | static void |
204 | static void |
183 | queue_events (W *events, int eventcnt, int type) |
205 | fd_reify (void) |
184 | { |
206 | { |
185 | int i; |
207 | int i; |
186 | |
208 | |
187 | for (i = 0; i < eventcnt; ++i) |
209 | for (i = 0; i < fdchangecnt; ++i) |
188 | event (events [i], type); |
210 | { |
|
|
211 | int fd = fdchanges [i]; |
|
|
212 | ANFD *anfd = anfds + fd; |
|
|
213 | struct ev_io *w; |
|
|
214 | |
|
|
215 | int events = 0; |
|
|
216 | |
|
|
217 | for (w = anfd->head; w; w = w->next) |
|
|
218 | events |= w->events; |
|
|
219 | |
|
|
220 | anfd->events &= ~EV_REIFY; |
|
|
221 | |
|
|
222 | if (anfd->events != events) |
|
|
223 | { |
|
|
224 | method_modify (fd, anfd->events, events); |
|
|
225 | anfd->events = events; |
|
|
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | fdchangecnt = 0; |
|
|
230 | } |
|
|
231 | |
|
|
232 | static void |
|
|
233 | fd_change (int fd) |
|
|
234 | { |
|
|
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
|
|
236 | return; |
|
|
237 | |
|
|
238 | anfds [fd].events |= EV_REIFY; |
|
|
239 | |
|
|
240 | ++fdchangecnt; |
|
|
241 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
242 | fdchanges [fdchangecnt - 1] = fd; |
189 | } |
243 | } |
190 | |
244 | |
191 | /* called on EBADF to verify fds */ |
245 | /* called on EBADF to verify fds */ |
192 | static void |
246 | static void |
193 | fd_recheck () |
247 | fd_recheck (void) |
194 | { |
248 | { |
195 | int fd; |
249 | int fd; |
196 | |
250 | |
197 | for (fd = 0; fd < anfdmax; ++fd) |
251 | for (fd = 0; fd < anfdmax; ++fd) |
198 | if (anfds [fd].wev) |
252 | if (anfds [fd].events) |
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 | while (anfds [fd].head) |
254 | while (anfds [fd].head) |
|
|
255 | { |
|
|
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
201 | evio_stop (anfds [fd].head); |
257 | ev_io_stop (anfds [fd].head); |
|
|
258 | } |
202 | } |
259 | } |
203 | |
260 | |
204 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
205 | |
262 | |
206 | static struct ev_timer **timers; |
263 | static struct ev_timer **timers; |
… | |
… | |
315 | |
372 | |
316 | /* rather than sort out wether we really need nb, set it */ |
373 | /* rather than sort out wether we really need nb, set it */ |
317 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
374 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
318 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
375 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
319 | |
376 | |
320 | evio_set (&sigev, sigpipe [0], EV_READ); |
377 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
321 | evio_start (&sigev); |
378 | ev_io_start (&sigev); |
322 | } |
379 | } |
323 | |
380 | |
324 | /*****************************************************************************/ |
381 | /*****************************************************************************/ |
325 | |
382 | |
326 | static struct ev_idle **idles; |
383 | static struct ev_idle **idles; |
… | |
… | |
332 | static struct ev_check **checks; |
389 | static struct ev_check **checks; |
333 | static int checkmax, checkcnt; |
390 | static int checkmax, checkcnt; |
334 | |
391 | |
335 | /*****************************************************************************/ |
392 | /*****************************************************************************/ |
336 | |
393 | |
|
|
394 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
395 | static struct ev_signal childev; |
|
|
396 | |
|
|
397 | #ifndef WCONTINUED |
|
|
398 | # define WCONTINUED 0 |
|
|
399 | #endif |
|
|
400 | |
|
|
401 | static void |
|
|
402 | childcb (struct ev_signal *sw, int revents) |
|
|
403 | { |
|
|
404 | struct ev_child *w; |
|
|
405 | int pid, status; |
|
|
406 | |
|
|
407 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
408 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
409 | if (w->pid == pid || w->pid == -1) |
|
|
410 | { |
|
|
411 | w->status = status; |
|
|
412 | event ((W)w, EV_CHILD); |
|
|
413 | } |
|
|
414 | } |
|
|
415 | |
|
|
416 | /*****************************************************************************/ |
|
|
417 | |
337 | #if HAVE_EPOLL |
418 | #if EV_USE_EPOLL |
338 | # include "ev_epoll.c" |
419 | # include "ev_epoll.c" |
339 | #endif |
420 | #endif |
340 | #if HAVE_SELECT |
421 | #if EV_USE_SELECT |
341 | # include "ev_select.c" |
422 | # include "ev_select.c" |
342 | #endif |
423 | #endif |
343 | |
424 | |
|
|
425 | int |
|
|
426 | ev_version_major (void) |
|
|
427 | { |
|
|
428 | return EV_VERSION_MAJOR; |
|
|
429 | } |
|
|
430 | |
|
|
431 | int |
|
|
432 | ev_version_minor (void) |
|
|
433 | { |
|
|
434 | return EV_VERSION_MINOR; |
|
|
435 | } |
|
|
436 | |
344 | int ev_init (int flags) |
437 | int ev_init (int flags) |
345 | { |
438 | { |
346 | #if HAVE_MONOTONIC |
|
|
347 | { |
|
|
348 | struct timespec ts; |
|
|
349 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
350 | have_monotonic = 1; |
|
|
351 | } |
|
|
352 | #endif |
|
|
353 | |
|
|
354 | ev_now = ev_time (); |
|
|
355 | now = get_clock (); |
|
|
356 | diff = ev_now - now; |
|
|
357 | |
|
|
358 | if (pipe (sigpipe)) |
|
|
359 | return 0; |
|
|
360 | |
|
|
361 | ev_method = EVMETHOD_NONE; |
|
|
362 | #if HAVE_EPOLL |
|
|
363 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
364 | #endif |
|
|
365 | #if HAVE_SELECT |
|
|
366 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
367 | #endif |
|
|
368 | |
|
|
369 | if (ev_method) |
439 | if (!ev_method) |
|
|
440 | { |
|
|
441 | #if EV_USE_MONOTONIC |
370 | { |
442 | { |
|
|
443 | struct timespec ts; |
|
|
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
445 | have_monotonic = 1; |
|
|
446 | } |
|
|
447 | #endif |
|
|
448 | |
|
|
449 | ev_now = ev_time (); |
|
|
450 | now = get_clock (); |
|
|
451 | diff = ev_now - now; |
|
|
452 | |
|
|
453 | if (pipe (sigpipe)) |
|
|
454 | return 0; |
|
|
455 | |
|
|
456 | ev_method = EVMETHOD_NONE; |
|
|
457 | #if EV_USE_EPOLL |
|
|
458 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
459 | #endif |
|
|
460 | #if EV_USE_SELECT |
|
|
461 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
462 | #endif |
|
|
463 | |
|
|
464 | if (ev_method) |
|
|
465 | { |
371 | evw_init (&sigev, sigcb); |
466 | ev_watcher_init (&sigev, sigcb); |
372 | siginit (); |
467 | siginit (); |
|
|
468 | |
|
|
469 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
470 | ev_signal_start (&childev); |
|
|
471 | } |
373 | } |
472 | } |
374 | |
473 | |
375 | return ev_method; |
474 | return ev_method; |
376 | } |
475 | } |
377 | |
476 | |
378 | /*****************************************************************************/ |
477 | /*****************************************************************************/ |
379 | |
478 | |
|
|
479 | void |
380 | void ev_prefork (void) |
480 | ev_prefork (void) |
381 | { |
481 | { |
382 | /* nop */ |
482 | /* nop */ |
383 | } |
483 | } |
384 | |
484 | |
|
|
485 | void |
385 | void ev_postfork_parent (void) |
486 | ev_postfork_parent (void) |
386 | { |
487 | { |
387 | /* nop */ |
488 | /* nop */ |
388 | } |
489 | } |
389 | |
490 | |
|
|
491 | void |
390 | void ev_postfork_child (void) |
492 | ev_postfork_child (void) |
391 | { |
493 | { |
392 | #if HAVE_EPOLL |
494 | #if EV_USE_EPOLL |
393 | if (ev_method == EVMETHOD_EPOLL) |
495 | if (ev_method == EVMETHOD_EPOLL) |
394 | epoll_postfork_child (); |
496 | epoll_postfork_child (); |
395 | #endif |
497 | #endif |
396 | |
498 | |
397 | evio_stop (&sigev); |
499 | ev_io_stop (&sigev); |
398 | close (sigpipe [0]); |
500 | close (sigpipe [0]); |
399 | close (sigpipe [1]); |
501 | close (sigpipe [1]); |
400 | pipe (sigpipe); |
502 | pipe (sigpipe); |
401 | siginit (); |
503 | siginit (); |
402 | } |
504 | } |
403 | |
505 | |
404 | /*****************************************************************************/ |
506 | /*****************************************************************************/ |
405 | |
507 | |
406 | static void |
508 | static void |
407 | fd_reify (void) |
|
|
408 | { |
|
|
409 | int i; |
|
|
410 | |
|
|
411 | for (i = 0; i < fdchangecnt; ++i) |
|
|
412 | { |
|
|
413 | int fd = fdchanges [i]; |
|
|
414 | ANFD *anfd = anfds + fd; |
|
|
415 | struct ev_io *w; |
|
|
416 | |
|
|
417 | int wev = 0; |
|
|
418 | |
|
|
419 | for (w = anfd->head; w; w = w->next) |
|
|
420 | wev |= w->events; |
|
|
421 | |
|
|
422 | if (anfd->wev != wev) |
|
|
423 | { |
|
|
424 | method_modify (fd, anfd->wev, wev); |
|
|
425 | anfd->wev = wev; |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | fdchangecnt = 0; |
|
|
430 | } |
|
|
431 | |
|
|
432 | static void |
|
|
433 | call_pending () |
509 | call_pending (void) |
434 | { |
510 | { |
435 | while (pendingcnt) |
511 | while (pendingcnt) |
436 | { |
512 | { |
437 | ANPENDING *p = pendings + --pendingcnt; |
513 | ANPENDING *p = pendings + --pendingcnt; |
438 | |
514 | |
… | |
… | |
443 | } |
519 | } |
444 | } |
520 | } |
445 | } |
521 | } |
446 | |
522 | |
447 | static void |
523 | static void |
448 | timers_reify () |
524 | timers_reify (void) |
449 | { |
525 | { |
450 | while (timercnt && timers [0]->at <= now) |
526 | while (timercnt && timers [0]->at <= now) |
451 | { |
527 | { |
452 | struct ev_timer *w = timers [0]; |
528 | struct ev_timer *w = timers [0]; |
453 | |
|
|
454 | event ((W)w, EV_TIMEOUT); |
|
|
455 | |
529 | |
456 | /* first reschedule or stop timer */ |
530 | /* first reschedule or stop timer */ |
457 | if (w->repeat) |
531 | if (w->repeat) |
458 | { |
532 | { |
459 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
460 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
461 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
462 | } |
536 | } |
463 | else |
537 | else |
464 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
465 | } |
|
|
466 | } |
|
|
467 | |
539 | |
|
|
540 | event ((W)w, EV_TIMEOUT); |
|
|
541 | } |
|
|
542 | } |
|
|
543 | |
468 | static void |
544 | static void |
469 | periodics_reify () |
545 | periodics_reify (void) |
470 | { |
546 | { |
471 | while (periodiccnt && periodics [0]->at <= ev_now) |
547 | while (periodiccnt && periodics [0]->at <= ev_now) |
472 | { |
548 | { |
473 | struct ev_periodic *w = periodics [0]; |
549 | struct ev_periodic *w = periodics [0]; |
474 | |
550 | |
… | |
… | |
478 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
479 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
555 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
480 | downheap ((WT *)periodics, periodiccnt, 0); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
481 | } |
557 | } |
482 | else |
558 | else |
483 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
484 | |
560 | |
485 | event ((W)w, EV_TIMEOUT); |
561 | event ((W)w, EV_TIMEOUT); |
486 | } |
562 | } |
487 | } |
563 | } |
488 | |
564 | |
… | |
… | |
500 | { |
576 | { |
501 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
577 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
502 | |
578 | |
503 | if (fabs (diff) >= 1e-4) |
579 | if (fabs (diff) >= 1e-4) |
504 | { |
580 | { |
505 | evperiodic_stop (w); |
581 | ev_periodic_stop (w); |
506 | evperiodic_start (w); |
582 | ev_periodic_start (w); |
507 | |
583 | |
508 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
584 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
509 | } |
585 | } |
510 | } |
586 | } |
511 | } |
587 | } |
512 | } |
588 | } |
513 | |
589 | |
514 | static void |
590 | static void |
515 | time_update () |
591 | time_update (void) |
516 | { |
592 | { |
517 | int i; |
593 | int i; |
518 | |
594 | |
519 | ev_now = ev_time (); |
595 | ev_now = ev_time (); |
520 | |
596 | |
… | |
… | |
554 | int ev_loop_done; |
630 | int ev_loop_done; |
555 | |
631 | |
556 | void ev_loop (int flags) |
632 | void ev_loop (int flags) |
557 | { |
633 | { |
558 | double block; |
634 | double block; |
559 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
560 | |
636 | |
561 | do |
637 | do |
562 | { |
638 | { |
563 | /* queue check watchers (and execute them) */ |
639 | /* queue check watchers (and execute them) */ |
564 | if (checkcnt) |
640 | if (preparecnt) |
565 | { |
641 | { |
566 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
642 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
567 | call_pending (); |
643 | call_pending (); |
568 | } |
644 | } |
569 | |
645 | |
570 | /* update fd-related kernel structures */ |
646 | /* update fd-related kernel structures */ |
571 | fd_reify (); |
647 | fd_reify (); |
572 | |
648 | |
573 | /* calculate blocking time */ |
649 | /* calculate blocking time */ |
574 | |
650 | |
575 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
651 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
652 | always have timers, we just calculate it always */ |
576 | ev_now = ev_time (); |
653 | ev_now = ev_time (); |
577 | |
654 | |
578 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
655 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
579 | block = 0.; |
656 | block = 0.; |
580 | else |
657 | else |
… | |
… | |
668 | } |
745 | } |
669 | |
746 | |
670 | /*****************************************************************************/ |
747 | /*****************************************************************************/ |
671 | |
748 | |
672 | void |
749 | void |
673 | evio_start (struct ev_io *w) |
750 | ev_io_start (struct ev_io *w) |
674 | { |
751 | { |
675 | if (ev_is_active (w)) |
752 | if (ev_is_active (w)) |
676 | return; |
753 | return; |
677 | |
754 | |
678 | int fd = w->fd; |
755 | int fd = w->fd; |
679 | |
756 | |
680 | ev_start ((W)w, 1); |
757 | ev_start ((W)w, 1); |
681 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
682 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
683 | |
760 | |
684 | ++fdchangecnt; |
761 | fd_change (fd); |
685 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
686 | fdchanges [fdchangecnt - 1] = fd; |
|
|
687 | } |
762 | } |
688 | |
763 | |
689 | void |
764 | void |
690 | evio_stop (struct ev_io *w) |
765 | ev_io_stop (struct ev_io *w) |
691 | { |
766 | { |
692 | ev_clear ((W)w); |
767 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
768 | if (!ev_is_active (w)) |
694 | return; |
769 | return; |
695 | |
770 | |
696 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
697 | ev_stop ((W)w); |
772 | ev_stop ((W)w); |
698 | |
773 | |
699 | ++fdchangecnt; |
774 | fd_change (w->fd); |
700 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
701 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
702 | } |
775 | } |
703 | |
776 | |
704 | void |
777 | void |
705 | evtimer_start (struct ev_timer *w) |
778 | ev_timer_start (struct ev_timer *w) |
706 | { |
779 | { |
707 | if (ev_is_active (w)) |
780 | if (ev_is_active (w)) |
708 | return; |
781 | return; |
709 | |
782 | |
710 | w->at += now; |
783 | w->at += now; |
… | |
… | |
716 | timers [timercnt - 1] = w; |
789 | timers [timercnt - 1] = w; |
717 | upheap ((WT *)timers, timercnt - 1); |
790 | upheap ((WT *)timers, timercnt - 1); |
718 | } |
791 | } |
719 | |
792 | |
720 | void |
793 | void |
721 | evtimer_stop (struct ev_timer *w) |
794 | ev_timer_stop (struct ev_timer *w) |
722 | { |
795 | { |
723 | ev_clear ((W)w); |
796 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
725 | return; |
798 | return; |
726 | |
799 | |
… | |
… | |
734 | |
807 | |
735 | ev_stop ((W)w); |
808 | ev_stop ((W)w); |
736 | } |
809 | } |
737 | |
810 | |
738 | void |
811 | void |
739 | evtimer_again (struct ev_timer *w) |
812 | ev_timer_again (struct ev_timer *w) |
740 | { |
813 | { |
741 | if (ev_is_active (w)) |
814 | if (ev_is_active (w)) |
742 | { |
815 | { |
743 | if (w->repeat) |
816 | if (w->repeat) |
744 | { |
817 | { |
745 | w->at = now + w->repeat; |
818 | w->at = now + w->repeat; |
746 | downheap ((WT *)timers, timercnt, w->active - 1); |
819 | downheap ((WT *)timers, timercnt, w->active - 1); |
747 | } |
820 | } |
748 | else |
821 | else |
749 | evtimer_stop (w); |
822 | ev_timer_stop (w); |
750 | } |
823 | } |
751 | else if (w->repeat) |
824 | else if (w->repeat) |
752 | evtimer_start (w); |
825 | ev_timer_start (w); |
753 | } |
826 | } |
754 | |
827 | |
755 | void |
828 | void |
756 | evperiodic_start (struct ev_periodic *w) |
829 | ev_periodic_start (struct ev_periodic *w) |
757 | { |
830 | { |
758 | if (ev_is_active (w)) |
831 | if (ev_is_active (w)) |
759 | return; |
832 | return; |
760 | |
833 | |
761 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
769 | periodics [periodiccnt - 1] = w; |
842 | periodics [periodiccnt - 1] = w; |
770 | upheap ((WT *)periodics, periodiccnt - 1); |
843 | upheap ((WT *)periodics, periodiccnt - 1); |
771 | } |
844 | } |
772 | |
845 | |
773 | void |
846 | void |
774 | evperiodic_stop (struct ev_periodic *w) |
847 | ev_periodic_stop (struct ev_periodic *w) |
775 | { |
848 | { |
776 | ev_clear ((W)w); |
849 | ev_clear ((W)w); |
777 | if (!ev_is_active (w)) |
850 | if (!ev_is_active (w)) |
778 | return; |
851 | return; |
779 | |
852 | |
… | |
… | |
785 | |
858 | |
786 | ev_stop ((W)w); |
859 | ev_stop ((W)w); |
787 | } |
860 | } |
788 | |
861 | |
789 | void |
862 | void |
790 | evsignal_start (struct ev_signal *w) |
863 | ev_signal_start (struct ev_signal *w) |
791 | { |
864 | { |
792 | if (ev_is_active (w)) |
865 | if (ev_is_active (w)) |
793 | return; |
866 | return; |
794 | |
867 | |
795 | ev_start ((W)w, 1); |
868 | ev_start ((W)w, 1); |
… | |
… | |
805 | sigaction (w->signum, &sa, 0); |
878 | sigaction (w->signum, &sa, 0); |
806 | } |
879 | } |
807 | } |
880 | } |
808 | |
881 | |
809 | void |
882 | void |
810 | evsignal_stop (struct ev_signal *w) |
883 | ev_signal_stop (struct ev_signal *w) |
811 | { |
884 | { |
812 | ev_clear ((W)w); |
885 | ev_clear ((W)w); |
813 | if (!ev_is_active (w)) |
886 | if (!ev_is_active (w)) |
814 | return; |
887 | return; |
815 | |
888 | |
… | |
… | |
818 | |
891 | |
819 | if (!signals [w->signum - 1].head) |
892 | if (!signals [w->signum - 1].head) |
820 | signal (w->signum, SIG_DFL); |
893 | signal (w->signum, SIG_DFL); |
821 | } |
894 | } |
822 | |
895 | |
|
|
896 | void |
823 | void evidle_start (struct ev_idle *w) |
897 | ev_idle_start (struct ev_idle *w) |
824 | { |
898 | { |
825 | if (ev_is_active (w)) |
899 | if (ev_is_active (w)) |
826 | return; |
900 | return; |
827 | |
901 | |
828 | ev_start ((W)w, ++idlecnt); |
902 | ev_start ((W)w, ++idlecnt); |
829 | array_needsize (idles, idlemax, idlecnt, ); |
903 | array_needsize (idles, idlemax, idlecnt, ); |
830 | idles [idlecnt - 1] = w; |
904 | idles [idlecnt - 1] = w; |
831 | } |
905 | } |
832 | |
906 | |
|
|
907 | void |
833 | void evidle_stop (struct ev_idle *w) |
908 | ev_idle_stop (struct ev_idle *w) |
834 | { |
909 | { |
835 | ev_clear ((W)w); |
910 | ev_clear ((W)w); |
836 | if (ev_is_active (w)) |
911 | if (ev_is_active (w)) |
837 | return; |
912 | return; |
838 | |
913 | |
839 | idles [w->active - 1] = idles [--idlecnt]; |
914 | idles [w->active - 1] = idles [--idlecnt]; |
840 | ev_stop ((W)w); |
915 | ev_stop ((W)w); |
841 | } |
916 | } |
842 | |
917 | |
|
|
918 | void |
843 | void evprepare_start (struct ev_prepare *w) |
919 | ev_prepare_start (struct ev_prepare *w) |
844 | { |
920 | { |
845 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
846 | return; |
922 | return; |
847 | |
923 | |
848 | ev_start ((W)w, ++preparecnt); |
924 | ev_start ((W)w, ++preparecnt); |
849 | array_needsize (prepares, preparemax, preparecnt, ); |
925 | array_needsize (prepares, preparemax, preparecnt, ); |
850 | prepares [preparecnt - 1] = w; |
926 | prepares [preparecnt - 1] = w; |
851 | } |
927 | } |
852 | |
928 | |
|
|
929 | void |
853 | void evprepare_stop (struct ev_prepare *w) |
930 | ev_prepare_stop (struct ev_prepare *w) |
854 | { |
931 | { |
855 | ev_clear ((W)w); |
932 | ev_clear ((W)w); |
856 | if (ev_is_active (w)) |
933 | if (ev_is_active (w)) |
857 | return; |
934 | return; |
858 | |
935 | |
859 | prepares [w->active - 1] = prepares [--preparecnt]; |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
860 | ev_stop ((W)w); |
937 | ev_stop ((W)w); |
861 | } |
938 | } |
862 | |
939 | |
|
|
940 | void |
863 | void evcheck_start (struct ev_check *w) |
941 | ev_check_start (struct ev_check *w) |
864 | { |
942 | { |
865 | if (ev_is_active (w)) |
943 | if (ev_is_active (w)) |
866 | return; |
944 | return; |
867 | |
945 | |
868 | ev_start ((W)w, ++checkcnt); |
946 | ev_start ((W)w, ++checkcnt); |
869 | array_needsize (checks, checkmax, checkcnt, ); |
947 | array_needsize (checks, checkmax, checkcnt, ); |
870 | checks [checkcnt - 1] = w; |
948 | checks [checkcnt - 1] = w; |
871 | } |
949 | } |
872 | |
950 | |
|
|
951 | void |
873 | void evcheck_stop (struct ev_check *w) |
952 | ev_check_stop (struct ev_check *w) |
874 | { |
953 | { |
875 | ev_clear ((W)w); |
954 | ev_clear ((W)w); |
876 | if (ev_is_active (w)) |
955 | if (ev_is_active (w)) |
877 | return; |
956 | return; |
878 | |
957 | |
879 | checks [w->active - 1] = checks [--checkcnt]; |
958 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
959 | ev_stop ((W)w); |
|
|
960 | } |
|
|
961 | |
|
|
962 | void |
|
|
963 | ev_child_start (struct ev_child *w) |
|
|
964 | { |
|
|
965 | if (ev_is_active (w)) |
|
|
966 | return; |
|
|
967 | |
|
|
968 | ev_start ((W)w, 1); |
|
|
969 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
970 | } |
|
|
971 | |
|
|
972 | void |
|
|
973 | ev_child_stop (struct ev_child *w) |
|
|
974 | { |
|
|
975 | ev_clear ((W)w); |
|
|
976 | if (ev_is_active (w)) |
|
|
977 | return; |
|
|
978 | |
|
|
979 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
880 | ev_stop ((W)w); |
980 | ev_stop ((W)w); |
881 | } |
981 | } |
882 | |
982 | |
883 | /*****************************************************************************/ |
983 | /*****************************************************************************/ |
884 | |
984 | |
… | |
… | |
894 | once_cb (struct ev_once *once, int revents) |
994 | once_cb (struct ev_once *once, int revents) |
895 | { |
995 | { |
896 | void (*cb)(int revents, void *arg) = once->cb; |
996 | void (*cb)(int revents, void *arg) = once->cb; |
897 | void *arg = once->arg; |
997 | void *arg = once->arg; |
898 | |
998 | |
899 | evio_stop (&once->io); |
999 | ev_io_stop (&once->io); |
900 | evtimer_stop (&once->to); |
1000 | ev_timer_stop (&once->to); |
901 | free (once); |
1001 | free (once); |
902 | |
1002 | |
903 | cb (revents, arg); |
1003 | cb (revents, arg); |
904 | } |
1004 | } |
905 | |
1005 | |
… | |
… | |
919 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1019 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
920 | { |
1020 | { |
921 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1021 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
922 | |
1022 | |
923 | if (!once) |
1023 | if (!once) |
924 | cb (EV_ERROR, arg); |
1024 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
925 | else |
1025 | else |
926 | { |
1026 | { |
927 | once->cb = cb; |
1027 | once->cb = cb; |
928 | once->arg = arg; |
1028 | once->arg = arg; |
929 | |
1029 | |
930 | evw_init (&once->io, once_cb_io); |
1030 | ev_watcher_init (&once->io, once_cb_io); |
931 | |
|
|
932 | if (fd >= 0) |
1031 | if (fd >= 0) |
933 | { |
1032 | { |
934 | evio_set (&once->io, fd, events); |
1033 | ev_io_set (&once->io, fd, events); |
935 | evio_start (&once->io); |
1034 | ev_io_start (&once->io); |
936 | } |
1035 | } |
937 | |
1036 | |
938 | evw_init (&once->to, once_cb_to); |
1037 | ev_watcher_init (&once->to, once_cb_to); |
939 | |
|
|
940 | if (timeout >= 0.) |
1038 | if (timeout >= 0.) |
941 | { |
1039 | { |
942 | evtimer_set (&once->to, timeout, 0.); |
1040 | ev_timer_set (&once->to, timeout, 0.); |
943 | evtimer_start (&once->to); |
1041 | ev_timer_start (&once->to); |
944 | } |
1042 | } |
945 | } |
1043 | } |
946 | } |
1044 | } |
947 | |
1045 | |
948 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
… | |
… | |
959 | |
1057 | |
960 | static void |
1058 | static void |
961 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
962 | { |
1060 | { |
963 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
964 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
965 | evtimer_start (w); |
1063 | ev_timer_start (w); |
966 | } |
1064 | } |
967 | |
1065 | |
968 | static void |
1066 | static void |
969 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
970 | { |
1068 | { |
971 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
972 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
973 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
974 | } |
1072 | } |
975 | |
1073 | |
976 | static void |
1074 | static void |
977 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
978 | { |
1076 | { |
… | |
… | |
982 | |
1080 | |
983 | int main (void) |
1081 | int main (void) |
984 | { |
1082 | { |
985 | ev_init (0); |
1083 | ev_init (0); |
986 | |
1084 | |
987 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
988 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
989 | |
1087 | |
990 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
991 | |
1089 | |
992 | #if 0 |
1090 | #if 0 |
993 | int i; |
1091 | int i; |
994 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
995 | { |
1093 | { |
996 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
997 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
998 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
999 | evtimer_start (w); |
1097 | ev_timer_start (w); |
1000 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
1001 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
1002 | } |
1100 | } |
1003 | #endif |
1101 | #endif |
1004 | |
1102 | |
1005 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
1006 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
1007 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
1008 | |
1106 | |
1009 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
1010 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
1011 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
1012 | |
1110 | |
1013 | struct ev_check cw; |
1111 | struct ev_check cw; |
1014 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
1015 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
1016 | |
1114 | |
1017 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
1018 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
1019 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
1020 | |
1118 | |
1021 | ev_loop (0); |
1119 | ev_loop (0); |
1022 | |
1120 | |
1023 | return 0; |
1121 | return 0; |
1024 | } |
1122 | } |