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