|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
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. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
45 | |
16 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
19 | # endif |
49 | # endif |
… | |
… | |
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
32 | #endif |
62 | #endif |
33 | |
63 | |
34 | #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) */ |
35 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
36 | |
67 | |
37 | #include "ev.h" |
68 | #include "ev.h" |
38 | |
|
|
39 | struct ev_watcher { |
|
|
40 | EV_WATCHER (ev_watcher); |
|
|
41 | }; |
|
|
42 | |
|
|
43 | struct ev_watcher_list { |
|
|
44 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
45 | }; |
|
|
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
69 | |
51 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
52 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
53 | typedef struct ev_watcher_time *WT; |
72 | typedef struct ev_watcher_time *WT; |
54 | |
73 | |
… | |
… | |
94 | } |
113 | } |
95 | |
114 | |
96 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
97 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
98 | { \ |
117 | { \ |
99 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
100 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
101 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
102 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
103 | cur = newcnt; \ |
127 | cur = newcnt; \ |
104 | } |
128 | } |
105 | |
129 | |
… | |
… | |
138 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
139 | |
163 | |
140 | static void |
164 | static void |
141 | event (W w, int events) |
165 | event (W w, int events) |
142 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
143 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
144 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
145 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
146 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
147 | } |
174 | } |
148 | |
175 | |
149 | static void |
176 | static void |
150 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
151 | { |
178 | { |
… | |
… | |
166 | { |
193 | { |
167 | int i; |
194 | int i; |
168 | |
195 | |
169 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
170 | event (events [i], type); |
197 | event (events [i], type); |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck (void) |
|
|
203 | { |
|
|
204 | int fd; |
|
|
205 | |
|
|
206 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
207 | if (anfds [fd].wev) |
|
|
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
209 | while (anfds [fd].head) |
|
|
210 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
171 | } |
214 | } |
172 | |
215 | |
173 | /*****************************************************************************/ |
216 | /*****************************************************************************/ |
174 | |
217 | |
175 | static struct ev_timer **timers; |
218 | static struct ev_timer **timers; |
… | |
… | |
293 | /*****************************************************************************/ |
336 | /*****************************************************************************/ |
294 | |
337 | |
295 | static struct ev_idle **idles; |
338 | static struct ev_idle **idles; |
296 | static int idlemax, idlecnt; |
339 | static int idlemax, idlecnt; |
297 | |
340 | |
|
|
341 | static struct ev_prepare **prepares; |
|
|
342 | static int preparemax, preparecnt; |
|
|
343 | |
298 | static struct ev_check **checks; |
344 | static struct ev_check **checks; |
299 | static int checkmax, checkcnt; |
345 | static int checkmax, checkcnt; |
300 | |
346 | |
301 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
302 | |
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
|
|
372 | |
303 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
304 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
305 | #endif |
375 | #endif |
306 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
307 | # include "ev_select.c" |
377 | # include "ev_select.c" |
308 | #endif |
378 | #endif |
309 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
310 | int ev_init (int flags) |
392 | int ev_init (int flags) |
311 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
312 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
313 | { |
397 | { |
314 | struct timespec ts; |
398 | struct timespec ts; |
315 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
316 | have_monotonic = 1; |
400 | have_monotonic = 1; |
317 | } |
401 | } |
318 | #endif |
402 | #endif |
319 | |
403 | |
320 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
321 | now = get_clock (); |
405 | now = get_clock (); |
322 | diff = ev_now - now; |
406 | diff = ev_now - now; |
323 | |
407 | |
324 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
325 | return 0; |
409 | return 0; |
326 | |
410 | |
327 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
328 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
329 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
330 | #endif |
414 | #endif |
331 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
332 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
333 | #endif |
417 | #endif |
334 | |
418 | |
335 | if (ev_method) |
419 | if (ev_method) |
336 | { |
420 | { |
337 | evw_init (&sigev, sigcb); |
421 | evw_init (&sigev, sigcb); |
338 | siginit (); |
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
339 | } |
427 | } |
340 | |
428 | |
341 | return ev_method; |
429 | return ev_method; |
342 | } |
430 | } |
343 | |
431 | |
344 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
345 | |
433 | |
|
|
434 | void |
346 | void ev_prefork (void) |
435 | ev_prefork (void) |
347 | { |
436 | { |
348 | /* nop */ |
437 | /* nop */ |
349 | } |
438 | } |
350 | |
439 | |
|
|
440 | void |
351 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
352 | { |
442 | { |
353 | /* nop */ |
443 | /* nop */ |
354 | } |
444 | } |
355 | |
445 | |
|
|
446 | void |
356 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
357 | { |
448 | { |
358 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
359 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
360 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
361 | #endif |
452 | #endif |
… | |
… | |
394 | |
485 | |
395 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
396 | } |
487 | } |
397 | |
488 | |
398 | static void |
489 | static void |
399 | call_pending () |
490 | call_pending (void) |
400 | { |
491 | { |
401 | int i; |
492 | while (pendingcnt) |
402 | |
|
|
403 | for (i = 0; i < pendingcnt; ++i) |
|
|
404 | { |
493 | { |
405 | ANPENDING *p = pendings + i; |
494 | ANPENDING *p = pendings + --pendingcnt; |
406 | |
495 | |
407 | if (p->w) |
496 | if (p->w) |
408 | { |
497 | { |
409 | p->w->pending = 0; |
498 | p->w->pending = 0; |
410 | p->w->cb (p->w, p->events); |
499 | p->w->cb (p->w, p->events); |
411 | } |
500 | } |
412 | } |
501 | } |
413 | |
|
|
414 | pendingcnt = 0; |
|
|
415 | } |
502 | } |
416 | |
503 | |
417 | static void |
504 | static void |
418 | timers_reify () |
505 | timers_reify (void) |
419 | { |
506 | { |
420 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
421 | { |
508 | { |
422 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
|
|
510 | |
|
|
511 | event ((W)w, EV_TIMEOUT); |
423 | |
512 | |
424 | /* first reschedule or stop timer */ |
513 | /* first reschedule or stop timer */ |
425 | if (w->repeat) |
514 | if (w->repeat) |
426 | { |
515 | { |
427 | w->at = now + w->repeat; |
516 | w->at = now + w->repeat; |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
429 | downheap ((WT *)timers, timercnt, 0); |
518 | downheap ((WT *)timers, timercnt, 0); |
430 | } |
519 | } |
431 | else |
520 | else |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
433 | |
|
|
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
522 | } |
436 | } |
523 | } |
437 | |
524 | |
438 | static void |
525 | static void |
439 | periodics_reify () |
526 | periodics_reify (void) |
440 | { |
527 | { |
441 | while (periodiccnt && periodics [0]->at <= ev_now) |
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
442 | { |
529 | { |
443 | struct ev_periodic *w = periodics [0]; |
530 | struct ev_periodic *w = periodics [0]; |
444 | |
531 | |
… | |
… | |
455 | event ((W)w, EV_TIMEOUT); |
542 | event ((W)w, EV_TIMEOUT); |
456 | } |
543 | } |
457 | } |
544 | } |
458 | |
545 | |
459 | static void |
546 | static void |
460 | time_jump (ev_tstamp diff) |
547 | periodics_reschedule (ev_tstamp diff) |
461 | { |
548 | { |
462 | int i; |
549 | int i; |
463 | |
550 | |
464 | /* adjust periodics */ |
551 | /* adjust periodics after time jump */ |
465 | for (i = 0; i < periodiccnt; ++i) |
552 | for (i = 0; i < periodiccnt; ++i) |
466 | { |
553 | { |
467 | struct ev_periodic *w = periodics [i]; |
554 | struct ev_periodic *w = periodics [i]; |
468 | |
555 | |
469 | if (w->interval) |
556 | if (w->interval) |
… | |
… | |
477 | |
564 | |
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
565 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
479 | } |
566 | } |
480 | } |
567 | } |
481 | } |
568 | } |
482 | |
|
|
483 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
484 | for (i = 0; i < timercnt; ++i) |
|
|
485 | timers [i]->at += diff; |
|
|
486 | } |
569 | } |
487 | |
570 | |
488 | static void |
571 | static void |
489 | time_update () |
572 | time_update (void) |
490 | { |
573 | { |
491 | int i; |
574 | int i; |
492 | |
575 | |
493 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
494 | |
577 | |
… | |
… | |
505 | return; /* all is well */ |
588 | return; /* all is well */ |
506 | |
589 | |
507 | ev_now = ev_time (); |
590 | ev_now = ev_time (); |
508 | } |
591 | } |
509 | |
592 | |
510 | time_jump (diff - odiff); |
593 | periodics_reschedule (diff - odiff); |
|
|
594 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
511 | } |
595 | } |
512 | else |
596 | else |
513 | { |
597 | { |
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
598 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
515 | time_jump (ev_now - now); |
599 | { |
|
|
600 | periodics_reschedule (ev_now - now); |
|
|
601 | |
|
|
602 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
603 | for (i = 0; i < timercnt; ++i) |
|
|
604 | timers [i]->at += diff; |
|
|
605 | } |
516 | |
606 | |
517 | now = ev_now; |
607 | now = ev_now; |
518 | } |
608 | } |
519 | } |
609 | } |
520 | |
610 | |
521 | int ev_loop_done; |
611 | int ev_loop_done; |
522 | |
612 | |
523 | void ev_loop (int flags) |
613 | void ev_loop (int flags) |
524 | { |
614 | { |
525 | double block; |
615 | double block; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
616 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
527 | |
|
|
528 | if (checkcnt) |
|
|
529 | { |
|
|
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
531 | call_pending (); |
|
|
532 | } |
|
|
533 | |
617 | |
534 | do |
618 | do |
535 | { |
619 | { |
|
|
620 | /* queue check watchers (and execute them) */ |
|
|
621 | if (preparecnt) |
|
|
622 | { |
|
|
623 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
624 | call_pending (); |
|
|
625 | } |
|
|
626 | |
536 | /* update fd-related kernel structures */ |
627 | /* update fd-related kernel structures */ |
537 | fd_reify (); |
628 | fd_reify (); |
538 | |
629 | |
539 | /* calculate blocking time */ |
630 | /* calculate blocking time */ |
540 | |
631 | |
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
632 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
633 | always have timers, we just calculate it always */ |
542 | ev_now = ev_time (); |
634 | ev_now = ev_time (); |
543 | |
635 | |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
636 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
545 | block = 0.; |
637 | block = 0.; |
546 | else |
638 | else |
547 | { |
639 | { |
548 | block = MAX_BLOCKTIME; |
640 | block = MAX_BLOCKTIME; |
549 | |
641 | |
550 | if (timercnt) |
642 | if (timercnt) |
551 | { |
643 | { |
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
644 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
553 | if (block > to) block = to; |
645 | if (block > to) block = to; |
554 | } |
646 | } |
555 | |
647 | |
556 | if (periodiccnt) |
648 | if (periodiccnt) |
557 | { |
649 | { |
… | |
… | |
566 | |
658 | |
567 | /* update ev_now, do magic */ |
659 | /* update ev_now, do magic */ |
568 | time_update (); |
660 | time_update (); |
569 | |
661 | |
570 | /* queue pending timers and reschedule them */ |
662 | /* queue pending timers and reschedule them */ |
|
|
663 | timers_reify (); /* relative timers called last */ |
571 | periodics_reify (); /* absolute timers first */ |
664 | periodics_reify (); /* absolute timers called first */ |
572 | timers_reify (); /* relative timers second */ |
|
|
573 | |
665 | |
574 | /* queue idle watchers unless io or timers are pending */ |
666 | /* queue idle watchers unless io or timers are pending */ |
575 | if (!pendingcnt) |
667 | if (!pendingcnt) |
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
668 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
577 | |
669 | |
578 | /* queue check and possibly idle watchers */ |
670 | /* queue check watchers, to be executed first */ |
|
|
671 | if (checkcnt) |
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
672 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
580 | |
673 | |
581 | call_pending (); |
674 | call_pending (); |
582 | } |
675 | } |
583 | while (!ev_loop_done); |
676 | while (!ev_loop_done); |
|
|
677 | |
|
|
678 | if (ev_loop_done != 2) |
|
|
679 | ev_loop_done = 0; |
584 | } |
680 | } |
585 | |
681 | |
586 | /*****************************************************************************/ |
682 | /*****************************************************************************/ |
587 | |
683 | |
588 | static void |
684 | static void |
… | |
… | |
606 | head = &(*head)->next; |
702 | head = &(*head)->next; |
607 | } |
703 | } |
608 | } |
704 | } |
609 | |
705 | |
610 | static void |
706 | static void |
|
|
707 | ev_clear (W w) |
|
|
708 | { |
|
|
709 | if (w->pending) |
|
|
710 | { |
|
|
711 | pendings [w->pending - 1].w = 0; |
|
|
712 | w->pending = 0; |
|
|
713 | } |
|
|
714 | } |
|
|
715 | |
|
|
716 | static void |
611 | ev_start (W w, int active) |
717 | ev_start (W w, int active) |
612 | { |
718 | { |
613 | w->pending = 0; |
|
|
614 | w->active = active; |
719 | w->active = active; |
615 | } |
720 | } |
616 | |
721 | |
617 | static void |
722 | static void |
618 | ev_stop (W w) |
723 | ev_stop (W w) |
619 | { |
724 | { |
620 | if (w->pending) |
|
|
621 | pendings [w->pending - 1].w = 0; |
|
|
622 | |
|
|
623 | w->active = 0; |
725 | w->active = 0; |
624 | } |
726 | } |
625 | |
727 | |
626 | /*****************************************************************************/ |
728 | /*****************************************************************************/ |
627 | |
729 | |
… | |
… | |
638 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
639 | |
741 | |
640 | ++fdchangecnt; |
742 | ++fdchangecnt; |
641 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
642 | fdchanges [fdchangecnt - 1] = fd; |
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
643 | } |
748 | } |
644 | |
749 | |
645 | void |
750 | void |
646 | evio_stop (struct ev_io *w) |
751 | evio_stop (struct ev_io *w) |
647 | { |
752 | { |
|
|
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
|
|
755 | ev_clear ((W)w); |
648 | if (!ev_is_active (w)) |
756 | if (!ev_is_active (w)) |
649 | return; |
757 | return; |
650 | |
758 | |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
652 | ev_stop ((W)w); |
760 | ev_stop ((W)w); |
… | |
… | |
654 | ++fdchangecnt; |
762 | ++fdchangecnt; |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
763 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
764 | fdchanges [fdchangecnt - 1] = w->fd; |
657 | } |
765 | } |
658 | |
766 | |
659 | |
|
|
660 | void |
767 | void |
661 | evtimer_start (struct ev_timer *w) |
768 | evtimer_start (struct ev_timer *w) |
662 | { |
769 | { |
663 | if (ev_is_active (w)) |
770 | if (ev_is_active (w)) |
664 | return; |
771 | return; |
665 | |
772 | |
666 | w->at += now; |
773 | w->at += now; |
|
|
774 | |
|
|
775 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
667 | |
776 | |
668 | ev_start ((W)w, ++timercnt); |
777 | ev_start ((W)w, ++timercnt); |
669 | array_needsize (timers, timermax, timercnt, ); |
778 | array_needsize (timers, timermax, timercnt, ); |
670 | timers [timercnt - 1] = w; |
779 | timers [timercnt - 1] = w; |
671 | upheap ((WT *)timers, timercnt - 1); |
780 | upheap ((WT *)timers, timercnt - 1); |
672 | } |
781 | } |
673 | |
782 | |
674 | void |
783 | void |
675 | evtimer_stop (struct ev_timer *w) |
784 | evtimer_stop (struct ev_timer *w) |
676 | { |
785 | { |
|
|
786 | ev_clear ((W)w); |
677 | if (!ev_is_active (w)) |
787 | if (!ev_is_active (w)) |
678 | return; |
788 | return; |
679 | |
789 | |
680 | if (w->active < timercnt--) |
790 | if (w->active < timercnt--) |
681 | { |
791 | { |
682 | timers [w->active - 1] = timers [timercnt]; |
792 | timers [w->active - 1] = timers [timercnt]; |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
793 | downheap ((WT *)timers, timercnt, w->active - 1); |
684 | } |
794 | } |
685 | |
795 | |
|
|
796 | w->at = w->repeat; |
|
|
797 | |
686 | ev_stop ((W)w); |
798 | ev_stop ((W)w); |
687 | } |
799 | } |
688 | |
800 | |
689 | void |
801 | void |
|
|
802 | evtimer_again (struct ev_timer *w) |
|
|
803 | { |
|
|
804 | if (ev_is_active (w)) |
|
|
805 | { |
|
|
806 | if (w->repeat) |
|
|
807 | { |
|
|
808 | w->at = now + w->repeat; |
|
|
809 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
810 | } |
|
|
811 | else |
|
|
812 | evtimer_stop (w); |
|
|
813 | } |
|
|
814 | else if (w->repeat) |
|
|
815 | evtimer_start (w); |
|
|
816 | } |
|
|
817 | |
|
|
818 | void |
690 | evperiodic_start (struct ev_periodic *w) |
819 | evperiodic_start (struct ev_periodic *w) |
691 | { |
820 | { |
692 | if (ev_is_active (w)) |
821 | if (ev_is_active (w)) |
693 | return; |
822 | return; |
|
|
823 | |
|
|
824 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
694 | |
825 | |
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
826 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
696 | if (w->interval) |
827 | if (w->interval) |
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
828 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
698 | |
829 | |
… | |
… | |
703 | } |
834 | } |
704 | |
835 | |
705 | void |
836 | void |
706 | evperiodic_stop (struct ev_periodic *w) |
837 | evperiodic_stop (struct ev_periodic *w) |
707 | { |
838 | { |
|
|
839 | ev_clear ((W)w); |
708 | if (!ev_is_active (w)) |
840 | if (!ev_is_active (w)) |
709 | return; |
841 | return; |
710 | |
842 | |
711 | if (w->active < periodiccnt--) |
843 | if (w->active < periodiccnt--) |
712 | { |
844 | { |
… | |
… | |
738 | } |
870 | } |
739 | |
871 | |
740 | void |
872 | void |
741 | evsignal_stop (struct ev_signal *w) |
873 | evsignal_stop (struct ev_signal *w) |
742 | { |
874 | { |
|
|
875 | ev_clear ((W)w); |
743 | if (!ev_is_active (w)) |
876 | if (!ev_is_active (w)) |
744 | return; |
877 | return; |
745 | |
878 | |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
879 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
747 | ev_stop ((W)w); |
880 | ev_stop ((W)w); |
… | |
… | |
760 | idles [idlecnt - 1] = w; |
893 | idles [idlecnt - 1] = w; |
761 | } |
894 | } |
762 | |
895 | |
763 | void evidle_stop (struct ev_idle *w) |
896 | void evidle_stop (struct ev_idle *w) |
764 | { |
897 | { |
|
|
898 | ev_clear ((W)w); |
|
|
899 | if (ev_is_active (w)) |
|
|
900 | return; |
|
|
901 | |
765 | idles [w->active - 1] = idles [--idlecnt]; |
902 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
903 | ev_stop ((W)w); |
|
|
904 | } |
|
|
905 | |
|
|
906 | void evprepare_start (struct ev_prepare *w) |
|
|
907 | { |
|
|
908 | if (ev_is_active (w)) |
|
|
909 | return; |
|
|
910 | |
|
|
911 | ev_start ((W)w, ++preparecnt); |
|
|
912 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
913 | prepares [preparecnt - 1] = w; |
|
|
914 | } |
|
|
915 | |
|
|
916 | void evprepare_stop (struct ev_prepare *w) |
|
|
917 | { |
|
|
918 | ev_clear ((W)w); |
|
|
919 | if (ev_is_active (w)) |
|
|
920 | return; |
|
|
921 | |
|
|
922 | prepares [w->active - 1] = prepares [--preparecnt]; |
766 | ev_stop ((W)w); |
923 | ev_stop ((W)w); |
767 | } |
924 | } |
768 | |
925 | |
769 | void evcheck_start (struct ev_check *w) |
926 | void evcheck_start (struct ev_check *w) |
770 | { |
927 | { |
… | |
… | |
776 | checks [checkcnt - 1] = w; |
933 | checks [checkcnt - 1] = w; |
777 | } |
934 | } |
778 | |
935 | |
779 | void evcheck_stop (struct ev_check *w) |
936 | void evcheck_stop (struct ev_check *w) |
780 | { |
937 | { |
|
|
938 | ev_clear ((W)w); |
|
|
939 | if (ev_is_active (w)) |
|
|
940 | return; |
|
|
941 | |
781 | checks [w->active - 1] = checks [--checkcnt]; |
942 | checks [w->active - 1] = checks [--checkcnt]; |
782 | ev_stop ((W)w); |
943 | ev_stop ((W)w); |
783 | } |
944 | } |
784 | |
945 | |
785 | /*****************************************************************************/ |
946 | void evchild_start (struct ev_child *w) |
|
|
947 | { |
|
|
948 | if (ev_is_active (w)) |
|
|
949 | return; |
786 | |
950 | |
|
|
951 | ev_start ((W)w, 1); |
|
|
952 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
953 | } |
|
|
954 | |
|
|
955 | void evchild_stop (struct ev_child *w) |
|
|
956 | { |
|
|
957 | ev_clear ((W)w); |
|
|
958 | if (ev_is_active (w)) |
|
|
959 | return; |
|
|
960 | |
|
|
961 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
962 | ev_stop ((W)w); |
|
|
963 | } |
|
|
964 | |
|
|
965 | /*****************************************************************************/ |
|
|
966 | |
|
|
967 | struct ev_once |
|
|
968 | { |
|
|
969 | struct ev_io io; |
|
|
970 | struct ev_timer to; |
|
|
971 | void (*cb)(int revents, void *arg); |
|
|
972 | void *arg; |
|
|
973 | }; |
|
|
974 | |
|
|
975 | static void |
|
|
976 | once_cb (struct ev_once *once, int revents) |
|
|
977 | { |
|
|
978 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
979 | void *arg = once->arg; |
|
|
980 | |
|
|
981 | evio_stop (&once->io); |
|
|
982 | evtimer_stop (&once->to); |
|
|
983 | free (once); |
|
|
984 | |
|
|
985 | cb (revents, arg); |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | once_cb_io (struct ev_io *w, int revents) |
|
|
990 | { |
|
|
991 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
992 | } |
|
|
993 | |
|
|
994 | static void |
|
|
995 | once_cb_to (struct ev_timer *w, int revents) |
|
|
996 | { |
|
|
997 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
998 | } |
|
|
999 | |
|
|
1000 | void |
|
|
1001 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1002 | { |
|
|
1003 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1004 | |
|
|
1005 | if (!once) |
|
|
1006 | cb (EV_ERROR, arg); |
|
|
1007 | else |
|
|
1008 | { |
|
|
1009 | once->cb = cb; |
|
|
1010 | once->arg = arg; |
|
|
1011 | |
|
|
1012 | evw_init (&once->io, once_cb_io); |
|
|
1013 | |
|
|
1014 | if (fd >= 0) |
|
|
1015 | { |
|
|
1016 | evio_set (&once->io, fd, events); |
|
|
1017 | evio_start (&once->io); |
|
|
1018 | } |
|
|
1019 | |
|
|
1020 | evw_init (&once->to, once_cb_to); |
|
|
1021 | |
|
|
1022 | if (timeout >= 0.) |
|
|
1023 | { |
|
|
1024 | evtimer_set (&once->to, timeout, 0.); |
|
|
1025 | evtimer_start (&once->to); |
|
|
1026 | } |
|
|
1027 | } |
|
|
1028 | } |
|
|
1029 | |
|
|
1030 | /*****************************************************************************/ |
|
|
1031 | |
787 | #if 1 |
1032 | #if 0 |
788 | |
1033 | |
789 | struct ev_io wio; |
1034 | struct ev_io wio; |
790 | |
1035 | |
791 | static void |
1036 | static void |
792 | sin_cb (struct ev_io *w, int revents) |
1037 | sin_cb (struct ev_io *w, int revents) |