ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.26
Committed: Wed Oct 31 21:50:15 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +1 -1 lines
Log Message:
dohdohdoh

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