… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
|
|
29 | #if EV_USE_CONFIG_H |
|
|
30 | # include "config.h" |
|
|
31 | #endif |
29 | |
32 | |
30 | #include <math.h> |
33 | #include <math.h> |
31 | #include <stdlib.h> |
34 | #include <stdlib.h> |
32 | #include <unistd.h> |
35 | #include <unistd.h> |
33 | #include <fcntl.h> |
36 | #include <fcntl.h> |
… | |
… | |
36 | |
39 | |
37 | #include <stdio.h> |
40 | #include <stdio.h> |
38 | |
41 | |
39 | #include <assert.h> |
42 | #include <assert.h> |
40 | #include <errno.h> |
43 | #include <errno.h> |
|
|
44 | #include <sys/types.h> |
|
|
45 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
46 | #include <sys/time.h> |
42 | #include <time.h> |
47 | #include <time.h> |
43 | |
48 | |
44 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
46 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
47 | # endif |
52 | # endif |
48 | #endif |
53 | #endif |
49 | |
54 | |
50 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
51 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
52 | #endif |
57 | #endif |
53 | |
58 | |
54 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
55 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
56 | #endif |
61 | #endif |
57 | |
62 | |
|
|
63 | #ifndef CLOCK_REALTIME |
|
|
64 | # define EV_USE_REALTIME 0 |
|
|
65 | #endif |
58 | #ifndef HAVE_REALTIME |
66 | #ifndef EV_USE_REALTIME |
59 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
60 | #endif |
68 | #endif |
61 | |
69 | |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 | #define MAX_BLOCKTIME 60. |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
|
|
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
64 | |
74 | |
65 | #include "ev.h" |
75 | #include "ev.h" |
66 | |
76 | |
67 | typedef struct ev_watcher *W; |
77 | typedef struct ev_watcher *W; |
68 | typedef struct ev_watcher_list *WL; |
78 | typedef struct ev_watcher_list *WL; |
… | |
… | |
81 | /*****************************************************************************/ |
91 | /*****************************************************************************/ |
82 | |
92 | |
83 | ev_tstamp |
93 | ev_tstamp |
84 | ev_time (void) |
94 | ev_time (void) |
85 | { |
95 | { |
86 | #if HAVE_REALTIME |
96 | #if EV_USE_REALTIME |
87 | struct timespec ts; |
97 | struct timespec ts; |
88 | clock_gettime (CLOCK_REALTIME, &ts); |
98 | clock_gettime (CLOCK_REALTIME, &ts); |
89 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
99 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
90 | #else |
100 | #else |
91 | struct timeval tv; |
101 | struct timeval tv; |
… | |
… | |
95 | } |
105 | } |
96 | |
106 | |
97 | static ev_tstamp |
107 | static ev_tstamp |
98 | get_clock (void) |
108 | get_clock (void) |
99 | { |
109 | { |
100 | #if HAVE_MONOTONIC |
110 | #if EV_USE_MONOTONIC |
101 | if (have_monotonic) |
111 | if (have_monotonic) |
102 | { |
112 | { |
103 | struct timespec ts; |
113 | struct timespec ts; |
104 | clock_gettime (CLOCK_MONOTONIC, &ts); |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
105 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
107 | #endif |
117 | #endif |
108 | |
118 | |
109 | return ev_time (); |
119 | return ev_time (); |
110 | } |
120 | } |
111 | |
121 | |
|
|
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
123 | |
112 | #define array_needsize(base,cur,cnt,init) \ |
124 | #define array_needsize(base,cur,cnt,init) \ |
113 | if ((cnt) > cur) \ |
125 | if ((cnt) > cur) \ |
114 | { \ |
126 | { \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
127 | int newcnt = cur; \ |
|
|
128 | do \ |
|
|
129 | { \ |
|
|
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
|
|
131 | } \ |
|
|
132 | while ((cnt) > newcnt); \ |
|
|
133 | \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
134 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 | init (base + cur, newcnt - cur); \ |
135 | init (base + cur, newcnt - cur); \ |
118 | cur = newcnt; \ |
136 | cur = newcnt; \ |
119 | } |
137 | } |
120 | |
138 | |
121 | /*****************************************************************************/ |
139 | /*****************************************************************************/ |
122 | |
140 | |
123 | typedef struct |
141 | typedef struct |
124 | { |
142 | { |
125 | struct ev_io *head; |
143 | struct ev_io *head; |
126 | unsigned char wev, rev; /* want, received event set */ |
144 | int events; |
127 | } ANFD; |
145 | } ANFD; |
128 | |
146 | |
129 | static ANFD *anfds; |
147 | static ANFD *anfds; |
130 | static int anfdmax; |
148 | static int anfdmax; |
131 | |
149 | |
132 | static int *fdchanges; |
|
|
133 | static int fdchangemax, fdchangecnt; |
|
|
134 | |
|
|
135 | static void |
150 | static void |
136 | anfds_init (ANFD *base, int count) |
151 | anfds_init (ANFD *base, int count) |
137 | { |
152 | { |
138 | while (count--) |
153 | while (count--) |
139 | { |
154 | { |
140 | base->head = 0; |
155 | base->head = 0; |
141 | base->wev = base->rev = EV_NONE; |
156 | base->events = EV_NONE; |
142 | ++base; |
157 | ++base; |
143 | } |
158 | } |
144 | } |
159 | } |
145 | |
160 | |
146 | typedef struct |
161 | typedef struct |
… | |
… | |
153 | static int pendingmax, pendingcnt; |
168 | static int pendingmax, pendingcnt; |
154 | |
169 | |
155 | static void |
170 | static void |
156 | event (W w, int events) |
171 | event (W w, int events) |
157 | { |
172 | { |
158 | if (w->active) |
173 | if (w->pending) |
|
|
174 | { |
|
|
175 | pendings [w->pending - 1].events |= events; |
|
|
176 | return; |
159 | { |
177 | } |
|
|
178 | |
160 | w->pending = ++pendingcnt; |
179 | w->pending = ++pendingcnt; |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
180 | array_needsize (pendings, pendingmax, pendingcnt, ); |
162 | pendings [pendingcnt - 1].w = w; |
181 | pendings [pendingcnt - 1].w = w; |
163 | pendings [pendingcnt - 1].events = events; |
182 | pendings [pendingcnt - 1].events = events; |
164 | } |
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); |
165 | } |
192 | } |
166 | |
193 | |
167 | static void |
194 | static void |
168 | fd_event (int fd, int events) |
195 | fd_event (int fd, int events) |
169 | { |
196 | { |
… | |
… | |
177 | if (ev) |
204 | if (ev) |
178 | event ((W)w, ev); |
205 | event ((W)w, ev); |
179 | } |
206 | } |
180 | } |
207 | } |
181 | |
208 | |
|
|
209 | /*****************************************************************************/ |
|
|
210 | |
|
|
211 | static int *fdchanges; |
|
|
212 | static int fdchangemax, fdchangecnt; |
|
|
213 | |
182 | static void |
214 | static void |
183 | queue_events (W *events, int eventcnt, int type) |
215 | fd_reify (void) |
184 | { |
216 | { |
185 | int i; |
217 | int i; |
186 | |
218 | |
187 | for (i = 0; i < eventcnt; ++i) |
219 | for (i = 0; i < fdchangecnt; ++i) |
188 | 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; |
189 | } |
253 | } |
190 | |
254 | |
191 | /* called on EBADF to verify fds */ |
255 | /* called on EBADF to verify fds */ |
192 | static void |
256 | static void |
193 | fd_recheck () |
257 | fd_recheck (void) |
194 | { |
258 | { |
195 | int fd; |
259 | int fd; |
196 | |
260 | |
197 | for (fd = 0; fd < anfdmax; ++fd) |
261 | for (fd = 0; fd < anfdmax; ++fd) |
198 | if (anfds [fd].wev) |
262 | if (anfds [fd].events) |
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 | while (anfds [fd].head) |
264 | while (anfds [fd].head) |
|
|
265 | { |
201 | 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); |
|
|
268 | } |
202 | } |
269 | } |
203 | |
270 | |
204 | /*****************************************************************************/ |
271 | /*****************************************************************************/ |
205 | |
272 | |
206 | static struct ev_timer **timers; |
273 | static struct ev_timer **timers; |
… | |
… | |
315 | |
382 | |
316 | /* rather than sort out wether we really need nb, set it */ |
383 | /* rather than sort out wether we really need nb, set it */ |
317 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
384 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
318 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
385 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
319 | |
386 | |
320 | evio_set (&sigev, sigpipe [0], EV_READ); |
387 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
321 | evio_start (&sigev); |
388 | ev_io_start (&sigev); |
322 | } |
389 | } |
323 | |
390 | |
324 | /*****************************************************************************/ |
391 | /*****************************************************************************/ |
325 | |
392 | |
326 | static struct ev_idle **idles; |
393 | static struct ev_idle **idles; |
327 | static int idlemax, idlecnt; |
394 | static int idlemax, idlecnt; |
328 | |
395 | |
|
|
396 | static struct ev_prepare **prepares; |
|
|
397 | static int preparemax, preparecnt; |
|
|
398 | |
329 | static struct ev_check **checks; |
399 | static struct ev_check **checks; |
330 | static int checkmax, checkcnt; |
400 | static int checkmax, checkcnt; |
331 | |
401 | |
332 | /*****************************************************************************/ |
402 | /*****************************************************************************/ |
333 | |
403 | |
|
|
404 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
405 | static struct ev_signal childev; |
|
|
406 | |
|
|
407 | #ifndef WCONTINUED |
|
|
408 | # define WCONTINUED 0 |
|
|
409 | #endif |
|
|
410 | |
|
|
411 | static void |
|
|
412 | childcb (struct ev_signal *sw, int revents) |
|
|
413 | { |
|
|
414 | struct ev_child *w; |
|
|
415 | int pid, status; |
|
|
416 | |
|
|
417 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
418 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
419 | if (w->pid == pid || w->pid == -1) |
|
|
420 | { |
|
|
421 | w->status = status; |
|
|
422 | event ((W)w, EV_CHILD); |
|
|
423 | } |
|
|
424 | } |
|
|
425 | |
|
|
426 | /*****************************************************************************/ |
|
|
427 | |
334 | #if HAVE_EPOLL |
428 | #if EV_USE_EPOLL |
335 | # include "ev_epoll.c" |
429 | # include "ev_epoll.c" |
336 | #endif |
430 | #endif |
337 | #if HAVE_SELECT |
431 | #if EV_USE_SELECT |
338 | # include "ev_select.c" |
432 | # include "ev_select.c" |
339 | #endif |
433 | #endif |
340 | |
434 | |
|
|
435 | int |
|
|
436 | ev_version_major (void) |
|
|
437 | { |
|
|
438 | return EV_VERSION_MAJOR; |
|
|
439 | } |
|
|
440 | |
|
|
441 | int |
|
|
442 | ev_version_minor (void) |
|
|
443 | { |
|
|
444 | return EV_VERSION_MINOR; |
|
|
445 | } |
|
|
446 | |
341 | int ev_init (int flags) |
447 | int ev_init (int flags) |
342 | { |
448 | { |
343 | #if HAVE_MONOTONIC |
|
|
344 | { |
|
|
345 | struct timespec ts; |
|
|
346 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
347 | have_monotonic = 1; |
|
|
348 | } |
|
|
349 | #endif |
|
|
350 | |
|
|
351 | ev_now = ev_time (); |
|
|
352 | now = get_clock (); |
|
|
353 | diff = ev_now - now; |
|
|
354 | |
|
|
355 | if (pipe (sigpipe)) |
|
|
356 | return 0; |
|
|
357 | |
|
|
358 | ev_method = EVMETHOD_NONE; |
|
|
359 | #if HAVE_EPOLL |
|
|
360 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
361 | #endif |
|
|
362 | #if HAVE_SELECT |
|
|
363 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
364 | #endif |
|
|
365 | |
|
|
366 | if (ev_method) |
449 | if (!ev_method) |
|
|
450 | { |
|
|
451 | #if EV_USE_MONOTONIC |
367 | { |
452 | { |
|
|
453 | struct timespec ts; |
|
|
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
455 | have_monotonic = 1; |
|
|
456 | } |
|
|
457 | #endif |
|
|
458 | |
|
|
459 | ev_now = ev_time (); |
|
|
460 | now = get_clock (); |
|
|
461 | diff = ev_now - now; |
|
|
462 | |
|
|
463 | if (pipe (sigpipe)) |
|
|
464 | return 0; |
|
|
465 | |
|
|
466 | ev_method = EVMETHOD_NONE; |
|
|
467 | #if EV_USE_EPOLL |
|
|
468 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
469 | #endif |
|
|
470 | #if EV_USE_SELECT |
|
|
471 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
472 | #endif |
|
|
473 | |
|
|
474 | if (ev_method) |
|
|
475 | { |
368 | evw_init (&sigev, sigcb); |
476 | ev_watcher_init (&sigev, sigcb); |
369 | siginit (); |
477 | siginit (); |
|
|
478 | |
|
|
479 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
480 | ev_signal_start (&childev); |
|
|
481 | } |
370 | } |
482 | } |
371 | |
483 | |
372 | return ev_method; |
484 | return ev_method; |
373 | } |
485 | } |
374 | |
486 | |
375 | /*****************************************************************************/ |
487 | /*****************************************************************************/ |
376 | |
488 | |
|
|
489 | void |
377 | void ev_prefork (void) |
490 | ev_prefork (void) |
378 | { |
491 | { |
379 | /* nop */ |
492 | /* nop */ |
380 | } |
493 | } |
381 | |
494 | |
|
|
495 | void |
382 | void ev_postfork_parent (void) |
496 | ev_postfork_parent (void) |
383 | { |
497 | { |
384 | /* nop */ |
498 | /* nop */ |
385 | } |
499 | } |
386 | |
500 | |
|
|
501 | void |
387 | void ev_postfork_child (void) |
502 | ev_postfork_child (void) |
388 | { |
503 | { |
389 | #if HAVE_EPOLL |
504 | #if EV_USE_EPOLL |
390 | if (ev_method == EVMETHOD_EPOLL) |
505 | if (ev_method == EVMETHOD_EPOLL) |
391 | epoll_postfork_child (); |
506 | epoll_postfork_child (); |
392 | #endif |
507 | #endif |
393 | |
508 | |
394 | evio_stop (&sigev); |
509 | ev_io_stop (&sigev); |
395 | close (sigpipe [0]); |
510 | close (sigpipe [0]); |
396 | close (sigpipe [1]); |
511 | close (sigpipe [1]); |
397 | pipe (sigpipe); |
512 | pipe (sigpipe); |
398 | siginit (); |
513 | siginit (); |
399 | } |
514 | } |
400 | |
515 | |
401 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
402 | |
517 | |
403 | static void |
518 | static void |
404 | fd_reify (void) |
|
|
405 | { |
|
|
406 | int i; |
|
|
407 | |
|
|
408 | for (i = 0; i < fdchangecnt; ++i) |
|
|
409 | { |
|
|
410 | int fd = fdchanges [i]; |
|
|
411 | ANFD *anfd = anfds + fd; |
|
|
412 | struct ev_io *w; |
|
|
413 | |
|
|
414 | int wev = 0; |
|
|
415 | |
|
|
416 | for (w = anfd->head; w; w = w->next) |
|
|
417 | wev |= w->events; |
|
|
418 | |
|
|
419 | if (anfd->wev != wev) |
|
|
420 | { |
|
|
421 | method_modify (fd, anfd->wev, wev); |
|
|
422 | anfd->wev = wev; |
|
|
423 | } |
|
|
424 | } |
|
|
425 | |
|
|
426 | fdchangecnt = 0; |
|
|
427 | } |
|
|
428 | |
|
|
429 | static void |
|
|
430 | call_pending () |
519 | call_pending (void) |
431 | { |
520 | { |
432 | while (pendingcnt) |
521 | while (pendingcnt) |
433 | { |
522 | { |
434 | ANPENDING *p = pendings + --pendingcnt; |
523 | ANPENDING *p = pendings + --pendingcnt; |
435 | |
524 | |
… | |
… | |
440 | } |
529 | } |
441 | } |
530 | } |
442 | } |
531 | } |
443 | |
532 | |
444 | static void |
533 | static void |
445 | timers_reify () |
534 | timers_reify (void) |
446 | { |
535 | { |
447 | while (timercnt && timers [0]->at <= now) |
536 | while (timercnt && timers [0]->at <= now) |
448 | { |
537 | { |
449 | struct ev_timer *w = timers [0]; |
538 | struct ev_timer *w = timers [0]; |
450 | |
|
|
451 | event ((W)w, EV_TIMEOUT); |
|
|
452 | |
539 | |
453 | /* first reschedule or stop timer */ |
540 | /* first reschedule or stop timer */ |
454 | if (w->repeat) |
541 | if (w->repeat) |
455 | { |
542 | { |
456 | w->at = now + w->repeat; |
543 | w->at = now + w->repeat; |
457 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
458 | downheap ((WT *)timers, timercnt, 0); |
545 | downheap ((WT *)timers, timercnt, 0); |
459 | } |
546 | } |
460 | else |
547 | else |
461 | evtimer_stop (w); /* nonrepeating: stop timer */ |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
462 | } |
|
|
463 | } |
|
|
464 | |
549 | |
|
|
550 | event ((W)w, EV_TIMEOUT); |
|
|
551 | } |
|
|
552 | } |
|
|
553 | |
465 | static void |
554 | static void |
466 | periodics_reify () |
555 | periodics_reify (void) |
467 | { |
556 | { |
468 | while (periodiccnt && periodics [0]->at <= ev_now) |
557 | while (periodiccnt && periodics [0]->at <= ev_now) |
469 | { |
558 | { |
470 | struct ev_periodic *w = periodics [0]; |
559 | struct ev_periodic *w = periodics [0]; |
471 | |
560 | |
… | |
… | |
475 | 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; |
476 | 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)); |
477 | downheap ((WT *)periodics, periodiccnt, 0); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
478 | } |
567 | } |
479 | else |
568 | else |
480 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
481 | |
570 | |
482 | event ((W)w, EV_TIMEOUT); |
571 | event ((W)w, EV_TIMEOUT); |
483 | } |
572 | } |
484 | } |
573 | } |
485 | |
574 | |
… | |
… | |
497 | { |
586 | { |
498 | 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; |
499 | |
588 | |
500 | if (fabs (diff) >= 1e-4) |
589 | if (fabs (diff) >= 1e-4) |
501 | { |
590 | { |
502 | evperiodic_stop (w); |
591 | ev_periodic_stop (w); |
503 | evperiodic_start (w); |
592 | ev_periodic_start (w); |
504 | |
593 | |
505 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
594 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
506 | } |
595 | } |
507 | } |
596 | } |
508 | } |
597 | } |
509 | } |
598 | } |
510 | |
599 | |
511 | static void |
600 | static void |
512 | time_update () |
601 | time_update (void) |
513 | { |
602 | { |
514 | int i; |
603 | int i; |
515 | |
604 | |
516 | ev_now = ev_time (); |
605 | ev_now = ev_time (); |
517 | |
606 | |
… | |
… | |
551 | int ev_loop_done; |
640 | int ev_loop_done; |
552 | |
641 | |
553 | void ev_loop (int flags) |
642 | void ev_loop (int flags) |
554 | { |
643 | { |
555 | double block; |
644 | double block; |
556 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
645 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
557 | |
|
|
558 | if (checkcnt) |
|
|
559 | { |
|
|
560 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
561 | call_pending (); |
|
|
562 | } |
|
|
563 | |
646 | |
564 | do |
647 | do |
565 | { |
648 | { |
|
|
649 | /* queue check watchers (and execute them) */ |
|
|
650 | if (preparecnt) |
|
|
651 | { |
|
|
652 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
653 | call_pending (); |
|
|
654 | } |
|
|
655 | |
566 | /* update fd-related kernel structures */ |
656 | /* update fd-related kernel structures */ |
567 | fd_reify (); |
657 | fd_reify (); |
568 | |
658 | |
569 | /* calculate blocking time */ |
659 | /* calculate blocking time */ |
570 | |
660 | |
571 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
661 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
662 | always have timers, we just calculate it always */ |
572 | ev_now = ev_time (); |
663 | ev_now = ev_time (); |
573 | |
664 | |
574 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
665 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
575 | block = 0.; |
666 | block = 0.; |
576 | else |
667 | else |
… | |
… | |
596 | |
687 | |
597 | /* update ev_now, do magic */ |
688 | /* update ev_now, do magic */ |
598 | time_update (); |
689 | time_update (); |
599 | |
690 | |
600 | /* queue pending timers and reschedule them */ |
691 | /* queue pending timers and reschedule them */ |
|
|
692 | timers_reify (); /* relative timers called last */ |
601 | periodics_reify (); /* absolute timers first */ |
693 | periodics_reify (); /* absolute timers called first */ |
602 | timers_reify (); /* relative timers second */ |
|
|
603 | |
694 | |
604 | /* queue idle watchers unless io or timers are pending */ |
695 | /* queue idle watchers unless io or timers are pending */ |
605 | if (!pendingcnt) |
696 | if (!pendingcnt) |
606 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
697 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
607 | |
698 | |
608 | /* queue check and possibly idle watchers */ |
699 | /* queue check watchers, to be executed first */ |
|
|
700 | if (checkcnt) |
609 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
701 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
610 | |
702 | |
611 | call_pending (); |
703 | call_pending (); |
612 | } |
704 | } |
613 | while (!ev_loop_done); |
705 | while (!ev_loop_done); |
614 | |
706 | |
… | |
… | |
663 | } |
755 | } |
664 | |
756 | |
665 | /*****************************************************************************/ |
757 | /*****************************************************************************/ |
666 | |
758 | |
667 | void |
759 | void |
668 | evio_start (struct ev_io *w) |
760 | ev_io_start (struct ev_io *w) |
669 | { |
761 | { |
670 | if (ev_is_active (w)) |
762 | if (ev_is_active (w)) |
671 | return; |
763 | return; |
672 | |
764 | |
673 | int fd = w->fd; |
765 | int fd = w->fd; |
674 | |
766 | |
675 | ev_start ((W)w, 1); |
767 | ev_start ((W)w, 1); |
676 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
768 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
677 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
769 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
678 | |
770 | |
679 | ++fdchangecnt; |
771 | fd_change (fd); |
680 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
681 | fdchanges [fdchangecnt - 1] = fd; |
|
|
682 | } |
772 | } |
683 | |
773 | |
684 | void |
774 | void |
685 | evio_stop (struct ev_io *w) |
775 | ev_io_stop (struct ev_io *w) |
686 | { |
776 | { |
687 | ev_clear ((W)w); |
777 | ev_clear ((W)w); |
688 | if (!ev_is_active (w)) |
778 | if (!ev_is_active (w)) |
689 | return; |
779 | return; |
690 | |
780 | |
691 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
781 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
692 | ev_stop ((W)w); |
782 | ev_stop ((W)w); |
693 | |
783 | |
694 | ++fdchangecnt; |
784 | fd_change (w->fd); |
695 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
696 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
697 | } |
785 | } |
698 | |
786 | |
699 | void |
787 | void |
700 | evtimer_start (struct ev_timer *w) |
788 | ev_timer_start (struct ev_timer *w) |
701 | { |
789 | { |
702 | if (ev_is_active (w)) |
790 | if (ev_is_active (w)) |
703 | return; |
791 | return; |
704 | |
792 | |
705 | w->at += now; |
793 | w->at += now; |
… | |
… | |
711 | timers [timercnt - 1] = w; |
799 | timers [timercnt - 1] = w; |
712 | upheap ((WT *)timers, timercnt - 1); |
800 | upheap ((WT *)timers, timercnt - 1); |
713 | } |
801 | } |
714 | |
802 | |
715 | void |
803 | void |
716 | evtimer_stop (struct ev_timer *w) |
804 | ev_timer_stop (struct ev_timer *w) |
717 | { |
805 | { |
718 | ev_clear ((W)w); |
806 | ev_clear ((W)w); |
719 | if (!ev_is_active (w)) |
807 | if (!ev_is_active (w)) |
720 | return; |
808 | return; |
721 | |
809 | |
… | |
… | |
729 | |
817 | |
730 | ev_stop ((W)w); |
818 | ev_stop ((W)w); |
731 | } |
819 | } |
732 | |
820 | |
733 | void |
821 | void |
734 | evtimer_again (struct ev_timer *w) |
822 | ev_timer_again (struct ev_timer *w) |
735 | { |
823 | { |
736 | if (ev_is_active (w)) |
824 | if (ev_is_active (w)) |
737 | { |
825 | { |
738 | if (w->repeat) |
826 | if (w->repeat) |
739 | { |
827 | { |
740 | w->at = now + w->repeat; |
828 | w->at = now + w->repeat; |
741 | downheap ((WT *)timers, timercnt, w->active - 1); |
829 | downheap ((WT *)timers, timercnt, w->active - 1); |
742 | } |
830 | } |
743 | else |
831 | else |
744 | evtimer_stop (w); |
832 | ev_timer_stop (w); |
745 | } |
833 | } |
746 | else if (w->repeat) |
834 | else if (w->repeat) |
747 | evtimer_start (w); |
835 | ev_timer_start (w); |
748 | } |
836 | } |
749 | |
837 | |
750 | void |
838 | void |
751 | evperiodic_start (struct ev_periodic *w) |
839 | ev_periodic_start (struct ev_periodic *w) |
752 | { |
840 | { |
753 | if (ev_is_active (w)) |
841 | if (ev_is_active (w)) |
754 | return; |
842 | return; |
755 | |
843 | |
756 | 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.)); |
… | |
… | |
764 | periodics [periodiccnt - 1] = w; |
852 | periodics [periodiccnt - 1] = w; |
765 | upheap ((WT *)periodics, periodiccnt - 1); |
853 | upheap ((WT *)periodics, periodiccnt - 1); |
766 | } |
854 | } |
767 | |
855 | |
768 | void |
856 | void |
769 | evperiodic_stop (struct ev_periodic *w) |
857 | ev_periodic_stop (struct ev_periodic *w) |
770 | { |
858 | { |
771 | ev_clear ((W)w); |
859 | ev_clear ((W)w); |
772 | if (!ev_is_active (w)) |
860 | if (!ev_is_active (w)) |
773 | return; |
861 | return; |
774 | |
862 | |
… | |
… | |
780 | |
868 | |
781 | ev_stop ((W)w); |
869 | ev_stop ((W)w); |
782 | } |
870 | } |
783 | |
871 | |
784 | void |
872 | void |
785 | evsignal_start (struct ev_signal *w) |
873 | ev_signal_start (struct ev_signal *w) |
786 | { |
874 | { |
787 | if (ev_is_active (w)) |
875 | if (ev_is_active (w)) |
788 | return; |
876 | return; |
789 | |
877 | |
790 | ev_start ((W)w, 1); |
878 | ev_start ((W)w, 1); |
… | |
… | |
800 | sigaction (w->signum, &sa, 0); |
888 | sigaction (w->signum, &sa, 0); |
801 | } |
889 | } |
802 | } |
890 | } |
803 | |
891 | |
804 | void |
892 | void |
805 | evsignal_stop (struct ev_signal *w) |
893 | ev_signal_stop (struct ev_signal *w) |
806 | { |
894 | { |
807 | ev_clear ((W)w); |
895 | ev_clear ((W)w); |
808 | if (!ev_is_active (w)) |
896 | if (!ev_is_active (w)) |
809 | return; |
897 | return; |
810 | |
898 | |
… | |
… | |
813 | |
901 | |
814 | if (!signals [w->signum - 1].head) |
902 | if (!signals [w->signum - 1].head) |
815 | signal (w->signum, SIG_DFL); |
903 | signal (w->signum, SIG_DFL); |
816 | } |
904 | } |
817 | |
905 | |
|
|
906 | void |
818 | void evidle_start (struct ev_idle *w) |
907 | ev_idle_start (struct ev_idle *w) |
819 | { |
908 | { |
820 | if (ev_is_active (w)) |
909 | if (ev_is_active (w)) |
821 | return; |
910 | return; |
822 | |
911 | |
823 | ev_start ((W)w, ++idlecnt); |
912 | ev_start ((W)w, ++idlecnt); |
824 | array_needsize (idles, idlemax, idlecnt, ); |
913 | array_needsize (idles, idlemax, idlecnt, ); |
825 | idles [idlecnt - 1] = w; |
914 | idles [idlecnt - 1] = w; |
826 | } |
915 | } |
827 | |
916 | |
|
|
917 | void |
828 | void evidle_stop (struct ev_idle *w) |
918 | ev_idle_stop (struct ev_idle *w) |
829 | { |
919 | { |
830 | ev_clear ((W)w); |
920 | ev_clear ((W)w); |
831 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
832 | return; |
922 | return; |
833 | |
923 | |
834 | idles [w->active - 1] = idles [--idlecnt]; |
924 | idles [w->active - 1] = idles [--idlecnt]; |
835 | ev_stop ((W)w); |
925 | ev_stop ((W)w); |
836 | } |
926 | } |
837 | |
927 | |
|
|
928 | void |
|
|
929 | ev_prepare_start (struct ev_prepare *w) |
|
|
930 | { |
|
|
931 | if (ev_is_active (w)) |
|
|
932 | return; |
|
|
933 | |
|
|
934 | ev_start ((W)w, ++preparecnt); |
|
|
935 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
936 | prepares [preparecnt - 1] = w; |
|
|
937 | } |
|
|
938 | |
|
|
939 | void |
|
|
940 | ev_prepare_stop (struct ev_prepare *w) |
|
|
941 | { |
|
|
942 | ev_clear ((W)w); |
|
|
943 | if (ev_is_active (w)) |
|
|
944 | return; |
|
|
945 | |
|
|
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
947 | ev_stop ((W)w); |
|
|
948 | } |
|
|
949 | |
|
|
950 | void |
838 | void evcheck_start (struct ev_check *w) |
951 | ev_check_start (struct ev_check *w) |
839 | { |
952 | { |
840 | if (ev_is_active (w)) |
953 | if (ev_is_active (w)) |
841 | return; |
954 | return; |
842 | |
955 | |
843 | ev_start ((W)w, ++checkcnt); |
956 | ev_start ((W)w, ++checkcnt); |
844 | array_needsize (checks, checkmax, checkcnt, ); |
957 | array_needsize (checks, checkmax, checkcnt, ); |
845 | checks [checkcnt - 1] = w; |
958 | checks [checkcnt - 1] = w; |
846 | } |
959 | } |
847 | |
960 | |
|
|
961 | void |
848 | void evcheck_stop (struct ev_check *w) |
962 | ev_check_stop (struct ev_check *w) |
849 | { |
963 | { |
850 | ev_clear ((W)w); |
964 | ev_clear ((W)w); |
851 | if (ev_is_active (w)) |
965 | if (ev_is_active (w)) |
852 | return; |
966 | return; |
853 | |
967 | |
854 | checks [w->active - 1] = checks [--checkcnt]; |
968 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
969 | ev_stop ((W)w); |
|
|
970 | } |
|
|
971 | |
|
|
972 | void |
|
|
973 | ev_child_start (struct ev_child *w) |
|
|
974 | { |
|
|
975 | if (ev_is_active (w)) |
|
|
976 | return; |
|
|
977 | |
|
|
978 | ev_start ((W)w, 1); |
|
|
979 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
980 | } |
|
|
981 | |
|
|
982 | void |
|
|
983 | ev_child_stop (struct ev_child *w) |
|
|
984 | { |
|
|
985 | ev_clear ((W)w); |
|
|
986 | if (ev_is_active (w)) |
|
|
987 | return; |
|
|
988 | |
|
|
989 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
855 | ev_stop ((W)w); |
990 | ev_stop ((W)w); |
856 | } |
991 | } |
857 | |
992 | |
858 | /*****************************************************************************/ |
993 | /*****************************************************************************/ |
859 | |
994 | |
… | |
… | |
869 | once_cb (struct ev_once *once, int revents) |
1004 | once_cb (struct ev_once *once, int revents) |
870 | { |
1005 | { |
871 | void (*cb)(int revents, void *arg) = once->cb; |
1006 | void (*cb)(int revents, void *arg) = once->cb; |
872 | void *arg = once->arg; |
1007 | void *arg = once->arg; |
873 | |
1008 | |
874 | evio_stop (&once->io); |
1009 | ev_io_stop (&once->io); |
875 | evtimer_stop (&once->to); |
1010 | ev_timer_stop (&once->to); |
876 | free (once); |
1011 | free (once); |
877 | |
1012 | |
878 | cb (revents, arg); |
1013 | cb (revents, arg); |
879 | } |
1014 | } |
880 | |
1015 | |
… | |
… | |
894 | 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) |
895 | { |
1030 | { |
896 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1031 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
897 | |
1032 | |
898 | if (!once) |
1033 | if (!once) |
899 | cb (EV_ERROR, arg); |
1034 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
900 | else |
1035 | else |
901 | { |
1036 | { |
902 | once->cb = cb; |
1037 | once->cb = cb; |
903 | once->arg = arg; |
1038 | once->arg = arg; |
904 | |
1039 | |
905 | evw_init (&once->io, once_cb_io); |
1040 | ev_watcher_init (&once->io, once_cb_io); |
906 | |
|
|
907 | if (fd >= 0) |
1041 | if (fd >= 0) |
908 | { |
1042 | { |
909 | evio_set (&once->io, fd, events); |
1043 | ev_io_set (&once->io, fd, events); |
910 | evio_start (&once->io); |
1044 | ev_io_start (&once->io); |
911 | } |
1045 | } |
912 | |
1046 | |
913 | evw_init (&once->to, once_cb_to); |
1047 | ev_watcher_init (&once->to, once_cb_to); |
914 | |
|
|
915 | if (timeout >= 0.) |
1048 | if (timeout >= 0.) |
916 | { |
1049 | { |
917 | evtimer_set (&once->to, timeout, 0.); |
1050 | ev_timer_set (&once->to, timeout, 0.); |
918 | evtimer_start (&once->to); |
1051 | ev_timer_start (&once->to); |
919 | } |
1052 | } |
920 | } |
1053 | } |
921 | } |
1054 | } |
922 | |
1055 | |
923 | /*****************************************************************************/ |
1056 | /*****************************************************************************/ |
… | |
… | |
934 | |
1067 | |
935 | static void |
1068 | static void |
936 | ocb (struct ev_timer *w, int revents) |
1069 | ocb (struct ev_timer *w, int revents) |
937 | { |
1070 | { |
938 | //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); |
939 | evtimer_stop (w); |
1072 | ev_timer_stop (w); |
940 | evtimer_start (w); |
1073 | ev_timer_start (w); |
941 | } |
1074 | } |
942 | |
1075 | |
943 | static void |
1076 | static void |
944 | scb (struct ev_signal *w, int revents) |
1077 | scb (struct ev_signal *w, int revents) |
945 | { |
1078 | { |
946 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1079 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
947 | evio_stop (&wio); |
1080 | ev_io_stop (&wio); |
948 | evio_start (&wio); |
1081 | ev_io_start (&wio); |
949 | } |
1082 | } |
950 | |
1083 | |
951 | static void |
1084 | static void |
952 | gcb (struct ev_signal *w, int revents) |
1085 | gcb (struct ev_signal *w, int revents) |
953 | { |
1086 | { |
… | |
… | |
957 | |
1090 | |
958 | int main (void) |
1091 | int main (void) |
959 | { |
1092 | { |
960 | ev_init (0); |
1093 | ev_init (0); |
961 | |
1094 | |
962 | evio_init (&wio, sin_cb, 0, EV_READ); |
1095 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
963 | evio_start (&wio); |
1096 | ev_io_start (&wio); |
964 | |
1097 | |
965 | struct ev_timer t[10000]; |
1098 | struct ev_timer t[10000]; |
966 | |
1099 | |
967 | #if 0 |
1100 | #if 0 |
968 | int i; |
1101 | int i; |
969 | for (i = 0; i < 10000; ++i) |
1102 | for (i = 0; i < 10000; ++i) |
970 | { |
1103 | { |
971 | struct ev_timer *w = t + i; |
1104 | struct ev_timer *w = t + i; |
972 | evw_init (w, ocb, i); |
1105 | ev_watcher_init (w, ocb, i); |
973 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1106 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
974 | evtimer_start (w); |
1107 | ev_timer_start (w); |
975 | if (drand48 () < 0.5) |
1108 | if (drand48 () < 0.5) |
976 | evtimer_stop (w); |
1109 | ev_timer_stop (w); |
977 | } |
1110 | } |
978 | #endif |
1111 | #endif |
979 | |
1112 | |
980 | struct ev_timer t1; |
1113 | struct ev_timer t1; |
981 | evtimer_init (&t1, ocb, 5, 10); |
1114 | ev_timer_init (&t1, ocb, 5, 10); |
982 | evtimer_start (&t1); |
1115 | ev_timer_start (&t1); |
983 | |
1116 | |
984 | struct ev_signal sig; |
1117 | struct ev_signal sig; |
985 | evsignal_init (&sig, scb, SIGQUIT); |
1118 | ev_signal_init (&sig, scb, SIGQUIT); |
986 | evsignal_start (&sig); |
1119 | ev_signal_start (&sig); |
987 | |
1120 | |
988 | struct ev_check cw; |
1121 | struct ev_check cw; |
989 | evcheck_init (&cw, gcb); |
1122 | ev_check_init (&cw, gcb); |
990 | evcheck_start (&cw); |
1123 | ev_check_start (&cw); |
991 | |
1124 | |
992 | struct ev_idle iw; |
1125 | struct ev_idle iw; |
993 | evidle_init (&iw, gcb); |
1126 | ev_idle_init (&iw, gcb); |
994 | evidle_start (&iw); |
1127 | ev_idle_start (&iw); |
995 | |
1128 | |
996 | ev_loop (0); |
1129 | ev_loop (0); |
997 | |
1130 | |
998 | return 0; |
1131 | return 0; |
999 | } |
1132 | } |