… | |
… | |
36 | |
36 | |
37 | #include <stdio.h> |
37 | #include <stdio.h> |
38 | |
38 | |
39 | #include <assert.h> |
39 | #include <assert.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
43 | #include <sys/time.h> |
42 | #include <time.h> |
44 | #include <time.h> |
43 | |
45 | |
44 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
45 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
… | |
… | |
59 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
60 | #endif |
62 | #endif |
61 | |
63 | |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
64 | |
67 | |
65 | #include "ev.h" |
68 | #include "ev.h" |
66 | |
69 | |
67 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
68 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
110 | } |
113 | } |
111 | |
114 | |
112 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
113 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
114 | { \ |
117 | { \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
|
|
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
118 | cur = newcnt; \ |
127 | cur = newcnt; \ |
119 | } |
128 | } |
120 | |
129 | |
121 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
122 | |
131 | |
123 | typedef struct |
132 | typedef struct |
124 | { |
133 | { |
125 | struct ev_io *head; |
134 | struct ev_io *head; |
126 | unsigned char wev, rev; /* want, received event set */ |
135 | int events; |
127 | } ANFD; |
136 | } ANFD; |
128 | |
137 | |
129 | static ANFD *anfds; |
138 | static ANFD *anfds; |
130 | static int anfdmax; |
139 | static int anfdmax; |
131 | |
140 | |
132 | static int *fdchanges; |
|
|
133 | static int fdchangemax, fdchangecnt; |
|
|
134 | |
|
|
135 | static void |
141 | static void |
136 | anfds_init (ANFD *base, int count) |
142 | anfds_init (ANFD *base, int count) |
137 | { |
143 | { |
138 | while (count--) |
144 | while (count--) |
139 | { |
145 | { |
140 | base->head = 0; |
146 | base->head = 0; |
141 | base->wev = base->rev = EV_NONE; |
147 | base->events = EV_NONE; |
142 | ++base; |
148 | ++base; |
143 | } |
149 | } |
144 | } |
150 | } |
145 | |
151 | |
146 | typedef struct |
152 | typedef struct |
… | |
… | |
163 | pendings [pendingcnt - 1].events = events; |
169 | pendings [pendingcnt - 1].events = events; |
164 | } |
170 | } |
165 | } |
171 | } |
166 | |
172 | |
167 | static void |
173 | static void |
|
|
174 | queue_events (W *events, int eventcnt, int type) |
|
|
175 | { |
|
|
176 | int i; |
|
|
177 | |
|
|
178 | for (i = 0; i < eventcnt; ++i) |
|
|
179 | event (events [i], type); |
|
|
180 | } |
|
|
181 | |
|
|
182 | static void |
168 | fd_event (int fd, int events) |
183 | fd_event (int fd, int events) |
169 | { |
184 | { |
170 | ANFD *anfd = anfds + fd; |
185 | ANFD *anfd = anfds + fd; |
171 | struct ev_io *w; |
186 | struct ev_io *w; |
172 | |
187 | |
… | |
… | |
177 | if (ev) |
192 | if (ev) |
178 | event ((W)w, ev); |
193 | event ((W)w, ev); |
179 | } |
194 | } |
180 | } |
195 | } |
181 | |
196 | |
|
|
197 | /*****************************************************************************/ |
|
|
198 | |
|
|
199 | static int *fdchanges; |
|
|
200 | static int fdchangemax, fdchangecnt; |
|
|
201 | |
182 | static void |
202 | static void |
183 | queue_events (W *events, int eventcnt, int type) |
203 | fd_reify (void) |
184 | { |
204 | { |
185 | int i; |
205 | int i; |
186 | |
206 | |
187 | for (i = 0; i < eventcnt; ++i) |
207 | for (i = 0; i < fdchangecnt; ++i) |
188 | event (events [i], type); |
208 | { |
|
|
209 | int fd = fdchanges [i]; |
|
|
210 | ANFD *anfd = anfds + fd; |
|
|
211 | struct ev_io *w; |
|
|
212 | |
|
|
213 | int events = 0; |
|
|
214 | |
|
|
215 | for (w = anfd->head; w; w = w->next) |
|
|
216 | events |= w->events; |
|
|
217 | |
|
|
218 | anfd->events &= ~EV_REIFY; |
|
|
219 | |
|
|
220 | if (anfd->events != events) |
|
|
221 | { |
|
|
222 | method_modify (fd, anfd->events, events); |
|
|
223 | anfd->events = events; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | fdchangecnt = 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void |
|
|
231 | fd_change (int fd) |
|
|
232 | { |
|
|
233 | if (anfds [fd].events & EV_REIFY) |
|
|
234 | return; |
|
|
235 | |
|
|
236 | anfds [fd].events |= EV_REIFY; |
|
|
237 | |
|
|
238 | ++fdchangecnt; |
|
|
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
240 | fdchanges [fdchangecnt - 1] = fd; |
189 | } |
241 | } |
190 | |
242 | |
191 | /* called on EBADF to verify fds */ |
243 | /* called on EBADF to verify fds */ |
192 | static void |
244 | static void |
193 | fd_recheck () |
245 | fd_recheck (void) |
194 | { |
246 | { |
195 | int fd; |
247 | int fd; |
196 | |
248 | |
197 | for (fd = 0; fd < anfdmax; ++fd) |
249 | for (fd = 0; fd < anfdmax; ++fd) |
198 | if (anfds [fd].wev) |
250 | if (anfds [fd].events) |
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 | while (anfds [fd].head) |
252 | while (anfds [fd].head) |
|
|
253 | { |
|
|
254 | event ((W)anfds [fd].head, EV_ERROR); |
201 | evio_stop (anfds [fd].head); |
255 | ev_io_stop (anfds [fd].head); |
|
|
256 | } |
202 | } |
257 | } |
203 | |
258 | |
204 | /*****************************************************************************/ |
259 | /*****************************************************************************/ |
205 | |
260 | |
206 | static struct ev_timer **timers; |
261 | static struct ev_timer **timers; |
… | |
… | |
315 | |
370 | |
316 | /* rather than sort out wether we really need nb, set it */ |
371 | /* rather than sort out wether we really need nb, set it */ |
317 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
318 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
319 | |
374 | |
320 | evio_set (&sigev, sigpipe [0], EV_READ); |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
321 | evio_start (&sigev); |
376 | ev_io_start (&sigev); |
322 | } |
377 | } |
323 | |
378 | |
324 | /*****************************************************************************/ |
379 | /*****************************************************************************/ |
325 | |
380 | |
326 | static struct ev_idle **idles; |
381 | static struct ev_idle **idles; |
… | |
… | |
332 | static struct ev_check **checks; |
387 | static struct ev_check **checks; |
333 | static int checkmax, checkcnt; |
388 | static int checkmax, checkcnt; |
334 | |
389 | |
335 | /*****************************************************************************/ |
390 | /*****************************************************************************/ |
336 | |
391 | |
|
|
392 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
393 | static struct ev_signal childev; |
|
|
394 | |
|
|
395 | #ifndef WCONTINUED |
|
|
396 | # define WCONTINUED 0 |
|
|
397 | #endif |
|
|
398 | |
|
|
399 | static void |
|
|
400 | childcb (struct ev_signal *sw, int revents) |
|
|
401 | { |
|
|
402 | struct ev_child *w; |
|
|
403 | int pid, status; |
|
|
404 | |
|
|
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
407 | if (w->pid == pid || w->pid == -1) |
|
|
408 | { |
|
|
409 | w->status = status; |
|
|
410 | event ((W)w, EV_CHILD); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | |
|
|
414 | /*****************************************************************************/ |
|
|
415 | |
337 | #if HAVE_EPOLL |
416 | #if HAVE_EPOLL |
338 | # include "ev_epoll.c" |
417 | # include "ev_epoll.c" |
339 | #endif |
418 | #endif |
340 | #if HAVE_SELECT |
419 | #if HAVE_SELECT |
341 | # include "ev_select.c" |
420 | # include "ev_select.c" |
342 | #endif |
421 | #endif |
343 | |
422 | |
|
|
423 | int |
|
|
424 | ev_version_major (void) |
|
|
425 | { |
|
|
426 | return EV_VERSION_MAJOR; |
|
|
427 | } |
|
|
428 | |
|
|
429 | int |
|
|
430 | ev_version_minor (void) |
|
|
431 | { |
|
|
432 | return EV_VERSION_MINOR; |
|
|
433 | } |
|
|
434 | |
344 | int ev_init (int flags) |
435 | int ev_init (int flags) |
345 | { |
436 | { |
|
|
437 | if (!ev_method) |
|
|
438 | { |
346 | #if HAVE_MONOTONIC |
439 | #if HAVE_MONOTONIC |
347 | { |
440 | { |
348 | struct timespec ts; |
441 | struct timespec ts; |
349 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
350 | have_monotonic = 1; |
443 | have_monotonic = 1; |
351 | } |
444 | } |
352 | #endif |
445 | #endif |
353 | |
446 | |
354 | ev_now = ev_time (); |
447 | ev_now = ev_time (); |
355 | now = get_clock (); |
448 | now = get_clock (); |
356 | diff = ev_now - now; |
449 | diff = ev_now - now; |
357 | |
450 | |
358 | if (pipe (sigpipe)) |
451 | if (pipe (sigpipe)) |
359 | return 0; |
452 | return 0; |
360 | |
453 | |
361 | ev_method = EVMETHOD_NONE; |
454 | ev_method = EVMETHOD_NONE; |
362 | #if HAVE_EPOLL |
455 | #if HAVE_EPOLL |
363 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
364 | #endif |
457 | #endif |
365 | #if HAVE_SELECT |
458 | #if HAVE_SELECT |
366 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
367 | #endif |
460 | #endif |
368 | |
461 | |
369 | if (ev_method) |
462 | if (ev_method) |
370 | { |
463 | { |
371 | evw_init (&sigev, sigcb); |
464 | ev_watcher_init (&sigev, sigcb); |
372 | siginit (); |
465 | siginit (); |
|
|
466 | |
|
|
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
468 | ev_signal_start (&childev); |
|
|
469 | } |
373 | } |
470 | } |
374 | |
471 | |
375 | return ev_method; |
472 | return ev_method; |
376 | } |
473 | } |
377 | |
474 | |
378 | /*****************************************************************************/ |
475 | /*****************************************************************************/ |
379 | |
476 | |
|
|
477 | void |
380 | void ev_prefork (void) |
478 | ev_prefork (void) |
381 | { |
479 | { |
382 | /* nop */ |
480 | /* nop */ |
383 | } |
481 | } |
384 | |
482 | |
|
|
483 | void |
385 | void ev_postfork_parent (void) |
484 | ev_postfork_parent (void) |
386 | { |
485 | { |
387 | /* nop */ |
486 | /* nop */ |
388 | } |
487 | } |
389 | |
488 | |
|
|
489 | void |
390 | void ev_postfork_child (void) |
490 | ev_postfork_child (void) |
391 | { |
491 | { |
392 | #if HAVE_EPOLL |
492 | #if HAVE_EPOLL |
393 | if (ev_method == EVMETHOD_EPOLL) |
493 | if (ev_method == EVMETHOD_EPOLL) |
394 | epoll_postfork_child (); |
494 | epoll_postfork_child (); |
395 | #endif |
495 | #endif |
396 | |
496 | |
397 | evio_stop (&sigev); |
497 | ev_io_stop (&sigev); |
398 | close (sigpipe [0]); |
498 | close (sigpipe [0]); |
399 | close (sigpipe [1]); |
499 | close (sigpipe [1]); |
400 | pipe (sigpipe); |
500 | pipe (sigpipe); |
401 | siginit (); |
501 | siginit (); |
402 | } |
502 | } |
403 | |
503 | |
404 | /*****************************************************************************/ |
504 | /*****************************************************************************/ |
405 | |
505 | |
406 | static void |
506 | static void |
407 | fd_reify (void) |
|
|
408 | { |
|
|
409 | int i; |
|
|
410 | |
|
|
411 | for (i = 0; i < fdchangecnt; ++i) |
|
|
412 | { |
|
|
413 | int fd = fdchanges [i]; |
|
|
414 | ANFD *anfd = anfds + fd; |
|
|
415 | struct ev_io *w; |
|
|
416 | |
|
|
417 | int wev = 0; |
|
|
418 | |
|
|
419 | for (w = anfd->head; w; w = w->next) |
|
|
420 | wev |= w->events; |
|
|
421 | |
|
|
422 | if (anfd->wev != wev) |
|
|
423 | { |
|
|
424 | method_modify (fd, anfd->wev, wev); |
|
|
425 | anfd->wev = wev; |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | fdchangecnt = 0; |
|
|
430 | } |
|
|
431 | |
|
|
432 | static void |
|
|
433 | call_pending () |
507 | call_pending (void) |
434 | { |
508 | { |
435 | while (pendingcnt) |
509 | while (pendingcnt) |
436 | { |
510 | { |
437 | ANPENDING *p = pendings + --pendingcnt; |
511 | ANPENDING *p = pendings + --pendingcnt; |
438 | |
512 | |
… | |
… | |
443 | } |
517 | } |
444 | } |
518 | } |
445 | } |
519 | } |
446 | |
520 | |
447 | static void |
521 | static void |
448 | timers_reify () |
522 | timers_reify (void) |
449 | { |
523 | { |
450 | while (timercnt && timers [0]->at <= now) |
524 | while (timercnt && timers [0]->at <= now) |
451 | { |
525 | { |
452 | struct ev_timer *w = timers [0]; |
526 | struct ev_timer *w = timers [0]; |
453 | |
527 | |
… | |
… | |
459 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
460 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
461 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
462 | } |
536 | } |
463 | else |
537 | else |
464 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
465 | } |
539 | } |
466 | } |
540 | } |
467 | |
541 | |
468 | static void |
542 | static void |
469 | periodics_reify () |
543 | periodics_reify (void) |
470 | { |
544 | { |
471 | while (periodiccnt && periodics [0]->at <= ev_now) |
545 | while (periodiccnt && periodics [0]->at <= ev_now) |
472 | { |
546 | { |
473 | struct ev_periodic *w = periodics [0]; |
547 | struct ev_periodic *w = periodics [0]; |
474 | |
548 | |
… | |
… | |
478 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
479 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
480 | downheap ((WT *)periodics, periodiccnt, 0); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
481 | } |
555 | } |
482 | else |
556 | else |
483 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
484 | |
558 | |
485 | event ((W)w, EV_TIMEOUT); |
559 | event ((W)w, EV_TIMEOUT); |
486 | } |
560 | } |
487 | } |
561 | } |
488 | |
562 | |
… | |
… | |
500 | { |
574 | { |
501 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
502 | |
576 | |
503 | if (fabs (diff) >= 1e-4) |
577 | if (fabs (diff) >= 1e-4) |
504 | { |
578 | { |
505 | evperiodic_stop (w); |
579 | ev_periodic_stop (w); |
506 | evperiodic_start (w); |
580 | ev_periodic_start (w); |
507 | |
581 | |
508 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
509 | } |
583 | } |
510 | } |
584 | } |
511 | } |
585 | } |
512 | } |
586 | } |
513 | |
587 | |
514 | static void |
588 | static void |
515 | time_update () |
589 | time_update (void) |
516 | { |
590 | { |
517 | int i; |
591 | int i; |
518 | |
592 | |
519 | ev_now = ev_time (); |
593 | ev_now = ev_time (); |
520 | |
594 | |
… | |
… | |
554 | int ev_loop_done; |
628 | int ev_loop_done; |
555 | |
629 | |
556 | void ev_loop (int flags) |
630 | void ev_loop (int flags) |
557 | { |
631 | { |
558 | double block; |
632 | double block; |
559 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
560 | |
634 | |
561 | do |
635 | do |
562 | { |
636 | { |
563 | /* queue check watchers (and execute them) */ |
637 | /* queue check watchers (and execute them) */ |
564 | if (checkcnt) |
638 | if (preparecnt) |
565 | { |
639 | { |
566 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
567 | call_pending (); |
641 | call_pending (); |
568 | } |
642 | } |
569 | |
643 | |
570 | /* update fd-related kernel structures */ |
644 | /* update fd-related kernel structures */ |
571 | fd_reify (); |
645 | fd_reify (); |
572 | |
646 | |
573 | /* calculate blocking time */ |
647 | /* calculate blocking time */ |
574 | |
648 | |
575 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
649 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
650 | always have timers, we just calculate it always */ |
576 | ev_now = ev_time (); |
651 | ev_now = ev_time (); |
577 | |
652 | |
578 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
579 | block = 0.; |
654 | block = 0.; |
580 | else |
655 | else |
… | |
… | |
668 | } |
743 | } |
669 | |
744 | |
670 | /*****************************************************************************/ |
745 | /*****************************************************************************/ |
671 | |
746 | |
672 | void |
747 | void |
673 | evio_start (struct ev_io *w) |
748 | ev_io_start (struct ev_io *w) |
674 | { |
749 | { |
675 | if (ev_is_active (w)) |
750 | if (ev_is_active (w)) |
676 | return; |
751 | return; |
677 | |
752 | |
678 | int fd = w->fd; |
753 | int fd = w->fd; |
679 | |
754 | |
680 | ev_start ((W)w, 1); |
755 | ev_start ((W)w, 1); |
681 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
682 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
683 | |
758 | |
684 | ++fdchangecnt; |
759 | fd_change (fd); |
685 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
686 | fdchanges [fdchangecnt - 1] = fd; |
|
|
687 | } |
760 | } |
688 | |
761 | |
689 | void |
762 | void |
690 | evio_stop (struct ev_io *w) |
763 | ev_io_stop (struct ev_io *w) |
691 | { |
764 | { |
692 | ev_clear ((W)w); |
765 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
766 | if (!ev_is_active (w)) |
694 | return; |
767 | return; |
695 | |
768 | |
696 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
697 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
698 | |
771 | |
699 | ++fdchangecnt; |
772 | fd_change (w->fd); |
700 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
701 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
702 | } |
773 | } |
703 | |
774 | |
704 | void |
775 | void |
705 | evtimer_start (struct ev_timer *w) |
776 | ev_timer_start (struct ev_timer *w) |
706 | { |
777 | { |
707 | if (ev_is_active (w)) |
778 | if (ev_is_active (w)) |
708 | return; |
779 | return; |
709 | |
780 | |
710 | w->at += now; |
781 | w->at += now; |
… | |
… | |
716 | timers [timercnt - 1] = w; |
787 | timers [timercnt - 1] = w; |
717 | upheap ((WT *)timers, timercnt - 1); |
788 | upheap ((WT *)timers, timercnt - 1); |
718 | } |
789 | } |
719 | |
790 | |
720 | void |
791 | void |
721 | evtimer_stop (struct ev_timer *w) |
792 | ev_timer_stop (struct ev_timer *w) |
722 | { |
793 | { |
723 | ev_clear ((W)w); |
794 | ev_clear ((W)w); |
724 | if (!ev_is_active (w)) |
795 | if (!ev_is_active (w)) |
725 | return; |
796 | return; |
726 | |
797 | |
… | |
… | |
734 | |
805 | |
735 | ev_stop ((W)w); |
806 | ev_stop ((W)w); |
736 | } |
807 | } |
737 | |
808 | |
738 | void |
809 | void |
739 | evtimer_again (struct ev_timer *w) |
810 | ev_timer_again (struct ev_timer *w) |
740 | { |
811 | { |
741 | if (ev_is_active (w)) |
812 | if (ev_is_active (w)) |
742 | { |
813 | { |
743 | if (w->repeat) |
814 | if (w->repeat) |
744 | { |
815 | { |
745 | w->at = now + w->repeat; |
816 | w->at = now + w->repeat; |
746 | downheap ((WT *)timers, timercnt, w->active - 1); |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
747 | } |
818 | } |
748 | else |
819 | else |
749 | evtimer_stop (w); |
820 | ev_timer_stop (w); |
750 | } |
821 | } |
751 | else if (w->repeat) |
822 | else if (w->repeat) |
752 | evtimer_start (w); |
823 | ev_timer_start (w); |
753 | } |
824 | } |
754 | |
825 | |
755 | void |
826 | void |
756 | evperiodic_start (struct ev_periodic *w) |
827 | ev_periodic_start (struct ev_periodic *w) |
757 | { |
828 | { |
758 | if (ev_is_active (w)) |
829 | if (ev_is_active (w)) |
759 | return; |
830 | return; |
760 | |
831 | |
761 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
769 | periodics [periodiccnt - 1] = w; |
840 | periodics [periodiccnt - 1] = w; |
770 | upheap ((WT *)periodics, periodiccnt - 1); |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
771 | } |
842 | } |
772 | |
843 | |
773 | void |
844 | void |
774 | evperiodic_stop (struct ev_periodic *w) |
845 | ev_periodic_stop (struct ev_periodic *w) |
775 | { |
846 | { |
776 | ev_clear ((W)w); |
847 | ev_clear ((W)w); |
777 | if (!ev_is_active (w)) |
848 | if (!ev_is_active (w)) |
778 | return; |
849 | return; |
779 | |
850 | |
… | |
… | |
785 | |
856 | |
786 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
787 | } |
858 | } |
788 | |
859 | |
789 | void |
860 | void |
790 | evsignal_start (struct ev_signal *w) |
861 | ev_signal_start (struct ev_signal *w) |
791 | { |
862 | { |
792 | if (ev_is_active (w)) |
863 | if (ev_is_active (w)) |
793 | return; |
864 | return; |
794 | |
865 | |
795 | ev_start ((W)w, 1); |
866 | ev_start ((W)w, 1); |
… | |
… | |
805 | sigaction (w->signum, &sa, 0); |
876 | sigaction (w->signum, &sa, 0); |
806 | } |
877 | } |
807 | } |
878 | } |
808 | |
879 | |
809 | void |
880 | void |
810 | evsignal_stop (struct ev_signal *w) |
881 | ev_signal_stop (struct ev_signal *w) |
811 | { |
882 | { |
812 | ev_clear ((W)w); |
883 | ev_clear ((W)w); |
813 | if (!ev_is_active (w)) |
884 | if (!ev_is_active (w)) |
814 | return; |
885 | return; |
815 | |
886 | |
… | |
… | |
818 | |
889 | |
819 | if (!signals [w->signum - 1].head) |
890 | if (!signals [w->signum - 1].head) |
820 | signal (w->signum, SIG_DFL); |
891 | signal (w->signum, SIG_DFL); |
821 | } |
892 | } |
822 | |
893 | |
|
|
894 | void |
823 | void evidle_start (struct ev_idle *w) |
895 | ev_idle_start (struct ev_idle *w) |
824 | { |
896 | { |
825 | if (ev_is_active (w)) |
897 | if (ev_is_active (w)) |
826 | return; |
898 | return; |
827 | |
899 | |
828 | ev_start ((W)w, ++idlecnt); |
900 | ev_start ((W)w, ++idlecnt); |
829 | array_needsize (idles, idlemax, idlecnt, ); |
901 | array_needsize (idles, idlemax, idlecnt, ); |
830 | idles [idlecnt - 1] = w; |
902 | idles [idlecnt - 1] = w; |
831 | } |
903 | } |
832 | |
904 | |
|
|
905 | void |
833 | void evidle_stop (struct ev_idle *w) |
906 | ev_idle_stop (struct ev_idle *w) |
834 | { |
907 | { |
835 | ev_clear ((W)w); |
908 | ev_clear ((W)w); |
836 | if (ev_is_active (w)) |
909 | if (ev_is_active (w)) |
837 | return; |
910 | return; |
838 | |
911 | |
839 | idles [w->active - 1] = idles [--idlecnt]; |
912 | idles [w->active - 1] = idles [--idlecnt]; |
840 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
841 | } |
914 | } |
842 | |
915 | |
|
|
916 | void |
843 | void evprepare_start (struct ev_prepare *w) |
917 | ev_prepare_start (struct ev_prepare *w) |
844 | { |
918 | { |
845 | if (ev_is_active (w)) |
919 | if (ev_is_active (w)) |
846 | return; |
920 | return; |
847 | |
921 | |
848 | ev_start ((W)w, ++preparecnt); |
922 | ev_start ((W)w, ++preparecnt); |
849 | array_needsize (prepares, preparemax, preparecnt, ); |
923 | array_needsize (prepares, preparemax, preparecnt, ); |
850 | prepares [preparecnt - 1] = w; |
924 | prepares [preparecnt - 1] = w; |
851 | } |
925 | } |
852 | |
926 | |
|
|
927 | void |
853 | void evprepare_stop (struct ev_prepare *w) |
928 | ev_prepare_stop (struct ev_prepare *w) |
854 | { |
929 | { |
855 | ev_clear ((W)w); |
930 | ev_clear ((W)w); |
856 | if (ev_is_active (w)) |
931 | if (ev_is_active (w)) |
857 | return; |
932 | return; |
858 | |
933 | |
859 | prepares [w->active - 1] = prepares [--preparecnt]; |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
860 | ev_stop ((W)w); |
935 | ev_stop ((W)w); |
861 | } |
936 | } |
862 | |
937 | |
|
|
938 | void |
863 | void evcheck_start (struct ev_check *w) |
939 | ev_check_start (struct ev_check *w) |
864 | { |
940 | { |
865 | if (ev_is_active (w)) |
941 | if (ev_is_active (w)) |
866 | return; |
942 | return; |
867 | |
943 | |
868 | ev_start ((W)w, ++checkcnt); |
944 | ev_start ((W)w, ++checkcnt); |
869 | array_needsize (checks, checkmax, checkcnt, ); |
945 | array_needsize (checks, checkmax, checkcnt, ); |
870 | checks [checkcnt - 1] = w; |
946 | checks [checkcnt - 1] = w; |
871 | } |
947 | } |
872 | |
948 | |
|
|
949 | void |
873 | void evcheck_stop (struct ev_check *w) |
950 | ev_check_stop (struct ev_check *w) |
874 | { |
951 | { |
875 | ev_clear ((W)w); |
952 | ev_clear ((W)w); |
876 | if (ev_is_active (w)) |
953 | if (ev_is_active (w)) |
877 | return; |
954 | return; |
878 | |
955 | |
879 | checks [w->active - 1] = checks [--checkcnt]; |
956 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
957 | ev_stop ((W)w); |
|
|
958 | } |
|
|
959 | |
|
|
960 | void |
|
|
961 | ev_child_start (struct ev_child *w) |
|
|
962 | { |
|
|
963 | if (ev_is_active (w)) |
|
|
964 | return; |
|
|
965 | |
|
|
966 | ev_start ((W)w, 1); |
|
|
967 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
968 | } |
|
|
969 | |
|
|
970 | void |
|
|
971 | ev_child_stop (struct ev_child *w) |
|
|
972 | { |
|
|
973 | ev_clear ((W)w); |
|
|
974 | if (ev_is_active (w)) |
|
|
975 | return; |
|
|
976 | |
|
|
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
880 | ev_stop ((W)w); |
978 | ev_stop ((W)w); |
881 | } |
979 | } |
882 | |
980 | |
883 | /*****************************************************************************/ |
981 | /*****************************************************************************/ |
884 | |
982 | |
… | |
… | |
894 | once_cb (struct ev_once *once, int revents) |
992 | once_cb (struct ev_once *once, int revents) |
895 | { |
993 | { |
896 | void (*cb)(int revents, void *arg) = once->cb; |
994 | void (*cb)(int revents, void *arg) = once->cb; |
897 | void *arg = once->arg; |
995 | void *arg = once->arg; |
898 | |
996 | |
899 | evio_stop (&once->io); |
997 | ev_io_stop (&once->io); |
900 | evtimer_stop (&once->to); |
998 | ev_timer_stop (&once->to); |
901 | free (once); |
999 | free (once); |
902 | |
1000 | |
903 | cb (revents, arg); |
1001 | cb (revents, arg); |
904 | } |
1002 | } |
905 | |
1003 | |
… | |
… | |
925 | else |
1023 | else |
926 | { |
1024 | { |
927 | once->cb = cb; |
1025 | once->cb = cb; |
928 | once->arg = arg; |
1026 | once->arg = arg; |
929 | |
1027 | |
930 | evw_init (&once->io, once_cb_io); |
1028 | ev_watcher_init (&once->io, once_cb_io); |
931 | |
1029 | |
932 | if (fd >= 0) |
1030 | if (fd >= 0) |
933 | { |
1031 | { |
934 | evio_set (&once->io, fd, events); |
1032 | ev_io_set (&once->io, fd, events); |
935 | evio_start (&once->io); |
1033 | ev_io_start (&once->io); |
936 | } |
1034 | } |
937 | |
1035 | |
938 | evw_init (&once->to, once_cb_to); |
1036 | ev_watcher_init (&once->to, once_cb_to); |
939 | |
1037 | |
940 | if (timeout >= 0.) |
1038 | if (timeout >= 0.) |
941 | { |
1039 | { |
942 | evtimer_set (&once->to, timeout, 0.); |
1040 | ev_timer_set (&once->to, timeout, 0.); |
943 | evtimer_start (&once->to); |
1041 | ev_timer_start (&once->to); |
944 | } |
1042 | } |
945 | } |
1043 | } |
946 | } |
1044 | } |
947 | |
1045 | |
948 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
… | |
… | |
959 | |
1057 | |
960 | static void |
1058 | static void |
961 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
962 | { |
1060 | { |
963 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
964 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
965 | evtimer_start (w); |
1063 | ev_timer_start (w); |
966 | } |
1064 | } |
967 | |
1065 | |
968 | static void |
1066 | static void |
969 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
970 | { |
1068 | { |
971 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
972 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
973 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
974 | } |
1072 | } |
975 | |
1073 | |
976 | static void |
1074 | static void |
977 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
978 | { |
1076 | { |
… | |
… | |
982 | |
1080 | |
983 | int main (void) |
1081 | int main (void) |
984 | { |
1082 | { |
985 | ev_init (0); |
1083 | ev_init (0); |
986 | |
1084 | |
987 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
988 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
989 | |
1087 | |
990 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
991 | |
1089 | |
992 | #if 0 |
1090 | #if 0 |
993 | int i; |
1091 | int i; |
994 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
995 | { |
1093 | { |
996 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
997 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
998 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
999 | evtimer_start (w); |
1097 | ev_timer_start (w); |
1000 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
1001 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
1002 | } |
1100 | } |
1003 | #endif |
1101 | #endif |
1004 | |
1102 | |
1005 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
1006 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
1007 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
1008 | |
1106 | |
1009 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
1010 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
1011 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
1012 | |
1110 | |
1013 | struct ev_check cw; |
1111 | struct ev_check cw; |
1014 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
1015 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
1016 | |
1114 | |
1017 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
1018 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
1019 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
1020 | |
1118 | |
1021 | ev_loop (0); |
1119 | ev_loop (0); |
1022 | |
1120 | |
1023 | return 0; |
1121 | return 0; |
1024 | } |
1122 | } |