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