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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines