ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.19
Committed: Wed Oct 31 17:55:55 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.18: +13 -2 lines
Log Message:
beginners mistake

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