ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.39
Committed: Thu Nov 1 17:17:32 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.38: +1 -1 lines
Log Message:
created new documentation

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