ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.42
Committed: Fri Nov 2 20:05:05 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.41: +23 -17 lines
Log Message:
impleemnt static priorities

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