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

Comparing libev/ev.c (file contents):
Revision 1.29 by root, Thu Nov 1 08:10:03 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
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 }
113#endif 154#endif
114 155
115 return ev_time (); 156 return ev_time ();
116} 157}
117 158
118#define array_nextsize(n) (((n) << 1) | 4 & ~3) 159#define array_roundsize(base,n) ((n) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120 160
121#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
122 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
123 { \ 163 { \
124 int newcnt = cur; \ 164 int newcnt = cur; \
125 do \ 165 do \
126 { \ 166 { \
127 newcnt = array_nextsize (newcnt); \ 167 newcnt = array_roundsize (base, newcnt << 1); \
128 } \ 168 } \
129 while ((cnt) > newcnt); \ 169 while ((cnt) > newcnt); \
130 \ 170 \
131 base = realloc (base, sizeof (*base) * (newcnt)); \ 171 base = realloc (base, sizeof (*base) * (newcnt)); \
132 init (base + cur, newcnt - cur); \ 172 init (base + cur, newcnt - cur); \
136/*****************************************************************************/ 176/*****************************************************************************/
137 177
138typedef struct 178typedef struct
139{ 179{
140 struct ev_io *head; 180 struct ev_io *head;
141 int events; 181 unsigned char events;
182 unsigned char reify;
142} ANFD; 183} ANFD;
143 184
144static ANFD *anfds; 185static ANFD *anfds;
145static int anfdmax; 186static int anfdmax;
146 187
149{ 190{
150 while (count--) 191 while (count--)
151 { 192 {
152 base->head = 0; 193 base->head = 0;
153 base->events = EV_NONE; 194 base->events = EV_NONE;
195 base->reify = 0;
196
154 ++base; 197 ++base;
155 } 198 }
156} 199}
157 200
158typedef struct 201typedef struct
159{ 202{
160 W w; 203 W w;
161 int events; 204 int events;
162} ANPENDING; 205} ANPENDING;
163 206
164static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
165static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
166 209
167static void 210static void
168event (W w, int events) 211event (W w, int events)
169{ 212{
170 if (w->active) 213 if (w->pending)
171 { 214 {
172 w->pending = ++pendingcnt;
173 array_needsize (pendings, pendingmax, pendingcnt, );
174 pendings [pendingcnt - 1].w = w;
175 pendings [pendingcnt - 1].events = events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
216 return;
176 } 217 }
218
219 w->pending = ++pendingcnt [ABSPRI (w)];
220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
221 pendings [ABSPRI (w)][w->pending - 1].w = w;
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
343 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
344 415
345 if (!gotsig) 416 if (!gotsig)
346 { 417 {
347 gotsig = 1; 418 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 419 write (sigpipe [1], &signum, 1);
349 } 420 }
350} 421}
351 422
352static void 423static void
353sigcb (struct ev_io *iow, int revents) 424sigcb (struct ev_io *iow, int revents)
354{ 425{
355 struct ev_signal *w; 426 struct ev_signal *w;
356 int sig; 427 int signum;
357 428
429 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 430 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 431
361 for (sig = signalmax; sig--; ) 432 for (signum = signalmax; signum--; )
362 if (signals [sig].gotsig) 433 if (signals [signum].gotsig)
363 { 434 {
364 signals [sig].gotsig = 0; 435 signals [signum].gotsig = 0;
365 436
366 for (w = signals [sig].head; w; w = w->next) 437 for (w = signals [signum].head; w; w = w->next)
367 event ((W)w, EV_SIGNAL); 438 event ((W)w, EV_SIGNAL);
368 } 439 }
369} 440}
370 441
371static void 442static void
372siginit (void) 443siginit (void)
373{ 444{
445#ifndef WIN32
374 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
375 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
376 448
377 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
380 453
381 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
382 ev_io_start (&sigev); 455 ev_io_start (&sigev);
383} 456}
384 457
396/*****************************************************************************/ 469/*****************************************************************************/
397 470
398static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
399static struct ev_signal childev; 472static struct ev_signal childev;
400 473
474#ifndef WIN32
475
401#ifndef WCONTINUED 476#ifndef WCONTINUED
402# define WCONTINUED 0 477# define WCONTINUED 0
403#endif 478#endif
404 479
405static void 480static void
408 struct ev_child *w; 483 struct ev_child *w;
409 int pid, status; 484 int pid, status;
410 485
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
413 if (w->pid == pid || w->pid == -1) 488 if (w->pid == pid || !w->pid)
414 { 489 {
415 w->status = status; 490 w->status = status;
416 event ((W)w, EV_CHILD); 491 event ((W)w, EV_CHILD);
417 } 492 }
418} 493}
419 494
420/*****************************************************************************/ 495#endif
421 496
497/*****************************************************************************/
498
499#if EV_USE_KQUEUE
500# include "ev_kqueue.c"
501#endif
422#if EV_USE_EPOLL 502#if EV_USE_EPOLL
423# include "ev_epoll.c" 503# include "ev_epoll.c"
424#endif 504#endif
505#if EV_USE_POLL
506# include "ev_poll.c"
507#endif
425#if EV_USE_SELECT 508#if EV_USE_SELECT
426# include "ev_select.c" 509# include "ev_select.c"
427#endif 510#endif
428 511
429int 512int
436ev_version_minor (void) 519ev_version_minor (void)
437{ 520{
438 return EV_VERSION_MINOR; 521 return EV_VERSION_MINOR;
439} 522}
440 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
441int ev_init (int flags) 532int ev_init (int methods)
442{ 533{
443 if (!ev_method) 534 if (!ev_method)
444 { 535 {
445#if EV_USE_MONOTONIC 536#if EV_USE_MONOTONIC
446 { 537 {
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 539 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1; 540 have_monotonic = 1;
450 } 541 }
451#endif 542#endif
452 543
453 ev_now = ev_time (); 544 ev_now = ev_time ();
454 now = get_clock (); 545 now = get_clock ();
546 now_floor = now;
455 diff = ev_now - now; 547 diff = ev_now - now;
456 548
457 if (pipe (sigpipe)) 549 if (pipe (sigpipe))
458 return 0; 550 return 0;
459 551
552 if (methods == EVMETHOD_AUTO)
553 if (!enable_secure () && getenv ("LIBEV_METHODS"))
554 methods = atoi (getenv ("LIBEV_METHODS"));
555 else
460 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
461#if EV_USE_EPOLL 562#if EV_USE_EPOLL
462 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);
463#endif 567#endif
464#if EV_USE_SELECT 568#if EV_USE_SELECT
465 if (ev_method == EVMETHOD_NONE) select_init (flags); 569 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
466#endif 570#endif
467 571
468 if (ev_method) 572 if (ev_method)
469 { 573 {
470 ev_watcher_init (&sigev, sigcb); 574 ev_watcher_init (&sigev, sigcb);
471 siginit (); 575 siginit ();
472 576
577#ifndef WIN32
473 ev_signal_init (&childev, childcb, SIGCHLD); 578 ev_signal_init (&childev, childcb, SIGCHLD);
474 ev_signal_start (&childev); 579 ev_signal_start (&childev);
580#endif
475 } 581 }
476 } 582 }
477 583
478 return ev_method; 584 return ev_method;
479} 585}
480 586
481/*****************************************************************************/ 587/*****************************************************************************/
482 588
483void 589void
484ev_prefork (void) 590ev_fork_prepare (void)
485{ 591{
486 /* nop */ 592 /* nop */
487} 593}
488 594
489void 595void
490ev_postfork_parent (void) 596ev_fork_parent (void)
491{ 597{
492 /* nop */ 598 /* nop */
493} 599}
494 600
495void 601void
496ev_postfork_child (void) 602ev_fork_child (void)
497{ 603{
498#if EV_USE_EPOLL 604#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 605 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 606 epoll_postfork_child ();
501#endif 607#endif
510/*****************************************************************************/ 616/*****************************************************************************/
511 617
512static void 618static void
513call_pending (void) 619call_pending (void)
514{ 620{
621 int pri;
622
623 for (pri = NUMPRI; pri--; )
515 while (pendingcnt) 624 while (pendingcnt [pri])
516 { 625 {
517 ANPENDING *p = pendings + --pendingcnt; 626 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
518 627
519 if (p->w) 628 if (p->w)
520 { 629 {
521 p->w->pending = 0; 630 p->w->pending = 0;
522 p->w->cb (p->w, p->events); 631 p->w->cb (p->w, p->events);
523 } 632 }
524 } 633 }
525} 634}
526 635
527static void 636static void
528timers_reify (void) 637timers_reify (void)
529{ 638{
530 while (timercnt && timers [0]->at <= now) 639 while (timercnt && timers [0]->at <= now)
531 { 640 {
532 struct ev_timer *w = timers [0]; 641 struct ev_timer *w = timers [0];
533
534 event ((W)w, EV_TIMEOUT);
535 642
536 /* first reschedule or stop timer */ 643 /* first reschedule or stop timer */
537 if (w->repeat) 644 if (w->repeat)
538 { 645 {
646 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
539 w->at = now + w->repeat; 647 w->at = now + w->repeat;
540 assert (("timer timeout in the past, negative repeat?", w->at > now));
541 downheap ((WT *)timers, timercnt, 0); 648 downheap ((WT *)timers, timercnt, 0);
542 } 649 }
543 else 650 else
544 ev_timer_stop (w); /* nonrepeating: stop timer */ 651 ev_timer_stop (w); /* nonrepeating: stop timer */
652
653 event ((W)w, EV_TIMEOUT);
545 } 654 }
546} 655}
547 656
548static void 657static void
549periodics_reify (void) 658periodics_reify (void)
554 663
555 /* first reschedule or stop timer */ 664 /* first reschedule or stop timer */
556 if (w->interval) 665 if (w->interval)
557 { 666 {
558 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;
559 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));
560 downheap ((WT *)periodics, periodiccnt, 0); 669 downheap ((WT *)periodics, periodiccnt, 0);
561 } 670 }
562 else 671 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 672 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 673
565 event ((W)w, EV_TIMEOUT); 674 event ((W)w, EV_PERIODIC);
566 } 675 }
567} 676}
568 677
569static void 678static void
570periodics_reschedule (ev_tstamp diff) 679periodics_reschedule (ev_tstamp diff)
589 } 698 }
590 } 699 }
591 } 700 }
592} 701}
593 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
594static void 721static void
595time_update (void) 722time_update (void)
596{ 723{
597 int i; 724 int i;
598 725
599 ev_now = ev_time (); 726#if EV_USE_MONOTONIC
600
601 if (have_monotonic) 727 if (expect_true (have_monotonic))
602 { 728 {
729 if (time_update_monotonic ())
730 {
603 ev_tstamp odiff = diff; 731 ev_tstamp odiff = diff;
604 732
605 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 */
606 { 734 {
607 now = get_clock ();
608 diff = ev_now - now; 735 diff = ev_now - now;
609 736
610 if (fabs (odiff - diff) < MIN_TIMEJUMP) 737 if (fabs (odiff - diff) < MIN_TIMEJUMP)
611 return; /* all is well */ 738 return; /* all is well */
612 739
613 ev_now = ev_time (); 740 ev_now = ev_time ();
741 now = get_clock ();
742 now_floor = now;
614 } 743 }
615 744
616 periodics_reschedule (diff - odiff); 745 periodics_reschedule (diff - odiff);
617 /* no timer adjustment, as the monotonic clock doesn't jump */ 746 /* no timer adjustment, as the monotonic clock doesn't jump */
747 }
618 } 748 }
619 else 749 else
750#endif
620 { 751 {
752 ev_now = ev_time ();
753
621 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))
622 { 755 {
623 periodics_reschedule (ev_now - now); 756 periodics_reschedule (ev_now - now);
624 757
625 /* 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 */
626 for (i = 0; i < timercnt; ++i) 759 for (i = 0; i < timercnt; ++i)
639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 772 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
640 773
641 do 774 do
642 { 775 {
643 /* queue check watchers (and execute them) */ 776 /* queue check watchers (and execute them) */
644 if (preparecnt) 777 if (expect_false (preparecnt))
645 { 778 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 779 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending (); 780 call_pending ();
648 } 781 }
649 782
652 785
653 /* calculate blocking time */ 786 /* calculate blocking time */
654 787
655 /* 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
656 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 {
657 ev_now = ev_time (); 796 ev_now = ev_time ();
797 now = ev_now;
798 }
658 799
659 if (flags & EVLOOP_NONBLOCK || idlecnt) 800 if (flags & EVLOOP_NONBLOCK || idlecnt)
660 block = 0.; 801 block = 0.;
661 else 802 else
662 { 803 {
663 block = MAX_BLOCKTIME; 804 block = MAX_BLOCKTIME;
664 805
665 if (timercnt) 806 if (timercnt)
666 { 807 {
667 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 808 ev_tstamp to = timers [0]->at - now + method_fudge;
668 if (block > to) block = to; 809 if (block > to) block = to;
669 } 810 }
670 811
671 if (periodiccnt) 812 if (periodiccnt)
672 { 813 {
725 head = &(*head)->next; 866 head = &(*head)->next;
726 } 867 }
727} 868}
728 869
729static void 870static void
730ev_clear (W w) 871ev_clear_pending (W w)
731{ 872{
732 if (w->pending) 873 if (w->pending)
733 { 874 {
734 pendings [w->pending - 1].w = 0; 875 pendings [ABSPRI (w)][w->pending - 1].w = 0;
735 w->pending = 0; 876 w->pending = 0;
736 } 877 }
737} 878}
738 879
739static void 880static void
740ev_start (W w, int active) 881ev_start (W w, int active)
741{ 882{
883 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
884 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
885
742 w->active = active; 886 w->active = active;
743} 887}
744 888
745static void 889static void
746ev_stop (W w) 890ev_stop (W w)
751/*****************************************************************************/ 895/*****************************************************************************/
752 896
753void 897void
754ev_io_start (struct ev_io *w) 898ev_io_start (struct ev_io *w)
755{ 899{
900 int fd = w->fd;
901
756 if (ev_is_active (w)) 902 if (ev_is_active (w))
757 return; 903 return;
758 904
759 int fd = w->fd; 905 assert (("ev_io_start called with negative fd", fd >= 0));
760 906
761 ev_start ((W)w, 1); 907 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 908 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 909 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 910
766} 912}
767 913
768void 914void
769ev_io_stop (struct ev_io *w) 915ev_io_stop (struct ev_io *w)
770{ 916{
771 ev_clear ((W)w); 917 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 918 if (!ev_is_active (w))
773 return; 919 return;
774 920
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 921 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 922 ev_stop ((W)w);
784 if (ev_is_active (w)) 930 if (ev_is_active (w))
785 return; 931 return;
786 932
787 w->at += now; 933 w->at += now;
788 934
789 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.));
790 936
791 ev_start ((W)w, ++timercnt); 937 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 938 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 939 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 940 upheap ((WT *)timers, timercnt - 1);
795} 941}
796 942
797void 943void
798ev_timer_stop (struct ev_timer *w) 944ev_timer_stop (struct ev_timer *w)
799{ 945{
800 ev_clear ((W)w); 946 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 947 if (!ev_is_active (w))
802 return; 948 return;
803 949
804 if (w->active < timercnt--) 950 if (w->active < timercnt--)
805 { 951 {
833ev_periodic_start (struct ev_periodic *w) 979ev_periodic_start (struct ev_periodic *w)
834{ 980{
835 if (ev_is_active (w)) 981 if (ev_is_active (w))
836 return; 982 return;
837 983
838 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.));
839 985
840 /* 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 */
841 if (w->interval) 987 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 988 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 989
848} 994}
849 995
850void 996void
851ev_periodic_stop (struct ev_periodic *w) 997ev_periodic_stop (struct ev_periodic *w)
852{ 998{
853 ev_clear ((W)w); 999 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 1000 if (!ev_is_active (w))
855 return; 1001 return;
856 1002
857 if (w->active < periodiccnt--) 1003 if (w->active < periodiccnt--)
858 { 1004 {
866void 1012void
867ev_signal_start (struct ev_signal *w) 1013ev_signal_start (struct ev_signal *w)
868{ 1014{
869 if (ev_is_active (w)) 1015 if (ev_is_active (w))
870 return; 1016 return;
1017
1018 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 1019
872 ev_start ((W)w, 1); 1020 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 1021 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1022 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 1023
884} 1032}
885 1033
886void 1034void
887ev_signal_stop (struct ev_signal *w) 1035ev_signal_stop (struct ev_signal *w)
888{ 1036{
889 ev_clear ((W)w); 1037 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 1038 if (!ev_is_active (w))
891 return; 1039 return;
892 1040
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1041 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 1042 ev_stop ((W)w);
909} 1057}
910 1058
911void 1059void
912ev_idle_stop (struct ev_idle *w) 1060ev_idle_stop (struct ev_idle *w)
913{ 1061{
914 ev_clear ((W)w); 1062 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 1063 if (ev_is_active (w))
916 return; 1064 return;
917 1065
918 idles [w->active - 1] = idles [--idlecnt]; 1066 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 1067 ev_stop ((W)w);
931} 1079}
932 1080
933void 1081void
934ev_prepare_stop (struct ev_prepare *w) 1082ev_prepare_stop (struct ev_prepare *w)
935{ 1083{
936 ev_clear ((W)w); 1084 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 1085 if (ev_is_active (w))
938 return; 1086 return;
939 1087
940 prepares [w->active - 1] = prepares [--preparecnt]; 1088 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 1089 ev_stop ((W)w);
953} 1101}
954 1102
955void 1103void
956ev_check_stop (struct ev_check *w) 1104ev_check_stop (struct ev_check *w)
957{ 1105{
958 ev_clear ((W)w); 1106 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 1107 if (ev_is_active (w))
960 return; 1108 return;
961 1109
962 checks [w->active - 1] = checks [--checkcnt]; 1110 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 1111 ev_stop ((W)w);
974} 1122}
975 1123
976void 1124void
977ev_child_stop (struct ev_child *w) 1125ev_child_stop (struct ev_child *w)
978{ 1126{
979 ev_clear ((W)w); 1127 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 1128 if (ev_is_active (w))
981 return; 1129 return;
982 1130
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1131 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1132 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines