ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.30
Committed: Thu Nov 1 08:28:33 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.29: +8 -12 lines
Log Message:
remove pointless and buggy active check

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