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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines