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