ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.44
Committed: Fri Nov 2 20:59:14 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.43: +10 -0 lines
Log Message:
added kqeueue backend, completely untested

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