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

Comparing libev/ev.c (file contents):
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC vs.
Revision 1.49 by root, Sat Nov 3 16:16:58 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
40#include <stdio.h> 42#include <stdio.h>
41 43
42#include <assert.h> 44#include <assert.h>
43#include <errno.h> 45#include <errno.h>
44#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
45#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
46#include <sys/time.h> 50#include <sys/time.h>
47#include <time.h> 51#include <time.h>
48 52
53/**/
54
49#ifndef EV_USE_MONOTONIC 55#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 56# define EV_USE_MONOTONIC 1
52# endif
53#endif 57#endif
54 58
55#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
57#endif 61#endif
58 62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
59#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
61#endif 69#endif
62 70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
80
81#ifndef CLOCK_MONOTONIC
82# undef EV_USE_MONOTONIC
83# define EV_USE_MONOTONIC 0
84#endif
85
63#ifndef CLOCK_REALTIME 86#ifndef CLOCK_REALTIME
87# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 88# define EV_USE_REALTIME 0
65#endif 89#endif
66#ifndef EV_USE_REALTIME 90
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 91/**/
68#endif
69 92
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 97
75#include "ev.h" 98#include "ev.h"
99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
76 113
77typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
80 117
81static ev_tstamp now, diff; /* monotonic clock */ 118static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 119ev_tstamp ev_now;
83int ev_method; 120int ev_method;
84 121
85static int have_monotonic; /* runtime */ 122static int have_monotonic; /* runtime */
86 123
106 143
107static ev_tstamp 144static ev_tstamp
108get_clock (void) 145get_clock (void)
109{ 146{
110#if EV_USE_MONOTONIC 147#if EV_USE_MONOTONIC
111 if (have_monotonic) 148 if (expect_true (have_monotonic))
112 { 149 {
113 struct timespec ts; 150 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 151 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 152 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 153 }
120} 157}
121 158
122#define array_roundsize(base,n) ((n) | 4 & ~3) 159#define array_roundsize(base,n) ((n) | 4 & ~3)
123 160
124#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
126 { \ 163 { \
127 int newcnt = cur; \ 164 int newcnt = cur; \
128 do \ 165 do \
129 { \ 166 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 167 newcnt = array_roundsize (base, newcnt << 1); \
139/*****************************************************************************/ 176/*****************************************************************************/
140 177
141typedef struct 178typedef struct
142{ 179{
143 struct ev_io *head; 180 struct ev_io *head;
144 int events; 181 unsigned char events;
182 unsigned char reify;
145} ANFD; 183} ANFD;
146 184
147static ANFD *anfds; 185static ANFD *anfds;
148static int anfdmax; 186static int anfdmax;
149 187
152{ 190{
153 while (count--) 191 while (count--)
154 { 192 {
155 base->head = 0; 193 base->head = 0;
156 base->events = EV_NONE; 194 base->events = EV_NONE;
195 base->reify = 0;
196
157 ++base; 197 ++base;
158 } 198 }
159} 199}
160 200
161typedef struct 201typedef struct
162{ 202{
163 W w; 203 W w;
164 int events; 204 int events;
165} ANPENDING; 205} ANPENDING;
166 206
167static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
168static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
169 209
170static void 210static void
171event (W w, int events) 211event (W w, int events)
172{ 212{
173 if (w->pending) 213 if (w->pending)
174 { 214 {
175 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
176 return; 216 return;
177 } 217 }
178 218
179 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
180 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
181 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
182 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
183} 223}
184 224
185static void 225static void
186queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
187{ 227{
225 int events = 0; 265 int events = 0;
226 266
227 for (w = anfd->head; w; w = w->next) 267 for (w = anfd->head; w; w = w->next)
228 events |= w->events; 268 events |= w->events;
229 269
230 anfd->events &= ~EV_REIFY; 270 anfd->reify = 0;
231 271
232 if (anfd->events != events) 272 if (anfd->events != events)
233 { 273 {
234 method_modify (fd, anfd->events, events); 274 method_modify (fd, anfd->events, events);
235 anfd->events = events; 275 anfd->events = events;
240} 280}
241 281
242static void 282static void
243fd_change (int fd) 283fd_change (int fd)
244{ 284{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 285 if (anfds [fd].reify || fdchangecnt < 0)
246 return; 286 return;
247 287
248 anfds [fd].events |= EV_REIFY; 288 anfds [fd].reify = 1;
249 289
250 ++fdchangecnt; 290 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
253} 293}
254 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head))
302 {
303 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
255/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
256static void 309static void
257fd_recheck (void) 310fd_ebadf (void)
258{ 311{
259 int fd; 312 int fd;
260 313
261 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
262 if (anfds [fd].events) 315 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head) 317 fd_kill (fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (void)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
265 { 328 {
266 ev_io_stop (anfds [fd].head); 329 close (fd);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 330 fd_kill (fd);
331 return;
268 } 332 }
269} 333}
270 334
271/*****************************************************************************/ 335/*****************************************************************************/
272 336
273static struct ev_timer **timers; 337static struct ev_timer **timers;
320/*****************************************************************************/ 384/*****************************************************************************/
321 385
322typedef struct 386typedef struct
323{ 387{
324 struct ev_signal *head; 388 struct ev_signal *head;
325 sig_atomic_t gotsig; 389 sig_atomic_t volatile gotsig;
326} ANSIG; 390} ANSIG;
327 391
328static ANSIG *signals; 392static ANSIG *signals;
329static int signalmax; 393static int signalmax;
330 394
331static int sigpipe [2]; 395static int sigpipe [2];
332static sig_atomic_t gotsig; 396static sig_atomic_t volatile gotsig;
333static struct ev_io sigev; 397static struct ev_io sigev;
334 398
335static void 399static void
336signals_init (ANSIG *base, int count) 400signals_init (ANSIG *base, int count)
337{ 401{
338 while (count--) 402 while (count--)
339 { 403 {
340 base->head = 0; 404 base->head = 0;
341 base->gotsig = 0; 405 base->gotsig = 0;
406
342 ++base; 407 ++base;
343 } 408 }
344} 409}
345 410
346static void 411static void
348{ 413{
349 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
350 415
351 if (!gotsig) 416 if (!gotsig)
352 { 417 {
418 int old_errno = errno;
353 gotsig = 1; 419 gotsig = 1;
354 write (sigpipe [1], &gotsig, 1); 420 write (sigpipe [1], &signum, 1);
421 errno = old_errno;
355 } 422 }
356} 423}
357 424
358static void 425static void
359sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
360{ 427{
361 struct ev_signal *w; 428 struct ev_signal *w;
362 int sig; 429 int signum;
363 430
431 read (sigpipe [0], &revents, 1);
364 gotsig = 0; 432 gotsig = 0;
365 read (sigpipe [0], &revents, 1);
366 433
367 for (sig = signalmax; sig--; ) 434 for (signum = signalmax; signum--; )
368 if (signals [sig].gotsig) 435 if (signals [signum].gotsig)
369 { 436 {
370 signals [sig].gotsig = 0; 437 signals [signum].gotsig = 0;
371 438
372 for (w = signals [sig].head; w; w = w->next) 439 for (w = signals [signum].head; w; w = w->next)
373 event ((W)w, EV_SIGNAL); 440 event ((W)w, EV_SIGNAL);
374 } 441 }
375} 442}
376 443
377static void 444static void
378siginit (void) 445siginit (void)
379{ 446{
447#ifndef WIN32
380 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
381 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 449 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
382 450
383 /* rather than sort out wether we really need nb, set it */ 451 /* rather than sort out wether we really need nb, set it */
384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
454#endif
386 455
387 ev_io_set (&sigev, sigpipe [0], EV_READ); 456 ev_io_set (&sigev, sigpipe [0], EV_READ);
388 ev_io_start (&sigev); 457 ev_io_start (&sigev);
389} 458}
390 459
402/*****************************************************************************/ 471/*****************************************************************************/
403 472
404static struct ev_child *childs [PID_HASHSIZE]; 473static struct ev_child *childs [PID_HASHSIZE];
405static struct ev_signal childev; 474static struct ev_signal childev;
406 475
476#ifndef WIN32
477
407#ifndef WCONTINUED 478#ifndef WCONTINUED
408# define WCONTINUED 0 479# define WCONTINUED 0
409#endif 480#endif
410 481
411static void 482static void
483child_reap (struct ev_signal *sw, int chain, int pid, int status)
484{
485 struct ev_child *w;
486
487 for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next)
488 if (w->pid == pid || !w->pid)
489 {
490 w->priority = sw->priority; /* need to do it *now* */
491 w->rpid = pid;
492 w->rstatus = status;
493 printf ("rpid %p %d %d\n", w, pid, w->pid);//D
494 event ((W)w, EV_CHILD);
495 }
496}
497
498static void
412childcb (struct ev_signal *sw, int revents) 499childcb (struct ev_signal *sw, int revents)
413{ 500{
414 struct ev_child *w;
415 int pid, status; 501 int pid, status;
416 502
503 printf ("chld %x\n", revents);//D
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 504 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 505 {
419 if (w->pid == pid || w->pid == -1) 506 /* make sure we are called again until all childs have been reaped */
420 {
421 w->status = status;
422 event ((W)w, EV_CHILD); 507 event ((W)sw, EV_SIGNAL);
423 } 508
509 child_reap (sw, pid, pid, status);
510 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
511 }
424} 512}
513
514#endif
425 515
426/*****************************************************************************/ 516/*****************************************************************************/
427 517
518#if EV_USE_KQUEUE
519# include "ev_kqueue.c"
520#endif
428#if EV_USE_EPOLL 521#if EV_USE_EPOLL
429# include "ev_epoll.c" 522# include "ev_epoll.c"
430#endif 523#endif
524#if EV_USE_POLL
525# include "ev_poll.c"
526#endif
431#if EV_USE_SELECT 527#if EV_USE_SELECT
432# include "ev_select.c" 528# include "ev_select.c"
433#endif 529#endif
434 530
435int 531int
442ev_version_minor (void) 538ev_version_minor (void)
443{ 539{
444 return EV_VERSION_MINOR; 540 return EV_VERSION_MINOR;
445} 541}
446 542
543/* return true if we are running with elevated privileges and should ignore env variables */
544static int
545enable_secure ()
546{
547#ifdef WIN32
548 return 0;
549#else
550 return getuid () != geteuid ()
551 || getgid () != getegid ();
552#endif
553}
554
447int ev_init (int flags) 555int ev_init (int methods)
448{ 556{
449 if (!ev_method) 557 if (!ev_method)
450 { 558 {
451#if EV_USE_MONOTONIC 559#if EV_USE_MONOTONIC
452 { 560 {
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 562 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1; 563 have_monotonic = 1;
456 } 564 }
457#endif 565#endif
458 566
459 ev_now = ev_time (); 567 ev_now = ev_time ();
460 now = get_clock (); 568 now = get_clock ();
569 now_floor = now;
461 diff = ev_now - now; 570 diff = ev_now - now;
462 571
463 if (pipe (sigpipe)) 572 if (pipe (sigpipe))
464 return 0; 573 return 0;
465 574
575 if (methods == EVMETHOD_AUTO)
576 if (!enable_secure () && getenv ("LIBEV_METHODS"))
577 methods = atoi (getenv ("LIBEV_METHODS"));
578 else
466 ev_method = EVMETHOD_NONE; 579 methods = EVMETHOD_ANY;
580
581 ev_method = 0;
582#if EV_USE_KQUEUE
583 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
584#endif
467#if EV_USE_EPOLL 585#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 586 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
587#endif
588#if EV_USE_POLL
589 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
469#endif 590#endif
470#if EV_USE_SELECT 591#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags); 592 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
472#endif 593#endif
473 594
474 if (ev_method) 595 if (ev_method)
475 { 596 {
476 ev_watcher_init (&sigev, sigcb); 597 ev_watcher_init (&sigev, sigcb);
598 ev_set_priority (&sigev, EV_MAXPRI);
477 siginit (); 599 siginit ();
478 600
601#ifndef WIN32
479 ev_signal_init (&childev, childcb, SIGCHLD); 602 ev_signal_init (&childev, childcb, SIGCHLD);
603 ev_set_priority (&childev, EV_MAXPRI);
480 ev_signal_start (&childev); 604 ev_signal_start (&childev);
605#endif
481 } 606 }
482 } 607 }
483 608
484 return ev_method; 609 return ev_method;
485} 610}
486 611
487/*****************************************************************************/ 612/*****************************************************************************/
488 613
489void 614void
490ev_prefork (void) 615ev_fork_prepare (void)
491{ 616{
492 /* nop */ 617 /* nop */
493} 618}
494 619
495void 620void
496ev_postfork_parent (void) 621ev_fork_parent (void)
497{ 622{
498 /* nop */ 623 /* nop */
499} 624}
500 625
501void 626void
502ev_postfork_child (void) 627ev_fork_child (void)
503{ 628{
504#if EV_USE_EPOLL 629#if EV_USE_EPOLL
505 if (ev_method == EVMETHOD_EPOLL) 630 if (ev_method == EVMETHOD_EPOLL)
506 epoll_postfork_child (); 631 epoll_postfork_child ();
507#endif 632#endif
516/*****************************************************************************/ 641/*****************************************************************************/
517 642
518static void 643static void
519call_pending (void) 644call_pending (void)
520{ 645{
646 int pri;
647
648 for (pri = NUMPRI; pri--; )
521 while (pendingcnt) 649 while (pendingcnt [pri])
522 { 650 {
523 ANPENDING *p = pendings + --pendingcnt; 651 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
524 652
525 if (p->w) 653 if (p->w)
526 { 654 {
527 p->w->pending = 0; 655 p->w->pending = 0;
528 p->w->cb (p->w, p->events); 656 p->w->cb (p->w, p->events);
529 } 657 }
530 } 658 }
531} 659}
532 660
533static void 661static void
534timers_reify (void) 662timers_reify (void)
535{ 663{
538 struct ev_timer *w = timers [0]; 666 struct ev_timer *w = timers [0];
539 667
540 /* first reschedule or stop timer */ 668 /* first reschedule or stop timer */
541 if (w->repeat) 669 if (w->repeat)
542 { 670 {
671 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 672 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 673 downheap ((WT *)timers, timercnt, 0);
546 } 674 }
547 else 675 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 676 ev_timer_stop (w); /* nonrepeating: stop timer */
549 677
560 688
561 /* first reschedule or stop timer */ 689 /* first reschedule or stop timer */
562 if (w->interval) 690 if (w->interval)
563 { 691 {
564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 692 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 693 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
566 downheap ((WT *)periodics, periodiccnt, 0); 694 downheap ((WT *)periodics, periodiccnt, 0);
567 } 695 }
568 else 696 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 697 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 698
571 event ((W)w, EV_TIMEOUT); 699 event ((W)w, EV_PERIODIC);
572 } 700 }
573} 701}
574 702
575static void 703static void
576periodics_reschedule (ev_tstamp diff) 704periodics_reschedule (ev_tstamp diff)
595 } 723 }
596 } 724 }
597 } 725 }
598} 726}
599 727
728static int
729time_update_monotonic (void)
730{
731 now = get_clock ();
732
733 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
734 {
735 ev_now = now + diff;
736 return 0;
737 }
738 else
739 {
740 now_floor = now;
741 ev_now = ev_time ();
742 return 1;
743 }
744}
745
600static void 746static void
601time_update (void) 747time_update (void)
602{ 748{
603 int i; 749 int i;
604 750
605 ev_now = ev_time (); 751#if EV_USE_MONOTONIC
606
607 if (have_monotonic) 752 if (expect_true (have_monotonic))
608 { 753 {
609 ev_tstamp odiff = diff; 754 if (time_update_monotonic ())
610
611 for (i = 4; --i; ) /* loop a few times, before making important decisions */
612 { 755 {
613 now = get_clock (); 756 ev_tstamp odiff = diff;
757
758 for (i = 4; --i; ) /* loop a few times, before making important decisions */
759 {
614 diff = ev_now - now; 760 diff = ev_now - now;
615 761
616 if (fabs (odiff - diff) < MIN_TIMEJUMP) 762 if (fabs (odiff - diff) < MIN_TIMEJUMP)
617 return; /* all is well */ 763 return; /* all is well */
618 764
619 ev_now = ev_time (); 765 ev_now = ev_time ();
766 now = get_clock ();
767 now_floor = now;
768 }
769
770 periodics_reschedule (diff - odiff);
771 /* no timer adjustment, as the monotonic clock doesn't jump */
620 } 772 }
621
622 periodics_reschedule (diff - odiff);
623 /* no timer adjustment, as the monotonic clock doesn't jump */
624 } 773 }
625 else 774 else
775#endif
626 { 776 {
777 ev_now = ev_time ();
778
627 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 779 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
628 { 780 {
629 periodics_reschedule (ev_now - now); 781 periodics_reschedule (ev_now - now);
630 782
631 /* adjust timers. this is easy, as the offset is the same for all */ 783 /* adjust timers. this is easy, as the offset is the same for all */
632 for (i = 0; i < timercnt; ++i) 784 for (i = 0; i < timercnt; ++i)
645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 797 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
646 798
647 do 799 do
648 { 800 {
649 /* queue check watchers (and execute them) */ 801 /* queue check watchers (and execute them) */
650 if (preparecnt) 802 if (expect_false (preparecnt))
651 { 803 {
652 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 804 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
653 call_pending (); 805 call_pending ();
654 } 806 }
655 807
658 810
659 /* calculate blocking time */ 811 /* calculate blocking time */
660 812
661 /* we only need this for !monotonic clockor timers, but as we basically 813 /* we only need this for !monotonic clockor timers, but as we basically
662 always have timers, we just calculate it always */ 814 always have timers, we just calculate it always */
815#if EV_USE_MONOTONIC
816 if (expect_true (have_monotonic))
817 time_update_monotonic ();
818 else
819#endif
820 {
663 ev_now = ev_time (); 821 ev_now = ev_time ();
822 now = ev_now;
823 }
664 824
665 if (flags & EVLOOP_NONBLOCK || idlecnt) 825 if (flags & EVLOOP_NONBLOCK || idlecnt)
666 block = 0.; 826 block = 0.;
667 else 827 else
668 { 828 {
669 block = MAX_BLOCKTIME; 829 block = MAX_BLOCKTIME;
670 830
671 if (timercnt) 831 if (timercnt)
672 { 832 {
673 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 833 ev_tstamp to = timers [0]->at - now + method_fudge;
674 if (block > to) block = to; 834 if (block > to) block = to;
675 } 835 }
676 836
677 if (periodiccnt) 837 if (periodiccnt)
678 { 838 {
731 head = &(*head)->next; 891 head = &(*head)->next;
732 } 892 }
733} 893}
734 894
735static void 895static void
736ev_clear (W w) 896ev_clear_pending (W w)
737{ 897{
738 if (w->pending) 898 if (w->pending)
739 { 899 {
740 pendings [w->pending - 1].w = 0; 900 pendings [ABSPRI (w)][w->pending - 1].w = 0;
741 w->pending = 0; 901 w->pending = 0;
742 } 902 }
743} 903}
744 904
745static void 905static void
746ev_start (W w, int active) 906ev_start (W w, int active)
747{ 907{
908 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
909 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
910
748 w->active = active; 911 w->active = active;
749} 912}
750 913
751static void 914static void
752ev_stop (W w) 915ev_stop (W w)
757/*****************************************************************************/ 920/*****************************************************************************/
758 921
759void 922void
760ev_io_start (struct ev_io *w) 923ev_io_start (struct ev_io *w)
761{ 924{
925 int fd = w->fd;
926
762 if (ev_is_active (w)) 927 if (ev_is_active (w))
763 return; 928 return;
764 929
765 int fd = w->fd; 930 assert (("ev_io_start called with negative fd", fd >= 0));
766 931
767 ev_start ((W)w, 1); 932 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 933 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 934 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 935
772} 937}
773 938
774void 939void
775ev_io_stop (struct ev_io *w) 940ev_io_stop (struct ev_io *w)
776{ 941{
777 ev_clear ((W)w); 942 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 943 if (!ev_is_active (w))
779 return; 944 return;
780 945
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 946 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 947 ev_stop ((W)w);
790 if (ev_is_active (w)) 955 if (ev_is_active (w))
791 return; 956 return;
792 957
793 w->at += now; 958 w->at += now;
794 959
795 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 960 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
796 961
797 ev_start ((W)w, ++timercnt); 962 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 963 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 964 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 965 upheap ((WT *)timers, timercnt - 1);
801} 966}
802 967
803void 968void
804ev_timer_stop (struct ev_timer *w) 969ev_timer_stop (struct ev_timer *w)
805{ 970{
806 ev_clear ((W)w); 971 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 972 if (!ev_is_active (w))
808 return; 973 return;
809 974
810 if (w->active < timercnt--) 975 if (w->active < timercnt--)
811 { 976 {
839ev_periodic_start (struct ev_periodic *w) 1004ev_periodic_start (struct ev_periodic *w)
840{ 1005{
841 if (ev_is_active (w)) 1006 if (ev_is_active (w))
842 return; 1007 return;
843 1008
844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 1009 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
845 1010
846 /* this formula differs from the one in periodic_reify because we do not always round up */ 1011 /* this formula differs from the one in periodic_reify because we do not always round up */
847 if (w->interval) 1012 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1013 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 1014
854} 1019}
855 1020
856void 1021void
857ev_periodic_stop (struct ev_periodic *w) 1022ev_periodic_stop (struct ev_periodic *w)
858{ 1023{
859 ev_clear ((W)w); 1024 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 1025 if (!ev_is_active (w))
861 return; 1026 return;
862 1027
863 if (w->active < periodiccnt--) 1028 if (w->active < periodiccnt--)
864 { 1029 {
867 } 1032 }
868 1033
869 ev_stop ((W)w); 1034 ev_stop ((W)w);
870} 1035}
871 1036
1037#ifndef SA_RESTART
1038# define SA_RESTART 0
1039#endif
1040
872void 1041void
873ev_signal_start (struct ev_signal *w) 1042ev_signal_start (struct ev_signal *w)
874{ 1043{
875 if (ev_is_active (w)) 1044 if (ev_is_active (w))
876 return; 1045 return;
1046
1047 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 1048
878 ev_start ((W)w, 1); 1049 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 1050 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1051 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 1052
882 if (!w->next) 1053 if (!w->next)
883 { 1054 {
884 struct sigaction sa; 1055 struct sigaction sa;
885 sa.sa_handler = sighandler; 1056 sa.sa_handler = sighandler;
886 sigfillset (&sa.sa_mask); 1057 sigfillset (&sa.sa_mask);
887 sa.sa_flags = 0; 1058 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
888 sigaction (w->signum, &sa, 0); 1059 sigaction (w->signum, &sa, 0);
889 } 1060 }
890} 1061}
891 1062
892void 1063void
893ev_signal_stop (struct ev_signal *w) 1064ev_signal_stop (struct ev_signal *w)
894{ 1065{
895 ev_clear ((W)w); 1066 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 1067 if (!ev_is_active (w))
897 return; 1068 return;
898 1069
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1070 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 1071 ev_stop ((W)w);
915} 1086}
916 1087
917void 1088void
918ev_idle_stop (struct ev_idle *w) 1089ev_idle_stop (struct ev_idle *w)
919{ 1090{
920 ev_clear ((W)w); 1091 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 1092 if (ev_is_active (w))
922 return; 1093 return;
923 1094
924 idles [w->active - 1] = idles [--idlecnt]; 1095 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 1096 ev_stop ((W)w);
937} 1108}
938 1109
939void 1110void
940ev_prepare_stop (struct ev_prepare *w) 1111ev_prepare_stop (struct ev_prepare *w)
941{ 1112{
942 ev_clear ((W)w); 1113 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 1114 if (ev_is_active (w))
944 return; 1115 return;
945 1116
946 prepares [w->active - 1] = prepares [--preparecnt]; 1117 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 1118 ev_stop ((W)w);
959} 1130}
960 1131
961void 1132void
962ev_check_stop (struct ev_check *w) 1133ev_check_stop (struct ev_check *w)
963{ 1134{
964 ev_clear ((W)w); 1135 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 1136 if (ev_is_active (w))
966 return; 1137 return;
967 1138
968 checks [w->active - 1] = checks [--checkcnt]; 1139 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 1140 ev_stop ((W)w);
980} 1151}
981 1152
982void 1153void
983ev_child_stop (struct ev_child *w) 1154ev_child_stop (struct ev_child *w)
984{ 1155{
985 ev_clear ((W)w); 1156 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 1157 if (ev_is_active (w))
987 return; 1158 return;
988 1159
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1160 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 1161 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines