ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.17
Committed: Wed Oct 31 14:44:15 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.16: +29 -0 lines
Log Message:
legalese

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     int i;
422    
423     for (i = 0; i < pendingcnt; ++i)
424     {
425     ANPENDING *p = pendings + i;
426    
427     if (p->w)
428     {
429     p->w->pending = 0;
430     p->w->cb (p->w, p->events);
431     }
432     }
433    
434     pendingcnt = 0;
435     }
436    
437     static void
438 root 1.12 timers_reify ()
439 root 1.1 {
440 root 1.4 while (timercnt && timers [0]->at <= now)
441 root 1.1 {
442     struct ev_timer *w = timers [0];
443    
444 root 1.16 event ((W)w, EV_TIMEOUT);
445    
446 root 1.4 /* first reschedule or stop timer */
447 root 1.1 if (w->repeat)
448     {
449 root 1.12 w->at = now + w->repeat;
450     assert (("timer timeout in the past, negative repeat?", w->at > now));
451     downheap ((WT *)timers, timercnt, 0);
452     }
453     else
454     evtimer_stop (w); /* nonrepeating: stop timer */
455     }
456     }
457 root 1.4
458 root 1.12 static void
459     periodics_reify ()
460     {
461     while (periodiccnt && periodics [0]->at <= ev_now)
462     {
463     struct ev_periodic *w = periodics [0];
464 root 1.1
465 root 1.12 /* first reschedule or stop timer */
466     if (w->interval)
467     {
468     w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
469     assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
470     downheap ((WT *)periodics, periodiccnt, 0);
471 root 1.1 }
472     else
473 root 1.12 evperiodic_stop (w); /* nonrepeating: stop timer */
474    
475     event ((W)w, EV_TIMEOUT);
476     }
477     }
478    
479     static void
480 root 1.13 periodics_reschedule (ev_tstamp diff)
481 root 1.12 {
482     int i;
483    
484 root 1.13 /* adjust periodics after time jump */
485 root 1.12 for (i = 0; i < periodiccnt; ++i)
486     {
487     struct ev_periodic *w = periodics [i];
488    
489     if (w->interval)
490 root 1.4 {
491 root 1.12 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
492    
493     if (fabs (diff) >= 1e-4)
494     {
495     evperiodic_stop (w);
496     evperiodic_start (w);
497    
498     i = 0; /* restart loop, inefficient, but time jumps should be rare */
499     }
500 root 1.4 }
501 root 1.12 }
502 root 1.1 }
503    
504 root 1.4 static void
505     time_update ()
506     {
507     int i;
508 root 1.12
509 root 1.4 ev_now = ev_time ();
510    
511     if (have_monotonic)
512     {
513     ev_tstamp odiff = diff;
514    
515 root 1.12 for (i = 4; --i; ) /* loop a few times, before making important decisions */
516 root 1.4 {
517     now = get_clock ();
518     diff = ev_now - now;
519    
520     if (fabs (odiff - diff) < MIN_TIMEJUMP)
521     return; /* all is well */
522    
523     ev_now = ev_time ();
524     }
525    
526 root 1.13 periodics_reschedule (diff - odiff);
527     /* no timer adjustment, as the monotonic clock doesn't jump */
528 root 1.4 }
529     else
530     {
531     if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
532 root 1.13 {
533     periodics_reschedule (ev_now - now);
534    
535     /* adjust timers. this is easy, as the offset is the same for all */
536     for (i = 0; i < timercnt; ++i)
537     timers [i]->at += diff;
538     }
539 root 1.4
540     now = ev_now;
541     }
542     }
543    
544 root 1.1 int ev_loop_done;
545    
546 root 1.4 void ev_loop (int flags)
547 root 1.1 {
548     double block;
549 root 1.13 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
550 root 1.1
551 root 1.9 if (checkcnt)
552     {
553 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
554 root 1.9 call_pending ();
555     }
556    
557 root 1.1 do
558     {
559     /* update fd-related kernel structures */
560 root 1.5 fd_reify ();
561 root 1.1
562     /* calculate blocking time */
563 root 1.12
564     /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
565     ev_now = ev_time ();
566    
567 root 1.9 if (flags & EVLOOP_NONBLOCK || idlecnt)
568 root 1.1 block = 0.;
569     else
570     {
571 root 1.4 block = MAX_BLOCKTIME;
572    
573 root 1.12 if (timercnt)
574 root 1.4 {
575 root 1.14 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
576 root 1.4 if (block > to) block = to;
577     }
578    
579 root 1.12 if (periodiccnt)
580 root 1.4 {
581 root 1.12 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
582 root 1.4 if (block > to) block = to;
583     }
584    
585 root 1.1 if (block < 0.) block = 0.;
586     }
587    
588     method_poll (block);
589    
590 root 1.4 /* update ev_now, do magic */
591     time_update ();
592    
593 root 1.9 /* queue pending timers and reschedule them */
594 root 1.12 periodics_reify (); /* absolute timers first */
595     timers_reify (); /* relative timers second */
596 root 1.1
597 root 1.9 /* queue idle watchers unless io or timers are pending */
598     if (!pendingcnt)
599 root 1.10 queue_events ((W *)idles, idlecnt, EV_IDLE);
600 root 1.9
601     /* queue check and possibly idle watchers */
602 root 1.10 queue_events ((W *)checks, checkcnt, EV_CHECK);
603 root 1.9
604 root 1.1 call_pending ();
605     }
606     while (!ev_loop_done);
607 root 1.13
608     if (ev_loop_done != 2)
609     ev_loop_done = 0;
610 root 1.1 }
611    
612 root 1.8 /*****************************************************************************/
613    
614 root 1.1 static void
615 root 1.10 wlist_add (WL *head, WL elem)
616 root 1.1 {
617     elem->next = *head;
618     *head = elem;
619     }
620    
621     static void
622 root 1.10 wlist_del (WL *head, WL elem)
623 root 1.1 {
624     while (*head)
625     {
626     if (*head == elem)
627     {
628     *head = elem->next;
629     return;
630     }
631    
632     head = &(*head)->next;
633     }
634     }
635    
636     static void
637 root 1.16 ev_clear (W w)
638     {
639     if (w->pending)
640     {
641     pendings [w->pending - 1].w = 0;
642     w->pending = 0;
643     }
644     }
645    
646     static void
647 root 1.10 ev_start (W w, int active)
648 root 1.1 {
649     w->active = active;
650     }
651    
652     static void
653 root 1.10 ev_stop (W w)
654 root 1.1 {
655     w->active = 0;
656     }
657    
658 root 1.8 /*****************************************************************************/
659    
660 root 1.1 void
661     evio_start (struct ev_io *w)
662     {
663     if (ev_is_active (w))
664     return;
665    
666     int fd = w->fd;
667    
668 root 1.10 ev_start ((W)w, 1);
669 root 1.1 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
670 root 1.10 wlist_add ((WL *)&anfds[fd].head, (WL)w);
671 root 1.1
672     ++fdchangecnt;
673     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
674     fdchanges [fdchangecnt - 1] = fd;
675     }
676    
677     void
678     evio_stop (struct ev_io *w)
679     {
680 root 1.16 ev_clear ((W)w);
681 root 1.1 if (!ev_is_active (w))
682     return;
683    
684 root 1.10 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
685     ev_stop ((W)w);
686 root 1.1
687     ++fdchangecnt;
688     array_needsize (fdchanges, fdchangemax, fdchangecnt, );
689     fdchanges [fdchangecnt - 1] = w->fd;
690     }
691    
692     void
693     evtimer_start (struct ev_timer *w)
694     {
695     if (ev_is_active (w))
696     return;
697    
698 root 1.12 w->at += now;
699    
700 root 1.13 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
701    
702 root 1.12 ev_start ((W)w, ++timercnt);
703     array_needsize (timers, timermax, timercnt, );
704     timers [timercnt - 1] = w;
705     upheap ((WT *)timers, timercnt - 1);
706     }
707    
708     void
709     evtimer_stop (struct ev_timer *w)
710     {
711 root 1.16 ev_clear ((W)w);
712 root 1.12 if (!ev_is_active (w))
713     return;
714    
715     if (w->active < timercnt--)
716 root 1.1 {
717 root 1.12 timers [w->active - 1] = timers [timercnt];
718     downheap ((WT *)timers, timercnt, w->active - 1);
719     }
720 root 1.4
721 root 1.14 w->at = w->repeat;
722    
723 root 1.12 ev_stop ((W)w);
724     }
725 root 1.4
726 root 1.12 void
727 root 1.14 evtimer_again (struct ev_timer *w)
728     {
729     if (ev_is_active (w))
730     {
731     if (w->repeat)
732     {
733     w->at = now + w->repeat;
734     downheap ((WT *)timers, timercnt, w->active - 1);
735     }
736     else
737     evtimer_stop (w);
738     }
739     else if (w->repeat)
740     evtimer_start (w);
741     }
742    
743     void
744 root 1.12 evperiodic_start (struct ev_periodic *w)
745     {
746     if (ev_is_active (w))
747     return;
748 root 1.1
749 root 1.13 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
750    
751 root 1.12 /* this formula differs from the one in periodic_reify because we do not always round up */
752     if (w->interval)
753     w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
754    
755     ev_start ((W)w, ++periodiccnt);
756     array_needsize (periodics, periodicmax, periodiccnt, );
757     periodics [periodiccnt - 1] = w;
758     upheap ((WT *)periodics, periodiccnt - 1);
759 root 1.1 }
760    
761     void
762 root 1.12 evperiodic_stop (struct ev_periodic *w)
763 root 1.1 {
764 root 1.16 ev_clear ((W)w);
765 root 1.1 if (!ev_is_active (w))
766     return;
767    
768 root 1.12 if (w->active < periodiccnt--)
769 root 1.2 {
770 root 1.12 periodics [w->active - 1] = periodics [periodiccnt];
771     downheap ((WT *)periodics, periodiccnt, w->active - 1);
772 root 1.2 }
773    
774 root 1.10 ev_stop ((W)w);
775 root 1.1 }
776    
777     void
778     evsignal_start (struct ev_signal *w)
779     {
780     if (ev_is_active (w))
781     return;
782    
783 root 1.10 ev_start ((W)w, 1);
784 root 1.1 array_needsize (signals, signalmax, w->signum, signals_init);
785 root 1.10 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
786 root 1.7
787     if (!w->next)
788     {
789     struct sigaction sa;
790     sa.sa_handler = sighandler;
791     sigfillset (&sa.sa_mask);
792     sa.sa_flags = 0;
793     sigaction (w->signum, &sa, 0);
794     }
795 root 1.1 }
796    
797     void
798     evsignal_stop (struct ev_signal *w)
799     {
800 root 1.16 ev_clear ((W)w);
801 root 1.1 if (!ev_is_active (w))
802     return;
803    
804 root 1.10 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
805     ev_stop ((W)w);
806 root 1.7
807     if (!signals [w->signum - 1].head)
808     signal (w->signum, SIG_DFL);
809 root 1.1 }
810    
811 root 1.9 void evidle_start (struct ev_idle *w)
812     {
813     if (ev_is_active (w))
814     return;
815    
816 root 1.10 ev_start ((W)w, ++idlecnt);
817 root 1.9 array_needsize (idles, idlemax, idlecnt, );
818     idles [idlecnt - 1] = w;
819     }
820    
821     void evidle_stop (struct ev_idle *w)
822     {
823 root 1.16 ev_clear ((W)w);
824     if (ev_is_active (w))
825     return;
826    
827 root 1.9 idles [w->active - 1] = idles [--idlecnt];
828 root 1.10 ev_stop ((W)w);
829 root 1.9 }
830    
831     void evcheck_start (struct ev_check *w)
832     {
833     if (ev_is_active (w))
834     return;
835    
836 root 1.10 ev_start ((W)w, ++checkcnt);
837 root 1.9 array_needsize (checks, checkmax, checkcnt, );
838     checks [checkcnt - 1] = w;
839     }
840    
841     void evcheck_stop (struct ev_check *w)
842     {
843 root 1.16 ev_clear ((W)w);
844     if (ev_is_active (w))
845     return;
846    
847 root 1.9 checks [w->active - 1] = checks [--checkcnt];
848 root 1.10 ev_stop ((W)w);
849 root 1.9 }
850    
851 root 1.1 /*****************************************************************************/
852 root 1.10
853 root 1.16 struct ev_once
854     {
855     struct ev_io io;
856     struct ev_timer to;
857     void (*cb)(int revents, void *arg);
858     void *arg;
859     };
860    
861     static void
862     once_cb (struct ev_once *once, int revents)
863     {
864     void (*cb)(int revents, void *arg) = once->cb;
865     void *arg = once->arg;
866    
867     evio_stop (&once->io);
868     evtimer_stop (&once->to);
869     free (once);
870    
871     cb (revents, arg);
872     }
873    
874     static void
875     once_cb_io (struct ev_io *w, int revents)
876     {
877     once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
878     }
879    
880     static void
881     once_cb_to (struct ev_timer *w, int revents)
882     {
883     once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
884     }
885    
886     void
887     ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
888     {
889     struct ev_once *once = malloc (sizeof (struct ev_once));
890    
891     if (!once)
892     cb (EV_ERROR, arg);
893     else
894     {
895     once->cb = cb;
896     once->arg = arg;
897    
898     evw_init (&once->io, once_cb_io);
899    
900     if (fd >= 0)
901     {
902     evio_set (&once->io, fd, events);
903     evio_start (&once->io);
904     }
905    
906     evw_init (&once->to, once_cb_to);
907    
908     if (timeout >= 0.)
909     {
910     evtimer_set (&once->to, timeout, 0.);
911     evtimer_start (&once->to);
912     }
913     }
914     }
915    
916     /*****************************************************************************/
917    
918 root 1.13 #if 0
919 root 1.12
920     struct ev_io wio;
921 root 1.1
922     static void
923     sin_cb (struct ev_io *w, int revents)
924     {
925     fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
926     }
927    
928     static void
929     ocb (struct ev_timer *w, int revents)
930     {
931 root 1.4 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
932     evtimer_stop (w);
933     evtimer_start (w);
934 root 1.1 }
935    
936 root 1.7 static void
937     scb (struct ev_signal *w, int revents)
938     {
939     fprintf (stderr, "signal %x,%d\n", revents, w->signum);
940 root 1.12 evio_stop (&wio);
941     evio_start (&wio);
942 root 1.7 }
943    
944 root 1.9 static void
945     gcb (struct ev_signal *w, int revents)
946     {
947     fprintf (stderr, "generic %x\n", revents);
948 root 1.12
949 root 1.9 }
950    
951 root 1.1 int main (void)
952     {
953     ev_init (0);
954    
955 root 1.12 evio_init (&wio, sin_cb, 0, EV_READ);
956     evio_start (&wio);
957 root 1.1
958 root 1.4 struct ev_timer t[10000];
959 root 1.2
960 root 1.9 #if 0
961 root 1.2 int i;
962 root 1.4 for (i = 0; i < 10000; ++i)
963 root 1.2 {
964     struct ev_timer *w = t + i;
965     evw_init (w, ocb, i);
966 root 1.12 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
967 root 1.2 evtimer_start (w);
968     if (drand48 () < 0.5)
969     evtimer_stop (w);
970     }
971 root 1.4 #endif
972    
973     struct ev_timer t1;
974 root 1.12 evtimer_init (&t1, ocb, 5, 10);
975 root 1.4 evtimer_start (&t1);
976 root 1.1
977 root 1.7 struct ev_signal sig;
978 root 1.12 evsignal_init (&sig, scb, SIGQUIT);
979 root 1.7 evsignal_start (&sig);
980    
981 root 1.9 struct ev_check cw;
982 root 1.12 evcheck_init (&cw, gcb);
983 root 1.9 evcheck_start (&cw);
984    
985     struct ev_idle iw;
986 root 1.12 evidle_init (&iw, gcb);
987 root 1.9 evidle_start (&iw);
988    
989 root 1.1 ev_loop (0);
990    
991     return 0;
992     }
993    
994     #endif
995    
996    
997    
998