ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.21 by root, Wed Oct 31 18:37:38 2007 UTC vs.
Revision 1.46 by root, Sat Nov 3 09:20:12 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines