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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines