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

Comparing libev/ev.c (file contents):
Revision 1.24 by root, Wed Oct 31 20:46:44 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 (void) 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)
210 { 292 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head); 293 ev_io_stop (anfds [fd].head);
294 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
213 } 295 }
214} 296}
215 297
216/*****************************************************************************/ 298/*****************************************************************************/
217 299
265/*****************************************************************************/ 347/*****************************************************************************/
266 348
267typedef struct 349typedef struct
268{ 350{
269 struct ev_signal *head; 351 struct ev_signal *head;
270 sig_atomic_t gotsig; 352 sig_atomic_t volatile gotsig;
271} ANSIG; 353} ANSIG;
272 354
273static ANSIG *signals; 355static ANSIG *signals;
274static int signalmax; 356static int signalmax;
275 357
276static int sigpipe [2]; 358static int sigpipe [2];
277static sig_atomic_t gotsig; 359static sig_atomic_t volatile gotsig;
278static struct ev_io sigev; 360static struct ev_io sigev;
279 361
280static void 362static void
281signals_init (ANSIG *base, int count) 363signals_init (ANSIG *base, int count)
282{ 364{
283 while (count--) 365 while (count--)
284 { 366 {
285 base->head = 0; 367 base->head = 0;
286 base->gotsig = 0; 368 base->gotsig = 0;
369
287 ++base; 370 ++base;
288 } 371 }
289} 372}
290 373
291static void 374static void
294 signals [signum - 1].gotsig = 1; 377 signals [signum - 1].gotsig = 1;
295 378
296 if (!gotsig) 379 if (!gotsig)
297 { 380 {
298 gotsig = 1; 381 gotsig = 1;
299 write (sigpipe [1], &gotsig, 1); 382 write (sigpipe [1], &signum, 1);
300 } 383 }
301} 384}
302 385
303static void 386static void
304sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
305{ 388{
306 struct ev_signal *w; 389 struct ev_signal *w;
307 int sig; 390 int signum;
308 391
392 read (sigpipe [0], &revents, 1);
309 gotsig = 0; 393 gotsig = 0;
310 read (sigpipe [0], &revents, 1);
311 394
312 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
313 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
314 { 397 {
315 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
316 399
317 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
318 event ((W)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
319 } 402 }
320} 403}
321 404
322static void 405static void
327 410
328 /* rather than sort out wether we really need nb, set it */ 411 /* rather than sort out wether we really need nb, set it */
329 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
330 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
331 414
332 evio_set (&sigev, sigpipe [0], EV_READ); 415 ev_io_set (&sigev, sigpipe [0], EV_READ);
333 evio_start (&sigev); 416 ev_io_start (&sigev);
334} 417}
335 418
336/*****************************************************************************/ 419/*****************************************************************************/
337 420
338static struct ev_idle **idles; 421static struct ev_idle **idles;
359 struct ev_child *w; 442 struct ev_child *w;
360 int pid, status; 443 int pid, status;
361 444
362 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
363 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
364 if (w->pid == pid || w->pid == -1) 447 if (w->pid == pid || !w->pid)
365 { 448 {
366 w->status = status; 449 w->status = status;
367 event ((W)w, EV_CHILD); 450 event ((W)w, EV_CHILD);
368 } 451 }
369} 452}
370 453
371/*****************************************************************************/ 454/*****************************************************************************/
372 455
373#if HAVE_EPOLL 456#if EV_USE_EPOLL
374# include "ev_epoll.c" 457# include "ev_epoll.c"
375#endif 458#endif
376#if HAVE_SELECT 459#if EV_USE_SELECT
377# include "ev_select.c" 460# include "ev_select.c"
378#endif 461#endif
379 462
380int 463int
381ev_version_major (void) 464ev_version_major (void)
391 474
392int ev_init (int flags) 475int ev_init (int flags)
393{ 476{
394 if (!ev_method) 477 if (!ev_method)
395 { 478 {
396#if HAVE_MONOTONIC 479#if EV_USE_MONOTONIC
397 { 480 {
398 struct timespec ts; 481 struct timespec ts;
399 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
400 have_monotonic = 1; 483 have_monotonic = 1;
401 } 484 }
402#endif 485#endif
403 486
404 ev_now = ev_time (); 487 ev_now = ev_time ();
405 now = get_clock (); 488 now = get_clock ();
489 now_floor = now;
406 diff = ev_now - now; 490 diff = ev_now - now;
407 491
408 if (pipe (sigpipe)) 492 if (pipe (sigpipe))
409 return 0; 493 return 0;
410 494
411 ev_method = EVMETHOD_NONE; 495 ev_method = EVMETHOD_NONE;
412#if HAVE_EPOLL 496#if EV_USE_EPOLL
413 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 497 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
414#endif 498#endif
415#if HAVE_SELECT 499#if EV_USE_SELECT
416 if (ev_method == EVMETHOD_NONE) select_init (flags); 500 if (ev_method == EVMETHOD_NONE) select_init (flags);
417#endif 501#endif
418 502
419 if (ev_method) 503 if (ev_method)
420 { 504 {
421 evw_init (&sigev, sigcb); 505 ev_watcher_init (&sigev, sigcb);
422 siginit (); 506 siginit ();
423 507
424 evsignal_init (&childev, childcb, SIGCHLD); 508 ev_signal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev); 509 ev_signal_start (&childev);
426 } 510 }
427 } 511 }
428 512
429 return ev_method; 513 return ev_method;
430} 514}
431 515
432/*****************************************************************************/ 516/*****************************************************************************/
433 517
434void 518void
435ev_prefork (void) 519ev_fork_prepare (void)
436{ 520{
437 /* nop */ 521 /* nop */
438} 522}
439 523
440void 524void
441ev_postfork_parent (void) 525ev_fork_parent (void)
442{ 526{
443 /* nop */ 527 /* nop */
444} 528}
445 529
446void 530void
447ev_postfork_child (void) 531ev_fork_child (void)
448{ 532{
449#if HAVE_EPOLL 533#if EV_USE_EPOLL
450 if (ev_method == EVMETHOD_EPOLL) 534 if (ev_method == EVMETHOD_EPOLL)
451 epoll_postfork_child (); 535 epoll_postfork_child ();
452#endif 536#endif
453 537
454 evio_stop (&sigev); 538 ev_io_stop (&sigev);
455 close (sigpipe [0]); 539 close (sigpipe [0]);
456 close (sigpipe [1]); 540 close (sigpipe [1]);
457 pipe (sigpipe); 541 pipe (sigpipe);
458 siginit (); 542 siginit ();
459} 543}
460 544
461/*****************************************************************************/ 545/*****************************************************************************/
462 546
463static void 547static void
464fd_reify (void)
465{
466 int i;
467
468 for (i = 0; i < fdchangecnt; ++i)
469 {
470 int fd = fdchanges [i];
471 ANFD *anfd = anfds + fd;
472 struct ev_io *w;
473
474 int wev = 0;
475
476 for (w = anfd->head; w; w = w->next)
477 wev |= w->events;
478
479 if (anfd->wev != wev)
480 {
481 method_modify (fd, anfd->wev, wev);
482 anfd->wev = wev;
483 }
484 }
485
486 fdchangecnt = 0;
487}
488
489static void
490call_pending (void) 548call_pending (void)
491{ 549{
492 while (pendingcnt) 550 while (pendingcnt)
493 { 551 {
494 ANPENDING *p = pendings + --pendingcnt; 552 ANPENDING *p = pendings + --pendingcnt;
506{ 564{
507 while (timercnt && timers [0]->at <= now) 565 while (timercnt && timers [0]->at <= now)
508 { 566 {
509 struct ev_timer *w = timers [0]; 567 struct ev_timer *w = timers [0];
510 568
511 event ((W)w, EV_TIMEOUT);
512
513 /* first reschedule or stop timer */ 569 /* first reschedule or stop timer */
514 if (w->repeat) 570 if (w->repeat)
515 { 571 {
572 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
516 w->at = now + w->repeat; 573 w->at = now + w->repeat;
517 assert (("timer timeout in the past, negative repeat?", w->at > now));
518 downheap ((WT *)timers, timercnt, 0); 574 downheap ((WT *)timers, timercnt, 0);
519 } 575 }
520 else 576 else
521 evtimer_stop (w); /* nonrepeating: stop timer */ 577 ev_timer_stop (w); /* nonrepeating: stop timer */
578
579 event ((W)w, EV_TIMEOUT);
522 } 580 }
523} 581}
524 582
525static void 583static void
526periodics_reify (void) 584periodics_reify (void)
531 589
532 /* first reschedule or stop timer */ 590 /* first reschedule or stop timer */
533 if (w->interval) 591 if (w->interval)
534 { 592 {
535 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;
536 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));
537 downheap ((WT *)periodics, periodiccnt, 0); 595 downheap ((WT *)periodics, periodiccnt, 0);
538 } 596 }
539 else 597 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */ 598 ev_periodic_stop (w); /* nonrepeating: stop timer */
541 599
542 event ((W)w, EV_TIMEOUT); 600 event ((W)w, EV_PERIODIC);
543 } 601 }
544} 602}
545 603
546static void 604static void
547periodics_reschedule (ev_tstamp diff) 605periodics_reschedule (ev_tstamp diff)
557 { 615 {
558 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;
559 617
560 if (fabs (diff) >= 1e-4) 618 if (fabs (diff) >= 1e-4)
561 { 619 {
562 evperiodic_stop (w); 620 ev_periodic_stop (w);
563 evperiodic_start (w); 621 ev_periodic_start (w);
564 622
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 623 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 } 624 }
567 } 625 }
568 } 626 }
569} 627}
570 628
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
571static void 647static void
572time_update (void) 648time_update (void)
573{ 649{
574 int i; 650 int i;
575 651
576 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
577
578 if (have_monotonic) 653 if (expect_true (have_monotonic))
579 { 654 {
655 if (time_update_monotonic ())
656 {
580 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
581 658
582 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 */
583 { 660 {
584 now = get_clock ();
585 diff = ev_now - now; 661 diff = ev_now - now;
586 662
587 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
588 return; /* all is well */ 664 return; /* all is well */
589 665
590 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
591 } 669 }
592 670
593 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
594 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
595 } 674 }
596 else 675 else
676#endif
597 { 677 {
678 ev_now = ev_time ();
679
598 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))
599 { 681 {
600 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
601 683
602 /* 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 */
603 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
611int ev_loop_done; 693int ev_loop_done;
612 694
613void ev_loop (int flags) 695void ev_loop (int flags)
614{ 696{
615 double block; 697 double block;
616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
617 699
618 do 700 do
619 { 701 {
620 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
621 if (preparecnt) 703 if (expect_false (preparecnt))
622 { 704 {
623 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
624 call_pending (); 706 call_pending ();
625 } 707 }
626 708
629 711
630 /* calculate blocking time */ 712 /* calculate blocking time */
631 713
632 /* 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
633 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 {
634 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
635 725
636 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
637 block = 0.; 727 block = 0.;
638 else 728 else
639 { 729 {
640 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
641 731
642 if (timercnt) 732 if (timercnt)
643 { 733 {
644 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
645 if (block > to) block = to; 735 if (block > to) block = to;
646 } 736 }
647 737
648 if (periodiccnt) 738 if (periodiccnt)
649 { 739 {
702 head = &(*head)->next; 792 head = &(*head)->next;
703 } 793 }
704} 794}
705 795
706static void 796static void
707ev_clear (W w) 797ev_clear_pending (W w)
708{ 798{
709 if (w->pending) 799 if (w->pending)
710 { 800 {
711 pendings [w->pending - 1].w = 0; 801 pendings [w->pending - 1].w = 0;
712 w->pending = 0; 802 w->pending = 0;
726} 816}
727 817
728/*****************************************************************************/ 818/*****************************************************************************/
729 819
730void 820void
731evio_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
732{ 822{
823 int fd = w->fd;
824
733 if (ev_is_active (w)) 825 if (ev_is_active (w))
734 return; 826 return;
735 827
736 int fd = w->fd; 828 assert (("ev_io_start called with negative fd", fd >= 0));
737 829
738 ev_start ((W)w, 1); 830 ev_start ((W)w, 1);
739 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
740 wlist_add ((WL *)&anfds[fd].head, (WL)w); 832 wlist_add ((WL *)&anfds[fd].head, (WL)w);
741 833
742 ++fdchangecnt; 834 fd_change (fd);
743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
744 fdchanges [fdchangecnt - 1] = fd;
745} 835}
746 836
747void 837void
748evio_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
749{ 839{
750 ev_clear ((W)w); 840 ev_clear_pending ((W)w);
751 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
752 return; 842 return;
753 843
754 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
755 ev_stop ((W)w); 845 ev_stop ((W)w);
756 846
757 ++fdchangecnt; 847 fd_change (w->fd);
758 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
759 fdchanges [fdchangecnt - 1] = w->fd;
760} 848}
761 849
762void 850void
763evtimer_start (struct ev_timer *w) 851ev_timer_start (struct ev_timer *w)
764{ 852{
765 if (ev_is_active (w)) 853 if (ev_is_active (w))
766 return; 854 return;
767 855
768 w->at += now; 856 w->at += now;
769 857
770 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.));
771 859
772 ev_start ((W)w, ++timercnt); 860 ev_start ((W)w, ++timercnt);
773 array_needsize (timers, timermax, timercnt, ); 861 array_needsize (timers, timermax, timercnt, );
774 timers [timercnt - 1] = w; 862 timers [timercnt - 1] = w;
775 upheap ((WT *)timers, timercnt - 1); 863 upheap ((WT *)timers, timercnt - 1);
776} 864}
777 865
778void 866void
779evtimer_stop (struct ev_timer *w) 867ev_timer_stop (struct ev_timer *w)
780{ 868{
781 ev_clear ((W)w); 869 ev_clear_pending ((W)w);
782 if (!ev_is_active (w)) 870 if (!ev_is_active (w))
783 return; 871 return;
784 872
785 if (w->active < timercnt--) 873 if (w->active < timercnt--)
786 { 874 {
792 880
793 ev_stop ((W)w); 881 ev_stop ((W)w);
794} 882}
795 883
796void 884void
797evtimer_again (struct ev_timer *w) 885ev_timer_again (struct ev_timer *w)
798{ 886{
799 if (ev_is_active (w)) 887 if (ev_is_active (w))
800 { 888 {
801 if (w->repeat) 889 if (w->repeat)
802 { 890 {
803 w->at = now + w->repeat; 891 w->at = now + w->repeat;
804 downheap ((WT *)timers, timercnt, w->active - 1); 892 downheap ((WT *)timers, timercnt, w->active - 1);
805 } 893 }
806 else 894 else
807 evtimer_stop (w); 895 ev_timer_stop (w);
808 } 896 }
809 else if (w->repeat) 897 else if (w->repeat)
810 evtimer_start (w); 898 ev_timer_start (w);
811} 899}
812 900
813void 901void
814evperiodic_start (struct ev_periodic *w) 902ev_periodic_start (struct ev_periodic *w)
815{ 903{
816 if (ev_is_active (w)) 904 if (ev_is_active (w))
817 return; 905 return;
818 906
819 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.));
820 908
821 /* 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 */
822 if (w->interval) 910 if (w->interval)
823 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
824 912
827 periodics [periodiccnt - 1] = w; 915 periodics [periodiccnt - 1] = w;
828 upheap ((WT *)periodics, periodiccnt - 1); 916 upheap ((WT *)periodics, periodiccnt - 1);
829} 917}
830 918
831void 919void
832evperiodic_stop (struct ev_periodic *w) 920ev_periodic_stop (struct ev_periodic *w)
833{ 921{
834 ev_clear ((W)w); 922 ev_clear_pending ((W)w);
835 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
836 return; 924 return;
837 925
838 if (w->active < periodiccnt--) 926 if (w->active < periodiccnt--)
839 { 927 {
843 931
844 ev_stop ((W)w); 932 ev_stop ((W)w);
845} 933}
846 934
847void 935void
848evsignal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
849{ 937{
850 if (ev_is_active (w)) 938 if (ev_is_active (w))
851 return; 939 return;
940
941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
852 942
853 ev_start ((W)w, 1); 943 ev_start ((W)w, 1);
854 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
855 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
856 946
863 sigaction (w->signum, &sa, 0); 953 sigaction (w->signum, &sa, 0);
864 } 954 }
865} 955}
866 956
867void 957void
868evsignal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
869{ 959{
870 ev_clear ((W)w); 960 ev_clear_pending ((W)w);
871 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
872 return; 962 return;
873 963
874 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
875 ev_stop ((W)w); 965 ev_stop ((W)w);
876 966
877 if (!signals [w->signum - 1].head) 967 if (!signals [w->signum - 1].head)
878 signal (w->signum, SIG_DFL); 968 signal (w->signum, SIG_DFL);
879} 969}
880 970
971void
881void evidle_start (struct ev_idle *w) 972ev_idle_start (struct ev_idle *w)
882{ 973{
883 if (ev_is_active (w)) 974 if (ev_is_active (w))
884 return; 975 return;
885 976
886 ev_start ((W)w, ++idlecnt); 977 ev_start ((W)w, ++idlecnt);
887 array_needsize (idles, idlemax, idlecnt, ); 978 array_needsize (idles, idlemax, idlecnt, );
888 idles [idlecnt - 1] = w; 979 idles [idlecnt - 1] = w;
889} 980}
890 981
982void
891void evidle_stop (struct ev_idle *w) 983ev_idle_stop (struct ev_idle *w)
892{ 984{
893 ev_clear ((W)w); 985 ev_clear_pending ((W)w);
894 if (ev_is_active (w)) 986 if (ev_is_active (w))
895 return; 987 return;
896 988
897 idles [w->active - 1] = idles [--idlecnt]; 989 idles [w->active - 1] = idles [--idlecnt];
898 ev_stop ((W)w); 990 ev_stop ((W)w);
899} 991}
900 992
993void
901void evprepare_start (struct ev_prepare *w) 994ev_prepare_start (struct ev_prepare *w)
902{ 995{
903 if (ev_is_active (w)) 996 if (ev_is_active (w))
904 return; 997 return;
905 998
906 ev_start ((W)w, ++preparecnt); 999 ev_start ((W)w, ++preparecnt);
907 array_needsize (prepares, preparemax, preparecnt, ); 1000 array_needsize (prepares, preparemax, preparecnt, );
908 prepares [preparecnt - 1] = w; 1001 prepares [preparecnt - 1] = w;
909} 1002}
910 1003
1004void
911void evprepare_stop (struct ev_prepare *w) 1005ev_prepare_stop (struct ev_prepare *w)
912{ 1006{
913 ev_clear ((W)w); 1007 ev_clear_pending ((W)w);
914 if (ev_is_active (w)) 1008 if (ev_is_active (w))
915 return; 1009 return;
916 1010
917 prepares [w->active - 1] = prepares [--preparecnt]; 1011 prepares [w->active - 1] = prepares [--preparecnt];
918 ev_stop ((W)w); 1012 ev_stop ((W)w);
919} 1013}
920 1014
1015void
921void evcheck_start (struct ev_check *w) 1016ev_check_start (struct ev_check *w)
922{ 1017{
923 if (ev_is_active (w)) 1018 if (ev_is_active (w))
924 return; 1019 return;
925 1020
926 ev_start ((W)w, ++checkcnt); 1021 ev_start ((W)w, ++checkcnt);
927 array_needsize (checks, checkmax, checkcnt, ); 1022 array_needsize (checks, checkmax, checkcnt, );
928 checks [checkcnt - 1] = w; 1023 checks [checkcnt - 1] = w;
929} 1024}
930 1025
1026void
931void evcheck_stop (struct ev_check *w) 1027ev_check_stop (struct ev_check *w)
932{ 1028{
933 ev_clear ((W)w); 1029 ev_clear_pending ((W)w);
934 if (ev_is_active (w)) 1030 if (ev_is_active (w))
935 return; 1031 return;
936 1032
937 checks [w->active - 1] = checks [--checkcnt]; 1033 checks [w->active - 1] = checks [--checkcnt];
938 ev_stop ((W)w); 1034 ev_stop ((W)w);
939} 1035}
940 1036
1037void
941void evchild_start (struct ev_child *w) 1038ev_child_start (struct ev_child *w)
942{ 1039{
943 if (ev_is_active (w)) 1040 if (ev_is_active (w))
944 return; 1041 return;
945 1042
946 ev_start ((W)w, 1); 1043 ev_start ((W)w, 1);
947 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1044 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
948} 1045}
949 1046
1047void
950void evchild_stop (struct ev_child *w) 1048ev_child_stop (struct ev_child *w)
951{ 1049{
952 ev_clear ((W)w); 1050 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1051 if (ev_is_active (w))
954 return; 1052 return;
955 1053
956 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1054 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
957 ev_stop ((W)w); 1055 ev_stop ((W)w);
971once_cb (struct ev_once *once, int revents) 1069once_cb (struct ev_once *once, int revents)
972{ 1070{
973 void (*cb)(int revents, void *arg) = once->cb; 1071 void (*cb)(int revents, void *arg) = once->cb;
974 void *arg = once->arg; 1072 void *arg = once->arg;
975 1073
976 evio_stop (&once->io); 1074 ev_io_stop (&once->io);
977 evtimer_stop (&once->to); 1075 ev_timer_stop (&once->to);
978 free (once); 1076 free (once);
979 1077
980 cb (revents, arg); 1078 cb (revents, arg);
981} 1079}
982 1080
996ev_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)
997{ 1095{
998 struct ev_once *once = malloc (sizeof (struct ev_once)); 1096 struct ev_once *once = malloc (sizeof (struct ev_once));
999 1097
1000 if (!once) 1098 if (!once)
1001 cb (EV_ERROR, arg); 1099 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1002 else 1100 else
1003 { 1101 {
1004 once->cb = cb; 1102 once->cb = cb;
1005 once->arg = arg; 1103 once->arg = arg;
1006 1104
1007 evw_init (&once->io, once_cb_io); 1105 ev_watcher_init (&once->io, once_cb_io);
1008
1009 if (fd >= 0) 1106 if (fd >= 0)
1010 { 1107 {
1011 evio_set (&once->io, fd, events); 1108 ev_io_set (&once->io, fd, events);
1012 evio_start (&once->io); 1109 ev_io_start (&once->io);
1013 } 1110 }
1014 1111
1015 evw_init (&once->to, once_cb_to); 1112 ev_watcher_init (&once->to, once_cb_to);
1016
1017 if (timeout >= 0.) 1113 if (timeout >= 0.)
1018 { 1114 {
1019 evtimer_set (&once->to, timeout, 0.); 1115 ev_timer_set (&once->to, timeout, 0.);
1020 evtimer_start (&once->to); 1116 ev_timer_start (&once->to);
1021 } 1117 }
1022 } 1118 }
1023} 1119}
1024 1120
1025/*****************************************************************************/ 1121/*****************************************************************************/
1036 1132
1037static void 1133static void
1038ocb (struct ev_timer *w, int revents) 1134ocb (struct ev_timer *w, int revents)
1039{ 1135{
1040 //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);
1041 evtimer_stop (w); 1137 ev_timer_stop (w);
1042 evtimer_start (w); 1138 ev_timer_start (w);
1043} 1139}
1044 1140
1045static void 1141static void
1046scb (struct ev_signal *w, int revents) 1142scb (struct ev_signal *w, int revents)
1047{ 1143{
1048 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1144 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1049 evio_stop (&wio); 1145 ev_io_stop (&wio);
1050 evio_start (&wio); 1146 ev_io_start (&wio);
1051} 1147}
1052 1148
1053static void 1149static void
1054gcb (struct ev_signal *w, int revents) 1150gcb (struct ev_signal *w, int revents)
1055{ 1151{
1059 1155
1060int main (void) 1156int main (void)
1061{ 1157{
1062 ev_init (0); 1158 ev_init (0);
1063 1159
1064 evio_init (&wio, sin_cb, 0, EV_READ); 1160 ev_io_init (&wio, sin_cb, 0, EV_READ);
1065 evio_start (&wio); 1161 ev_io_start (&wio);
1066 1162
1067 struct ev_timer t[10000]; 1163 struct ev_timer t[10000];
1068 1164
1069#if 0 1165#if 0
1070 int i; 1166 int i;
1071 for (i = 0; i < 10000; ++i) 1167 for (i = 0; i < 10000; ++i)
1072 { 1168 {
1073 struct ev_timer *w = t + i; 1169 struct ev_timer *w = t + i;
1074 evw_init (w, ocb, i); 1170 ev_watcher_init (w, ocb, i);
1075 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1171 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1076 evtimer_start (w); 1172 ev_timer_start (w);
1077 if (drand48 () < 0.5) 1173 if (drand48 () < 0.5)
1078 evtimer_stop (w); 1174 ev_timer_stop (w);
1079 } 1175 }
1080#endif 1176#endif
1081 1177
1082 struct ev_timer t1; 1178 struct ev_timer t1;
1083 evtimer_init (&t1, ocb, 5, 10); 1179 ev_timer_init (&t1, ocb, 5, 10);
1084 evtimer_start (&t1); 1180 ev_timer_start (&t1);
1085 1181
1086 struct ev_signal sig; 1182 struct ev_signal sig;
1087 evsignal_init (&sig, scb, SIGQUIT); 1183 ev_signal_init (&sig, scb, SIGQUIT);
1088 evsignal_start (&sig); 1184 ev_signal_start (&sig);
1089 1185
1090 struct ev_check cw; 1186 struct ev_check cw;
1091 evcheck_init (&cw, gcb); 1187 ev_check_init (&cw, gcb);
1092 evcheck_start (&cw); 1188 ev_check_start (&cw);
1093 1189
1094 struct ev_idle iw; 1190 struct ev_idle iw;
1095 evidle_init (&iw, gcb); 1191 ev_idle_init (&iw, gcb);
1096 evidle_start (&iw); 1192 ev_idle_start (&iw);
1097 1193
1098 ev_loop (0); 1194 ev_loop (0);
1099 1195
1100 return 0; 1196 return 0;
1101} 1197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines