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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines