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