… | |
… | |
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 | #define HAVE_EPOLL 1 |
|
|
45 | |
|
|
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 |
|
|
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
66 | |
70 | |
67 | #include "ev.h" |
71 | #include "ev.h" |
68 | |
72 | |
69 | typedef struct ev_watcher *W; |
73 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher_list *WL; |
74 | typedef struct ev_watcher_list *WL; |
… | |
… | |
83 | /*****************************************************************************/ |
87 | /*****************************************************************************/ |
84 | |
88 | |
85 | ev_tstamp |
89 | ev_tstamp |
86 | ev_time (void) |
90 | ev_time (void) |
87 | { |
91 | { |
88 | #if HAVE_REALTIME |
92 | #if EV_USE_REALTIME |
89 | struct timespec ts; |
93 | struct timespec ts; |
90 | clock_gettime (CLOCK_REALTIME, &ts); |
94 | clock_gettime (CLOCK_REALTIME, &ts); |
91 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
95 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
92 | #else |
96 | #else |
93 | struct timeval tv; |
97 | struct timeval tv; |
… | |
… | |
97 | } |
101 | } |
98 | |
102 | |
99 | static ev_tstamp |
103 | static ev_tstamp |
100 | get_clock (void) |
104 | get_clock (void) |
101 | { |
105 | { |
102 | #if HAVE_MONOTONIC |
106 | #if EV_USE_MONOTONIC |
103 | if (have_monotonic) |
107 | if (have_monotonic) |
104 | { |
108 | { |
105 | struct timespec ts; |
109 | struct timespec ts; |
106 | clock_gettime (CLOCK_MONOTONIC, &ts); |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
107 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
109 | #endif |
113 | #endif |
110 | |
114 | |
111 | return ev_time (); |
115 | return ev_time (); |
112 | } |
116 | } |
113 | |
117 | |
|
|
118 | #define array_nextsize(n) (((n) << 1) | 4 & ~3) |
|
|
119 | #define array_prevsize(n) (((n) >> 1) | 4 & ~3) |
|
|
120 | |
114 | #define array_needsize(base,cur,cnt,init) \ |
121 | #define array_needsize(base,cur,cnt,init) \ |
115 | if ((cnt) > cur) \ |
122 | if ((cnt) > cur) \ |
116 | { \ |
123 | { \ |
117 | int newcnt = cur ? cur << 1 : 16; \ |
124 | int newcnt = cur; \ |
|
|
125 | do \ |
|
|
126 | { \ |
|
|
127 | newcnt = array_nextsize (newcnt); \ |
|
|
128 | } \ |
|
|
129 | while ((cnt) > newcnt); \ |
|
|
130 | \ |
118 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
131 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
119 | init (base + cur, newcnt - cur); \ |
132 | init (base + cur, newcnt - cur); \ |
120 | cur = newcnt; \ |
133 | cur = newcnt; \ |
121 | } |
134 | } |
122 | |
135 | |
123 | /*****************************************************************************/ |
136 | /*****************************************************************************/ |
124 | |
137 | |
125 | typedef struct |
138 | typedef struct |
126 | { |
139 | { |
127 | struct ev_io *head; |
140 | struct ev_io *head; |
128 | unsigned char wev, rev; /* want, received event set */ |
141 | int events; |
129 | } ANFD; |
142 | } ANFD; |
130 | |
143 | |
131 | static ANFD *anfds; |
144 | static ANFD *anfds; |
132 | static int anfdmax; |
145 | static int anfdmax; |
133 | |
146 | |
134 | static int *fdchanges; |
|
|
135 | static int fdchangemax, fdchangecnt; |
|
|
136 | |
|
|
137 | static void |
147 | static void |
138 | anfds_init (ANFD *base, int count) |
148 | anfds_init (ANFD *base, int count) |
139 | { |
149 | { |
140 | while (count--) |
150 | while (count--) |
141 | { |
151 | { |
142 | base->head = 0; |
152 | base->head = 0; |
143 | base->wev = base->rev = EV_NONE; |
153 | base->events = EV_NONE; |
144 | ++base; |
154 | ++base; |
145 | } |
155 | } |
146 | } |
156 | } |
147 | |
157 | |
148 | typedef struct |
158 | typedef struct |
… | |
… | |
165 | pendings [pendingcnt - 1].events = events; |
175 | pendings [pendingcnt - 1].events = events; |
166 | } |
176 | } |
167 | } |
177 | } |
168 | |
178 | |
169 | 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 |
170 | fd_event (int fd, int events) |
189 | fd_event (int fd, int events) |
171 | { |
190 | { |
172 | ANFD *anfd = anfds + fd; |
191 | ANFD *anfd = anfds + fd; |
173 | struct ev_io *w; |
192 | struct ev_io *w; |
174 | |
193 | |
… | |
… | |
179 | if (ev) |
198 | if (ev) |
180 | event ((W)w, ev); |
199 | event ((W)w, ev); |
181 | } |
200 | } |
182 | } |
201 | } |
183 | |
202 | |
|
|
203 | /*****************************************************************************/ |
|
|
204 | |
|
|
205 | static int *fdchanges; |
|
|
206 | static int fdchangemax, fdchangecnt; |
|
|
207 | |
184 | static void |
208 | static void |
185 | queue_events (W *events, int eventcnt, int type) |
209 | fd_reify (void) |
186 | { |
210 | { |
187 | int i; |
211 | int i; |
188 | |
212 | |
189 | for (i = 0; i < eventcnt; ++i) |
213 | for (i = 0; i < fdchangecnt; ++i) |
190 | 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; |
|
|
247 | } |
|
|
248 | |
|
|
249 | /* called on EBADF to verify fds */ |
|
|
250 | static void |
|
|
251 | fd_recheck (void) |
|
|
252 | { |
|
|
253 | int fd; |
|
|
254 | |
|
|
255 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
256 | if (anfds [fd].events) |
|
|
257 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
258 | while (anfds [fd].head) |
|
|
259 | { |
|
|
260 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
|
|
261 | ev_io_stop (anfds [fd].head); |
|
|
262 | } |
191 | } |
263 | } |
192 | |
264 | |
193 | /*****************************************************************************/ |
265 | /*****************************************************************************/ |
194 | |
266 | |
195 | static struct ev_timer **timers; |
267 | static struct ev_timer **timers; |
… | |
… | |
304 | |
376 | |
305 | /* rather than sort out wether we really need nb, set it */ |
377 | /* rather than sort out wether we really need nb, set it */ |
306 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
378 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
307 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
379 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
308 | |
380 | |
309 | evio_set (&sigev, sigpipe [0], EV_READ); |
381 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
310 | evio_start (&sigev); |
382 | ev_io_start (&sigev); |
311 | } |
383 | } |
312 | |
384 | |
313 | /*****************************************************************************/ |
385 | /*****************************************************************************/ |
314 | |
386 | |
315 | static struct ev_idle **idles; |
387 | static struct ev_idle **idles; |
316 | static int idlemax, idlecnt; |
388 | static int idlemax, idlecnt; |
317 | |
389 | |
|
|
390 | static struct ev_prepare **prepares; |
|
|
391 | static int preparemax, preparecnt; |
|
|
392 | |
318 | static struct ev_check **checks; |
393 | static struct ev_check **checks; |
319 | static int checkmax, checkcnt; |
394 | static int checkmax, checkcnt; |
320 | |
395 | |
321 | /*****************************************************************************/ |
396 | /*****************************************************************************/ |
322 | |
397 | |
|
|
398 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
399 | static struct ev_signal childev; |
|
|
400 | |
|
|
401 | #ifndef WCONTINUED |
|
|
402 | # define WCONTINUED 0 |
|
|
403 | #endif |
|
|
404 | |
|
|
405 | static void |
|
|
406 | childcb (struct ev_signal *sw, int revents) |
|
|
407 | { |
|
|
408 | struct ev_child *w; |
|
|
409 | int pid, status; |
|
|
410 | |
|
|
411 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
412 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
413 | if (w->pid == pid || w->pid == -1) |
|
|
414 | { |
|
|
415 | w->status = status; |
|
|
416 | event ((W)w, EV_CHILD); |
|
|
417 | } |
|
|
418 | } |
|
|
419 | |
|
|
420 | /*****************************************************************************/ |
|
|
421 | |
323 | #if HAVE_EPOLL |
422 | #if EV_USE_EPOLL |
324 | # include "ev_epoll.c" |
423 | # include "ev_epoll.c" |
325 | #endif |
424 | #endif |
326 | #if HAVE_SELECT |
425 | #if EV_USE_SELECT |
327 | # include "ev_select.c" |
426 | # include "ev_select.c" |
328 | #endif |
427 | #endif |
329 | |
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 | |
330 | int ev_init (int flags) |
441 | int ev_init (int flags) |
331 | { |
442 | { |
332 | #if HAVE_MONOTONIC |
|
|
333 | { |
|
|
334 | struct timespec ts; |
|
|
335 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
336 | have_monotonic = 1; |
|
|
337 | } |
|
|
338 | #endif |
|
|
339 | |
|
|
340 | ev_now = ev_time (); |
|
|
341 | now = get_clock (); |
|
|
342 | diff = ev_now - now; |
|
|
343 | |
|
|
344 | if (pipe (sigpipe)) |
|
|
345 | return 0; |
|
|
346 | |
|
|
347 | ev_method = EVMETHOD_NONE; |
|
|
348 | #if HAVE_EPOLL |
|
|
349 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
350 | #endif |
|
|
351 | #if HAVE_SELECT |
|
|
352 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
353 | #endif |
|
|
354 | |
|
|
355 | if (ev_method) |
443 | if (!ev_method) |
|
|
444 | { |
|
|
445 | #if EV_USE_MONOTONIC |
356 | { |
446 | { |
|
|
447 | struct timespec ts; |
|
|
448 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
449 | have_monotonic = 1; |
|
|
450 | } |
|
|
451 | #endif |
|
|
452 | |
|
|
453 | ev_now = ev_time (); |
|
|
454 | now = get_clock (); |
|
|
455 | diff = ev_now - now; |
|
|
456 | |
|
|
457 | if (pipe (sigpipe)) |
|
|
458 | return 0; |
|
|
459 | |
|
|
460 | ev_method = EVMETHOD_NONE; |
|
|
461 | #if EV_USE_EPOLL |
|
|
462 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
463 | #endif |
|
|
464 | #if EV_USE_SELECT |
|
|
465 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
466 | #endif |
|
|
467 | |
|
|
468 | if (ev_method) |
|
|
469 | { |
357 | evw_init (&sigev, sigcb); |
470 | ev_watcher_init (&sigev, sigcb); |
358 | siginit (); |
471 | siginit (); |
|
|
472 | |
|
|
473 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
474 | ev_signal_start (&childev); |
|
|
475 | } |
359 | } |
476 | } |
360 | |
477 | |
361 | return ev_method; |
478 | return ev_method; |
362 | } |
479 | } |
363 | |
480 | |
364 | /*****************************************************************************/ |
481 | /*****************************************************************************/ |
365 | |
482 | |
|
|
483 | void |
366 | void ev_prefork (void) |
484 | ev_prefork (void) |
367 | { |
485 | { |
368 | /* nop */ |
486 | /* nop */ |
369 | } |
487 | } |
370 | |
488 | |
|
|
489 | void |
371 | void ev_postfork_parent (void) |
490 | ev_postfork_parent (void) |
372 | { |
491 | { |
373 | /* nop */ |
492 | /* nop */ |
374 | } |
493 | } |
375 | |
494 | |
|
|
495 | void |
376 | void ev_postfork_child (void) |
496 | ev_postfork_child (void) |
377 | { |
497 | { |
378 | #if HAVE_EPOLL |
498 | #if EV_USE_EPOLL |
379 | if (ev_method == EVMETHOD_EPOLL) |
499 | if (ev_method == EVMETHOD_EPOLL) |
380 | epoll_postfork_child (); |
500 | epoll_postfork_child (); |
381 | #endif |
501 | #endif |
382 | |
502 | |
383 | evio_stop (&sigev); |
503 | ev_io_stop (&sigev); |
384 | close (sigpipe [0]); |
504 | close (sigpipe [0]); |
385 | close (sigpipe [1]); |
505 | close (sigpipe [1]); |
386 | pipe (sigpipe); |
506 | pipe (sigpipe); |
387 | siginit (); |
507 | siginit (); |
388 | } |
508 | } |
389 | |
509 | |
390 | /*****************************************************************************/ |
510 | /*****************************************************************************/ |
391 | |
511 | |
392 | static void |
512 | static void |
393 | fd_reify (void) |
|
|
394 | { |
|
|
395 | int i; |
|
|
396 | |
|
|
397 | for (i = 0; i < fdchangecnt; ++i) |
|
|
398 | { |
|
|
399 | int fd = fdchanges [i]; |
|
|
400 | ANFD *anfd = anfds + fd; |
|
|
401 | struct ev_io *w; |
|
|
402 | |
|
|
403 | int wev = 0; |
|
|
404 | |
|
|
405 | for (w = anfd->head; w; w = w->next) |
|
|
406 | wev |= w->events; |
|
|
407 | |
|
|
408 | if (anfd->wev != wev) |
|
|
409 | { |
|
|
410 | method_modify (fd, anfd->wev, wev); |
|
|
411 | anfd->wev = wev; |
|
|
412 | } |
|
|
413 | } |
|
|
414 | |
|
|
415 | fdchangecnt = 0; |
|
|
416 | } |
|
|
417 | |
|
|
418 | static void |
|
|
419 | call_pending () |
513 | call_pending (void) |
420 | { |
514 | { |
421 | while (pendingcnt) |
515 | while (pendingcnt) |
422 | { |
516 | { |
423 | ANPENDING *p = pendings + --pendingcnt; |
517 | ANPENDING *p = pendings + --pendingcnt; |
424 | |
518 | |
… | |
… | |
429 | } |
523 | } |
430 | } |
524 | } |
431 | } |
525 | } |
432 | |
526 | |
433 | static void |
527 | static void |
434 | timers_reify () |
528 | timers_reify (void) |
435 | { |
529 | { |
436 | while (timercnt && timers [0]->at <= now) |
530 | while (timercnt && timers [0]->at <= now) |
437 | { |
531 | { |
438 | struct ev_timer *w = timers [0]; |
532 | struct ev_timer *w = timers [0]; |
439 | |
533 | |
… | |
… | |
445 | w->at = now + w->repeat; |
539 | w->at = now + w->repeat; |
446 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
540 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
447 | downheap ((WT *)timers, timercnt, 0); |
541 | downheap ((WT *)timers, timercnt, 0); |
448 | } |
542 | } |
449 | else |
543 | else |
450 | evtimer_stop (w); /* nonrepeating: stop timer */ |
544 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
451 | } |
545 | } |
452 | } |
546 | } |
453 | |
547 | |
454 | static void |
548 | static void |
455 | periodics_reify () |
549 | periodics_reify (void) |
456 | { |
550 | { |
457 | while (periodiccnt && periodics [0]->at <= ev_now) |
551 | while (periodiccnt && periodics [0]->at <= ev_now) |
458 | { |
552 | { |
459 | struct ev_periodic *w = periodics [0]; |
553 | struct ev_periodic *w = periodics [0]; |
460 | |
554 | |
… | |
… | |
464 | 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; |
465 | 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)); |
466 | downheap ((WT *)periodics, periodiccnt, 0); |
560 | downheap ((WT *)periodics, periodiccnt, 0); |
467 | } |
561 | } |
468 | else |
562 | else |
469 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
563 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
470 | |
564 | |
471 | event ((W)w, EV_TIMEOUT); |
565 | event ((W)w, EV_TIMEOUT); |
472 | } |
566 | } |
473 | } |
567 | } |
474 | |
568 | |
… | |
… | |
486 | { |
580 | { |
487 | 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; |
488 | |
582 | |
489 | if (fabs (diff) >= 1e-4) |
583 | if (fabs (diff) >= 1e-4) |
490 | { |
584 | { |
491 | evperiodic_stop (w); |
585 | ev_periodic_stop (w); |
492 | evperiodic_start (w); |
586 | ev_periodic_start (w); |
493 | |
587 | |
494 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
588 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
495 | } |
589 | } |
496 | } |
590 | } |
497 | } |
591 | } |
498 | } |
592 | } |
499 | |
593 | |
500 | static void |
594 | static void |
501 | time_update () |
595 | time_update (void) |
502 | { |
596 | { |
503 | int i; |
597 | int i; |
504 | |
598 | |
505 | ev_now = ev_time (); |
599 | ev_now = ev_time (); |
506 | |
600 | |
… | |
… | |
540 | int ev_loop_done; |
634 | int ev_loop_done; |
541 | |
635 | |
542 | void ev_loop (int flags) |
636 | void ev_loop (int flags) |
543 | { |
637 | { |
544 | double block; |
638 | double block; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
639 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
546 | |
|
|
547 | if (checkcnt) |
|
|
548 | { |
|
|
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
550 | call_pending (); |
|
|
551 | } |
|
|
552 | |
640 | |
553 | do |
641 | do |
554 | { |
642 | { |
|
|
643 | /* queue check watchers (and execute them) */ |
|
|
644 | if (preparecnt) |
|
|
645 | { |
|
|
646 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
647 | call_pending (); |
|
|
648 | } |
|
|
649 | |
555 | /* update fd-related kernel structures */ |
650 | /* update fd-related kernel structures */ |
556 | fd_reify (); |
651 | fd_reify (); |
557 | |
652 | |
558 | /* calculate blocking time */ |
653 | /* calculate blocking time */ |
559 | |
654 | |
560 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
655 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
656 | always have timers, we just calculate it always */ |
561 | ev_now = ev_time (); |
657 | ev_now = ev_time (); |
562 | |
658 | |
563 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
659 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
564 | block = 0.; |
660 | block = 0.; |
565 | else |
661 | else |
… | |
… | |
585 | |
681 | |
586 | /* update ev_now, do magic */ |
682 | /* update ev_now, do magic */ |
587 | time_update (); |
683 | time_update (); |
588 | |
684 | |
589 | /* queue pending timers and reschedule them */ |
685 | /* queue pending timers and reschedule them */ |
|
|
686 | timers_reify (); /* relative timers called last */ |
590 | periodics_reify (); /* absolute timers first */ |
687 | periodics_reify (); /* absolute timers called first */ |
591 | timers_reify (); /* relative timers second */ |
|
|
592 | |
688 | |
593 | /* queue idle watchers unless io or timers are pending */ |
689 | /* queue idle watchers unless io or timers are pending */ |
594 | if (!pendingcnt) |
690 | if (!pendingcnt) |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
691 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
596 | |
692 | |
597 | /* queue check and possibly idle watchers */ |
693 | /* queue check watchers, to be executed first */ |
|
|
694 | if (checkcnt) |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
695 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
599 | |
696 | |
600 | call_pending (); |
697 | call_pending (); |
601 | } |
698 | } |
602 | while (!ev_loop_done); |
699 | while (!ev_loop_done); |
603 | |
700 | |
… | |
… | |
652 | } |
749 | } |
653 | |
750 | |
654 | /*****************************************************************************/ |
751 | /*****************************************************************************/ |
655 | |
752 | |
656 | void |
753 | void |
657 | evio_start (struct ev_io *w) |
754 | ev_io_start (struct ev_io *w) |
658 | { |
755 | { |
659 | if (ev_is_active (w)) |
756 | if (ev_is_active (w)) |
660 | return; |
757 | return; |
661 | |
758 | |
662 | int fd = w->fd; |
759 | int fd = w->fd; |
663 | |
760 | |
664 | ev_start ((W)w, 1); |
761 | ev_start ((W)w, 1); |
665 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
762 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
666 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
763 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
667 | |
764 | |
668 | ++fdchangecnt; |
765 | fd_change (fd); |
669 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
670 | fdchanges [fdchangecnt - 1] = fd; |
|
|
671 | } |
766 | } |
672 | |
767 | |
673 | void |
768 | void |
674 | evio_stop (struct ev_io *w) |
769 | ev_io_stop (struct ev_io *w) |
675 | { |
770 | { |
676 | ev_clear ((W)w); |
771 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
772 | if (!ev_is_active (w)) |
678 | return; |
773 | return; |
679 | |
774 | |
680 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
775 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
681 | ev_stop ((W)w); |
776 | ev_stop ((W)w); |
682 | |
777 | |
683 | ++fdchangecnt; |
778 | fd_change (w->fd); |
684 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
685 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
686 | } |
779 | } |
687 | |
780 | |
688 | void |
781 | void |
689 | evtimer_start (struct ev_timer *w) |
782 | ev_timer_start (struct ev_timer *w) |
690 | { |
783 | { |
691 | if (ev_is_active (w)) |
784 | if (ev_is_active (w)) |
692 | return; |
785 | return; |
693 | |
786 | |
694 | w->at += now; |
787 | w->at += now; |
… | |
… | |
700 | timers [timercnt - 1] = w; |
793 | timers [timercnt - 1] = w; |
701 | upheap ((WT *)timers, timercnt - 1); |
794 | upheap ((WT *)timers, timercnt - 1); |
702 | } |
795 | } |
703 | |
796 | |
704 | void |
797 | void |
705 | evtimer_stop (struct ev_timer *w) |
798 | ev_timer_stop (struct ev_timer *w) |
706 | { |
799 | { |
707 | ev_clear ((W)w); |
800 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
801 | if (!ev_is_active (w)) |
709 | return; |
802 | return; |
710 | |
803 | |
… | |
… | |
718 | |
811 | |
719 | ev_stop ((W)w); |
812 | ev_stop ((W)w); |
720 | } |
813 | } |
721 | |
814 | |
722 | void |
815 | void |
723 | evtimer_again (struct ev_timer *w) |
816 | ev_timer_again (struct ev_timer *w) |
724 | { |
817 | { |
725 | if (ev_is_active (w)) |
818 | if (ev_is_active (w)) |
726 | { |
819 | { |
727 | if (w->repeat) |
820 | if (w->repeat) |
728 | { |
821 | { |
729 | w->at = now + w->repeat; |
822 | w->at = now + w->repeat; |
730 | downheap ((WT *)timers, timercnt, w->active - 1); |
823 | downheap ((WT *)timers, timercnt, w->active - 1); |
731 | } |
824 | } |
732 | else |
825 | else |
733 | evtimer_stop (w); |
826 | ev_timer_stop (w); |
734 | } |
827 | } |
735 | else if (w->repeat) |
828 | else if (w->repeat) |
736 | evtimer_start (w); |
829 | ev_timer_start (w); |
737 | } |
830 | } |
738 | |
831 | |
739 | void |
832 | void |
740 | evperiodic_start (struct ev_periodic *w) |
833 | ev_periodic_start (struct ev_periodic *w) |
741 | { |
834 | { |
742 | if (ev_is_active (w)) |
835 | if (ev_is_active (w)) |
743 | return; |
836 | return; |
744 | |
837 | |
745 | 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.)); |
… | |
… | |
753 | periodics [periodiccnt - 1] = w; |
846 | periodics [periodiccnt - 1] = w; |
754 | upheap ((WT *)periodics, periodiccnt - 1); |
847 | upheap ((WT *)periodics, periodiccnt - 1); |
755 | } |
848 | } |
756 | |
849 | |
757 | void |
850 | void |
758 | evperiodic_stop (struct ev_periodic *w) |
851 | ev_periodic_stop (struct ev_periodic *w) |
759 | { |
852 | { |
760 | ev_clear ((W)w); |
853 | ev_clear ((W)w); |
761 | if (!ev_is_active (w)) |
854 | if (!ev_is_active (w)) |
762 | return; |
855 | return; |
763 | |
856 | |
… | |
… | |
769 | |
862 | |
770 | ev_stop ((W)w); |
863 | ev_stop ((W)w); |
771 | } |
864 | } |
772 | |
865 | |
773 | void |
866 | void |
774 | evsignal_start (struct ev_signal *w) |
867 | ev_signal_start (struct ev_signal *w) |
775 | { |
868 | { |
776 | if (ev_is_active (w)) |
869 | if (ev_is_active (w)) |
777 | return; |
870 | return; |
778 | |
871 | |
779 | ev_start ((W)w, 1); |
872 | ev_start ((W)w, 1); |
… | |
… | |
789 | sigaction (w->signum, &sa, 0); |
882 | sigaction (w->signum, &sa, 0); |
790 | } |
883 | } |
791 | } |
884 | } |
792 | |
885 | |
793 | void |
886 | void |
794 | evsignal_stop (struct ev_signal *w) |
887 | ev_signal_stop (struct ev_signal *w) |
795 | { |
888 | { |
796 | ev_clear ((W)w); |
889 | ev_clear ((W)w); |
797 | if (!ev_is_active (w)) |
890 | if (!ev_is_active (w)) |
798 | return; |
891 | return; |
799 | |
892 | |
… | |
… | |
802 | |
895 | |
803 | if (!signals [w->signum - 1].head) |
896 | if (!signals [w->signum - 1].head) |
804 | signal (w->signum, SIG_DFL); |
897 | signal (w->signum, SIG_DFL); |
805 | } |
898 | } |
806 | |
899 | |
|
|
900 | void |
807 | void evidle_start (struct ev_idle *w) |
901 | ev_idle_start (struct ev_idle *w) |
808 | { |
902 | { |
809 | if (ev_is_active (w)) |
903 | if (ev_is_active (w)) |
810 | return; |
904 | return; |
811 | |
905 | |
812 | ev_start ((W)w, ++idlecnt); |
906 | ev_start ((W)w, ++idlecnt); |
813 | array_needsize (idles, idlemax, idlecnt, ); |
907 | array_needsize (idles, idlemax, idlecnt, ); |
814 | idles [idlecnt - 1] = w; |
908 | idles [idlecnt - 1] = w; |
815 | } |
909 | } |
816 | |
910 | |
|
|
911 | void |
817 | void evidle_stop (struct ev_idle *w) |
912 | ev_idle_stop (struct ev_idle *w) |
818 | { |
913 | { |
819 | ev_clear ((W)w); |
914 | ev_clear ((W)w); |
820 | if (ev_is_active (w)) |
915 | if (ev_is_active (w)) |
821 | return; |
916 | return; |
822 | |
917 | |
823 | idles [w->active - 1] = idles [--idlecnt]; |
918 | idles [w->active - 1] = idles [--idlecnt]; |
824 | ev_stop ((W)w); |
919 | ev_stop ((W)w); |
825 | } |
920 | } |
826 | |
921 | |
|
|
922 | void |
|
|
923 | ev_prepare_start (struct ev_prepare *w) |
|
|
924 | { |
|
|
925 | if (ev_is_active (w)) |
|
|
926 | return; |
|
|
927 | |
|
|
928 | ev_start ((W)w, ++preparecnt); |
|
|
929 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
930 | prepares [preparecnt - 1] = w; |
|
|
931 | } |
|
|
932 | |
|
|
933 | void |
|
|
934 | ev_prepare_stop (struct ev_prepare *w) |
|
|
935 | { |
|
|
936 | ev_clear ((W)w); |
|
|
937 | if (ev_is_active (w)) |
|
|
938 | return; |
|
|
939 | |
|
|
940 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
941 | ev_stop ((W)w); |
|
|
942 | } |
|
|
943 | |
|
|
944 | void |
827 | void evcheck_start (struct ev_check *w) |
945 | ev_check_start (struct ev_check *w) |
828 | { |
946 | { |
829 | if (ev_is_active (w)) |
947 | if (ev_is_active (w)) |
830 | return; |
948 | return; |
831 | |
949 | |
832 | ev_start ((W)w, ++checkcnt); |
950 | ev_start ((W)w, ++checkcnt); |
833 | array_needsize (checks, checkmax, checkcnt, ); |
951 | array_needsize (checks, checkmax, checkcnt, ); |
834 | checks [checkcnt - 1] = w; |
952 | checks [checkcnt - 1] = w; |
835 | } |
953 | } |
836 | |
954 | |
|
|
955 | void |
837 | void evcheck_stop (struct ev_check *w) |
956 | ev_check_stop (struct ev_check *w) |
838 | { |
957 | { |
839 | ev_clear ((W)w); |
958 | ev_clear ((W)w); |
840 | if (ev_is_active (w)) |
959 | if (ev_is_active (w)) |
841 | return; |
960 | return; |
842 | |
961 | |
843 | checks [w->active - 1] = checks [--checkcnt]; |
962 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
963 | ev_stop ((W)w); |
|
|
964 | } |
|
|
965 | |
|
|
966 | void |
|
|
967 | ev_child_start (struct ev_child *w) |
|
|
968 | { |
|
|
969 | if (ev_is_active (w)) |
|
|
970 | return; |
|
|
971 | |
|
|
972 | ev_start ((W)w, 1); |
|
|
973 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
974 | } |
|
|
975 | |
|
|
976 | void |
|
|
977 | ev_child_stop (struct ev_child *w) |
|
|
978 | { |
|
|
979 | ev_clear ((W)w); |
|
|
980 | if (ev_is_active (w)) |
|
|
981 | return; |
|
|
982 | |
|
|
983 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
844 | ev_stop ((W)w); |
984 | ev_stop ((W)w); |
845 | } |
985 | } |
846 | |
986 | |
847 | /*****************************************************************************/ |
987 | /*****************************************************************************/ |
848 | |
988 | |
… | |
… | |
858 | once_cb (struct ev_once *once, int revents) |
998 | once_cb (struct ev_once *once, int revents) |
859 | { |
999 | { |
860 | void (*cb)(int revents, void *arg) = once->cb; |
1000 | void (*cb)(int revents, void *arg) = once->cb; |
861 | void *arg = once->arg; |
1001 | void *arg = once->arg; |
862 | |
1002 | |
863 | evio_stop (&once->io); |
1003 | ev_io_stop (&once->io); |
864 | evtimer_stop (&once->to); |
1004 | ev_timer_stop (&once->to); |
865 | free (once); |
1005 | free (once); |
866 | |
1006 | |
867 | cb (revents, arg); |
1007 | cb (revents, arg); |
868 | } |
1008 | } |
869 | |
1009 | |
… | |
… | |
883 | 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) |
884 | { |
1024 | { |
885 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1025 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
886 | |
1026 | |
887 | if (!once) |
1027 | if (!once) |
888 | cb (EV_ERROR, arg); |
1028 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
889 | else |
1029 | else |
890 | { |
1030 | { |
891 | once->cb = cb; |
1031 | once->cb = cb; |
892 | once->arg = arg; |
1032 | once->arg = arg; |
893 | |
1033 | |
894 | evw_init (&once->io, once_cb_io); |
1034 | ev_watcher_init (&once->io, once_cb_io); |
895 | |
|
|
896 | if (fd >= 0) |
1035 | if (fd >= 0) |
897 | { |
1036 | { |
898 | evio_set (&once->io, fd, events); |
1037 | ev_io_set (&once->io, fd, events); |
899 | evio_start (&once->io); |
1038 | ev_io_start (&once->io); |
900 | } |
1039 | } |
901 | |
1040 | |
902 | evw_init (&once->to, once_cb_to); |
1041 | ev_watcher_init (&once->to, once_cb_to); |
903 | |
|
|
904 | if (timeout >= 0.) |
1042 | if (timeout >= 0.) |
905 | { |
1043 | { |
906 | evtimer_set (&once->to, timeout, 0.); |
1044 | ev_timer_set (&once->to, timeout, 0.); |
907 | evtimer_start (&once->to); |
1045 | ev_timer_start (&once->to); |
908 | } |
1046 | } |
909 | } |
1047 | } |
910 | } |
1048 | } |
911 | |
1049 | |
912 | /*****************************************************************************/ |
1050 | /*****************************************************************************/ |
… | |
… | |
923 | |
1061 | |
924 | static void |
1062 | static void |
925 | ocb (struct ev_timer *w, int revents) |
1063 | ocb (struct ev_timer *w, int revents) |
926 | { |
1064 | { |
927 | //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); |
928 | evtimer_stop (w); |
1066 | ev_timer_stop (w); |
929 | evtimer_start (w); |
1067 | ev_timer_start (w); |
930 | } |
1068 | } |
931 | |
1069 | |
932 | static void |
1070 | static void |
933 | scb (struct ev_signal *w, int revents) |
1071 | scb (struct ev_signal *w, int revents) |
934 | { |
1072 | { |
935 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1073 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
936 | evio_stop (&wio); |
1074 | ev_io_stop (&wio); |
937 | evio_start (&wio); |
1075 | ev_io_start (&wio); |
938 | } |
1076 | } |
939 | |
1077 | |
940 | static void |
1078 | static void |
941 | gcb (struct ev_signal *w, int revents) |
1079 | gcb (struct ev_signal *w, int revents) |
942 | { |
1080 | { |
… | |
… | |
946 | |
1084 | |
947 | int main (void) |
1085 | int main (void) |
948 | { |
1086 | { |
949 | ev_init (0); |
1087 | ev_init (0); |
950 | |
1088 | |
951 | evio_init (&wio, sin_cb, 0, EV_READ); |
1089 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
952 | evio_start (&wio); |
1090 | ev_io_start (&wio); |
953 | |
1091 | |
954 | struct ev_timer t[10000]; |
1092 | struct ev_timer t[10000]; |
955 | |
1093 | |
956 | #if 0 |
1094 | #if 0 |
957 | int i; |
1095 | int i; |
958 | for (i = 0; i < 10000; ++i) |
1096 | for (i = 0; i < 10000; ++i) |
959 | { |
1097 | { |
960 | struct ev_timer *w = t + i; |
1098 | struct ev_timer *w = t + i; |
961 | evw_init (w, ocb, i); |
1099 | ev_watcher_init (w, ocb, i); |
962 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1100 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
963 | evtimer_start (w); |
1101 | ev_timer_start (w); |
964 | if (drand48 () < 0.5) |
1102 | if (drand48 () < 0.5) |
965 | evtimer_stop (w); |
1103 | ev_timer_stop (w); |
966 | } |
1104 | } |
967 | #endif |
1105 | #endif |
968 | |
1106 | |
969 | struct ev_timer t1; |
1107 | struct ev_timer t1; |
970 | evtimer_init (&t1, ocb, 5, 10); |
1108 | ev_timer_init (&t1, ocb, 5, 10); |
971 | evtimer_start (&t1); |
1109 | ev_timer_start (&t1); |
972 | |
1110 | |
973 | struct ev_signal sig; |
1111 | struct ev_signal sig; |
974 | evsignal_init (&sig, scb, SIGQUIT); |
1112 | ev_signal_init (&sig, scb, SIGQUIT); |
975 | evsignal_start (&sig); |
1113 | ev_signal_start (&sig); |
976 | |
1114 | |
977 | struct ev_check cw; |
1115 | struct ev_check cw; |
978 | evcheck_init (&cw, gcb); |
1116 | ev_check_init (&cw, gcb); |
979 | evcheck_start (&cw); |
1117 | ev_check_start (&cw); |
980 | |
1118 | |
981 | struct ev_idle iw; |
1119 | struct ev_idle iw; |
982 | evidle_init (&iw, gcb); |
1120 | ev_idle_init (&iw, gcb); |
983 | evidle_start (&iw); |
1121 | ev_idle_start (&iw); |
984 | |
1122 | |
985 | ev_loop (0); |
1123 | ev_loop (0); |
986 | |
1124 | |
987 | return 0; |
1125 | return 0; |
988 | } |
1126 | } |