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

File Contents

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