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

Comparing libev/ev.c (file contents):
Revision 1.14 by root, Wed Oct 31 11:52:12 2007 UTC vs.
Revision 1.40 by root, Fri Nov 2 11:02:23 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines