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.45 by root, Sat Nov 3 09:19: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
349 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
350 415
351 if (!gotsig) 416 if (!gotsig)
352 { 417 {
353 gotsig = 1; 418 gotsig = 1;
354 write (sigpipe [1], &gotsig, 1); 419 write (sigpipe [1], &signum, 1);
355 } 420 }
356} 421}
357 422
358static void 423static void
359sigcb (struct ev_io *iow, int revents) 424sigcb (struct ev_io *iow, int revents)
360{ 425{
361 struct ev_signal *w; 426 struct ev_signal *w;
362 int sig; 427 int signum;
363 428
429 read (sigpipe [0], &revents, 1);
364 gotsig = 0; 430 gotsig = 0;
365 read (sigpipe [0], &revents, 1);
366 431
367 for (sig = signalmax; sig--; ) 432 for (signum = signalmax; signum--; )
368 if (signals [sig].gotsig) 433 if (signals [signum].gotsig)
369 { 434 {
370 signals [sig].gotsig = 0; 435 signals [signum].gotsig = 0;
371 436
372 for (w = signals [sig].head; w; w = w->next) 437 for (w = signals [signum].head; w; w = w->next)
373 event ((W)w, EV_SIGNAL); 438 event ((W)w, EV_SIGNAL);
374 } 439 }
375} 440}
376 441
377static void 442static void
378siginit (void) 443siginit (void)
379{ 444{
445#ifndef WIN32
380 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
381 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
382 448
383 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
386 453
387 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
388 ev_io_start (&sigev); 455 ev_io_start (&sigev);
389} 456}
390 457
402/*****************************************************************************/ 469/*****************************************************************************/
403 470
404static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
405static struct ev_signal childev; 472static struct ev_signal childev;
406 473
474#ifndef WIN32
475
407#ifndef WCONTINUED 476#ifndef WCONTINUED
408# define WCONTINUED 0 477# define WCONTINUED 0
409#endif 478#endif
410 479
411static void 480static void
414 struct ev_child *w; 483 struct ev_child *w;
415 int pid, status; 484 int pid, status;
416 485
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1) 488 if (w->pid == pid || !w->pid)
420 { 489 {
421 w->status = status; 490 w->status = status;
422 event ((W)w, EV_CHILD); 491 event ((W)w, EV_CHILD);
423 } 492 }
424} 493}
425 494
426/*****************************************************************************/ 495#endif
427 496
497/*****************************************************************************/
498
499#if EV_USE_KQUEUE
500# include "ev_kqueue.c"
501#endif
428#if EV_USE_EPOLL 502#if EV_USE_EPOLL
429# include "ev_epoll.c" 503# include "ev_epoll.c"
430#endif 504#endif
505#if EV_USE_POLL
506# include "ev_poll.c"
507#endif
431#if EV_USE_SELECT 508#if EV_USE_SELECT
432# include "ev_select.c" 509# include "ev_select.c"
433#endif 510#endif
434 511
435int 512int
442ev_version_minor (void) 519ev_version_minor (void)
443{ 520{
444 return EV_VERSION_MINOR; 521 return EV_VERSION_MINOR;
445} 522}
446 523
524/* return true if we are running with elevated privileges and ignore env variables */
525static int
526enable_secure ()
527{
528 return getuid () != geteuid ()
529 || getgid () != getegid ();
530}
531
447int ev_init (int flags) 532int ev_init (int methods)
448{ 533{
449 if (!ev_method) 534 if (!ev_method)
450 { 535 {
451#if EV_USE_MONOTONIC 536#if EV_USE_MONOTONIC
452 { 537 {
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 539 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1; 540 have_monotonic = 1;
456 } 541 }
457#endif 542#endif
458 543
459 ev_now = ev_time (); 544 ev_now = ev_time ();
460 now = get_clock (); 545 now = get_clock ();
546 now_floor = now;
461 diff = ev_now - now; 547 diff = ev_now - now;
462 548
463 if (pipe (sigpipe)) 549 if (pipe (sigpipe))
464 return 0; 550 return 0;
465 551
552 if (methods == EVMETHOD_AUTO)
553 if (!enable_secure () && getenv ("LIBEV_METHODS"))
554 methods = atoi (getenv ("LIBEV_METHODS"));
555 else
466 ev_method = EVMETHOD_NONE; 556 methods = EVMETHOD_ANY;
557
558 ev_method = 0;
559#if EV_USE_KQUEUE
560 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
561#endif
467#if EV_USE_EPOLL 562#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 563 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
564#endif
565#if EV_USE_POLL
566 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
469#endif 567#endif
470#if EV_USE_SELECT 568#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags); 569 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
472#endif 570#endif
473 571
474 if (ev_method) 572 if (ev_method)
475 { 573 {
476 ev_watcher_init (&sigev, sigcb); 574 ev_watcher_init (&sigev, sigcb);
477 siginit (); 575 siginit ();
478 576
577#ifndef WIN32
479 ev_signal_init (&childev, childcb, SIGCHLD); 578 ev_signal_init (&childev, childcb, SIGCHLD);
480 ev_signal_start (&childev); 579 ev_signal_start (&childev);
580#endif
481 } 581 }
482 } 582 }
483 583
484 return ev_method; 584 return ev_method;
485} 585}
486 586
487/*****************************************************************************/ 587/*****************************************************************************/
488 588
489void 589void
490ev_prefork (void) 590ev_fork_prepare (void)
491{ 591{
492 /* nop */ 592 /* nop */
493} 593}
494 594
495void 595void
496ev_postfork_parent (void) 596ev_fork_parent (void)
497{ 597{
498 /* nop */ 598 /* nop */
499} 599}
500 600
501void 601void
502ev_postfork_child (void) 602ev_fork_child (void)
503{ 603{
504#if EV_USE_EPOLL 604#if EV_USE_EPOLL
505 if (ev_method == EVMETHOD_EPOLL) 605 if (ev_method == EVMETHOD_EPOLL)
506 epoll_postfork_child (); 606 epoll_postfork_child ();
507#endif 607#endif
516/*****************************************************************************/ 616/*****************************************************************************/
517 617
518static void 618static void
519call_pending (void) 619call_pending (void)
520{ 620{
621 int pri;
622
623 for (pri = NUMPRI; pri--; )
521 while (pendingcnt) 624 while (pendingcnt [pri])
522 { 625 {
523 ANPENDING *p = pendings + --pendingcnt; 626 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
524 627
525 if (p->w) 628 if (p->w)
526 { 629 {
527 p->w->pending = 0; 630 p->w->pending = 0;
528 p->w->cb (p->w, p->events); 631 p->w->cb (p->w, p->events);
529 } 632 }
530 } 633 }
531} 634}
532 635
533static void 636static void
534timers_reify (void) 637timers_reify (void)
535{ 638{
538 struct ev_timer *w = timers [0]; 641 struct ev_timer *w = timers [0];
539 642
540 /* first reschedule or stop timer */ 643 /* first reschedule or stop timer */
541 if (w->repeat) 644 if (w->repeat)
542 { 645 {
646 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 647 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 648 downheap ((WT *)timers, timercnt, 0);
546 } 649 }
547 else 650 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 651 ev_timer_stop (w); /* nonrepeating: stop timer */
549 652
560 663
561 /* first reschedule or stop timer */ 664 /* first reschedule or stop timer */
562 if (w->interval) 665 if (w->interval)
563 { 666 {
564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 667 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)); 668 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
566 downheap ((WT *)periodics, periodiccnt, 0); 669 downheap ((WT *)periodics, periodiccnt, 0);
567 } 670 }
568 else 671 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 672 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 673
571 event ((W)w, EV_TIMEOUT); 674 event ((W)w, EV_PERIODIC);
572 } 675 }
573} 676}
574 677
575static void 678static void
576periodics_reschedule (ev_tstamp diff) 679periodics_reschedule (ev_tstamp diff)
595 } 698 }
596 } 699 }
597 } 700 }
598} 701}
599 702
703static int
704time_update_monotonic (void)
705{
706 now = get_clock ();
707
708 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
709 {
710 ev_now = now + diff;
711 return 0;
712 }
713 else
714 {
715 now_floor = now;
716 ev_now = ev_time ();
717 return 1;
718 }
719}
720
600static void 721static void
601time_update (void) 722time_update (void)
602{ 723{
603 int i; 724 int i;
604 725
605 ev_now = ev_time (); 726#if EV_USE_MONOTONIC
606
607 if (have_monotonic) 727 if (expect_true (have_monotonic))
608 { 728 {
729 if (time_update_monotonic ())
730 {
609 ev_tstamp odiff = diff; 731 ev_tstamp odiff = diff;
610 732
611 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 733 for (i = 4; --i; ) /* loop a few times, before making important decisions */
612 { 734 {
613 now = get_clock ();
614 diff = ev_now - now; 735 diff = ev_now - now;
615 736
616 if (fabs (odiff - diff) < MIN_TIMEJUMP) 737 if (fabs (odiff - diff) < MIN_TIMEJUMP)
617 return; /* all is well */ 738 return; /* all is well */
618 739
619 ev_now = ev_time (); 740 ev_now = ev_time ();
741 now = get_clock ();
742 now_floor = now;
620 } 743 }
621 744
622 periodics_reschedule (diff - odiff); 745 periodics_reschedule (diff - odiff);
623 /* no timer adjustment, as the monotonic clock doesn't jump */ 746 /* no timer adjustment, as the monotonic clock doesn't jump */
747 }
624 } 748 }
625 else 749 else
750#endif
626 { 751 {
752 ev_now = ev_time ();
753
627 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 754 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
628 { 755 {
629 periodics_reschedule (ev_now - now); 756 periodics_reschedule (ev_now - now);
630 757
631 /* adjust timers. this is easy, as the offset is the same for all */ 758 /* adjust timers. this is easy, as the offset is the same for all */
632 for (i = 0; i < timercnt; ++i) 759 for (i = 0; i < timercnt; ++i)
645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 772 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
646 773
647 do 774 do
648 { 775 {
649 /* queue check watchers (and execute them) */ 776 /* queue check watchers (and execute them) */
650 if (preparecnt) 777 if (expect_false (preparecnt))
651 { 778 {
652 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 779 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
653 call_pending (); 780 call_pending ();
654 } 781 }
655 782
658 785
659 /* calculate blocking time */ 786 /* calculate blocking time */
660 787
661 /* we only need this for !monotonic clockor timers, but as we basically 788 /* we only need this for !monotonic clockor timers, but as we basically
662 always have timers, we just calculate it always */ 789 always have timers, we just calculate it always */
790#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic))
792 time_update_monotonic ();
793 else
794#endif
795 {
663 ev_now = ev_time (); 796 ev_now = ev_time ();
797 now = ev_now;
798 }
664 799
665 if (flags & EVLOOP_NONBLOCK || idlecnt) 800 if (flags & EVLOOP_NONBLOCK || idlecnt)
666 block = 0.; 801 block = 0.;
667 else 802 else
668 { 803 {
669 block = MAX_BLOCKTIME; 804 block = MAX_BLOCKTIME;
670 805
671 if (timercnt) 806 if (timercnt)
672 { 807 {
673 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 808 ev_tstamp to = timers [0]->at - now + method_fudge;
674 if (block > to) block = to; 809 if (block > to) block = to;
675 } 810 }
676 811
677 if (periodiccnt) 812 if (periodiccnt)
678 { 813 {
731 head = &(*head)->next; 866 head = &(*head)->next;
732 } 867 }
733} 868}
734 869
735static void 870static void
736ev_clear (W w) 871ev_clear_pending (W w)
737{ 872{
738 if (w->pending) 873 if (w->pending)
739 { 874 {
740 pendings [w->pending - 1].w = 0; 875 pendings [ABSPRI (w)][w->pending - 1].w = 0;
741 w->pending = 0; 876 w->pending = 0;
742 } 877 }
743} 878}
744 879
745static void 880static void
746ev_start (W w, int active) 881ev_start (W w, int active)
747{ 882{
883 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
884 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
885
748 w->active = active; 886 w->active = active;
749} 887}
750 888
751static void 889static void
752ev_stop (W w) 890ev_stop (W w)
757/*****************************************************************************/ 895/*****************************************************************************/
758 896
759void 897void
760ev_io_start (struct ev_io *w) 898ev_io_start (struct ev_io *w)
761{ 899{
900 int fd = w->fd;
901
762 if (ev_is_active (w)) 902 if (ev_is_active (w))
763 return; 903 return;
764 904
765 int fd = w->fd; 905 assert (("ev_io_start called with negative fd", fd >= 0));
766 906
767 ev_start ((W)w, 1); 907 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 908 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 909 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 910
772} 912}
773 913
774void 914void
775ev_io_stop (struct ev_io *w) 915ev_io_stop (struct ev_io *w)
776{ 916{
777 ev_clear ((W)w); 917 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 918 if (!ev_is_active (w))
779 return; 919 return;
780 920
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 921 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 922 ev_stop ((W)w);
790 if (ev_is_active (w)) 930 if (ev_is_active (w))
791 return; 931 return;
792 932
793 w->at += now; 933 w->at += now;
794 934
795 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 935 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
796 936
797 ev_start ((W)w, ++timercnt); 937 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 938 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 939 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 940 upheap ((WT *)timers, timercnt - 1);
801} 941}
802 942
803void 943void
804ev_timer_stop (struct ev_timer *w) 944ev_timer_stop (struct ev_timer *w)
805{ 945{
806 ev_clear ((W)w); 946 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 947 if (!ev_is_active (w))
808 return; 948 return;
809 949
810 if (w->active < timercnt--) 950 if (w->active < timercnt--)
811 { 951 {
839ev_periodic_start (struct ev_periodic *w) 979ev_periodic_start (struct ev_periodic *w)
840{ 980{
841 if (ev_is_active (w)) 981 if (ev_is_active (w))
842 return; 982 return;
843 983
844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 984 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
845 985
846 /* this formula differs from the one in periodic_reify because we do not always round up */ 986 /* this formula differs from the one in periodic_reify because we do not always round up */
847 if (w->interval) 987 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 988 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 989
854} 994}
855 995
856void 996void
857ev_periodic_stop (struct ev_periodic *w) 997ev_periodic_stop (struct ev_periodic *w)
858{ 998{
859 ev_clear ((W)w); 999 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 1000 if (!ev_is_active (w))
861 return; 1001 return;
862 1002
863 if (w->active < periodiccnt--) 1003 if (w->active < periodiccnt--)
864 { 1004 {
872void 1012void
873ev_signal_start (struct ev_signal *w) 1013ev_signal_start (struct ev_signal *w)
874{ 1014{
875 if (ev_is_active (w)) 1015 if (ev_is_active (w))
876 return; 1016 return;
1017
1018 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 1019
878 ev_start ((W)w, 1); 1020 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 1021 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1022 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 1023
890} 1032}
891 1033
892void 1034void
893ev_signal_stop (struct ev_signal *w) 1035ev_signal_stop (struct ev_signal *w)
894{ 1036{
895 ev_clear ((W)w); 1037 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 1038 if (!ev_is_active (w))
897 return; 1039 return;
898 1040
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1041 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 1042 ev_stop ((W)w);
915} 1057}
916 1058
917void 1059void
918ev_idle_stop (struct ev_idle *w) 1060ev_idle_stop (struct ev_idle *w)
919{ 1061{
920 ev_clear ((W)w); 1062 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 1063 if (ev_is_active (w))
922 return; 1064 return;
923 1065
924 idles [w->active - 1] = idles [--idlecnt]; 1066 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 1067 ev_stop ((W)w);
937} 1079}
938 1080
939void 1081void
940ev_prepare_stop (struct ev_prepare *w) 1082ev_prepare_stop (struct ev_prepare *w)
941{ 1083{
942 ev_clear ((W)w); 1084 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 1085 if (ev_is_active (w))
944 return; 1086 return;
945 1087
946 prepares [w->active - 1] = prepares [--preparecnt]; 1088 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 1089 ev_stop ((W)w);
959} 1101}
960 1102
961void 1103void
962ev_check_stop (struct ev_check *w) 1104ev_check_stop (struct ev_check *w)
963{ 1105{
964 ev_clear ((W)w); 1106 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 1107 if (ev_is_active (w))
966 return; 1108 return;
967 1109
968 checks [w->active - 1] = checks [--checkcnt]; 1110 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 1111 ev_stop ((W)w);
980} 1122}
981 1123
982void 1124void
983ev_child_stop (struct ev_child *w) 1125ev_child_stop (struct ev_child *w)
984{ 1126{
985 ev_clear ((W)w); 1127 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 1128 if (ev_is_active (w))
987 return; 1129 return;
988 1130
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1131 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 1132 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines