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

Comparing libev/ev.c (file contents):
Revision 1.20 by root, Wed Oct 31 18:28:00 2007 UTC vs.
Revision 1.40 by root, Fri Nov 2 11:02:23 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
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
36 41
37#include <stdio.h> 42#include <stdio.h>
38 43
39#include <assert.h> 44#include <assert.h>
40#include <errno.h> 45#include <errno.h>
46#include <sys/types.h>
47#include <sys/wait.h>
41#include <sys/time.h> 48#include <sys/time.h>
42#include <time.h> 49#include <time.h>
43 50
51/**/
52
44#ifndef HAVE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
54# define EV_USE_MONOTONIC 1
55#endif
56
57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1
59#endif
60
61#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0
63#endif
64
65#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1
67#endif
68
69/**/
70
45# ifdef CLOCK_MONOTONIC 71#ifndef CLOCK_MONOTONIC
72# undef EV_USE_MONOTONIC
46# define HAVE_MONOTONIC 1 73# define EV_USE_MONOTONIC 0
47# endif 74#endif
48#endif
49 75
50#ifndef HAVE_SELECT
51# define HAVE_SELECT 1
52#endif
53
54#ifndef HAVE_EPOLL
55# define HAVE_EPOLL 0
56#endif
57
58#ifndef HAVE_REALTIME 76#ifndef CLOCK_REALTIME
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 77# undef EV_USE_REALTIME
78# define EV_USE_REALTIME 0
60#endif 79#endif
80
81/**/
61 82
62#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 83#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
63#define MAX_BLOCKTIME 60. 84#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
85#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
86/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
64 87
65#include "ev.h" 88#include "ev.h"
89
90#if __GNUC__ >= 3
91# define expect(expr,value) __builtin_expect ((expr),(value))
92# define inline inline
93#else
94# define expect(expr,value) (expr)
95# define inline static
96#endif
97
98#define expect_false(expr) expect ((expr) != 0, 0)
99#define expect_true(expr) expect ((expr) != 0, 1)
66 100
67typedef struct ev_watcher *W; 101typedef struct ev_watcher *W;
68typedef struct ev_watcher_list *WL; 102typedef struct ev_watcher_list *WL;
69typedef struct ev_watcher_time *WT; 103typedef struct ev_watcher_time *WT;
70 104
71static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
72ev_tstamp ev_now; 106ev_tstamp ev_now;
73int ev_method; 107int ev_method;
74 108
75static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
76 110
81/*****************************************************************************/ 115/*****************************************************************************/
82 116
83ev_tstamp 117ev_tstamp
84ev_time (void) 118ev_time (void)
85{ 119{
86#if HAVE_REALTIME 120#if EV_USE_REALTIME
87 struct timespec ts; 121 struct timespec ts;
88 clock_gettime (CLOCK_REALTIME, &ts); 122 clock_gettime (CLOCK_REALTIME, &ts);
89 return ts.tv_sec + ts.tv_nsec * 1e-9; 123 return ts.tv_sec + ts.tv_nsec * 1e-9;
90#else 124#else
91 struct timeval tv; 125 struct timeval tv;
95} 129}
96 130
97static ev_tstamp 131static ev_tstamp
98get_clock (void) 132get_clock (void)
99{ 133{
100#if HAVE_MONOTONIC 134#if EV_USE_MONOTONIC
101 if (have_monotonic) 135 if (expect_true (have_monotonic))
102 { 136 {
103 struct timespec ts; 137 struct timespec ts;
104 clock_gettime (CLOCK_MONOTONIC, &ts); 138 clock_gettime (CLOCK_MONOTONIC, &ts);
105 return ts.tv_sec + ts.tv_nsec * 1e-9; 139 return ts.tv_sec + ts.tv_nsec * 1e-9;
106 } 140 }
107#endif 141#endif
108 142
109 return ev_time (); 143 return ev_time ();
110} 144}
111 145
146#define array_roundsize(base,n) ((n) | 4 & ~3)
147
112#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
113 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
114 { \ 150 { \
115 int newcnt = cur ? cur << 1 : 16; \ 151 int newcnt = cur; \
152 do \
153 { \
154 newcnt = array_roundsize (base, newcnt << 1); \
155 } \
156 while ((cnt) > newcnt); \
157 \
116 base = realloc (base, sizeof (*base) * (newcnt)); \ 158 base = realloc (base, sizeof (*base) * (newcnt)); \
117 init (base + cur, newcnt - cur); \ 159 init (base + cur, newcnt - cur); \
118 cur = newcnt; \ 160 cur = newcnt; \
119 } 161 }
120 162
121/*****************************************************************************/ 163/*****************************************************************************/
122 164
123typedef struct 165typedef struct
124{ 166{
125 struct ev_io *head; 167 struct ev_io *head;
126 unsigned char wev, rev; /* want, received event set */ 168 unsigned char events;
169 unsigned char reify;
127} ANFD; 170} ANFD;
128 171
129static ANFD *anfds; 172static ANFD *anfds;
130static int anfdmax; 173static int anfdmax;
131 174
132static int *fdchanges;
133static int fdchangemax, fdchangecnt;
134
135static void 175static void
136anfds_init (ANFD *base, int count) 176anfds_init (ANFD *base, int count)
137{ 177{
138 while (count--) 178 while (count--)
139 { 179 {
140 base->head = 0; 180 base->head = 0;
141 base->wev = base->rev = EV_NONE; 181 base->events = EV_NONE;
182 base->reify = 0;
183
142 ++base; 184 ++base;
143 } 185 }
144} 186}
145 187
146typedef struct 188typedef struct
153static int pendingmax, pendingcnt; 195static int pendingmax, pendingcnt;
154 196
155static void 197static void
156event (W w, int events) 198event (W w, int events)
157{ 199{
158 if (w->active) 200 if (w->pending)
201 {
202 pendings [w->pending - 1].events |= events;
203 return;
159 { 204 }
205
160 w->pending = ++pendingcnt; 206 w->pending = ++pendingcnt;
161 array_needsize (pendings, pendingmax, pendingcnt, ); 207 array_needsize (pendings, pendingmax, pendingcnt, );
162 pendings [pendingcnt - 1].w = w; 208 pendings [pendingcnt - 1].w = w;
163 pendings [pendingcnt - 1].events = events; 209 pendings [pendingcnt - 1].events = events;
164 } 210}
211
212static void
213queue_events (W *events, int eventcnt, int type)
214{
215 int i;
216
217 for (i = 0; i < eventcnt; ++i)
218 event (events [i], type);
165} 219}
166 220
167static void 221static void
168fd_event (int fd, int events) 222fd_event (int fd, int events)
169{ 223{
177 if (ev) 231 if (ev)
178 event ((W)w, ev); 232 event ((W)w, ev);
179 } 233 }
180} 234}
181 235
236/*****************************************************************************/
237
238static int *fdchanges;
239static int fdchangemax, fdchangecnt;
240
182static void 241static void
183queue_events (W *events, int eventcnt, int type) 242fd_reify (void)
184{ 243{
185 int i; 244 int i;
186 245
187 for (i = 0; i < eventcnt; ++i) 246 for (i = 0; i < fdchangecnt; ++i)
188 event (events [i], type); 247 {
248 int fd = fdchanges [i];
249 ANFD *anfd = anfds + fd;
250 struct ev_io *w;
251
252 int events = 0;
253
254 for (w = anfd->head; w; w = w->next)
255 events |= w->events;
256
257 anfd->reify = 0;
258
259 if (anfd->events != events)
260 {
261 method_modify (fd, anfd->events, events);
262 anfd->events = events;
263 }
264 }
265
266 fdchangecnt = 0;
267}
268
269static void
270fd_change (int fd)
271{
272 if (anfds [fd].reify || fdchangecnt < 0)
273 return;
274
275 anfds [fd].reify = 1;
276
277 ++fdchangecnt;
278 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
279 fdchanges [fdchangecnt - 1] = fd;
189} 280}
190 281
191/* called on EBADF to verify fds */ 282/* called on EBADF to verify fds */
192static void 283static void
193fd_recheck () 284fd_recheck (void)
194{ 285{
195 int fd; 286 int fd;
196 287
197 for (fd = 0; fd < anfdmax; ++fd) 288 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 289 if (anfds [fd].events)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 290 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 291 while (anfds [fd].head)
292 {
201 evio_stop (anfds [fd].head); 293 ev_io_stop (anfds [fd].head);
294 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
295 }
202} 296}
203 297
204/*****************************************************************************/ 298/*****************************************************************************/
205 299
206static struct ev_timer **timers; 300static struct ev_timer **timers;
253/*****************************************************************************/ 347/*****************************************************************************/
254 348
255typedef struct 349typedef struct
256{ 350{
257 struct ev_signal *head; 351 struct ev_signal *head;
258 sig_atomic_t gotsig; 352 sig_atomic_t volatile gotsig;
259} ANSIG; 353} ANSIG;
260 354
261static ANSIG *signals; 355static ANSIG *signals;
262static int signalmax; 356static int signalmax;
263 357
264static int sigpipe [2]; 358static int sigpipe [2];
265static sig_atomic_t gotsig; 359static sig_atomic_t volatile gotsig;
266static struct ev_io sigev; 360static struct ev_io sigev;
267 361
268static void 362static void
269signals_init (ANSIG *base, int count) 363signals_init (ANSIG *base, int count)
270{ 364{
271 while (count--) 365 while (count--)
272 { 366 {
273 base->head = 0; 367 base->head = 0;
274 base->gotsig = 0; 368 base->gotsig = 0;
369
275 ++base; 370 ++base;
276 } 371 }
277} 372}
278 373
279static void 374static void
282 signals [signum - 1].gotsig = 1; 377 signals [signum - 1].gotsig = 1;
283 378
284 if (!gotsig) 379 if (!gotsig)
285 { 380 {
286 gotsig = 1; 381 gotsig = 1;
287 write (sigpipe [1], &gotsig, 1); 382 write (sigpipe [1], &signum, 1);
288 } 383 }
289} 384}
290 385
291static void 386static void
292sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
293{ 388{
294 struct ev_signal *w; 389 struct ev_signal *w;
295 int sig; 390 int signum;
296 391
392 read (sigpipe [0], &revents, 1);
297 gotsig = 0; 393 gotsig = 0;
298 read (sigpipe [0], &revents, 1);
299 394
300 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
301 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
302 { 397 {
303 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
304 399
305 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
306 event ((W)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
307 } 402 }
308} 403}
309 404
310static void 405static void
315 410
316 /* rather than sort out wether we really need nb, set it */ 411 /* rather than sort out wether we really need nb, set it */
317 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
318 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
319 414
320 evio_set (&sigev, sigpipe [0], EV_READ); 415 ev_io_set (&sigev, sigpipe [0], EV_READ);
321 evio_start (&sigev); 416 ev_io_start (&sigev);
322} 417}
323 418
324/*****************************************************************************/ 419/*****************************************************************************/
325 420
326static struct ev_idle **idles; 421static struct ev_idle **idles;
332static struct ev_check **checks; 427static struct ev_check **checks;
333static int checkmax, checkcnt; 428static int checkmax, checkcnt;
334 429
335/*****************************************************************************/ 430/*****************************************************************************/
336 431
432static struct ev_child *childs [PID_HASHSIZE];
433static struct ev_signal childev;
434
435#ifndef WCONTINUED
436# define WCONTINUED 0
437#endif
438
439static void
440childcb (struct ev_signal *sw, int revents)
441{
442 struct ev_child *w;
443 int pid, status;
444
445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
447 if (w->pid == pid || !w->pid)
448 {
449 w->status = status;
450 event ((W)w, EV_CHILD);
451 }
452}
453
454/*****************************************************************************/
455
337#if HAVE_EPOLL 456#if EV_USE_EPOLL
338# include "ev_epoll.c" 457# include "ev_epoll.c"
339#endif 458#endif
340#if HAVE_SELECT 459#if EV_USE_SELECT
341# include "ev_select.c" 460# include "ev_select.c"
342#endif 461#endif
343 462
463int
464ev_version_major (void)
465{
466 return EV_VERSION_MAJOR;
467}
468
469int
470ev_version_minor (void)
471{
472 return EV_VERSION_MINOR;
473}
474
344int ev_init (int flags) 475int ev_init (int flags)
345{ 476{
346#if HAVE_MONOTONIC
347 {
348 struct timespec ts;
349 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
350 have_monotonic = 1;
351 }
352#endif
353
354 ev_now = ev_time ();
355 now = get_clock ();
356 diff = ev_now - now;
357
358 if (pipe (sigpipe))
359 return 0;
360
361 ev_method = EVMETHOD_NONE;
362#if HAVE_EPOLL
363 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
364#endif
365#if HAVE_SELECT
366 if (ev_method == EVMETHOD_NONE) select_init (flags);
367#endif
368
369 if (ev_method) 477 if (!ev_method)
478 {
479#if EV_USE_MONOTONIC
370 { 480 {
481 struct timespec ts;
482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
483 have_monotonic = 1;
484 }
485#endif
486
487 ev_now = ev_time ();
488 now = get_clock ();
489 now_floor = now;
490 diff = ev_now - now;
491
492 if (pipe (sigpipe))
493 return 0;
494
495 ev_method = EVMETHOD_NONE;
496#if EV_USE_EPOLL
497 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
498#endif
499#if EV_USE_SELECT
500 if (ev_method == EVMETHOD_NONE) select_init (flags);
501#endif
502
503 if (ev_method)
504 {
371 evw_init (&sigev, sigcb); 505 ev_watcher_init (&sigev, sigcb);
372 siginit (); 506 siginit ();
507
508 ev_signal_init (&childev, childcb, SIGCHLD);
509 ev_signal_start (&childev);
510 }
373 } 511 }
374 512
375 return ev_method; 513 return ev_method;
376} 514}
377 515
378/*****************************************************************************/ 516/*****************************************************************************/
379 517
380void ev_prefork (void) 518void
519ev_fork_prepare (void)
381{ 520{
382 /* nop */ 521 /* nop */
383} 522}
384 523
524void
385void ev_postfork_parent (void) 525ev_fork_parent (void)
386{ 526{
387 /* nop */ 527 /* nop */
388} 528}
389 529
530void
390void ev_postfork_child (void) 531ev_fork_child (void)
391{ 532{
392#if HAVE_EPOLL 533#if EV_USE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 534 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 535 epoll_postfork_child ();
395#endif 536#endif
396 537
397 evio_stop (&sigev); 538 ev_io_stop (&sigev);
398 close (sigpipe [0]); 539 close (sigpipe [0]);
399 close (sigpipe [1]); 540 close (sigpipe [1]);
400 pipe (sigpipe); 541 pipe (sigpipe);
401 siginit (); 542 siginit ();
402} 543}
403 544
404/*****************************************************************************/ 545/*****************************************************************************/
405 546
406static void 547static void
407fd_reify (void)
408{
409 int i;
410
411 for (i = 0; i < fdchangecnt; ++i)
412 {
413 int fd = fdchanges [i];
414 ANFD *anfd = anfds + fd;
415 struct ev_io *w;
416
417 int wev = 0;
418
419 for (w = anfd->head; w; w = w->next)
420 wev |= w->events;
421
422 if (anfd->wev != wev)
423 {
424 method_modify (fd, anfd->wev, wev);
425 anfd->wev = wev;
426 }
427 }
428
429 fdchangecnt = 0;
430}
431
432static void
433call_pending () 548call_pending (void)
434{ 549{
435 while (pendingcnt) 550 while (pendingcnt)
436 { 551 {
437 ANPENDING *p = pendings + --pendingcnt; 552 ANPENDING *p = pendings + --pendingcnt;
438 553
443 } 558 }
444 } 559 }
445} 560}
446 561
447static void 562static void
448timers_reify () 563timers_reify (void)
449{ 564{
450 while (timercnt && timers [0]->at <= now) 565 while (timercnt && timers [0]->at <= now)
451 { 566 {
452 struct ev_timer *w = timers [0]; 567 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
455 568
456 /* first reschedule or stop timer */ 569 /* first reschedule or stop timer */
457 if (w->repeat) 570 if (w->repeat)
458 { 571 {
572 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
459 w->at = now + w->repeat; 573 w->at = now + w->repeat;
460 assert (("timer timeout in the past, negative repeat?", w->at > now));
461 downheap ((WT *)timers, timercnt, 0); 574 downheap ((WT *)timers, timercnt, 0);
462 } 575 }
463 else 576 else
464 evtimer_stop (w); /* nonrepeating: stop timer */ 577 ev_timer_stop (w); /* nonrepeating: stop timer */
465 }
466}
467 578
579 event ((W)w, EV_TIMEOUT);
580 }
581}
582
468static void 583static void
469periodics_reify () 584periodics_reify (void)
470{ 585{
471 while (periodiccnt && periodics [0]->at <= ev_now) 586 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 587 {
473 struct ev_periodic *w = periodics [0]; 588 struct ev_periodic *w = periodics [0];
474 589
475 /* first reschedule or stop timer */ 590 /* first reschedule or stop timer */
476 if (w->interval) 591 if (w->interval)
477 { 592 {
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 593 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
479 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 594 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0); 595 downheap ((WT *)periodics, periodiccnt, 0);
481 } 596 }
482 else 597 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */ 598 ev_periodic_stop (w); /* nonrepeating: stop timer */
484 599
485 event ((W)w, EV_TIMEOUT); 600 event ((W)w, EV_PERIODIC);
486 } 601 }
487} 602}
488 603
489static void 604static void
490periodics_reschedule (ev_tstamp diff) 605periodics_reschedule (ev_tstamp diff)
500 { 615 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 616 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502 617
503 if (fabs (diff) >= 1e-4) 618 if (fabs (diff) >= 1e-4)
504 { 619 {
505 evperiodic_stop (w); 620 ev_periodic_stop (w);
506 evperiodic_start (w); 621 ev_periodic_start (w);
507 622
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 623 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 } 624 }
510 } 625 }
511 } 626 }
512} 627}
513 628
514static void 629static int
630time_update_monotonic (void)
631{
632 now = get_clock ();
633
634 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
635 {
636 ev_now = now + diff;
637 return 0;
638 }
639 else
640 {
641 now_floor = now;
642 ev_now = ev_time ();
643 return 1;
644 }
645}
646
647static void
515time_update () 648time_update (void)
516{ 649{
517 int i; 650 int i;
518 651
519 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
520
521 if (have_monotonic) 653 if (expect_true (have_monotonic))
522 { 654 {
655 if (time_update_monotonic ())
656 {
523 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
524 658
525 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 659 for (i = 4; --i; ) /* loop a few times, before making important decisions */
526 { 660 {
527 now = get_clock ();
528 diff = ev_now - now; 661 diff = ev_now - now;
529 662
530 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
531 return; /* all is well */ 664 return; /* all is well */
532 665
533 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
534 } 669 }
535 670
536 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
537 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
538 } 674 }
539 else 675 else
676#endif
540 { 677 {
678 ev_now = ev_time ();
679
541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 680 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
542 { 681 {
543 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
544 683
545 /* adjust timers. this is easy, as the offset is the same for all */ 684 /* adjust timers. this is easy, as the offset is the same for all */
546 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
554int ev_loop_done; 693int ev_loop_done;
555 694
556void ev_loop (int flags) 695void ev_loop (int flags)
557{ 696{
558 double block; 697 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 699
561 do 700 do
562 { 701 {
563 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
564 if (checkcnt) 703 if (expect_false (preparecnt))
565 { 704 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending (); 706 call_pending ();
568 } 707 }
569 708
570 /* update fd-related kernel structures */ 709 /* update fd-related kernel structures */
571 fd_reify (); 710 fd_reify ();
572 711
573 /* calculate blocking time */ 712 /* calculate blocking time */
574 713
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 714 /* we only need this for !monotonic clockor timers, but as we basically
715 always have timers, we just calculate it always */
716#if EV_USE_MONOTONIC
717 if (expect_true (have_monotonic))
718 time_update_monotonic ();
719 else
720#endif
721 {
576 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
577 725
578 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
579 block = 0.; 727 block = 0.;
580 else 728 else
581 { 729 {
582 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
583 731
584 if (timercnt) 732 if (timercnt)
585 { 733 {
586 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
587 if (block > to) block = to; 735 if (block > to) block = to;
588 } 736 }
589 737
590 if (periodiccnt) 738 if (periodiccnt)
591 { 739 {
644 head = &(*head)->next; 792 head = &(*head)->next;
645 } 793 }
646} 794}
647 795
648static void 796static void
649ev_clear (W w) 797ev_clear_pending (W w)
650{ 798{
651 if (w->pending) 799 if (w->pending)
652 { 800 {
653 pendings [w->pending - 1].w = 0; 801 pendings [w->pending - 1].w = 0;
654 w->pending = 0; 802 w->pending = 0;
668} 816}
669 817
670/*****************************************************************************/ 818/*****************************************************************************/
671 819
672void 820void
673evio_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
674{ 822{
823 int fd = w->fd;
824
675 if (ev_is_active (w)) 825 if (ev_is_active (w))
676 return; 826 return;
677 827
678 int fd = w->fd; 828 assert (("ev_io_start called with negative fd", fd >= 0));
679 829
680 ev_start ((W)w, 1); 830 ev_start ((W)w, 1);
681 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
682 wlist_add ((WL *)&anfds[fd].head, (WL)w); 832 wlist_add ((WL *)&anfds[fd].head, (WL)w);
683 833
684 ++fdchangecnt; 834 fd_change (fd);
685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
686 fdchanges [fdchangecnt - 1] = fd;
687} 835}
688 836
689void 837void
690evio_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
691{ 839{
692 ev_clear ((W)w); 840 ev_clear_pending ((W)w);
693 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
694 return; 842 return;
695 843
696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
697 ev_stop ((W)w); 845 ev_stop ((W)w);
698 846
699 ++fdchangecnt; 847 fd_change (w->fd);
700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
701 fdchanges [fdchangecnt - 1] = w->fd;
702} 848}
703 849
704void 850void
705evtimer_start (struct ev_timer *w) 851ev_timer_start (struct ev_timer *w)
706{ 852{
707 if (ev_is_active (w)) 853 if (ev_is_active (w))
708 return; 854 return;
709 855
710 w->at += now; 856 w->at += now;
711 857
712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 858 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
713 859
714 ev_start ((W)w, ++timercnt); 860 ev_start ((W)w, ++timercnt);
715 array_needsize (timers, timermax, timercnt, ); 861 array_needsize (timers, timermax, timercnt, );
716 timers [timercnt - 1] = w; 862 timers [timercnt - 1] = w;
717 upheap ((WT *)timers, timercnt - 1); 863 upheap ((WT *)timers, timercnt - 1);
718} 864}
719 865
720void 866void
721evtimer_stop (struct ev_timer *w) 867ev_timer_stop (struct ev_timer *w)
722{ 868{
723 ev_clear ((W)w); 869 ev_clear_pending ((W)w);
724 if (!ev_is_active (w)) 870 if (!ev_is_active (w))
725 return; 871 return;
726 872
727 if (w->active < timercnt--) 873 if (w->active < timercnt--)
728 { 874 {
734 880
735 ev_stop ((W)w); 881 ev_stop ((W)w);
736} 882}
737 883
738void 884void
739evtimer_again (struct ev_timer *w) 885ev_timer_again (struct ev_timer *w)
740{ 886{
741 if (ev_is_active (w)) 887 if (ev_is_active (w))
742 { 888 {
743 if (w->repeat) 889 if (w->repeat)
744 { 890 {
745 w->at = now + w->repeat; 891 w->at = now + w->repeat;
746 downheap ((WT *)timers, timercnt, w->active - 1); 892 downheap ((WT *)timers, timercnt, w->active - 1);
747 } 893 }
748 else 894 else
749 evtimer_stop (w); 895 ev_timer_stop (w);
750 } 896 }
751 else if (w->repeat) 897 else if (w->repeat)
752 evtimer_start (w); 898 ev_timer_start (w);
753} 899}
754 900
755void 901void
756evperiodic_start (struct ev_periodic *w) 902ev_periodic_start (struct ev_periodic *w)
757{ 903{
758 if (ev_is_active (w)) 904 if (ev_is_active (w))
759 return; 905 return;
760 906
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 907 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
762 908
763 /* this formula differs from the one in periodic_reify because we do not always round up */ 909 /* this formula differs from the one in periodic_reify because we do not always round up */
764 if (w->interval) 910 if (w->interval)
765 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
766 912
769 periodics [periodiccnt - 1] = w; 915 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1); 916 upheap ((WT *)periodics, periodiccnt - 1);
771} 917}
772 918
773void 919void
774evperiodic_stop (struct ev_periodic *w) 920ev_periodic_stop (struct ev_periodic *w)
775{ 921{
776 ev_clear ((W)w); 922 ev_clear_pending ((W)w);
777 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
778 return; 924 return;
779 925
780 if (w->active < periodiccnt--) 926 if (w->active < periodiccnt--)
781 { 927 {
785 931
786 ev_stop ((W)w); 932 ev_stop ((W)w);
787} 933}
788 934
789void 935void
790evsignal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
791{ 937{
792 if (ev_is_active (w)) 938 if (ev_is_active (w))
793 return; 939 return;
940
941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
794 942
795 ev_start ((W)w, 1); 943 ev_start ((W)w, 1);
796 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
797 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
798 946
805 sigaction (w->signum, &sa, 0); 953 sigaction (w->signum, &sa, 0);
806 } 954 }
807} 955}
808 956
809void 957void
810evsignal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
811{ 959{
812 ev_clear ((W)w); 960 ev_clear_pending ((W)w);
813 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
814 return; 962 return;
815 963
816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
817 ev_stop ((W)w); 965 ev_stop ((W)w);
818 966
819 if (!signals [w->signum - 1].head) 967 if (!signals [w->signum - 1].head)
820 signal (w->signum, SIG_DFL); 968 signal (w->signum, SIG_DFL);
821} 969}
822 970
971void
823void evidle_start (struct ev_idle *w) 972ev_idle_start (struct ev_idle *w)
824{ 973{
825 if (ev_is_active (w)) 974 if (ev_is_active (w))
826 return; 975 return;
827 976
828 ev_start ((W)w, ++idlecnt); 977 ev_start ((W)w, ++idlecnt);
829 array_needsize (idles, idlemax, idlecnt, ); 978 array_needsize (idles, idlemax, idlecnt, );
830 idles [idlecnt - 1] = w; 979 idles [idlecnt - 1] = w;
831} 980}
832 981
982void
833void evidle_stop (struct ev_idle *w) 983ev_idle_stop (struct ev_idle *w)
834{ 984{
835 ev_clear ((W)w); 985 ev_clear_pending ((W)w);
836 if (ev_is_active (w)) 986 if (ev_is_active (w))
837 return; 987 return;
838 988
839 idles [w->active - 1] = idles [--idlecnt]; 989 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w); 990 ev_stop ((W)w);
841} 991}
842 992
993void
843void evprepare_start (struct ev_prepare *w) 994ev_prepare_start (struct ev_prepare *w)
844{ 995{
845 if (ev_is_active (w)) 996 if (ev_is_active (w))
846 return; 997 return;
847 998
848 ev_start ((W)w, ++preparecnt); 999 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, ); 1000 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w; 1001 prepares [preparecnt - 1] = w;
851} 1002}
852 1003
1004void
853void evprepare_stop (struct ev_prepare *w) 1005ev_prepare_stop (struct ev_prepare *w)
854{ 1006{
855 ev_clear ((W)w); 1007 ev_clear_pending ((W)w);
856 if (ev_is_active (w)) 1008 if (ev_is_active (w))
857 return; 1009 return;
858 1010
859 prepares [w->active - 1] = prepares [--preparecnt]; 1011 prepares [w->active - 1] = prepares [--preparecnt];
860 ev_stop ((W)w); 1012 ev_stop ((W)w);
861} 1013}
862 1014
1015void
863void evcheck_start (struct ev_check *w) 1016ev_check_start (struct ev_check *w)
864{ 1017{
865 if (ev_is_active (w)) 1018 if (ev_is_active (w))
866 return; 1019 return;
867 1020
868 ev_start ((W)w, ++checkcnt); 1021 ev_start ((W)w, ++checkcnt);
869 array_needsize (checks, checkmax, checkcnt, ); 1022 array_needsize (checks, checkmax, checkcnt, );
870 checks [checkcnt - 1] = w; 1023 checks [checkcnt - 1] = w;
871} 1024}
872 1025
1026void
873void evcheck_stop (struct ev_check *w) 1027ev_check_stop (struct ev_check *w)
874{ 1028{
875 ev_clear ((W)w); 1029 ev_clear_pending ((W)w);
876 if (ev_is_active (w)) 1030 if (ev_is_active (w))
877 return; 1031 return;
878 1032
879 checks [w->active - 1] = checks [--checkcnt]; 1033 checks [w->active - 1] = checks [--checkcnt];
1034 ev_stop ((W)w);
1035}
1036
1037void
1038ev_child_start (struct ev_child *w)
1039{
1040 if (ev_is_active (w))
1041 return;
1042
1043 ev_start ((W)w, 1);
1044 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1045}
1046
1047void
1048ev_child_stop (struct ev_child *w)
1049{
1050 ev_clear_pending ((W)w);
1051 if (ev_is_active (w))
1052 return;
1053
1054 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
880 ev_stop ((W)w); 1055 ev_stop ((W)w);
881} 1056}
882 1057
883/*****************************************************************************/ 1058/*****************************************************************************/
884 1059
894once_cb (struct ev_once *once, int revents) 1069once_cb (struct ev_once *once, int revents)
895{ 1070{
896 void (*cb)(int revents, void *arg) = once->cb; 1071 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg; 1072 void *arg = once->arg;
898 1073
899 evio_stop (&once->io); 1074 ev_io_stop (&once->io);
900 evtimer_stop (&once->to); 1075 ev_timer_stop (&once->to);
901 free (once); 1076 free (once);
902 1077
903 cb (revents, arg); 1078 cb (revents, arg);
904} 1079}
905 1080
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1094ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{ 1095{
921 struct ev_once *once = malloc (sizeof (struct ev_once)); 1096 struct ev_once *once = malloc (sizeof (struct ev_once));
922 1097
923 if (!once) 1098 if (!once)
924 cb (EV_ERROR, arg); 1099 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
925 else 1100 else
926 { 1101 {
927 once->cb = cb; 1102 once->cb = cb;
928 once->arg = arg; 1103 once->arg = arg;
929 1104
930 evw_init (&once->io, once_cb_io); 1105 ev_watcher_init (&once->io, once_cb_io);
931
932 if (fd >= 0) 1106 if (fd >= 0)
933 { 1107 {
934 evio_set (&once->io, fd, events); 1108 ev_io_set (&once->io, fd, events);
935 evio_start (&once->io); 1109 ev_io_start (&once->io);
936 } 1110 }
937 1111
938 evw_init (&once->to, once_cb_to); 1112 ev_watcher_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.) 1113 if (timeout >= 0.)
941 { 1114 {
942 evtimer_set (&once->to, timeout, 0.); 1115 ev_timer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to); 1116 ev_timer_start (&once->to);
944 } 1117 }
945 } 1118 }
946} 1119}
947 1120
948/*****************************************************************************/ 1121/*****************************************************************************/
959 1132
960static void 1133static void
961ocb (struct ev_timer *w, int revents) 1134ocb (struct ev_timer *w, int revents)
962{ 1135{
963 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1136 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
964 evtimer_stop (w); 1137 ev_timer_stop (w);
965 evtimer_start (w); 1138 ev_timer_start (w);
966} 1139}
967 1140
968static void 1141static void
969scb (struct ev_signal *w, int revents) 1142scb (struct ev_signal *w, int revents)
970{ 1143{
971 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1144 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio); 1145 ev_io_stop (&wio);
973 evio_start (&wio); 1146 ev_io_start (&wio);
974} 1147}
975 1148
976static void 1149static void
977gcb (struct ev_signal *w, int revents) 1150gcb (struct ev_signal *w, int revents)
978{ 1151{
982 1155
983int main (void) 1156int main (void)
984{ 1157{
985 ev_init (0); 1158 ev_init (0);
986 1159
987 evio_init (&wio, sin_cb, 0, EV_READ); 1160 ev_io_init (&wio, sin_cb, 0, EV_READ);
988 evio_start (&wio); 1161 ev_io_start (&wio);
989 1162
990 struct ev_timer t[10000]; 1163 struct ev_timer t[10000];
991 1164
992#if 0 1165#if 0
993 int i; 1166 int i;
994 for (i = 0; i < 10000; ++i) 1167 for (i = 0; i < 10000; ++i)
995 { 1168 {
996 struct ev_timer *w = t + i; 1169 struct ev_timer *w = t + i;
997 evw_init (w, ocb, i); 1170 ev_watcher_init (w, ocb, i);
998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1171 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
999 evtimer_start (w); 1172 ev_timer_start (w);
1000 if (drand48 () < 0.5) 1173 if (drand48 () < 0.5)
1001 evtimer_stop (w); 1174 ev_timer_stop (w);
1002 } 1175 }
1003#endif 1176#endif
1004 1177
1005 struct ev_timer t1; 1178 struct ev_timer t1;
1006 evtimer_init (&t1, ocb, 5, 10); 1179 ev_timer_init (&t1, ocb, 5, 10);
1007 evtimer_start (&t1); 1180 ev_timer_start (&t1);
1008 1181
1009 struct ev_signal sig; 1182 struct ev_signal sig;
1010 evsignal_init (&sig, scb, SIGQUIT); 1183 ev_signal_init (&sig, scb, SIGQUIT);
1011 evsignal_start (&sig); 1184 ev_signal_start (&sig);
1012 1185
1013 struct ev_check cw; 1186 struct ev_check cw;
1014 evcheck_init (&cw, gcb); 1187 ev_check_init (&cw, gcb);
1015 evcheck_start (&cw); 1188 ev_check_start (&cw);
1016 1189
1017 struct ev_idle iw; 1190 struct ev_idle iw;
1018 evidle_init (&iw, gcb); 1191 ev_idle_init (&iw, gcb);
1019 evidle_start (&iw); 1192 ev_idle_start (&iw);
1020 1193
1021 ev_loop (0); 1194 ev_loop (0);
1022 1195
1023 return 0; 1196 return 0;
1024} 1197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines