ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.18
Committed: Wed Oct 31 16:29:52 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +2 -6 lines
Log Message:
make libev safer w.r.t. reentrancy

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