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