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

Comparing libev/ev.c (file contents):
Revision 1.8 by root, Wed Oct 31 00:32:33 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
51/**/
52
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
14#ifdef CLOCK_MONOTONIC 71#ifndef CLOCK_MONOTONIC
72# undef EV_USE_MONOTONIC
15# define HAVE_MONOTONIC 1 73# define EV_USE_MONOTONIC 0
16#endif 74#endif
17 75
76#ifndef CLOCK_REALTIME
77# undef EV_USE_REALTIME
18#define HAVE_REALTIME 1 78# define EV_USE_REALTIME 0
19#define HAVE_EPOLL 1 79#endif
20#define HAVE_SELECT 1 80
81/**/
21 82
22#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) */
23#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 */
24 87
25#include "ev.h" 88#include "ev.h"
26 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)
100
27struct ev_watcher { 101typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 102typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 103typedef struct ev_watcher_time *WT;
33};
34 104
35static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
36ev_tstamp ev_now; 106ev_tstamp ev_now;
37int ev_method; 107int ev_method;
38 108
39static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
40 110
45/*****************************************************************************/ 115/*****************************************************************************/
46 116
47ev_tstamp 117ev_tstamp
48ev_time (void) 118ev_time (void)
49{ 119{
50#if HAVE_REALTIME 120#if EV_USE_REALTIME
51 struct timespec ts; 121 struct timespec ts;
52 clock_gettime (CLOCK_REALTIME, &ts); 122 clock_gettime (CLOCK_REALTIME, &ts);
53 return ts.tv_sec + ts.tv_nsec * 1e-9; 123 return ts.tv_sec + ts.tv_nsec * 1e-9;
54#else 124#else
55 struct timeval tv; 125 struct timeval tv;
59} 129}
60 130
61static ev_tstamp 131static ev_tstamp
62get_clock (void) 132get_clock (void)
63{ 133{
64#if HAVE_MONOTONIC 134#if EV_USE_MONOTONIC
65 if (have_monotonic) 135 if (expect_true (have_monotonic))
66 { 136 {
67 struct timespec ts; 137 struct timespec ts;
68 clock_gettime (CLOCK_MONOTONIC, &ts); 138 clock_gettime (CLOCK_MONOTONIC, &ts);
69 return ts.tv_sec + ts.tv_nsec * 1e-9; 139 return ts.tv_sec + ts.tv_nsec * 1e-9;
70 } 140 }
71#endif 141#endif
72 142
73 return ev_time (); 143 return ev_time ();
74} 144}
75 145
146#define array_roundsize(base,n) ((n) | 4 & ~3)
147
76#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
77 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
78 { \ 150 { \
79 int newcnt = cur ? cur << 1 : 16; \ 151 int newcnt = cur; \
80 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 \
81 base = realloc (base, sizeof (*base) * (newcnt)); \ 158 base = realloc (base, sizeof (*base) * (newcnt)); \
82 init (base + cur, newcnt - cur); \ 159 init (base + cur, newcnt - cur); \
83 cur = newcnt; \ 160 cur = newcnt; \
84 } 161 }
85 162
86/*****************************************************************************/ 163/*****************************************************************************/
87 164
88typedef struct 165typedef struct
89{ 166{
90 struct ev_io *head; 167 struct ev_io *head;
91 unsigned char wev, rev; /* want, received event set */ 168 unsigned char events;
169 unsigned char reify;
92} ANFD; 170} ANFD;
93 171
94static ANFD *anfds; 172static ANFD *anfds;
95static int anfdmax; 173static int anfdmax;
96 174
97static int *fdchanges;
98static int fdchangemax, fdchangecnt;
99
100static void 175static void
101anfds_init (ANFD *base, int count) 176anfds_init (ANFD *base, int count)
102{ 177{
103 while (count--) 178 while (count--)
104 { 179 {
105 base->head = 0; 180 base->head = 0;
106 base->wev = base->rev = EV_NONE; 181 base->events = EV_NONE;
182 base->reify = 0;
183
107 ++base; 184 ++base;
108 } 185 }
109} 186}
110 187
111typedef struct 188typedef struct
112{ 189{
113 struct ev_watcher *w; 190 W w;
114 int events; 191 int events;
115} ANPENDING; 192} ANPENDING;
116 193
117static ANPENDING *pendings; 194static ANPENDING *pendings;
118static int pendingmax, pendingcnt; 195static int pendingmax, pendingcnt;
119 196
120static void 197static void
121event (struct ev_watcher *w, int events) 198event (W w, int events)
122{ 199{
200 if (w->pending)
201 {
202 pendings [w->pending - 1].events |= events;
203 return;
204 }
205
123 w->pending = ++pendingcnt; 206 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, ); 207 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w; 208 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events; 209 pendings [pendingcnt - 1].events = events;
127} 210}
128 211
129static 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
130fd_event (int fd, int events) 222fd_event (int fd, int events)
131{ 223{
132 ANFD *anfd = anfds + fd; 224 ANFD *anfd = anfds + fd;
133 struct ev_io *w; 225 struct ev_io *w;
134 226
135 for (w = anfd->head; w; w = w->next) 227 for (w = anfd->head; w; w = w->next)
136 { 228 {
137 int ev = w->events & events; 229 int ev = w->events & events;
138 230
139 if (ev) 231 if (ev)
140 event ((struct ev_watcher *)w, ev); 232 event ((W)w, ev);
233 }
234}
235
236/*****************************************************************************/
237
238static int *fdchanges;
239static int fdchangemax, fdchangecnt;
240
241static void
242fd_reify (void)
243{
244 int i;
245
246 for (i = 0; i < fdchangecnt; ++i)
141 } 247 {
142} 248 int fd = fdchanges [i];
249 ANFD *anfd = anfds + fd;
250 struct ev_io *w;
143 251
144/*****************************************************************************/ 252 int events = 0;
145 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 }
296}
297
298/*****************************************************************************/
299
146static struct ev_timer **atimers; 300static struct ev_timer **timers;
147static int atimermax, atimercnt; 301static int timermax, timercnt;
148 302
149static struct ev_timer **rtimers; 303static struct ev_periodic **periodics;
150static int rtimermax, rtimercnt; 304static int periodicmax, periodiccnt;
151 305
152static void 306static void
153upheap (struct ev_timer **timers, int k) 307upheap (WT *timers, int k)
154{ 308{
155 struct ev_timer *w = timers [k]; 309 WT w = timers [k];
156 310
157 while (k && timers [k >> 1]->at > w->at) 311 while (k && timers [k >> 1]->at > w->at)
158 { 312 {
159 timers [k] = timers [k >> 1]; 313 timers [k] = timers [k >> 1];
160 timers [k]->active = k + 1; 314 timers [k]->active = k + 1;
165 timers [k]->active = k + 1; 319 timers [k]->active = k + 1;
166 320
167} 321}
168 322
169static void 323static void
170downheap (struct ev_timer **timers, int N, int k) 324downheap (WT *timers, int N, int k)
171{ 325{
172 struct ev_timer *w = timers [k]; 326 WT w = timers [k];
173 327
174 while (k < (N >> 1)) 328 while (k < (N >> 1))
175 { 329 {
176 int j = k << 1; 330 int j = k << 1;
177 331
193/*****************************************************************************/ 347/*****************************************************************************/
194 348
195typedef struct 349typedef struct
196{ 350{
197 struct ev_signal *head; 351 struct ev_signal *head;
198 sig_atomic_t gotsig; 352 sig_atomic_t volatile gotsig;
199} ANSIG; 353} ANSIG;
200 354
201static ANSIG *signals; 355static ANSIG *signals;
202static int signalmax; 356static int signalmax;
203 357
204static int sigpipe [2]; 358static int sigpipe [2];
205static sig_atomic_t gotsig; 359static sig_atomic_t volatile gotsig;
206static struct ev_io sigev; 360static struct ev_io sigev;
207 361
208static void 362static void
209signals_init (ANSIG *base, int count) 363signals_init (ANSIG *base, int count)
210{ 364{
211 while (count--) 365 while (count--)
212 { 366 {
213 base->head = 0; 367 base->head = 0;
214 base->gotsig = 0; 368 base->gotsig = 0;
369
215 ++base; 370 ++base;
216 } 371 }
217} 372}
218 373
219static void 374static void
222 signals [signum - 1].gotsig = 1; 377 signals [signum - 1].gotsig = 1;
223 378
224 if (!gotsig) 379 if (!gotsig)
225 { 380 {
226 gotsig = 1; 381 gotsig = 1;
227 write (sigpipe [1], &gotsig, 1); 382 write (sigpipe [1], &signum, 1);
228 } 383 }
229} 384}
230 385
231static void 386static void
232sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
233{ 388{
234 struct ev_signal *w; 389 struct ev_signal *w;
235 int sig; 390 int signum;
236 391
392 read (sigpipe [0], &revents, 1);
237 gotsig = 0; 393 gotsig = 0;
238 read (sigpipe [0], &revents, 1);
239 394
240 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
241 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
242 { 397 {
243 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
244 399
245 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
246 event ((struct ev_watcher *)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
247 } 402 }
248} 403}
249 404
250static void 405static void
251siginit (void) 406siginit (void)
255 410
256 /* rather than sort out wether we really need nb, set it */ 411 /* rather than sort out wether we really need nb, set it */
257 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 412 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
258 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 413 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
259 414
260 evio_set (&sigev, sigpipe [0], EV_READ); 415 ev_io_set (&sigev, sigpipe [0], EV_READ);
261 evio_start (&sigev); 416 ev_io_start (&sigev);
262} 417}
263 418
264/*****************************************************************************/ 419/*****************************************************************************/
265 420
421static struct ev_idle **idles;
422static int idlemax, idlecnt;
423
424static struct ev_prepare **prepares;
425static int preparemax, preparecnt;
426
427static struct ev_check **checks;
428static int checkmax, checkcnt;
429
430/*****************************************************************************/
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
266#if HAVE_EPOLL 456#if EV_USE_EPOLL
267# include "ev_epoll.c" 457# include "ev_epoll.c"
268#endif 458#endif
269#if HAVE_SELECT 459#if EV_USE_SELECT
270# include "ev_select.c" 460# include "ev_select.c"
271#endif 461#endif
272 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
273int ev_init (int flags) 475int ev_init (int flags)
274{ 476{
275#if HAVE_MONOTONIC
276 {
277 struct timespec ts;
278 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
279 have_monotonic = 1;
280 }
281#endif
282
283 ev_now = ev_time ();
284 now = get_clock ();
285 diff = ev_now - now;
286
287 if (pipe (sigpipe))
288 return 0;
289
290 ev_method = EVMETHOD_NONE;
291#if HAVE_EPOLL
292 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
293#endif
294#if HAVE_SELECT
295 if (ev_method == EVMETHOD_NONE) select_init (flags);
296#endif
297
298 if (ev_method) 477 if (!ev_method)
478 {
479#if EV_USE_MONOTONIC
299 { 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 {
300 evw_init (&sigev, sigcb, 0); 505 ev_watcher_init (&sigev, sigcb);
301 siginit (); 506 siginit ();
507
508 ev_signal_init (&childev, childcb, SIGCHLD);
509 ev_signal_start (&childev);
510 }
302 } 511 }
303 512
304 return ev_method; 513 return ev_method;
305} 514}
306 515
307/*****************************************************************************/ 516/*****************************************************************************/
308 517
309void ev_prefork (void) 518void
519ev_fork_prepare (void)
310{ 520{
521 /* nop */
311} 522}
312 523
524void
313void ev_postfork_parent (void) 525ev_fork_parent (void)
314{ 526{
527 /* nop */
315} 528}
316 529
530void
317void ev_postfork_child (void) 531ev_fork_child (void)
318{ 532{
319#if HAVE_EPOLL 533#if EV_USE_EPOLL
320 if (ev_method == EVMETHOD_EPOLL) 534 if (ev_method == EVMETHOD_EPOLL)
321 epoll_postfork_child (); 535 epoll_postfork_child ();
322#endif 536#endif
323 537
324 evio_stop (&sigev); 538 ev_io_stop (&sigev);
325 close (sigpipe [0]); 539 close (sigpipe [0]);
326 close (sigpipe [1]); 540 close (sigpipe [1]);
327 pipe (sigpipe); 541 pipe (sigpipe);
328 siginit (); 542 siginit ();
329} 543}
330 544
331/*****************************************************************************/ 545/*****************************************************************************/
332 546
333static ev_hook hooks [EVHOOK_NUM];
334
335void
336ev_hook_register (int type, ev_hook hook)
337{
338 hooks [type] = hook;
339}
340
341void
342ev_hook_unregister (int type, ev_hook hook)
343{
344 hooks [type] = 0;
345}
346
347static void 547static void
348hook_call (int type)
349{
350 if (hooks [type])
351 hooks [type] ();
352}
353
354static void
355fd_reify (void)
356{
357 int i;
358
359 for (i = 0; i < fdchangecnt; ++i)
360 {
361 int fd = fdchanges [i];
362 ANFD *anfd = anfds + fd;
363 struct ev_io *w;
364
365 int wev = 0;
366
367 for (w = anfd->head; w; w = w->next)
368 wev |= w->events;
369
370 if (anfd->wev != wev)
371 {
372 method_modify (fd, anfd->wev, wev);
373 anfd->wev = wev;
374 }
375 }
376
377 fdchangecnt = 0;
378}
379
380static void
381call_pending () 548call_pending (void)
382{ 549{
383 int i; 550 while (pendingcnt)
384
385 for (i = 0; i < pendingcnt; ++i)
386 { 551 {
387 ANPENDING *p = pendings + i; 552 ANPENDING *p = pendings + --pendingcnt;
388 553
389 if (p->w) 554 if (p->w)
390 { 555 {
391 p->w->pending = 0; 556 p->w->pending = 0;
392 p->w->cb (p->w, p->events); 557 p->w->cb (p->w, p->events);
393 } 558 }
394 } 559 }
395
396 pendingcnt = 0;
397} 560}
398 561
399static void 562static void
400timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 563timers_reify (void)
401{ 564{
402 while (timercnt && timers [0]->at <= now) 565 while (timercnt && timers [0]->at <= now)
403 { 566 {
404 struct ev_timer *w = timers [0]; 567 struct ev_timer *w = timers [0];
405 568
406 /* first reschedule or stop timer */ 569 /* first reschedule or stop timer */
407 if (w->repeat) 570 if (w->repeat)
408 { 571 {
409 if (w->is_abs) 572 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
410 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
411 else
412 w->at = now + w->repeat; 573 w->at = now + w->repeat;
413
414 assert (w->at > now);
415
416 downheap (timers, timercnt, 0); 574 downheap ((WT *)timers, timercnt, 0);
417 } 575 }
418 else 576 else
419 {
420 evtimer_stop (w); /* nonrepeating: stop timer */ 577 ev_timer_stop (w); /* nonrepeating: stop timer */
421 --timercnt; /* maybe pass by reference instead? */ 578
579 event ((W)w, EV_TIMEOUT);
580 }
581}
582
583static void
584periodics_reify (void)
585{
586 while (periodiccnt && periodics [0]->at <= ev_now)
587 {
588 struct ev_periodic *w = periodics [0];
589
590 /* first reschedule or stop timer */
591 if (w->interval)
422 } 592 {
593 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
594 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
595 downheap ((WT *)periodics, periodiccnt, 0);
596 }
597 else
598 ev_periodic_stop (w); /* nonrepeating: stop timer */
423 599
424 event ((struct ev_watcher *)w, EV_TIMEOUT); 600 event ((W)w, EV_PERIODIC);
425 } 601 }
426} 602}
427 603
428static void 604static void
429time_update () 605periodics_reschedule (ev_tstamp diff)
430{ 606{
431 int i; 607 int i;
432 ev_now = ev_time ();
433 608
434 if (have_monotonic) 609 /* adjust periodics after time jump */
610 for (i = 0; i < periodiccnt; ++i)
435 { 611 {
436 ev_tstamp odiff = diff; 612 struct ev_periodic *w = periodics [i];
437 613
438 /* detecting time jumps is much more difficult */ 614 if (w->interval)
439 for (i = 2; --i; ) /* loop a few times, before making important decisions */
440 {
441 now = get_clock ();
442 diff = ev_now - now;
443
444 if (fabs (odiff - diff) < MIN_TIMEJUMP)
445 return; /* all is well */
446
447 ev_now = ev_time ();
448 } 615 {
616 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
449 617
450 /* time jump detected, reschedule atimers */ 618 if (fabs (diff) >= 1e-4)
451 for (i = 0; i < atimercnt; ++i) 619 {
620 ev_periodic_stop (w);
621 ev_periodic_start (w);
622
623 i = 0; /* restart loop, inefficient, but time jumps should be rare */
624 }
452 { 625 }
453 struct ev_timer *w = atimers [i]; 626 }
454 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; 627}
455 } 628
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;
456 } 638 }
457 else 639 else
458 { 640 {
641 now_floor = now;
642 ev_now = ev_time ();
643 return 1;
644 }
645}
646
647static void
648time_update (void)
649{
650 int i;
651
652#if EV_USE_MONOTONIC
653 if (expect_true (have_monotonic))
654 {
655 if (time_update_monotonic ())
656 {
657 ev_tstamp odiff = diff;
658
659 for (i = 4; --i; ) /* loop a few times, before making important decisions */
660 {
661 diff = ev_now - now;
662
663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
664 return; /* all is well */
665
666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
669 }
670
671 periodics_reschedule (diff - odiff);
672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
674 }
675 else
676#endif
677 {
678 ev_now = ev_time ();
679
459 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))
460 /* time jump detected, adjust rtimers */ 681 {
682 periodics_reschedule (ev_now - now);
683
684 /* adjust timers. this is easy, as the offset is the same for all */
461 for (i = 0; i < rtimercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
462 rtimers [i]->at += ev_now - now; 686 timers [i]->at += diff;
687 }
463 688
464 now = ev_now; 689 now = ev_now;
465 } 690 }
466} 691}
467 692
468int ev_loop_done; 693int ev_loop_done;
469 694
470void ev_loop (int flags) 695void ev_loop (int flags)
471{ 696{
472 double block; 697 double block;
473 ev_loop_done = flags & EVLOOP_ONESHOT; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
474 699
475 do 700 do
476 { 701 {
477 hook_call (EVHOOK_PREPOLL); 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 }
478 708
479 /* update fd-related kernel structures */ 709 /* update fd-related kernel structures */
480 fd_reify (); 710 fd_reify ();
481 711
482 /* calculate blocking time */ 712 /* calculate blocking time */
713
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 {
722 ev_now = ev_time ();
723 now = ev_now;
724 }
725
483 if (flags & EVLOOP_NONBLOCK) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 727 block = 0.;
485 else 728 else
486 { 729 {
487 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
488 731
489 if (rtimercnt) 732 if (timercnt)
490 { 733 {
491 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
492 if (block > to) block = to; 735 if (block > to) block = to;
493 } 736 }
494 737
495 if (atimercnt) 738 if (periodiccnt)
496 { 739 {
497 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 740 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
498 if (block > to) block = to; 741 if (block > to) block = to;
499 } 742 }
500 743
501 if (block < 0.) block = 0.; 744 if (block < 0.) block = 0.;
502 } 745 }
504 method_poll (block); 747 method_poll (block);
505 748
506 /* update ev_now, do magic */ 749 /* update ev_now, do magic */
507 time_update (); 750 time_update ();
508 751
509 hook_call (EVHOOK_POSTPOLL);
510
511 /* put pending timers into pendign queue and reschedule them */ 752 /* queue pending timers and reschedule them */
512 /* absolute timers first */ 753 timers_reify (); /* relative timers called last */
513 timers_reify (atimers, atimercnt, ev_now); 754 periodics_reify (); /* absolute timers called first */
514 /* relative timers second */ 755
515 timers_reify (rtimers, rtimercnt, now); 756 /* queue idle watchers unless io or timers are pending */
757 if (!pendingcnt)
758 queue_events ((W *)idles, idlecnt, EV_IDLE);
759
760 /* queue check watchers, to be executed first */
761 if (checkcnt)
762 queue_events ((W *)checks, checkcnt, EV_CHECK);
516 763
517 call_pending (); 764 call_pending ();
518 } 765 }
519 while (!ev_loop_done); 766 while (!ev_loop_done);
520}
521 767
522/*****************************************************************************/ 768 if (ev_loop_done != 2)
769 ev_loop_done = 0;
770}
523 771
772/*****************************************************************************/
773
524static void 774static void
525wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 775wlist_add (WL *head, WL elem)
526{ 776{
527 elem->next = *head; 777 elem->next = *head;
528 *head = elem; 778 *head = elem;
529} 779}
530 780
531static void 781static void
532wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 782wlist_del (WL *head, WL elem)
533{ 783{
534 while (*head) 784 while (*head)
535 { 785 {
536 if (*head == elem) 786 if (*head == elem)
537 { 787 {
542 head = &(*head)->next; 792 head = &(*head)->next;
543 } 793 }
544} 794}
545 795
546static void 796static void
547ev_start (struct ev_watcher *w, int active) 797ev_clear_pending (W w)
548{ 798{
799 if (w->pending)
800 {
801 pendings [w->pending - 1].w = 0;
549 w->pending = 0; 802 w->pending = 0;
803 }
804}
805
806static void
807ev_start (W w, int active)
808{
550 w->active = active; 809 w->active = active;
551} 810}
552 811
553static void 812static void
554ev_stop (struct ev_watcher *w) 813ev_stop (W w)
555{ 814{
556 if (w->pending)
557 pendings [w->pending - 1].w = 0;
558
559 w->active = 0; 815 w->active = 0;
560 /* nop */
561} 816}
562 817
563/*****************************************************************************/ 818/*****************************************************************************/
564 819
565void 820void
566evio_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
567{ 822{
823 int fd = w->fd;
824
568 if (ev_is_active (w)) 825 if (ev_is_active (w))
569 return; 826 return;
570 827
571 int fd = w->fd; 828 assert (("ev_io_start called with negative fd", fd >= 0));
572 829
573 ev_start ((struct ev_watcher *)w, 1); 830 ev_start ((W)w, 1);
574 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
575 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 832 wlist_add ((WL *)&anfds[fd].head, (WL)w);
576 833
577 ++fdchangecnt; 834 fd_change (fd);
578 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
579 fdchanges [fdchangecnt - 1] = fd;
580} 835}
581 836
582void 837void
583evio_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
584{ 839{
840 ev_clear_pending ((W)w);
585 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
586 return; 842 return;
587 843
588 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
589 ev_stop ((struct ev_watcher *)w); 845 ev_stop ((W)w);
590 846
591 ++fdchangecnt; 847 fd_change (w->fd);
592 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
593 fdchanges [fdchangecnt - 1] = w->fd;
594} 848}
595 849
596void 850void
597evtimer_start (struct ev_timer *w) 851ev_timer_start (struct ev_timer *w)
598{ 852{
599 if (ev_is_active (w)) 853 if (ev_is_active (w))
600 return; 854 return;
601 855
602 if (w->is_abs) 856 w->at += now;
857
858 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
859
860 ev_start ((W)w, ++timercnt);
861 array_needsize (timers, timermax, timercnt, );
862 timers [timercnt - 1] = w;
863 upheap ((WT *)timers, timercnt - 1);
864}
865
866void
867ev_timer_stop (struct ev_timer *w)
868{
869 ev_clear_pending ((W)w);
870 if (!ev_is_active (w))
871 return;
872
873 if (w->active < timercnt--)
874 {
875 timers [w->active - 1] = timers [timercnt];
876 downheap ((WT *)timers, timercnt, w->active - 1);
603 { 877 }
604 /* this formula differs from the one in timer_reify becuse we do not round up */ 878
879 w->at = w->repeat;
880
881 ev_stop ((W)w);
882}
883
884void
885ev_timer_again (struct ev_timer *w)
886{
887 if (ev_is_active (w))
888 {
605 if (w->repeat) 889 if (w->repeat)
890 {
891 w->at = now + w->repeat;
892 downheap ((WT *)timers, timercnt, w->active - 1);
893 }
894 else
895 ev_timer_stop (w);
896 }
897 else if (w->repeat)
898 ev_timer_start (w);
899}
900
901void
902ev_periodic_start (struct ev_periodic *w)
903{
904 if (ev_is_active (w))
905 return;
906
907 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
908
909 /* this formula differs from the one in periodic_reify because we do not always round up */
910 if (w->interval)
606 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
607 912
608 ev_start ((struct ev_watcher *)w, ++atimercnt); 913 ev_start ((W)w, ++periodiccnt);
609 array_needsize (atimers, atimermax, atimercnt, ); 914 array_needsize (periodics, periodicmax, periodiccnt, );
610 atimers [atimercnt - 1] = w; 915 periodics [periodiccnt - 1] = w;
611 upheap (atimers, atimercnt - 1); 916 upheap ((WT *)periodics, periodiccnt - 1);
612 }
613 else
614 {
615 w->at += now;
616
617 ev_start ((struct ev_watcher *)w, ++rtimercnt);
618 array_needsize (rtimers, rtimermax, rtimercnt, );
619 rtimers [rtimercnt - 1] = w;
620 upheap (rtimers, rtimercnt - 1);
621 }
622
623} 917}
624 918
625void 919void
626evtimer_stop (struct ev_timer *w) 920ev_periodic_stop (struct ev_periodic *w)
627{ 921{
922 ev_clear_pending ((W)w);
628 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
629 return; 924 return;
630 925
631 if (w->is_abs)
632 {
633 if (w->active < atimercnt--) 926 if (w->active < periodiccnt--)
634 {
635 atimers [w->active - 1] = atimers [atimercnt];
636 downheap (atimers, atimercnt, w->active - 1);
637 }
638 } 927 {
639 else 928 periodics [w->active - 1] = periodics [periodiccnt];
929 downheap ((WT *)periodics, periodiccnt, w->active - 1);
640 { 930 }
641 if (w->active < rtimercnt--)
642 {
643 rtimers [w->active - 1] = rtimers [rtimercnt];
644 downheap (rtimers, rtimercnt, w->active - 1);
645 }
646 }
647 931
648 ev_stop ((struct ev_watcher *)w); 932 ev_stop ((W)w);
649} 933}
650 934
651void 935void
652evsignal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
653{ 937{
654 if (ev_is_active (w)) 938 if (ev_is_active (w))
655 return; 939 return;
656 940
657 ev_start ((struct ev_watcher *)w, 1); 941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
942
943 ev_start ((W)w, 1);
658 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
659 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
660 946
661 if (!w->next) 947 if (!w->next)
662 { 948 {
663 struct sigaction sa; 949 struct sigaction sa;
664 sa.sa_handler = sighandler; 950 sa.sa_handler = sighandler;
667 sigaction (w->signum, &sa, 0); 953 sigaction (w->signum, &sa, 0);
668 } 954 }
669} 955}
670 956
671void 957void
672evsignal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
673{ 959{
960 ev_clear_pending ((W)w);
674 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
675 return; 962 return;
676 963
677 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
678 ev_stop ((struct ev_watcher *)w); 965 ev_stop ((W)w);
679 966
680 if (!signals [w->signum - 1].head) 967 if (!signals [w->signum - 1].head)
681 signal (w->signum, SIG_DFL); 968 signal (w->signum, SIG_DFL);
682} 969}
683 970
971void
972ev_idle_start (struct ev_idle *w)
973{
974 if (ev_is_active (w))
975 return;
976
977 ev_start ((W)w, ++idlecnt);
978 array_needsize (idles, idlemax, idlecnt, );
979 idles [idlecnt - 1] = w;
980}
981
982void
983ev_idle_stop (struct ev_idle *w)
984{
985 ev_clear_pending ((W)w);
986 if (ev_is_active (w))
987 return;
988
989 idles [w->active - 1] = idles [--idlecnt];
990 ev_stop ((W)w);
991}
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
1016ev_check_start (struct ev_check *w)
1017{
1018 if (ev_is_active (w))
1019 return;
1020
1021 ev_start ((W)w, ++checkcnt);
1022 array_needsize (checks, checkmax, checkcnt, );
1023 checks [checkcnt - 1] = w;
1024}
1025
1026void
1027ev_check_stop (struct ev_check *w)
1028{
1029 ev_clear_pending ((W)w);
1030 if (ev_is_active (w))
1031 return;
1032
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);
1055 ev_stop ((W)w);
1056}
1057
684/*****************************************************************************/ 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
1121/*****************************************************************************/
1122
685#if 1 1123#if 0
1124
1125struct ev_io wio;
686 1126
687static void 1127static void
688sin_cb (struct ev_io *w, int revents) 1128sin_cb (struct ev_io *w, int revents)
689{ 1129{
690 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1130 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
692 1132
693static void 1133static void
694ocb (struct ev_timer *w, int revents) 1134ocb (struct ev_timer *w, int revents)
695{ 1135{
696 //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);
697 evtimer_stop (w); 1137 ev_timer_stop (w);
698 evtimer_start (w); 1138 ev_timer_start (w);
699} 1139}
700 1140
701static void 1141static void
702scb (struct ev_signal *w, int revents) 1142scb (struct ev_signal *w, int revents)
703{ 1143{
704 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1144 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1145 ev_io_stop (&wio);
1146 ev_io_start (&wio);
1147}
1148
1149static void
1150gcb (struct ev_signal *w, int revents)
1151{
1152 fprintf (stderr, "generic %x\n", revents);
1153
705} 1154}
706 1155
707int main (void) 1156int main (void)
708{ 1157{
709 struct ev_io sin;
710
711 ev_init (0); 1158 ev_init (0);
712 1159
713 evw_init (&sin, sin_cb, 55);
714 evio_set (&sin, 0, EV_READ); 1160 ev_io_init (&wio, sin_cb, 0, EV_READ);
715 evio_start (&sin); 1161 ev_io_start (&wio);
716 1162
717 struct ev_timer t[10000]; 1163 struct ev_timer t[10000];
718 1164
719#if 1 1165#if 0
720 int i; 1166 int i;
721 for (i = 0; i < 10000; ++i) 1167 for (i = 0; i < 10000; ++i)
722 { 1168 {
723 struct ev_timer *w = t + i; 1169 struct ev_timer *w = t + i;
724 evw_init (w, ocb, i); 1170 ev_watcher_init (w, ocb, i);
725 evtimer_set_abs (w, drand48 (), 0.99775533); 1171 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
726 evtimer_start (w); 1172 ev_timer_start (w);
727 if (drand48 () < 0.5) 1173 if (drand48 () < 0.5)
728 evtimer_stop (w); 1174 ev_timer_stop (w);
729 } 1175 }
730#endif 1176#endif
731 1177
732 struct ev_timer t1; 1178 struct ev_timer t1;
733 evw_init (&t1, ocb, 0); 1179 ev_timer_init (&t1, ocb, 5, 10);
734 evtimer_set_abs (&t1, 5, 10);
735 evtimer_start (&t1); 1180 ev_timer_start (&t1);
736 1181
737 struct ev_signal sig; 1182 struct ev_signal sig;
738 evw_init (&sig, scb, 65535);
739 evsignal_set (&sig, SIGQUIT); 1183 ev_signal_init (&sig, scb, SIGQUIT);
740 evsignal_start (&sig); 1184 ev_signal_start (&sig);
1185
1186 struct ev_check cw;
1187 ev_check_init (&cw, gcb);
1188 ev_check_start (&cw);
1189
1190 struct ev_idle iw;
1191 ev_idle_init (&iw, gcb);
1192 ev_idle_start (&iw);
741 1193
742 ev_loop (0); 1194 ev_loop (0);
743 1195
744 return 0; 1196 return 0;
745} 1197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines