ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.33
Committed: Thu Nov 1 11:11:22 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.32: +27 -19 lines
Log Message:
*** empty log message ***

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