… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
|
|
29 | #if EV_USE_CONFIG_H |
|
|
30 | # include "config.h" |
|
|
31 | #endif |
29 | |
32 | |
30 | #include <math.h> |
33 | #include <math.h> |
31 | #include <stdlib.h> |
34 | #include <stdlib.h> |
32 | #include <unistd.h> |
35 | #include <unistd.h> |
33 | #include <fcntl.h> |
36 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
44 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
46 | #include <sys/time.h> |
44 | #include <time.h> |
47 | #include <time.h> |
45 | |
48 | |
46 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
49 | # endif |
52 | # endif |
50 | #endif |
53 | #endif |
51 | |
54 | |
52 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
53 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
54 | #endif |
57 | #endif |
55 | |
58 | |
56 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
57 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
58 | #endif |
61 | #endif |
59 | |
62 | |
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 |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
67 | |
70 | |
68 | #include "ev.h" |
71 | #include "ev.h" |
69 | |
72 | |
70 | typedef struct ev_watcher *W; |
73 | typedef struct ev_watcher *W; |
… | |
… | |
84 | /*****************************************************************************/ |
87 | /*****************************************************************************/ |
85 | |
88 | |
86 | ev_tstamp |
89 | ev_tstamp |
87 | ev_time (void) |
90 | ev_time (void) |
88 | { |
91 | { |
89 | #if HAVE_REALTIME |
92 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
93 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
94 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
95 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
96 | #else |
94 | struct timeval tv; |
97 | struct timeval tv; |
… | |
… | |
98 | } |
101 | } |
99 | |
102 | |
100 | static ev_tstamp |
103 | static ev_tstamp |
101 | get_clock (void) |
104 | get_clock (void) |
102 | { |
105 | { |
103 | #if HAVE_MONOTONIC |
106 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
107 | if (have_monotonic) |
105 | { |
108 | { |
106 | struct timespec ts; |
109 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
110 | #endif |
113 | #endif |
111 | |
114 | |
112 | return ev_time (); |
115 | return ev_time (); |
113 | } |
116 | } |
114 | |
117 | |
|
|
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
119 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
120 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
121 | if ((cnt) > cur) \ |
117 | { \ |
122 | { \ |
118 | 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 | \ |
119 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
130 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
120 | init (base + cur, newcnt - cur); \ |
131 | init (base + cur, newcnt - cur); \ |
121 | cur = newcnt; \ |
132 | cur = newcnt; \ |
122 | } |
133 | } |
123 | |
134 | |
124 | /*****************************************************************************/ |
135 | /*****************************************************************************/ |
125 | |
136 | |
126 | typedef struct |
137 | typedef struct |
127 | { |
138 | { |
128 | struct ev_io *head; |
139 | struct ev_io *head; |
129 | unsigned char wev, rev; /* want, received event set */ |
140 | int events; |
130 | } ANFD; |
141 | } ANFD; |
131 | |
142 | |
132 | static ANFD *anfds; |
143 | static ANFD *anfds; |
133 | static int anfdmax; |
144 | static int anfdmax; |
134 | |
145 | |
135 | static int *fdchanges; |
|
|
136 | static int fdchangemax, fdchangecnt; |
|
|
137 | |
|
|
138 | static void |
146 | static void |
139 | anfds_init (ANFD *base, int count) |
147 | anfds_init (ANFD *base, int count) |
140 | { |
148 | { |
141 | while (count--) |
149 | while (count--) |
142 | { |
150 | { |
143 | base->head = 0; |
151 | base->head = 0; |
144 | base->wev = base->rev = EV_NONE; |
152 | base->events = EV_NONE; |
145 | ++base; |
153 | ++base; |
146 | } |
154 | } |
147 | } |
155 | } |
148 | |
156 | |
149 | typedef struct |
157 | typedef struct |
… | |
… | |
156 | static int pendingmax, pendingcnt; |
164 | static int pendingmax, pendingcnt; |
157 | |
165 | |
158 | static void |
166 | static void |
159 | event (W w, int events) |
167 | event (W w, int events) |
160 | { |
168 | { |
161 | if (w->active) |
|
|
162 | { |
|
|
163 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
164 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
165 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
166 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
167 | } |
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); |
168 | } |
182 | } |
169 | |
183 | |
170 | static void |
184 | static void |
171 | fd_event (int fd, int events) |
185 | fd_event (int fd, int events) |
172 | { |
186 | { |
… | |
… | |
180 | if (ev) |
194 | if (ev) |
181 | event ((W)w, ev); |
195 | event ((W)w, ev); |
182 | } |
196 | } |
183 | } |
197 | } |
184 | |
198 | |
|
|
199 | /*****************************************************************************/ |
|
|
200 | |
|
|
201 | static int *fdchanges; |
|
|
202 | static int fdchangemax, fdchangecnt; |
|
|
203 | |
185 | static void |
204 | static void |
186 | queue_events (W *events, int eventcnt, int type) |
205 | fd_reify (void) |
187 | { |
206 | { |
188 | int i; |
207 | int i; |
189 | |
208 | |
190 | for (i = 0; i < eventcnt; ++i) |
209 | for (i = 0; i < fdchangecnt; ++i) |
191 | 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; |
192 | } |
243 | } |
193 | |
244 | |
194 | /* called on EBADF to verify fds */ |
245 | /* called on EBADF to verify fds */ |
195 | static void |
246 | static void |
196 | fd_recheck () |
247 | fd_recheck (void) |
197 | { |
248 | { |
198 | int fd; |
249 | int fd; |
199 | |
250 | |
200 | for (fd = 0; fd < anfdmax; ++fd) |
251 | for (fd = 0; fd < anfdmax; ++fd) |
201 | if (anfds [fd].wev) |
252 | if (anfds [fd].events) |
202 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
203 | while (anfds [fd].head) |
254 | while (anfds [fd].head) |
|
|
255 | { |
|
|
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
204 | evio_stop (anfds [fd].head); |
257 | ev_io_stop (anfds [fd].head); |
|
|
258 | } |
205 | } |
259 | } |
206 | |
260 | |
207 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
208 | |
262 | |
209 | static struct ev_timer **timers; |
263 | static struct ev_timer **timers; |
… | |
… | |
318 | |
372 | |
319 | /* rather than sort out wether we really need nb, set it */ |
373 | /* rather than sort out wether we really need nb, set it */ |
320 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
374 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
321 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
375 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
322 | |
376 | |
323 | evio_set (&sigev, sigpipe [0], EV_READ); |
377 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
324 | evio_start (&sigev); |
378 | ev_io_start (&sigev); |
325 | } |
379 | } |
326 | |
380 | |
327 | /*****************************************************************************/ |
381 | /*****************************************************************************/ |
328 | |
382 | |
329 | static struct ev_idle **idles; |
383 | static struct ev_idle **idles; |
… | |
… | |
359 | } |
413 | } |
360 | } |
414 | } |
361 | |
415 | |
362 | /*****************************************************************************/ |
416 | /*****************************************************************************/ |
363 | |
417 | |
364 | #if HAVE_EPOLL |
418 | #if EV_USE_EPOLL |
365 | # include "ev_epoll.c" |
419 | # include "ev_epoll.c" |
366 | #endif |
420 | #endif |
367 | #if HAVE_SELECT |
421 | #if EV_USE_SELECT |
368 | # include "ev_select.c" |
422 | # include "ev_select.c" |
369 | #endif |
423 | #endif |
370 | |
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 | |
371 | int ev_init (int flags) |
437 | int ev_init (int flags) |
372 | { |
438 | { |
373 | #if HAVE_MONOTONIC |
|
|
374 | { |
|
|
375 | struct timespec ts; |
|
|
376 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
377 | have_monotonic = 1; |
|
|
378 | } |
|
|
379 | #endif |
|
|
380 | |
|
|
381 | ev_now = ev_time (); |
|
|
382 | now = get_clock (); |
|
|
383 | diff = ev_now - now; |
|
|
384 | |
|
|
385 | if (pipe (sigpipe)) |
|
|
386 | return 0; |
|
|
387 | |
|
|
388 | ev_method = EVMETHOD_NONE; |
|
|
389 | #if HAVE_EPOLL |
|
|
390 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
391 | #endif |
|
|
392 | #if HAVE_SELECT |
|
|
393 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
394 | #endif |
|
|
395 | |
|
|
396 | if (ev_method) |
439 | if (!ev_method) |
|
|
440 | { |
|
|
441 | #if EV_USE_MONOTONIC |
397 | { |
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 | { |
398 | evw_init (&sigev, sigcb); |
466 | ev_watcher_init (&sigev, sigcb); |
399 | siginit (); |
467 | siginit (); |
400 | |
468 | |
401 | evsignal_init (&childev, childcb, SIGCHLD); |
469 | ev_signal_init (&childev, childcb, SIGCHLD); |
402 | evsignal_start (&childev); |
470 | ev_signal_start (&childev); |
|
|
471 | } |
403 | } |
472 | } |
404 | |
473 | |
405 | return ev_method; |
474 | return ev_method; |
406 | } |
475 | } |
407 | |
476 | |
408 | /*****************************************************************************/ |
477 | /*****************************************************************************/ |
409 | |
478 | |
|
|
479 | void |
410 | void ev_prefork (void) |
480 | ev_prefork (void) |
411 | { |
481 | { |
412 | /* nop */ |
482 | /* nop */ |
413 | } |
483 | } |
414 | |
484 | |
|
|
485 | void |
415 | void ev_postfork_parent (void) |
486 | ev_postfork_parent (void) |
416 | { |
487 | { |
417 | /* nop */ |
488 | /* nop */ |
418 | } |
489 | } |
419 | |
490 | |
|
|
491 | void |
420 | void ev_postfork_child (void) |
492 | ev_postfork_child (void) |
421 | { |
493 | { |
422 | #if HAVE_EPOLL |
494 | #if EV_USE_EPOLL |
423 | if (ev_method == EVMETHOD_EPOLL) |
495 | if (ev_method == EVMETHOD_EPOLL) |
424 | epoll_postfork_child (); |
496 | epoll_postfork_child (); |
425 | #endif |
497 | #endif |
426 | |
498 | |
427 | evio_stop (&sigev); |
499 | ev_io_stop (&sigev); |
428 | close (sigpipe [0]); |
500 | close (sigpipe [0]); |
429 | close (sigpipe [1]); |
501 | close (sigpipe [1]); |
430 | pipe (sigpipe); |
502 | pipe (sigpipe); |
431 | siginit (); |
503 | siginit (); |
432 | } |
504 | } |
433 | |
505 | |
434 | /*****************************************************************************/ |
506 | /*****************************************************************************/ |
435 | |
507 | |
436 | static void |
508 | static void |
437 | fd_reify (void) |
|
|
438 | { |
|
|
439 | int i; |
|
|
440 | |
|
|
441 | for (i = 0; i < fdchangecnt; ++i) |
|
|
442 | { |
|
|
443 | int fd = fdchanges [i]; |
|
|
444 | ANFD *anfd = anfds + fd; |
|
|
445 | struct ev_io *w; |
|
|
446 | |
|
|
447 | int wev = 0; |
|
|
448 | |
|
|
449 | for (w = anfd->head; w; w = w->next) |
|
|
450 | wev |= w->events; |
|
|
451 | |
|
|
452 | if (anfd->wev != wev) |
|
|
453 | { |
|
|
454 | method_modify (fd, anfd->wev, wev); |
|
|
455 | anfd->wev = wev; |
|
|
456 | } |
|
|
457 | } |
|
|
458 | |
|
|
459 | fdchangecnt = 0; |
|
|
460 | } |
|
|
461 | |
|
|
462 | static void |
|
|
463 | call_pending () |
509 | call_pending (void) |
464 | { |
510 | { |
465 | while (pendingcnt) |
511 | while (pendingcnt) |
466 | { |
512 | { |
467 | ANPENDING *p = pendings + --pendingcnt; |
513 | ANPENDING *p = pendings + --pendingcnt; |
468 | |
514 | |
… | |
… | |
473 | } |
519 | } |
474 | } |
520 | } |
475 | } |
521 | } |
476 | |
522 | |
477 | static void |
523 | static void |
478 | timers_reify () |
524 | timers_reify (void) |
479 | { |
525 | { |
480 | while (timercnt && timers [0]->at <= now) |
526 | while (timercnt && timers [0]->at <= now) |
481 | { |
527 | { |
482 | struct ev_timer *w = timers [0]; |
528 | struct ev_timer *w = timers [0]; |
483 | |
|
|
484 | event ((W)w, EV_TIMEOUT); |
|
|
485 | |
529 | |
486 | /* first reschedule or stop timer */ |
530 | /* first reschedule or stop timer */ |
487 | if (w->repeat) |
531 | if (w->repeat) |
488 | { |
532 | { |
489 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
490 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
491 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
492 | } |
536 | } |
493 | else |
537 | else |
494 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
495 | } |
|
|
496 | } |
|
|
497 | |
539 | |
|
|
540 | event ((W)w, EV_TIMEOUT); |
|
|
541 | } |
|
|
542 | } |
|
|
543 | |
498 | static void |
544 | static void |
499 | periodics_reify () |
545 | periodics_reify (void) |
500 | { |
546 | { |
501 | while (periodiccnt && periodics [0]->at <= ev_now) |
547 | while (periodiccnt && periodics [0]->at <= ev_now) |
502 | { |
548 | { |
503 | struct ev_periodic *w = periodics [0]; |
549 | struct ev_periodic *w = periodics [0]; |
504 | |
550 | |
… | |
… | |
508 | 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; |
509 | 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)); |
510 | downheap ((WT *)periodics, periodiccnt, 0); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
511 | } |
557 | } |
512 | else |
558 | else |
513 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
514 | |
560 | |
515 | event ((W)w, EV_TIMEOUT); |
561 | event ((W)w, EV_TIMEOUT); |
516 | } |
562 | } |
517 | } |
563 | } |
518 | |
564 | |
… | |
… | |
530 | { |
576 | { |
531 | 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; |
532 | |
578 | |
533 | if (fabs (diff) >= 1e-4) |
579 | if (fabs (diff) >= 1e-4) |
534 | { |
580 | { |
535 | evperiodic_stop (w); |
581 | ev_periodic_stop (w); |
536 | evperiodic_start (w); |
582 | ev_periodic_start (w); |
537 | |
583 | |
538 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
584 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
539 | } |
585 | } |
540 | } |
586 | } |
541 | } |
587 | } |
542 | } |
588 | } |
543 | |
589 | |
544 | static void |
590 | static void |
545 | time_update () |
591 | time_update (void) |
546 | { |
592 | { |
547 | int i; |
593 | int i; |
548 | |
594 | |
549 | ev_now = ev_time (); |
595 | ev_now = ev_time (); |
550 | |
596 | |
… | |
… | |
584 | int ev_loop_done; |
630 | int ev_loop_done; |
585 | |
631 | |
586 | void ev_loop (int flags) |
632 | void ev_loop (int flags) |
587 | { |
633 | { |
588 | double block; |
634 | double block; |
589 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
590 | |
636 | |
591 | do |
637 | do |
592 | { |
638 | { |
593 | /* queue check watchers (and execute them) */ |
639 | /* queue check watchers (and execute them) */ |
594 | if (preparecnt) |
640 | if (preparecnt) |
… | |
… | |
699 | } |
745 | } |
700 | |
746 | |
701 | /*****************************************************************************/ |
747 | /*****************************************************************************/ |
702 | |
748 | |
703 | void |
749 | void |
704 | evio_start (struct ev_io *w) |
750 | ev_io_start (struct ev_io *w) |
705 | { |
751 | { |
706 | if (ev_is_active (w)) |
752 | if (ev_is_active (w)) |
707 | return; |
753 | return; |
708 | |
754 | |
709 | int fd = w->fd; |
755 | int fd = w->fd; |
710 | |
756 | |
711 | ev_start ((W)w, 1); |
757 | ev_start ((W)w, 1); |
712 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
713 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
714 | |
760 | |
715 | ++fdchangecnt; |
761 | fd_change (fd); |
716 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
717 | fdchanges [fdchangecnt - 1] = fd; |
|
|
718 | } |
762 | } |
719 | |
763 | |
720 | void |
764 | void |
721 | evio_stop (struct ev_io *w) |
765 | ev_io_stop (struct ev_io *w) |
722 | { |
766 | { |
723 | ev_clear ((W)w); |
767 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
768 | if (!ev_is_active (w)) |
725 | return; |
769 | return; |
726 | |
770 | |
727 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
728 | ev_stop ((W)w); |
772 | ev_stop ((W)w); |
729 | |
773 | |
730 | ++fdchangecnt; |
774 | fd_change (w->fd); |
731 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
732 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
733 | } |
775 | } |
734 | |
776 | |
735 | void |
777 | void |
736 | evtimer_start (struct ev_timer *w) |
778 | ev_timer_start (struct ev_timer *w) |
737 | { |
779 | { |
738 | if (ev_is_active (w)) |
780 | if (ev_is_active (w)) |
739 | return; |
781 | return; |
740 | |
782 | |
741 | w->at += now; |
783 | w->at += now; |
… | |
… | |
747 | timers [timercnt - 1] = w; |
789 | timers [timercnt - 1] = w; |
748 | upheap ((WT *)timers, timercnt - 1); |
790 | upheap ((WT *)timers, timercnt - 1); |
749 | } |
791 | } |
750 | |
792 | |
751 | void |
793 | void |
752 | evtimer_stop (struct ev_timer *w) |
794 | ev_timer_stop (struct ev_timer *w) |
753 | { |
795 | { |
754 | ev_clear ((W)w); |
796 | ev_clear ((W)w); |
755 | if (!ev_is_active (w)) |
797 | if (!ev_is_active (w)) |
756 | return; |
798 | return; |
757 | |
799 | |
… | |
… | |
765 | |
807 | |
766 | ev_stop ((W)w); |
808 | ev_stop ((W)w); |
767 | } |
809 | } |
768 | |
810 | |
769 | void |
811 | void |
770 | evtimer_again (struct ev_timer *w) |
812 | ev_timer_again (struct ev_timer *w) |
771 | { |
813 | { |
772 | if (ev_is_active (w)) |
814 | if (ev_is_active (w)) |
773 | { |
815 | { |
774 | if (w->repeat) |
816 | if (w->repeat) |
775 | { |
817 | { |
776 | w->at = now + w->repeat; |
818 | w->at = now + w->repeat; |
777 | downheap ((WT *)timers, timercnt, w->active - 1); |
819 | downheap ((WT *)timers, timercnt, w->active - 1); |
778 | } |
820 | } |
779 | else |
821 | else |
780 | evtimer_stop (w); |
822 | ev_timer_stop (w); |
781 | } |
823 | } |
782 | else if (w->repeat) |
824 | else if (w->repeat) |
783 | evtimer_start (w); |
825 | ev_timer_start (w); |
784 | } |
826 | } |
785 | |
827 | |
786 | void |
828 | void |
787 | evperiodic_start (struct ev_periodic *w) |
829 | ev_periodic_start (struct ev_periodic *w) |
788 | { |
830 | { |
789 | if (ev_is_active (w)) |
831 | if (ev_is_active (w)) |
790 | return; |
832 | return; |
791 | |
833 | |
792 | 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.)); |
… | |
… | |
800 | periodics [periodiccnt - 1] = w; |
842 | periodics [periodiccnt - 1] = w; |
801 | upheap ((WT *)periodics, periodiccnt - 1); |
843 | upheap ((WT *)periodics, periodiccnt - 1); |
802 | } |
844 | } |
803 | |
845 | |
804 | void |
846 | void |
805 | evperiodic_stop (struct ev_periodic *w) |
847 | ev_periodic_stop (struct ev_periodic *w) |
806 | { |
848 | { |
807 | ev_clear ((W)w); |
849 | ev_clear ((W)w); |
808 | if (!ev_is_active (w)) |
850 | if (!ev_is_active (w)) |
809 | return; |
851 | return; |
810 | |
852 | |
… | |
… | |
816 | |
858 | |
817 | ev_stop ((W)w); |
859 | ev_stop ((W)w); |
818 | } |
860 | } |
819 | |
861 | |
820 | void |
862 | void |
821 | evsignal_start (struct ev_signal *w) |
863 | ev_signal_start (struct ev_signal *w) |
822 | { |
864 | { |
823 | if (ev_is_active (w)) |
865 | if (ev_is_active (w)) |
824 | return; |
866 | return; |
825 | |
867 | |
826 | ev_start ((W)w, 1); |
868 | ev_start ((W)w, 1); |
… | |
… | |
836 | sigaction (w->signum, &sa, 0); |
878 | sigaction (w->signum, &sa, 0); |
837 | } |
879 | } |
838 | } |
880 | } |
839 | |
881 | |
840 | void |
882 | void |
841 | evsignal_stop (struct ev_signal *w) |
883 | ev_signal_stop (struct ev_signal *w) |
842 | { |
884 | { |
843 | ev_clear ((W)w); |
885 | ev_clear ((W)w); |
844 | if (!ev_is_active (w)) |
886 | if (!ev_is_active (w)) |
845 | return; |
887 | return; |
846 | |
888 | |
… | |
… | |
849 | |
891 | |
850 | if (!signals [w->signum - 1].head) |
892 | if (!signals [w->signum - 1].head) |
851 | signal (w->signum, SIG_DFL); |
893 | signal (w->signum, SIG_DFL); |
852 | } |
894 | } |
853 | |
895 | |
|
|
896 | void |
854 | void evidle_start (struct ev_idle *w) |
897 | ev_idle_start (struct ev_idle *w) |
855 | { |
898 | { |
856 | if (ev_is_active (w)) |
899 | if (ev_is_active (w)) |
857 | return; |
900 | return; |
858 | |
901 | |
859 | ev_start ((W)w, ++idlecnt); |
902 | ev_start ((W)w, ++idlecnt); |
860 | array_needsize (idles, idlemax, idlecnt, ); |
903 | array_needsize (idles, idlemax, idlecnt, ); |
861 | idles [idlecnt - 1] = w; |
904 | idles [idlecnt - 1] = w; |
862 | } |
905 | } |
863 | |
906 | |
|
|
907 | void |
864 | void evidle_stop (struct ev_idle *w) |
908 | ev_idle_stop (struct ev_idle *w) |
865 | { |
909 | { |
866 | ev_clear ((W)w); |
910 | ev_clear ((W)w); |
867 | if (ev_is_active (w)) |
911 | if (ev_is_active (w)) |
868 | return; |
912 | return; |
869 | |
913 | |
870 | idles [w->active - 1] = idles [--idlecnt]; |
914 | idles [w->active - 1] = idles [--idlecnt]; |
871 | ev_stop ((W)w); |
915 | ev_stop ((W)w); |
872 | } |
916 | } |
873 | |
917 | |
|
|
918 | void |
874 | void evprepare_start (struct ev_prepare *w) |
919 | ev_prepare_start (struct ev_prepare *w) |
875 | { |
920 | { |
876 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
877 | return; |
922 | return; |
878 | |
923 | |
879 | ev_start ((W)w, ++preparecnt); |
924 | ev_start ((W)w, ++preparecnt); |
880 | array_needsize (prepares, preparemax, preparecnt, ); |
925 | array_needsize (prepares, preparemax, preparecnt, ); |
881 | prepares [preparecnt - 1] = w; |
926 | prepares [preparecnt - 1] = w; |
882 | } |
927 | } |
883 | |
928 | |
|
|
929 | void |
884 | void evprepare_stop (struct ev_prepare *w) |
930 | ev_prepare_stop (struct ev_prepare *w) |
885 | { |
931 | { |
886 | ev_clear ((W)w); |
932 | ev_clear ((W)w); |
887 | if (ev_is_active (w)) |
933 | if (ev_is_active (w)) |
888 | return; |
934 | return; |
889 | |
935 | |
890 | prepares [w->active - 1] = prepares [--preparecnt]; |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
891 | ev_stop ((W)w); |
937 | ev_stop ((W)w); |
892 | } |
938 | } |
893 | |
939 | |
|
|
940 | void |
894 | void evcheck_start (struct ev_check *w) |
941 | ev_check_start (struct ev_check *w) |
895 | { |
942 | { |
896 | if (ev_is_active (w)) |
943 | if (ev_is_active (w)) |
897 | return; |
944 | return; |
898 | |
945 | |
899 | ev_start ((W)w, ++checkcnt); |
946 | ev_start ((W)w, ++checkcnt); |
900 | array_needsize (checks, checkmax, checkcnt, ); |
947 | array_needsize (checks, checkmax, checkcnt, ); |
901 | checks [checkcnt - 1] = w; |
948 | checks [checkcnt - 1] = w; |
902 | } |
949 | } |
903 | |
950 | |
|
|
951 | void |
904 | void evcheck_stop (struct ev_check *w) |
952 | ev_check_stop (struct ev_check *w) |
905 | { |
953 | { |
906 | ev_clear ((W)w); |
954 | ev_clear ((W)w); |
907 | if (ev_is_active (w)) |
955 | if (ev_is_active (w)) |
908 | return; |
956 | return; |
909 | |
957 | |
910 | checks [w->active - 1] = checks [--checkcnt]; |
958 | checks [w->active - 1] = checks [--checkcnt]; |
911 | ev_stop ((W)w); |
959 | ev_stop ((W)w); |
912 | } |
960 | } |
913 | |
961 | |
|
|
962 | void |
914 | void evchild_start (struct ev_child *w) |
963 | ev_child_start (struct ev_child *w) |
915 | { |
964 | { |
916 | if (ev_is_active (w)) |
965 | if (ev_is_active (w)) |
917 | return; |
966 | return; |
918 | |
967 | |
919 | ev_start ((W)w, 1); |
968 | ev_start ((W)w, 1); |
920 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
969 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
921 | } |
970 | } |
922 | |
971 | |
|
|
972 | void |
923 | void evchild_stop (struct ev_child *w) |
973 | ev_child_stop (struct ev_child *w) |
924 | { |
974 | { |
925 | ev_clear ((W)w); |
975 | ev_clear ((W)w); |
926 | if (ev_is_active (w)) |
976 | if (ev_is_active (w)) |
927 | return; |
977 | return; |
928 | |
978 | |
… | |
… | |
944 | once_cb (struct ev_once *once, int revents) |
994 | once_cb (struct ev_once *once, int revents) |
945 | { |
995 | { |
946 | void (*cb)(int revents, void *arg) = once->cb; |
996 | void (*cb)(int revents, void *arg) = once->cb; |
947 | void *arg = once->arg; |
997 | void *arg = once->arg; |
948 | |
998 | |
949 | evio_stop (&once->io); |
999 | ev_io_stop (&once->io); |
950 | evtimer_stop (&once->to); |
1000 | ev_timer_stop (&once->to); |
951 | free (once); |
1001 | free (once); |
952 | |
1002 | |
953 | cb (revents, arg); |
1003 | cb (revents, arg); |
954 | } |
1004 | } |
955 | |
1005 | |
… | |
… | |
969 | 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) |
970 | { |
1020 | { |
971 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1021 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
972 | |
1022 | |
973 | if (!once) |
1023 | if (!once) |
974 | cb (EV_ERROR, arg); |
1024 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
975 | else |
1025 | else |
976 | { |
1026 | { |
977 | once->cb = cb; |
1027 | once->cb = cb; |
978 | once->arg = arg; |
1028 | once->arg = arg; |
979 | |
1029 | |
980 | evw_init (&once->io, once_cb_io); |
1030 | ev_watcher_init (&once->io, once_cb_io); |
981 | |
|
|
982 | if (fd >= 0) |
1031 | if (fd >= 0) |
983 | { |
1032 | { |
984 | evio_set (&once->io, fd, events); |
1033 | ev_io_set (&once->io, fd, events); |
985 | evio_start (&once->io); |
1034 | ev_io_start (&once->io); |
986 | } |
1035 | } |
987 | |
1036 | |
988 | evw_init (&once->to, once_cb_to); |
1037 | ev_watcher_init (&once->to, once_cb_to); |
989 | |
|
|
990 | if (timeout >= 0.) |
1038 | if (timeout >= 0.) |
991 | { |
1039 | { |
992 | evtimer_set (&once->to, timeout, 0.); |
1040 | ev_timer_set (&once->to, timeout, 0.); |
993 | evtimer_start (&once->to); |
1041 | ev_timer_start (&once->to); |
994 | } |
1042 | } |
995 | } |
1043 | } |
996 | } |
1044 | } |
997 | |
1045 | |
998 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
… | |
… | |
1009 | |
1057 | |
1010 | static void |
1058 | static void |
1011 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
1012 | { |
1060 | { |
1013 | //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); |
1014 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
1015 | evtimer_start (w); |
1063 | ev_timer_start (w); |
1016 | } |
1064 | } |
1017 | |
1065 | |
1018 | static void |
1066 | static void |
1019 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
1020 | { |
1068 | { |
1021 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1022 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
1023 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
1024 | } |
1072 | } |
1025 | |
1073 | |
1026 | static void |
1074 | static void |
1027 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
1028 | { |
1076 | { |
… | |
… | |
1032 | |
1080 | |
1033 | int main (void) |
1081 | int main (void) |
1034 | { |
1082 | { |
1035 | ev_init (0); |
1083 | ev_init (0); |
1036 | |
1084 | |
1037 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1038 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
1039 | |
1087 | |
1040 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
1041 | |
1089 | |
1042 | #if 0 |
1090 | #if 0 |
1043 | int i; |
1091 | int i; |
1044 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
1045 | { |
1093 | { |
1046 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
1047 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
1048 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1049 | evtimer_start (w); |
1097 | ev_timer_start (w); |
1050 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
1051 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
1052 | } |
1100 | } |
1053 | #endif |
1101 | #endif |
1054 | |
1102 | |
1055 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
1056 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
1057 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
1058 | |
1106 | |
1059 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
1060 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
1061 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
1062 | |
1110 | |
1063 | struct ev_check cw; |
1111 | struct ev_check cw; |
1064 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
1065 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
1066 | |
1114 | |
1067 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
1068 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
1069 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
1070 | |
1118 | |
1071 | ev_loop (0); |
1119 | ev_loop (0); |
1072 | |
1120 | |
1073 | return 0; |
1121 | return 0; |
1074 | } |
1122 | } |