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

Comparing libev/ev.c (file contents):
Revision 1.12 by root, Wed Oct 31 09:23:17 2007 UTC vs.
Revision 1.34 by root, Thu Nov 1 11:43:11 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines