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