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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines