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