ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.27
Committed: Wed Oct 31 22:16:36 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.26: +55 -47 lines
Log Message:
only add fds to fdchangeset one

File Contents

# User Rev Content
1 root 1.17 /*
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    
30 root 1.1 #include <math.h>
31     #include <stdlib.h>
32 root 1.7 #include <unistd.h>
33     #include <fcntl.h>
34     #include <signal.h>
35 root 1.16 #include <stddef.h>
36 root 1.1
37     #include <stdio.h>
38    
39 root 1.4 #include <assert.h>
40 root 1.1 #include <errno.h>
41 root 1.22 #include <sys/types.h>
42     #include <sys/wait.h>
43 root 1.1 #include <sys/time.h>
44     #include <time.h>
45    
46 root 1.10 #ifndef HAVE_MONOTONIC
47     # ifdef CLOCK_MONOTONIC
48     # define HAVE_MONOTONIC 1
49     # endif
50 root 1.1 #endif
51    
52 root 1.10 #ifndef HAVE_SELECT
53     # define HAVE_SELECT 1
54     #endif
55    
56     #ifndef HAVE_EPOLL
57     # define HAVE_EPOLL 0
58     #endif
59    
60     #ifndef HAVE_REALTIME
61     # define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62     #endif
63 root 1.1
64 root 1.4 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65 root 1.1 #define MAX_BLOCKTIME 60.
66 root 1.22 #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
67 root 1.1
68     #include "ev.h"
69    
70 root 1.10 typedef struct ev_watcher *W;
71     typedef struct ev_watcher_list *WL;
72 root 1.12 typedef struct ev_watcher_time *WT;
73 root 1.10
74 root 1.4 static ev_tstamp now, diff; /* monotonic clock */
75 root 1.1 ev_tstamp ev_now;
76     int ev_method;
77    
78     static int have_monotonic; /* runtime */
79    
80     static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
81 root 1.5 static void (*method_modify)(int fd, int oev, int nev);
82 root 1.1 static void (*method_poll)(ev_tstamp timeout);
83    
84 root 1.8 /*****************************************************************************/
85    
86 root 1.1 ev_tstamp
87     ev_time (void)
88     {
89     #if HAVE_REALTIME
90     struct timespec ts;
91     clock_gettime (CLOCK_REALTIME, &ts);
92     return ts.tv_sec + ts.tv_nsec * 1e-9;
93     #else
94     struct timeval tv;
95     gettimeofday (&tv, 0);
96     return tv.tv_sec + tv.tv_usec * 1e-6;
97     #endif
98     }
99    
100     static ev_tstamp
101     get_clock (void)
102     {
103     #if HAVE_MONOTONIC
104     if (have_monotonic)
105     {
106     struct timespec ts;
107     clock_gettime (CLOCK_MONOTONIC, &ts);
108     return ts.tv_sec + ts.tv_nsec * 1e-9;
109     }
110     #endif
111    
112     return ev_time ();
113     }
114    
115     #define array_needsize(base,cur,cnt,init) \
116     if ((cnt) > cur) \
117     { \
118 root 1.23 int newcnt = cur; \
119     do \
120     { \
121     newcnt = (newcnt << 1) | 4 & ~3; \
122     } \
123     while ((cnt) > newcnt); \
124     \
125 root 1.1 base = realloc (base, sizeof (*base) * (newcnt)); \
126     init (base + cur, newcnt - cur); \
127     cur = newcnt; \
128     }
129    
130 root 1.8 /*****************************************************************************/
131    
132 root 1.1 typedef struct
133     {
134     struct ev_io *head;
135 root 1.27 int events;
136 root 1.1 } ANFD;
137    
138     static ANFD *anfds;
139     static int anfdmax;
140    
141     static void
142     anfds_init (ANFD *base, int count)
143     {
144     while (count--)
145     {
146 root 1.27 base->head = 0;
147     base->events = EV_NONE;
148 root 1.1 ++base;
149     }
150     }
151    
152     typedef struct
153     {
154 root 1.10 W w;
155 root 1.1 int events;
156     } ANPENDING;
157    
158     static ANPENDING *pendings;
159     static int pendingmax, pendingcnt;
160    
161     static void
162 root 1.10 event (W w, int events)
163 root 1.1 {
164 root 1.16 if (w->active)
165     {
166     w->pending = ++pendingcnt;
167     array_needsize (pendings, pendingmax, pendingcnt, );
168     pendings [pendingcnt - 1].w = w;
169     pendings [pendingcnt - 1].events = events;
170     }
171 root 1.1 }
172    
173     static void
174 root 1.27 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
183 root 1.1 fd_event (int fd, int events)
184     {
185     ANFD *anfd = anfds + fd;
186     struct ev_io *w;
187    
188     for (w = anfd->head; w; w = w->next)
189     {
190     int ev = w->events & events;
191    
192     if (ev)
193 root 1.10 event ((W)w, ev);
194 root 1.1 }
195     }
196    
197 root 1.27 /*****************************************************************************/
198    
199     static int *fdchanges;
200     static int fdchangemax, fdchangecnt;
201    
202 root 1.9 static void
203 root 1.27 fd_reify (void)
204 root 1.9 {
205     int i;
206    
207 root 1.27 for (i = 0; i < fdchangecnt; ++i)
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;
241 root 1.9 }
242    
243 root 1.19 /* called on EBADF to verify fds */
244     static void
245 root 1.24 fd_recheck (void)
246 root 1.19 {
247     int fd;
248    
249     for (fd = 0; fd < anfdmax; ++fd)
250 root 1.27 if (anfds [fd].events)
251 root 1.19 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252     while (anfds [fd].head)
253 root 1.24 {
254     event ((W)anfds [fd].head, EV_ERROR);
255     evio_stop (anfds [fd].head);
256     }
257 root 1.19 }
258    
259 root 1.8 /*****************************************************************************/
260    
261 root 1.12 static struct ev_timer **timers;
262     static int timermax, timercnt;
263 root 1.4
264 root 1.12 static struct ev_periodic **periodics;
265     static int periodicmax, periodiccnt;
266 root 1.1
267     static void
268 root 1.12 upheap (WT *timers, int k)
269 root 1.1 {
270 root 1.12 WT w = timers [k];
271 root 1.1
272     while (k && timers [k >> 1]->at > w->at)
273     {
274     timers [k] = timers [k >> 1];
275     timers [k]->active = k + 1;
276     k >>= 1;
277     }
278    
279     timers [k] = w;
280     timers [k]->active = k + 1;
281    
282     }
283    
284     static void
285 root 1.12 downheap (WT *timers, int N, int k)
286 root 1.1 {
287 root 1.12 WT w = timers [k];
288 root 1.1
289 root 1.4 while (k < (N >> 1))
290 root 1.1 {
291     int j = k << 1;
292    
293 root 1.4 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
294 root 1.1 ++j;
295    
296     if (w->at <= timers [j]->at)
297     break;
298    
299     timers [k] = timers [j];
300 root 1.2 timers [k]->active = k + 1;
301 root 1.1 k = j;
302     }
303    
304     timers [k] = w;
305     timers [k]->active = k + 1;
306     }
307    
308 root 1.8 /*****************************************************************************/
309    
310 root 1.7 typedef struct
311     {
312     struct ev_signal *head;
313     sig_atomic_t gotsig;
314     } ANSIG;
315    
316     static ANSIG *signals;
317 root 1.4 static int signalmax;
318 root 1.1
319 root 1.7 static int sigpipe [2];
320     static sig_atomic_t gotsig;
321     static struct ev_io sigev;
322    
323 root 1.1 static void
324 root 1.7 signals_init (ANSIG *base, int count)
325 root 1.1 {
326     while (count--)
327 root 1.7 {
328     base->head = 0;
329     base->gotsig = 0;
330     ++base;
331     }
332     }
333    
334     static void
335     sighandler (int signum)
336     {
337     signals [signum - 1].gotsig = 1;
338    
339     if (!gotsig)
340     {
341     gotsig = 1;
342     write (sigpipe [1], &gotsig, 1);
343     }
344     }
345    
346     static void
347     sigcb (struct ev_io *iow, int revents)
348     {
349     struct ev_signal *w;
350     int sig;
351    
352     gotsig = 0;
353     read (sigpipe [0], &revents, 1);
354    
355     for (sig = signalmax; sig--; )
356     if (signals [sig].gotsig)
357     {
358     signals [sig].gotsig = 0;
359    
360     for (w = signals [sig].head; w; w = w->next)
361 root 1.10 event ((W)w, EV_SIGNAL);
362 root 1.7 }
363     }
364    
365     static void
366     siginit (void)
367     {
368     fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
369     fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
370    
371     /* rather than sort out wether we really need nb, set it */
372     fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373     fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
374    
375     evio_set (&sigev, sigpipe [0], EV_READ);
376     evio_start (&sigev);
377 root 1.1 }
378    
379 root 1.8 /*****************************************************************************/
380    
381 root 1.9 static struct ev_idle **idles;
382     static int idlemax, idlecnt;
383    
384 root 1.20 static struct ev_prepare **prepares;
385     static int preparemax, preparecnt;
386    
387 root 1.9 static struct ev_check **checks;
388     static int checkmax, checkcnt;
389    
390     /*****************************************************************************/
391    
392 root 1.22 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    
416 root 1.1 #if HAVE_EPOLL
417     # include "ev_epoll.c"
418     #endif
419     #if HAVE_SELECT
420     # include "ev_select.c"
421     #endif
422    
423 root 1.24 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    
435 root 1.1 int ev_init (int flags)
436     {
437 root 1.23 if (!ev_method)
438     {
439 root 1.1 #if HAVE_MONOTONIC
440 root 1.23 {
441     struct timespec ts;
442     if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443     have_monotonic = 1;
444     }
445 root 1.1 #endif
446    
447 root 1.23 ev_now = ev_time ();
448     now = get_clock ();
449     diff = ev_now - now;
450 root 1.1
451 root 1.23 if (pipe (sigpipe))
452     return 0;
453 root 1.7
454 root 1.23 ev_method = EVMETHOD_NONE;
455 root 1.1 #if HAVE_EPOLL
456 root 1.23 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457 root 1.1 #endif
458     #if HAVE_SELECT
459 root 1.23 if (ev_method == EVMETHOD_NONE) select_init (flags);
460 root 1.1 #endif
461    
462 root 1.23 if (ev_method)
463     {
464     evw_init (&sigev, sigcb);
465     siginit ();
466 root 1.22
467 root 1.23 evsignal_init (&childev, childcb, SIGCHLD);
468     evsignal_start (&childev);
469     }
470 root 1.7 }
471    
472 root 1.1 return ev_method;
473     }
474    
475 root 1.8 /*****************************************************************************/
476    
477 root 1.24 void
478     ev_prefork (void)
479 root 1.1 {
480 root 1.11 /* nop */
481 root 1.1 }
482    
483 root 1.24 void
484     ev_postfork_parent (void)
485 root 1.1 {
486 root 1.11 /* nop */
487 root 1.1 }
488    
489 root 1.24 void
490     ev_postfork_child (void)
491 root 1.1 {
492     #if HAVE_EPOLL
493 root 1.5 if (ev_method == EVMETHOD_EPOLL)
494     epoll_postfork_child ();
495 root 1.1 #endif
496 root 1.7
497     evio_stop (&sigev);
498     close (sigpipe [0]);
499     close (sigpipe [1]);
500     pipe (sigpipe);
501     siginit ();
502 root 1.1 }
503    
504 root 1.8 /*****************************************************************************/
505    
506 root 1.1 static void
507 root 1.24 call_pending (void)
508 root 1.1 {
509 root 1.18 while (pendingcnt)
510 root 1.1 {
511 root 1.18 ANPENDING *p = pendings + --pendingcnt;
512 root 1.1
513     if (p->w)
514     {
515     p->w->pending = 0;
516     p->w->cb (p->w, p->events);
517     }
518     }
519     }
520    
521     static void
522 root 1.24 timers_reify (void)
523 root 1.1 {
524 root 1.4 while (timercnt && timers [0]->at <= now)
525 root 1.1 {
526     struct ev_timer *w = timers [0];
527    
528 root 1.16 event ((W)w, EV_TIMEOUT);
529    
530 root 1.4 /* first reschedule or stop timer */
531 root 1.1 if (w->repeat)
532     {
533 root 1.12 w->at = now + w->repeat;
534     assert (("timer timeout in the past, negative repeat?", w->at > now));
535     downheap ((WT *)timers, timercnt, 0);
536     }
537     else
538     evtimer_stop (w); /* nonrepeating: stop timer */
539     }
540     }
541 root 1.4
542 root 1.12 static void
543 root 1.24 periodics_reify (void)
544 root 1.12 {
545     while (periodiccnt && periodics [0]->at <= ev_now)
546     {
547     struct ev_periodic *w = periodics [0];
548 root 1.1
549 root 1.12 /* first reschedule or stop timer */
550     if (w->interval)
551     {
552     w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
553     assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
554     downheap ((WT *)periodics, periodiccnt, 0);
555 root 1.1 }
556     else
557 root 1.12 evperiodic_stop (w); /* nonrepeating: stop timer */
558    
559     event ((W)w, EV_TIMEOUT);
560     }
561     }
562    
563     static void
564 root 1.13 periodics_reschedule (ev_tstamp diff)
565 root 1.12 {
566     int i;
567    
568 root 1.13 /* adjust periodics after time jump */
569 root 1.12 for (i = 0; i < periodiccnt; ++i)
570     {
571     struct ev_periodic *w = periodics [i];
572    
573     if (w->interval)
574 root 1.4 {
575 root 1.12 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
576    
577     if (fabs (diff) >= 1e-4)
578     {
579     evperiodic_stop (w);
580     evperiodic_start (w);
581    
582     i = 0; /* restart loop, inefficient, but time jumps should be rare */
583     }
584 root 1.4 }
585 root 1.12 }
586 root 1.1 }
587    
588 root 1.4 static void
589 root 1.24 time_update (void)
590 root 1.4 {
591     int i;
592 root 1.12
593 root 1.4 ev_now = ev_time ();
594    
595     if (have_monotonic)
596     {
597     ev_tstamp odiff = diff;
598    
599 root 1.12 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 root 1.4 {
601     now = get_clock ();
602     diff = ev_now - now;
603    
604     if (fabs (odiff - diff) < MIN_TIMEJUMP)
605     return; /* all is well */
606    
607     ev_now = ev_time ();
608     }
609    
610 root 1.13 periodics_reschedule (diff - odiff);
611     /* no timer adjustment, as the monotonic clock doesn't jump */
612 root 1.4 }
613     else
614     {
615     if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
616 root 1.13 {
617     periodics_reschedule (ev_now - now);
618    
619     /* adjust timers. this is easy, as the offset is the same for all */
620     for (i = 0; i < timercnt; ++i)
621     timers [i]->at += diff;
622     }
623 root 1.4
624     now = ev_now;
625     }
626     }
627    
628 root 1.1 int ev_loop_done;
629    
630 root 1.4 void ev_loop (int flags)
631 root 1.1 {
632     double block;
633 root 1.26 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 root 1.1
635 root 1.20 do
636 root 1.9 {
637 root 1.20 /* queue check watchers (and execute them) */
638 root 1.21 if (preparecnt)
639 root 1.20 {
640     queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641     call_pending ();
642     }
643 root 1.9
644 root 1.1 /* update fd-related kernel structures */
645 root 1.5 fd_reify ();
646 root 1.1
647     /* calculate blocking time */
648 root 1.12
649 root 1.21 /* we only need this for !monotonic clockor timers, but as we basically
650     always have timers, we just calculate it always */
651 root 1.12 ev_now = ev_time ();
652    
653 root 1.9 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 root 1.1 block = 0.;
655     else
656     {
657 root 1.4 block = MAX_BLOCKTIME;
658    
659 root 1.12 if (timercnt)
660 root 1.4 {
661 root 1.14 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
662 root 1.4 if (block > to) block = to;
663     }
664    
665 root 1.12 if (periodiccnt)
666 root 1.4 {
667 root 1.12 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
668 root 1.4 if (block > to) block = to;
669     }
670    
671 root 1.1 if (block < 0.) block = 0.;
672     }
673    
674     method_poll (block);
675    
676 root 1.4 /* update ev_now, do magic */
677     time_update ();
678    
679 root 1.9 /* queue pending timers and reschedule them */
680 root 1.20 timers_reify (); /* relative timers called last */
681     periodics_reify (); /* absolute timers called first */
682 root 1.1
683 root 1.9 /* queue idle watchers unless io or timers are pending */
684     if (!pendingcnt)
685 root 1.10 queue_events ((W *)idles, idlecnt, EV_IDLE);
686 root 1.9
687 root 1.20 /* queue check watchers, to be executed first */
688     if (checkcnt)
689     queue_events ((W *)checks, checkcnt, EV_CHECK);
690 root 1.9
691 root 1.1 call_pending ();
692     }
693     while (!ev_loop_done);
694 root 1.13
695     if (ev_loop_done != 2)
696     ev_loop_done = 0;
697 root 1.1 }
698    
699 root 1.8 /*****************************************************************************/
700    
701 root 1.1 static void
702 root 1.10 wlist_add (WL *head, WL elem)
703 root 1.1 {
704     elem->next = *head;
705     *head = elem;
706     }
707    
708     static void
709 root 1.10 wlist_del (WL *head, WL elem)
710 root 1.1 {
711     while (*head)
712     {
713     if (*head == elem)
714     {
715     *head = elem->next;
716     return;
717     }
718    
719     head = &(*head)->next;
720     }
721     }
722    
723     static void
724 root 1.16 ev_clear (W w)
725     {
726     if (w->pending)
727     {
728     pendings [w->pending - 1].w = 0;
729     w->pending = 0;
730     }
731     }
732    
733     static void
734 root 1.10 ev_start (W w, int active)
735 root 1.1 {
736     w->active = active;
737     }
738    
739     static void
740 root 1.10 ev_stop (W w)
741 root 1.1 {
742     w->active = 0;
743     }
744    
745 root 1.8 /*****************************************************************************/
746    
747 root 1.1 void
748     evio_start (struct ev_io *w)
749     {
750     if (ev_is_active (w))
751     return;
752    
753     int fd = w->fd;
754    
755 root 1.10 ev_start ((W)w, 1);
756 root 1.1 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 root 1.10 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 root 1.1
759 root 1.27 fd_change (fd);
760 root 1.1 }
761    
762     void
763     evio_stop (struct ev_io *w)
764     {
765 root 1.16 ev_clear ((W)w);
766 root 1.1 if (!ev_is_active (w))
767     return;
768    
769 root 1.10 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770     ev_stop ((W)w);
771 root 1.1
772 root 1.27 fd_change (w->fd);
773 root 1.1 }
774    
775     void
776     evtimer_start (struct ev_timer *w)
777     {
778     if (ev_is_active (w))
779     return;
780    
781 root 1.12 w->at += now;
782    
783 root 1.13 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
784    
785 root 1.12 ev_start ((W)w, ++timercnt);
786     array_needsize (timers, timermax, timercnt, );
787     timers [timercnt - 1] = w;
788     upheap ((WT *)timers, timercnt - 1);
789     }
790    
791     void
792     evtimer_stop (struct ev_timer *w)
793     {
794 root 1.16 ev_clear ((W)w);
795 root 1.12 if (!ev_is_active (w))
796     return;
797    
798     if (w->active < timercnt--)
799 root 1.1 {
800 root 1.12 timers [w->active - 1] = timers [timercnt];
801     downheap ((WT *)timers, timercnt, w->active - 1);
802     }
803 root 1.4
804 root 1.14 w->at = w->repeat;
805    
806 root 1.12 ev_stop ((W)w);
807     }
808 root 1.4
809 root 1.12 void
810 root 1.14 evtimer_again (struct ev_timer *w)
811     {
812     if (ev_is_active (w))
813     {
814     if (w->repeat)
815     {
816     w->at = now + w->repeat;
817     downheap ((WT *)timers, timercnt, w->active - 1);
818     }
819     else
820     evtimer_stop (w);
821     }
822     else if (w->repeat)
823     evtimer_start (w);
824     }
825    
826     void
827 root 1.12 evperiodic_start (struct ev_periodic *w)
828     {
829     if (ev_is_active (w))
830     return;
831 root 1.1
832 root 1.13 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
833    
834 root 1.12 /* this formula differs from the one in periodic_reify because we do not always round up */
835     if (w->interval)
836     w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837    
838     ev_start ((W)w, ++periodiccnt);
839     array_needsize (periodics, periodicmax, periodiccnt, );
840     periodics [periodiccnt - 1] = w;
841     upheap ((WT *)periodics, periodiccnt - 1);
842 root 1.1 }
843    
844     void
845 root 1.12 evperiodic_stop (struct ev_periodic *w)
846 root 1.1 {
847 root 1.16 ev_clear ((W)w);
848 root 1.1 if (!ev_is_active (w))
849     return;
850    
851 root 1.12 if (w->active < periodiccnt--)
852 root 1.2 {
853 root 1.12 periodics [w->active - 1] = periodics [periodiccnt];
854     downheap ((WT *)periodics, periodiccnt, w->active - 1);
855 root 1.2 }
856    
857 root 1.10 ev_stop ((W)w);
858 root 1.1 }
859    
860     void
861     evsignal_start (struct ev_signal *w)
862     {
863     if (ev_is_active (w))
864     return;
865    
866 root 1.10 ev_start ((W)w, 1);
867 root 1.1 array_needsize (signals, signalmax, w->signum, signals_init);
868 root 1.10 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 root 1.7
870     if (!w->next)
871     {
872     struct sigaction sa;
873     sa.sa_handler = sighandler;
874     sigfillset (&sa.sa_mask);
875     sa.sa_flags = 0;
876     sigaction (w->signum, &sa, 0);
877     }
878 root 1.1 }
879    
880     void
881     evsignal_stop (struct ev_signal *w)
882     {
883 root 1.16 ev_clear ((W)w);
884 root 1.1 if (!ev_is_active (w))
885     return;
886    
887 root 1.10 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888     ev_stop ((W)w);
889 root 1.7
890     if (!signals [w->signum - 1].head)
891     signal (w->signum, SIG_DFL);
892 root 1.1 }
893    
894 root 1.9 void evidle_start (struct ev_idle *w)
895     {
896     if (ev_is_active (w))
897     return;
898    
899 root 1.10 ev_start ((W)w, ++idlecnt);
900 root 1.9 array_needsize (idles, idlemax, idlecnt, );
901     idles [idlecnt - 1] = w;
902     }
903    
904     void evidle_stop (struct ev_idle *w)
905     {
906 root 1.16 ev_clear ((W)w);
907     if (ev_is_active (w))
908     return;
909    
910 root 1.9 idles [w->active - 1] = idles [--idlecnt];
911 root 1.10 ev_stop ((W)w);
912 root 1.9 }
913    
914 root 1.20 void evprepare_start (struct ev_prepare *w)
915     {
916     if (ev_is_active (w))
917     return;
918    
919     ev_start ((W)w, ++preparecnt);
920     array_needsize (prepares, preparemax, preparecnt, );
921     prepares [preparecnt - 1] = w;
922     }
923    
924     void evprepare_stop (struct ev_prepare *w)
925     {
926     ev_clear ((W)w);
927     if (ev_is_active (w))
928     return;
929    
930     prepares [w->active - 1] = prepares [--preparecnt];
931     ev_stop ((W)w);
932     }
933    
934 root 1.9 void evcheck_start (struct ev_check *w)
935     {
936     if (ev_is_active (w))
937     return;
938    
939 root 1.10 ev_start ((W)w, ++checkcnt);
940 root 1.9 array_needsize (checks, checkmax, checkcnt, );
941     checks [checkcnt - 1] = w;
942     }
943    
944     void evcheck_stop (struct ev_check *w)
945     {
946 root 1.16 ev_clear ((W)w);
947     if (ev_is_active (w))
948     return;
949    
950 root 1.9 checks [w->active - 1] = checks [--checkcnt];
951 root 1.10 ev_stop ((W)w);
952 root 1.9 }
953    
954 root 1.22 void evchild_start (struct ev_child *w)
955     {
956     if (ev_is_active (w))
957     return;
958    
959     ev_start ((W)w, 1);
960     wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961     }
962    
963     void evchild_stop (struct ev_child *w)
964     {
965     ev_clear ((W)w);
966     if (ev_is_active (w))
967     return;
968    
969     wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
970     ev_stop ((W)w);
971     }
972    
973 root 1.1 /*****************************************************************************/
974 root 1.10
975 root 1.16 struct ev_once
976     {
977     struct ev_io io;
978     struct ev_timer to;
979     void (*cb)(int revents, void *arg);
980     void *arg;
981     };
982    
983     static void
984     once_cb (struct ev_once *once, int revents)
985     {
986     void (*cb)(int revents, void *arg) = once->cb;
987     void *arg = once->arg;
988    
989     evio_stop (&once->io);
990     evtimer_stop (&once->to);
991     free (once);
992    
993     cb (revents, arg);
994     }
995    
996     static void
997     once_cb_io (struct ev_io *w, int revents)
998     {
999     once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1000     }
1001    
1002     static void
1003     once_cb_to (struct ev_timer *w, int revents)
1004     {
1005     once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1006     }
1007    
1008     void
1009     ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1010     {
1011     struct ev_once *once = malloc (sizeof (struct ev_once));
1012    
1013     if (!once)
1014     cb (EV_ERROR, arg);
1015     else
1016     {
1017     once->cb = cb;
1018     once->arg = arg;
1019    
1020     evw_init (&once->io, once_cb_io);
1021    
1022     if (fd >= 0)
1023     {
1024     evio_set (&once->io, fd, events);
1025     evio_start (&once->io);
1026     }
1027    
1028     evw_init (&once->to, once_cb_to);
1029    
1030     if (timeout >= 0.)
1031     {
1032     evtimer_set (&once->to, timeout, 0.);
1033     evtimer_start (&once->to);
1034     }
1035     }
1036     }
1037    
1038     /*****************************************************************************/
1039    
1040 root 1.13 #if 0
1041 root 1.12
1042     struct ev_io wio;
1043 root 1.1
1044     static void
1045     sin_cb (struct ev_io *w, int revents)
1046     {
1047     fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1048     }
1049    
1050     static void
1051     ocb (struct ev_timer *w, int revents)
1052     {
1053 root 1.4 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1054     evtimer_stop (w);
1055     evtimer_start (w);
1056 root 1.1 }
1057    
1058 root 1.7 static void
1059     scb (struct ev_signal *w, int revents)
1060     {
1061     fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1062 root 1.12 evio_stop (&wio);
1063     evio_start (&wio);
1064 root 1.7 }
1065    
1066 root 1.9 static void
1067     gcb (struct ev_signal *w, int revents)
1068     {
1069     fprintf (stderr, "generic %x\n", revents);
1070 root 1.12
1071 root 1.9 }
1072    
1073 root 1.1 int main (void)
1074     {
1075     ev_init (0);
1076    
1077 root 1.12 evio_init (&wio, sin_cb, 0, EV_READ);
1078     evio_start (&wio);
1079 root 1.1
1080 root 1.4 struct ev_timer t[10000];
1081 root 1.2
1082 root 1.9 #if 0
1083 root 1.2 int i;
1084 root 1.4 for (i = 0; i < 10000; ++i)
1085 root 1.2 {
1086     struct ev_timer *w = t + i;
1087     evw_init (w, ocb, i);
1088 root 1.12 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
1089 root 1.2 evtimer_start (w);
1090     if (drand48 () < 0.5)
1091     evtimer_stop (w);
1092     }
1093 root 1.4 #endif
1094    
1095     struct ev_timer t1;
1096 root 1.12 evtimer_init (&t1, ocb, 5, 10);
1097 root 1.4 evtimer_start (&t1);
1098 root 1.1
1099 root 1.7 struct ev_signal sig;
1100 root 1.12 evsignal_init (&sig, scb, SIGQUIT);
1101 root 1.7 evsignal_start (&sig);
1102    
1103 root 1.9 struct ev_check cw;
1104 root 1.12 evcheck_init (&cw, gcb);
1105 root 1.9 evcheck_start (&cw);
1106    
1107     struct ev_idle iw;
1108 root 1.12 evidle_init (&iw, gcb);
1109 root 1.9 evidle_start (&iw);
1110    
1111 root 1.1 ev_loop (0);
1112    
1113     return 0;
1114     }
1115    
1116     #endif
1117    
1118    
1119    
1120