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

Comparing libev/ev.c (file contents):
Revision 1.26 by root, Wed Oct 31 21:50: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>
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)
616 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 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
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
748} 835}
749 836
750void 837void
751evio_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
752{ 839{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w); 840 ev_clear_pending ((W)w);
756 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
757 return; 842 return;
758 843
759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
760 ev_stop ((W)w); 845 ev_stop ((W)w);
761 846
762 ++fdchangecnt; 847 fd_change (w->fd);
763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
764 fdchanges [fdchangecnt - 1] = w->fd;
765} 848}
766 849
767void 850void
768evtimer_start (struct ev_timer *w) 851ev_timer_start (struct ev_timer *w)
769{ 852{
770 if (ev_is_active (w)) 853 if (ev_is_active (w))
771 return; 854 return;
772 855
773 w->at += now; 856 w->at += now;
774 857
775 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.));
776 859
777 ev_start ((W)w, ++timercnt); 860 ev_start ((W)w, ++timercnt);
778 array_needsize (timers, timermax, timercnt, ); 861 array_needsize (timers, timermax, timercnt, );
779 timers [timercnt - 1] = w; 862 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1); 863 upheap ((WT *)timers, timercnt - 1);
781} 864}
782 865
783void 866void
784evtimer_stop (struct ev_timer *w) 867ev_timer_stop (struct ev_timer *w)
785{ 868{
786 ev_clear ((W)w); 869 ev_clear_pending ((W)w);
787 if (!ev_is_active (w)) 870 if (!ev_is_active (w))
788 return; 871 return;
789 872
790 if (w->active < timercnt--) 873 if (w->active < timercnt--)
791 { 874 {
797 880
798 ev_stop ((W)w); 881 ev_stop ((W)w);
799} 882}
800 883
801void 884void
802evtimer_again (struct ev_timer *w) 885ev_timer_again (struct ev_timer *w)
803{ 886{
804 if (ev_is_active (w)) 887 if (ev_is_active (w))
805 { 888 {
806 if (w->repeat) 889 if (w->repeat)
807 { 890 {
808 w->at = now + w->repeat; 891 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1); 892 downheap ((WT *)timers, timercnt, w->active - 1);
810 } 893 }
811 else 894 else
812 evtimer_stop (w); 895 ev_timer_stop (w);
813 } 896 }
814 else if (w->repeat) 897 else if (w->repeat)
815 evtimer_start (w); 898 ev_timer_start (w);
816} 899}
817 900
818void 901void
819evperiodic_start (struct ev_periodic *w) 902ev_periodic_start (struct ev_periodic *w)
820{ 903{
821 if (ev_is_active (w)) 904 if (ev_is_active (w))
822 return; 905 return;
823 906
824 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.));
825 908
826 /* 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 */
827 if (w->interval) 910 if (w->interval)
828 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
829 912
832 periodics [periodiccnt - 1] = w; 915 periodics [periodiccnt - 1] = w;
833 upheap ((WT *)periodics, periodiccnt - 1); 916 upheap ((WT *)periodics, periodiccnt - 1);
834} 917}
835 918
836void 919void
837evperiodic_stop (struct ev_periodic *w) 920ev_periodic_stop (struct ev_periodic *w)
838{ 921{
839 ev_clear ((W)w); 922 ev_clear_pending ((W)w);
840 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
841 return; 924 return;
842 925
843 if (w->active < periodiccnt--) 926 if (w->active < periodiccnt--)
844 { 927 {
848 931
849 ev_stop ((W)w); 932 ev_stop ((W)w);
850} 933}
851 934
852void 935void
853evsignal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
854{ 937{
855 if (ev_is_active (w)) 938 if (ev_is_active (w))
856 return; 939 return;
940
941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
857 942
858 ev_start ((W)w, 1); 943 ev_start ((W)w, 1);
859 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
860 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
861 946
868 sigaction (w->signum, &sa, 0); 953 sigaction (w->signum, &sa, 0);
869 } 954 }
870} 955}
871 956
872void 957void
873evsignal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
874{ 959{
875 ev_clear ((W)w); 960 ev_clear_pending ((W)w);
876 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
877 return; 962 return;
878 963
879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
880 ev_stop ((W)w); 965 ev_stop ((W)w);
881 966
882 if (!signals [w->signum - 1].head) 967 if (!signals [w->signum - 1].head)
883 signal (w->signum, SIG_DFL); 968 signal (w->signum, SIG_DFL);
884} 969}
885 970
971void
886void evidle_start (struct ev_idle *w) 972ev_idle_start (struct ev_idle *w)
887{ 973{
888 if (ev_is_active (w)) 974 if (ev_is_active (w))
889 return; 975 return;
890 976
891 ev_start ((W)w, ++idlecnt); 977 ev_start ((W)w, ++idlecnt);
892 array_needsize (idles, idlemax, idlecnt, ); 978 array_needsize (idles, idlemax, idlecnt, );
893 idles [idlecnt - 1] = w; 979 idles [idlecnt - 1] = w;
894} 980}
895 981
982void
896void evidle_stop (struct ev_idle *w) 983ev_idle_stop (struct ev_idle *w)
897{ 984{
898 ev_clear ((W)w); 985 ev_clear_pending ((W)w);
899 if (ev_is_active (w)) 986 if (ev_is_active (w))
900 return; 987 return;
901 988
902 idles [w->active - 1] = idles [--idlecnt]; 989 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w); 990 ev_stop ((W)w);
904} 991}
905 992
993void
906void evprepare_start (struct ev_prepare *w) 994ev_prepare_start (struct ev_prepare *w)
907{ 995{
908 if (ev_is_active (w)) 996 if (ev_is_active (w))
909 return; 997 return;
910 998
911 ev_start ((W)w, ++preparecnt); 999 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, ); 1000 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w; 1001 prepares [preparecnt - 1] = w;
914} 1002}
915 1003
1004void
916void evprepare_stop (struct ev_prepare *w) 1005ev_prepare_stop (struct ev_prepare *w)
917{ 1006{
918 ev_clear ((W)w); 1007 ev_clear_pending ((W)w);
919 if (ev_is_active (w)) 1008 if (ev_is_active (w))
920 return; 1009 return;
921 1010
922 prepares [w->active - 1] = prepares [--preparecnt]; 1011 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w); 1012 ev_stop ((W)w);
924} 1013}
925 1014
1015void
926void evcheck_start (struct ev_check *w) 1016ev_check_start (struct ev_check *w)
927{ 1017{
928 if (ev_is_active (w)) 1018 if (ev_is_active (w))
929 return; 1019 return;
930 1020
931 ev_start ((W)w, ++checkcnt); 1021 ev_start ((W)w, ++checkcnt);
932 array_needsize (checks, checkmax, checkcnt, ); 1022 array_needsize (checks, checkmax, checkcnt, );
933 checks [checkcnt - 1] = w; 1023 checks [checkcnt - 1] = w;
934} 1024}
935 1025
1026void
936void evcheck_stop (struct ev_check *w) 1027ev_check_stop (struct ev_check *w)
937{ 1028{
938 ev_clear ((W)w); 1029 ev_clear_pending ((W)w);
939 if (ev_is_active (w)) 1030 if (ev_is_active (w))
940 return; 1031 return;
941 1032
942 checks [w->active - 1] = checks [--checkcnt]; 1033 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w); 1034 ev_stop ((W)w);
944} 1035}
945 1036
1037void
946void evchild_start (struct ev_child *w) 1038ev_child_start (struct ev_child *w)
947{ 1039{
948 if (ev_is_active (w)) 1040 if (ev_is_active (w))
949 return; 1041 return;
950 1042
951 ev_start ((W)w, 1); 1043 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1044 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953} 1045}
954 1046
1047void
955void evchild_stop (struct ev_child *w) 1048ev_child_stop (struct ev_child *w)
956{ 1049{
957 ev_clear ((W)w); 1050 ev_clear_pending ((W)w);
958 if (ev_is_active (w)) 1051 if (ev_is_active (w))
959 return; 1052 return;
960 1053
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1054 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w); 1055 ev_stop ((W)w);
976once_cb (struct ev_once *once, int revents) 1069once_cb (struct ev_once *once, int revents)
977{ 1070{
978 void (*cb)(int revents, void *arg) = once->cb; 1071 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg; 1072 void *arg = once->arg;
980 1073
981 evio_stop (&once->io); 1074 ev_io_stop (&once->io);
982 evtimer_stop (&once->to); 1075 ev_timer_stop (&once->to);
983 free (once); 1076 free (once);
984 1077
985 cb (revents, arg); 1078 cb (revents, arg);
986} 1079}
987 1080
1001ev_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)
1002{ 1095{
1003 struct ev_once *once = malloc (sizeof (struct ev_once)); 1096 struct ev_once *once = malloc (sizeof (struct ev_once));
1004 1097
1005 if (!once) 1098 if (!once)
1006 cb (EV_ERROR, arg); 1099 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1007 else 1100 else
1008 { 1101 {
1009 once->cb = cb; 1102 once->cb = cb;
1010 once->arg = arg; 1103 once->arg = arg;
1011 1104
1012 evw_init (&once->io, once_cb_io); 1105 ev_watcher_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0) 1106 if (fd >= 0)
1015 { 1107 {
1016 evio_set (&once->io, fd, events); 1108 ev_io_set (&once->io, fd, events);
1017 evio_start (&once->io); 1109 ev_io_start (&once->io);
1018 } 1110 }
1019 1111
1020 evw_init (&once->to, once_cb_to); 1112 ev_watcher_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.) 1113 if (timeout >= 0.)
1023 { 1114 {
1024 evtimer_set (&once->to, timeout, 0.); 1115 ev_timer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to); 1116 ev_timer_start (&once->to);
1026 } 1117 }
1027 } 1118 }
1028} 1119}
1029 1120
1030/*****************************************************************************/ 1121/*****************************************************************************/
1041 1132
1042static void 1133static void
1043ocb (struct ev_timer *w, int revents) 1134ocb (struct ev_timer *w, int revents)
1044{ 1135{
1045 //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);
1046 evtimer_stop (w); 1137 ev_timer_stop (w);
1047 evtimer_start (w); 1138 ev_timer_start (w);
1048} 1139}
1049 1140
1050static void 1141static void
1051scb (struct ev_signal *w, int revents) 1142scb (struct ev_signal *w, int revents)
1052{ 1143{
1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1144 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio); 1145 ev_io_stop (&wio);
1055 evio_start (&wio); 1146 ev_io_start (&wio);
1056} 1147}
1057 1148
1058static void 1149static void
1059gcb (struct ev_signal *w, int revents) 1150gcb (struct ev_signal *w, int revents)
1060{ 1151{
1064 1155
1065int main (void) 1156int main (void)
1066{ 1157{
1067 ev_init (0); 1158 ev_init (0);
1068 1159
1069 evio_init (&wio, sin_cb, 0, EV_READ); 1160 ev_io_init (&wio, sin_cb, 0, EV_READ);
1070 evio_start (&wio); 1161 ev_io_start (&wio);
1071 1162
1072 struct ev_timer t[10000]; 1163 struct ev_timer t[10000];
1073 1164
1074#if 0 1165#if 0
1075 int i; 1166 int i;
1076 for (i = 0; i < 10000; ++i) 1167 for (i = 0; i < 10000; ++i)
1077 { 1168 {
1078 struct ev_timer *w = t + i; 1169 struct ev_timer *w = t + i;
1079 evw_init (w, ocb, i); 1170 ev_watcher_init (w, ocb, i);
1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1171 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1081 evtimer_start (w); 1172 ev_timer_start (w);
1082 if (drand48 () < 0.5) 1173 if (drand48 () < 0.5)
1083 evtimer_stop (w); 1174 ev_timer_stop (w);
1084 } 1175 }
1085#endif 1176#endif
1086 1177
1087 struct ev_timer t1; 1178 struct ev_timer t1;
1088 evtimer_init (&t1, ocb, 5, 10); 1179 ev_timer_init (&t1, ocb, 5, 10);
1089 evtimer_start (&t1); 1180 ev_timer_start (&t1);
1090 1181
1091 struct ev_signal sig; 1182 struct ev_signal sig;
1092 evsignal_init (&sig, scb, SIGQUIT); 1183 ev_signal_init (&sig, scb, SIGQUIT);
1093 evsignal_start (&sig); 1184 ev_signal_start (&sig);
1094 1185
1095 struct ev_check cw; 1186 struct ev_check cw;
1096 evcheck_init (&cw, gcb); 1187 ev_check_init (&cw, gcb);
1097 evcheck_start (&cw); 1188 ev_check_start (&cw);
1098 1189
1099 struct ev_idle iw; 1190 struct ev_idle iw;
1100 evidle_init (&iw, gcb); 1191 ev_idle_init (&iw, gcb);
1101 evidle_start (&iw); 1192 ev_idle_start (&iw);
1102 1193
1103 ev_loop (0); 1194 ev_loop (0);
1104 1195
1105 return 0; 1196 return 0;
1106} 1197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines