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

Comparing libev/ev.c (file contents):
Revision 1.31 by root, Thu Nov 1 09:05:33 2007 UTC vs.
Revision 1.48 by root, Sat Nov 3 12:19:31 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{
213 if (w->pending)
214 {
215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
216 return;
217 }
218
173 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
174 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
175 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
176 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
177} 223}
178 224
179static void 225static void
180queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
181{ 227{
219 int events = 0; 265 int events = 0;
220 266
221 for (w = anfd->head; w; w = w->next) 267 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 268 events |= w->events;
223 269
224 anfd->events &= ~EV_REIFY; 270 anfd->reify = 0;
225 271
226 if (anfd->events != events) 272 if (anfd->events != events)
227 { 273 {
228 method_modify (fd, anfd->events, events); 274 method_modify (fd, anfd->events, events);
229 anfd->events = events; 275 anfd->events = events;
234} 280}
235 281
236static void 282static void
237fd_change (int fd) 283fd_change (int fd)
238{ 284{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 285 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 286 return;
241 287
242 anfds [fd].events |= EV_REIFY; 288 anfds [fd].reify = 1;
243 289
244 ++fdchangecnt; 290 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
247} 293}
248 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
249/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
250static void 309static void
251fd_recheck (void) 310fd_ebadf (void)
252{ 311{
253 int fd; 312 int fd;
254 313
255 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 315 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 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)
259 { 328 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 329 close (fd);
261 ev_io_stop (anfds [fd].head); 330 fd_kill (fd);
331 return;
262 } 332 }
263} 333}
264 334
265/*****************************************************************************/ 335/*****************************************************************************/
266 336
267static struct ev_timer **timers; 337static struct ev_timer **timers;
314/*****************************************************************************/ 384/*****************************************************************************/
315 385
316typedef struct 386typedef struct
317{ 387{
318 struct ev_signal *head; 388 struct ev_signal *head;
319 sig_atomic_t gotsig; 389 sig_atomic_t volatile gotsig;
320} ANSIG; 390} ANSIG;
321 391
322static ANSIG *signals; 392static ANSIG *signals;
323static int signalmax; 393static int signalmax;
324 394
325static int sigpipe [2]; 395static int sigpipe [2];
326static sig_atomic_t gotsig; 396static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 397static struct ev_io sigev;
328 398
329static void 399static void
330signals_init (ANSIG *base, int count) 400signals_init (ANSIG *base, int count)
331{ 401{
332 while (count--) 402 while (count--)
333 { 403 {
334 base->head = 0; 404 base->head = 0;
335 base->gotsig = 0; 405 base->gotsig = 0;
406
336 ++base; 407 ++base;
337 } 408 }
338} 409}
339 410
340static void 411static void
342{ 413{
343 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
344 415
345 if (!gotsig) 416 if (!gotsig)
346 { 417 {
418 int old_errno = errno;
347 gotsig = 1; 419 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 420 write (sigpipe [1], &signum, 1);
421 errno = old_errno;
349 } 422 }
350} 423}
351 424
352static void 425static void
353sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
354{ 427{
355 struct ev_signal *w; 428 struct ev_signal *w;
356 int sig; 429 int signum;
357 430
431 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 432 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 433
361 for (sig = signalmax; sig--; ) 434 for (signum = signalmax; signum--; )
362 if (signals [sig].gotsig) 435 if (signals [signum].gotsig)
363 { 436 {
364 signals [sig].gotsig = 0; 437 signals [signum].gotsig = 0;
365 438
366 for (w = signals [sig].head; w; w = w->next) 439 for (w = signals [signum].head; w; w = w->next)
367 event ((W)w, EV_SIGNAL); 440 event ((W)w, EV_SIGNAL);
368 } 441 }
369} 442}
370 443
371static void 444static void
372siginit (void) 445siginit (void)
373{ 446{
447#ifndef WIN32
374 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
375 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 449 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
376 450
377 /* rather than sort out wether we really need nb, set it */ 451 /* rather than sort out wether we really need nb, set it */
378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
454#endif
380 455
381 ev_io_set (&sigev, sigpipe [0], EV_READ); 456 ev_io_set (&sigev, sigpipe [0], EV_READ);
382 ev_io_start (&sigev); 457 ev_io_start (&sigev);
383} 458}
384 459
396/*****************************************************************************/ 471/*****************************************************************************/
397 472
398static struct ev_child *childs [PID_HASHSIZE]; 473static struct ev_child *childs [PID_HASHSIZE];
399static struct ev_signal childev; 474static struct ev_signal childev;
400 475
476#ifndef WIN32
477
401#ifndef WCONTINUED 478#ifndef WCONTINUED
402# define WCONTINUED 0 479# define WCONTINUED 0
403#endif 480#endif
404 481
405static 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
406childcb (struct ev_signal *sw, int revents) 499childcb (struct ev_signal *sw, int revents)
407{ 500{
408 struct ev_child *w;
409 int pid, status; 501 int pid, status;
410 502
503 printf ("chld %x\n", revents);//D
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 504 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 505 {
413 if (w->pid == pid || w->pid == -1) 506 /* make sure we are called again until all childs have been reaped */
414 {
415 w->status = status;
416 event ((W)w, EV_CHILD); 507 event ((W)sw, EV_SIGNAL);
417 } 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 }
418} 512}
513
514#endif
419 515
420/*****************************************************************************/ 516/*****************************************************************************/
421 517
518#if EV_USE_KQUEUE
519# include "ev_kqueue.c"
520#endif
422#if EV_USE_EPOLL 521#if EV_USE_EPOLL
423# include "ev_epoll.c" 522# include "ev_epoll.c"
424#endif 523#endif
524#if EV_USE_POLL
525# include "ev_poll.c"
526#endif
425#if EV_USE_SELECT 527#if EV_USE_SELECT
426# include "ev_select.c" 528# include "ev_select.c"
427#endif 529#endif
428 530
429int 531int
436ev_version_minor (void) 538ev_version_minor (void)
437{ 539{
438 return EV_VERSION_MINOR; 540 return EV_VERSION_MINOR;
439} 541}
440 542
543/* return true if we are running with elevated privileges and ignore env variables */
544static int
545enable_secure ()
546{
547 return getuid () != geteuid ()
548 || getgid () != getegid ();
549}
550
441int ev_init (int flags) 551int ev_init (int methods)
442{ 552{
443 if (!ev_method) 553 if (!ev_method)
444 { 554 {
445#if EV_USE_MONOTONIC 555#if EV_USE_MONOTONIC
446 { 556 {
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 558 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1; 559 have_monotonic = 1;
450 } 560 }
451#endif 561#endif
452 562
453 ev_now = ev_time (); 563 ev_now = ev_time ();
454 now = get_clock (); 564 now = get_clock ();
565 now_floor = now;
455 diff = ev_now - now; 566 diff = ev_now - now;
456 567
457 if (pipe (sigpipe)) 568 if (pipe (sigpipe))
458 return 0; 569 return 0;
459 570
571 if (methods == EVMETHOD_AUTO)
572 if (!enable_secure () && getenv ("LIBEV_METHODS"))
573 methods = atoi (getenv ("LIBEV_METHODS"));
574 else
460 ev_method = EVMETHOD_NONE; 575 methods = EVMETHOD_ANY;
576
577 ev_method = 0;
578#if EV_USE_KQUEUE
579 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
580#endif
461#if EV_USE_EPOLL 581#if EV_USE_EPOLL
462 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 582 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
583#endif
584#if EV_USE_POLL
585 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
463#endif 586#endif
464#if EV_USE_SELECT 587#if EV_USE_SELECT
465 if (ev_method == EVMETHOD_NONE) select_init (flags); 588 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
466#endif 589#endif
467 590
468 if (ev_method) 591 if (ev_method)
469 { 592 {
470 ev_watcher_init (&sigev, sigcb); 593 ev_watcher_init (&sigev, sigcb);
594 ev_set_priority (&sigev, EV_MAXPRI);
471 siginit (); 595 siginit ();
472 596
597#ifndef WIN32
473 ev_signal_init (&childev, childcb, SIGCHLD); 598 ev_signal_init (&childev, childcb, SIGCHLD);
599 ev_set_priority (&childev, EV_MAXPRI);
474 ev_signal_start (&childev); 600 ev_signal_start (&childev);
601#endif
475 } 602 }
476 } 603 }
477 604
478 return ev_method; 605 return ev_method;
479} 606}
480 607
481/*****************************************************************************/ 608/*****************************************************************************/
482 609
483void 610void
484ev_prefork (void) 611ev_fork_prepare (void)
485{ 612{
486 /* nop */ 613 /* nop */
487} 614}
488 615
489void 616void
490ev_postfork_parent (void) 617ev_fork_parent (void)
491{ 618{
492 /* nop */ 619 /* nop */
493} 620}
494 621
495void 622void
496ev_postfork_child (void) 623ev_fork_child (void)
497{ 624{
498#if EV_USE_EPOLL 625#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 626 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 627 epoll_postfork_child ();
501#endif 628#endif
510/*****************************************************************************/ 637/*****************************************************************************/
511 638
512static void 639static void
513call_pending (void) 640call_pending (void)
514{ 641{
642 int pri;
643
644 for (pri = NUMPRI; pri--; )
515 while (pendingcnt) 645 while (pendingcnt [pri])
516 { 646 {
517 ANPENDING *p = pendings + --pendingcnt; 647 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
518 648
519 if (p->w) 649 if (p->w)
520 { 650 {
521 p->w->pending = 0; 651 p->w->pending = 0;
522 p->w->cb (p->w, p->events); 652 p->w->cb (p->w, p->events);
523 } 653 }
524 } 654 }
525} 655}
526 656
527static void 657static void
528timers_reify (void) 658timers_reify (void)
529{ 659{
532 struct ev_timer *w = timers [0]; 662 struct ev_timer *w = timers [0];
533 663
534 /* first reschedule or stop timer */ 664 /* first reschedule or stop timer */
535 if (w->repeat) 665 if (w->repeat)
536 { 666 {
667 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
537 w->at = now + w->repeat; 668 w->at = now + w->repeat;
538 assert (("timer timeout in the past, negative repeat?", w->at > now));
539 downheap ((WT *)timers, timercnt, 0); 669 downheap ((WT *)timers, timercnt, 0);
540 } 670 }
541 else 671 else
542 ev_timer_stop (w); /* nonrepeating: stop timer */ 672 ev_timer_stop (w); /* nonrepeating: stop timer */
543 673
554 684
555 /* first reschedule or stop timer */ 685 /* first reschedule or stop timer */
556 if (w->interval) 686 if (w->interval)
557 { 687 {
558 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 688 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
559 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 689 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
560 downheap ((WT *)periodics, periodiccnt, 0); 690 downheap ((WT *)periodics, periodiccnt, 0);
561 } 691 }
562 else 692 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 693 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 694
565 event ((W)w, EV_TIMEOUT); 695 event ((W)w, EV_PERIODIC);
566 } 696 }
567} 697}
568 698
569static void 699static void
570periodics_reschedule (ev_tstamp diff) 700periodics_reschedule (ev_tstamp diff)
589 } 719 }
590 } 720 }
591 } 721 }
592} 722}
593 723
724static int
725time_update_monotonic (void)
726{
727 now = get_clock ();
728
729 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
730 {
731 ev_now = now + diff;
732 return 0;
733 }
734 else
735 {
736 now_floor = now;
737 ev_now = ev_time ();
738 return 1;
739 }
740}
741
594static void 742static void
595time_update (void) 743time_update (void)
596{ 744{
597 int i; 745 int i;
598 746
599 ev_now = ev_time (); 747#if EV_USE_MONOTONIC
600
601 if (have_monotonic) 748 if (expect_true (have_monotonic))
602 { 749 {
603 ev_tstamp odiff = diff; 750 if (time_update_monotonic ())
604
605 for (i = 4; --i; ) /* loop a few times, before making important decisions */
606 { 751 {
607 now = get_clock (); 752 ev_tstamp odiff = diff;
753
754 for (i = 4; --i; ) /* loop a few times, before making important decisions */
755 {
608 diff = ev_now - now; 756 diff = ev_now - now;
609 757
610 if (fabs (odiff - diff) < MIN_TIMEJUMP) 758 if (fabs (odiff - diff) < MIN_TIMEJUMP)
611 return; /* all is well */ 759 return; /* all is well */
612 760
613 ev_now = ev_time (); 761 ev_now = ev_time ();
762 now = get_clock ();
763 now_floor = now;
764 }
765
766 periodics_reschedule (diff - odiff);
767 /* no timer adjustment, as the monotonic clock doesn't jump */
614 } 768 }
615
616 periodics_reschedule (diff - odiff);
617 /* no timer adjustment, as the monotonic clock doesn't jump */
618 } 769 }
619 else 770 else
771#endif
620 { 772 {
773 ev_now = ev_time ();
774
621 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 775 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
622 { 776 {
623 periodics_reschedule (ev_now - now); 777 periodics_reschedule (ev_now - now);
624 778
625 /* adjust timers. this is easy, as the offset is the same for all */ 779 /* adjust timers. this is easy, as the offset is the same for all */
626 for (i = 0; i < timercnt; ++i) 780 for (i = 0; i < timercnt; ++i)
639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 793 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
640 794
641 do 795 do
642 { 796 {
643 /* queue check watchers (and execute them) */ 797 /* queue check watchers (and execute them) */
644 if (preparecnt) 798 if (expect_false (preparecnt))
645 { 799 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 800 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending (); 801 call_pending ();
648 } 802 }
649 803
652 806
653 /* calculate blocking time */ 807 /* calculate blocking time */
654 808
655 /* we only need this for !monotonic clockor timers, but as we basically 809 /* we only need this for !monotonic clockor timers, but as we basically
656 always have timers, we just calculate it always */ 810 always have timers, we just calculate it always */
811#if EV_USE_MONOTONIC
812 if (expect_true (have_monotonic))
813 time_update_monotonic ();
814 else
815#endif
816 {
657 ev_now = ev_time (); 817 ev_now = ev_time ();
818 now = ev_now;
819 }
658 820
659 if (flags & EVLOOP_NONBLOCK || idlecnt) 821 if (flags & EVLOOP_NONBLOCK || idlecnt)
660 block = 0.; 822 block = 0.;
661 else 823 else
662 { 824 {
663 block = MAX_BLOCKTIME; 825 block = MAX_BLOCKTIME;
664 826
665 if (timercnt) 827 if (timercnt)
666 { 828 {
667 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 829 ev_tstamp to = timers [0]->at - now + method_fudge;
668 if (block > to) block = to; 830 if (block > to) block = to;
669 } 831 }
670 832
671 if (periodiccnt) 833 if (periodiccnt)
672 { 834 {
725 head = &(*head)->next; 887 head = &(*head)->next;
726 } 888 }
727} 889}
728 890
729static void 891static void
730ev_clear (W w) 892ev_clear_pending (W w)
731{ 893{
732 if (w->pending) 894 if (w->pending)
733 { 895 {
734 pendings [w->pending - 1].w = 0; 896 pendings [ABSPRI (w)][w->pending - 1].w = 0;
735 w->pending = 0; 897 w->pending = 0;
736 } 898 }
737} 899}
738 900
739static void 901static void
740ev_start (W w, int active) 902ev_start (W w, int active)
741{ 903{
904 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
905 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
906
742 w->active = active; 907 w->active = active;
743} 908}
744 909
745static void 910static void
746ev_stop (W w) 911ev_stop (W w)
751/*****************************************************************************/ 916/*****************************************************************************/
752 917
753void 918void
754ev_io_start (struct ev_io *w) 919ev_io_start (struct ev_io *w)
755{ 920{
921 int fd = w->fd;
922
756 if (ev_is_active (w)) 923 if (ev_is_active (w))
757 return; 924 return;
758 925
759 int fd = w->fd; 926 assert (("ev_io_start called with negative fd", fd >= 0));
760 927
761 ev_start ((W)w, 1); 928 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 929 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 930 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 931
766} 933}
767 934
768void 935void
769ev_io_stop (struct ev_io *w) 936ev_io_stop (struct ev_io *w)
770{ 937{
771 ev_clear ((W)w); 938 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 939 if (!ev_is_active (w))
773 return; 940 return;
774 941
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 942 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 943 ev_stop ((W)w);
784 if (ev_is_active (w)) 951 if (ev_is_active (w))
785 return; 952 return;
786 953
787 w->at += now; 954 w->at += now;
788 955
789 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 956 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
790 957
791 ev_start ((W)w, ++timercnt); 958 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 959 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 960 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 961 upheap ((WT *)timers, timercnt - 1);
795} 962}
796 963
797void 964void
798ev_timer_stop (struct ev_timer *w) 965ev_timer_stop (struct ev_timer *w)
799{ 966{
800 ev_clear ((W)w); 967 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 968 if (!ev_is_active (w))
802 return; 969 return;
803 970
804 if (w->active < timercnt--) 971 if (w->active < timercnt--)
805 { 972 {
833ev_periodic_start (struct ev_periodic *w) 1000ev_periodic_start (struct ev_periodic *w)
834{ 1001{
835 if (ev_is_active (w)) 1002 if (ev_is_active (w))
836 return; 1003 return;
837 1004
838 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 1005 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
839 1006
840 /* this formula differs from the one in periodic_reify because we do not always round up */ 1007 /* this formula differs from the one in periodic_reify because we do not always round up */
841 if (w->interval) 1008 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1009 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 1010
848} 1015}
849 1016
850void 1017void
851ev_periodic_stop (struct ev_periodic *w) 1018ev_periodic_stop (struct ev_periodic *w)
852{ 1019{
853 ev_clear ((W)w); 1020 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 1021 if (!ev_is_active (w))
855 return; 1022 return;
856 1023
857 if (w->active < periodiccnt--) 1024 if (w->active < periodiccnt--)
858 { 1025 {
861 } 1028 }
862 1029
863 ev_stop ((W)w); 1030 ev_stop ((W)w);
864} 1031}
865 1032
1033#ifndef SA_RESTART
1034# define SA_RESTART 0
1035#endif
1036
866void 1037void
867ev_signal_start (struct ev_signal *w) 1038ev_signal_start (struct ev_signal *w)
868{ 1039{
869 if (ev_is_active (w)) 1040 if (ev_is_active (w))
870 return; 1041 return;
1042
1043 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 1044
872 ev_start ((W)w, 1); 1045 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 1046 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1047 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 1048
876 if (!w->next) 1049 if (!w->next)
877 { 1050 {
878 struct sigaction sa; 1051 struct sigaction sa;
879 sa.sa_handler = sighandler; 1052 sa.sa_handler = sighandler;
880 sigfillset (&sa.sa_mask); 1053 sigfillset (&sa.sa_mask);
881 sa.sa_flags = 0; 1054 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
882 sigaction (w->signum, &sa, 0); 1055 sigaction (w->signum, &sa, 0);
883 } 1056 }
884} 1057}
885 1058
886void 1059void
887ev_signal_stop (struct ev_signal *w) 1060ev_signal_stop (struct ev_signal *w)
888{ 1061{
889 ev_clear ((W)w); 1062 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 1063 if (!ev_is_active (w))
891 return; 1064 return;
892 1065
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1066 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 1067 ev_stop ((W)w);
909} 1082}
910 1083
911void 1084void
912ev_idle_stop (struct ev_idle *w) 1085ev_idle_stop (struct ev_idle *w)
913{ 1086{
914 ev_clear ((W)w); 1087 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 1088 if (ev_is_active (w))
916 return; 1089 return;
917 1090
918 idles [w->active - 1] = idles [--idlecnt]; 1091 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 1092 ev_stop ((W)w);
931} 1104}
932 1105
933void 1106void
934ev_prepare_stop (struct ev_prepare *w) 1107ev_prepare_stop (struct ev_prepare *w)
935{ 1108{
936 ev_clear ((W)w); 1109 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 1110 if (ev_is_active (w))
938 return; 1111 return;
939 1112
940 prepares [w->active - 1] = prepares [--preparecnt]; 1113 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 1114 ev_stop ((W)w);
953} 1126}
954 1127
955void 1128void
956ev_check_stop (struct ev_check *w) 1129ev_check_stop (struct ev_check *w)
957{ 1130{
958 ev_clear ((W)w); 1131 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 1132 if (ev_is_active (w))
960 return; 1133 return;
961 1134
962 checks [w->active - 1] = checks [--checkcnt]; 1135 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 1136 ev_stop ((W)w);
974} 1147}
975 1148
976void 1149void
977ev_child_stop (struct ev_child *w) 1150ev_child_stop (struct ev_child *w)
978{ 1151{
979 ev_clear ((W)w); 1152 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 1153 if (ev_is_active (w))
981 return; 1154 return;
982 1155
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1156 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1157 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines