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.33 by root, Thu Nov 1 11:11:22 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#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
15# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
16# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
17# endif 52# endif
18#endif 53#endif
19 54
20#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
21# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
22#endif 57#endif
23 58
24#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
25# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
26#endif 61#endif
27 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
28#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
29# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
30#endif 68#endif
31 69
32#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) */
33#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 */
34 74
35#include "ev.h" 75#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 76
45typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT;
47 80
48static ev_tstamp now, diff; /* monotonic clock */ 81static ev_tstamp now, diff; /* monotonic clock */
49ev_tstamp ev_now; 82ev_tstamp ev_now;
50int ev_method; 83int ev_method;
51 84
58/*****************************************************************************/ 91/*****************************************************************************/
59 92
60ev_tstamp 93ev_tstamp
61ev_time (void) 94ev_time (void)
62{ 95{
63#if HAVE_REALTIME 96#if EV_USE_REALTIME
64 struct timespec ts; 97 struct timespec ts;
65 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
66 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
67#else 100#else
68 struct timeval tv; 101 struct timeval tv;
72} 105}
73 106
74static ev_tstamp 107static ev_tstamp
75get_clock (void) 108get_clock (void)
76{ 109{
77#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
78 if (have_monotonic) 111 if (have_monotonic)
79 { 112 {
80 struct timespec ts; 113 struct timespec ts;
81 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
82 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
84#endif 117#endif
85 118
86 return ev_time (); 119 return ev_time ();
87} 120}
88 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
89#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
90 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
91 { \ 126 { \
92 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
93 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 \
94 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
96 cur = newcnt; \ 136 cur = newcnt; \
97 } 137 }
98 138
99/*****************************************************************************/ 139/*****************************************************************************/
100 140
101typedef struct 141typedef struct
102{ 142{
103 struct ev_io *head; 143 struct ev_io *head;
104 unsigned char wev, rev; /* want, received event set */ 144 unsigned char events;
145 unsigned char reify;
105} ANFD; 146} ANFD;
106 147
107static ANFD *anfds; 148static ANFD *anfds;
108static int anfdmax; 149static int anfdmax;
109 150
110static int *fdchanges;
111static int fdchangemax, fdchangecnt;
112
113static void 151static void
114anfds_init (ANFD *base, int count) 152anfds_init (ANFD *base, int count)
115{ 153{
116 while (count--) 154 while (count--)
117 { 155 {
118 base->head = 0; 156 base->head = 0;
119 base->wev = base->rev = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
120 ++base; 160 ++base;
121 } 161 }
122} 162}
123 163
124typedef struct 164typedef struct
131static int pendingmax, pendingcnt; 171static int pendingmax, pendingcnt;
132 172
133static void 173static void
134event (W w, int events) 174event (W w, int events)
135{ 175{
176 if (w->pending)
177 {
178 pendings [w->pending - 1].events |= events;
179 return;
180 }
181
136 w->pending = ++pendingcnt; 182 w->pending = ++pendingcnt;
137 array_needsize (pendings, pendingmax, pendingcnt, ); 183 array_needsize (pendings, pendingmax, pendingcnt, );
138 pendings [pendingcnt - 1].w = w; 184 pendings [pendingcnt - 1].w = w;
139 pendings [pendingcnt - 1].events = events; 185 pendings [pendingcnt - 1].events = events;
140} 186}
141 187
142static 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
143fd_event (int fd, int events) 198fd_event (int fd, int events)
144{ 199{
145 ANFD *anfd = anfds + fd; 200 ANFD *anfd = anfds + fd;
146 struct ev_io *w; 201 struct ev_io *w;
147 202
152 if (ev) 207 if (ev)
153 event ((W)w, ev); 208 event ((W)w, ev);
154 } 209 }
155} 210}
156 211
212/*****************************************************************************/
213
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
157static void 217static void
158queue_events (W *events, int eventcnt, int type) 218fd_reify (void)
159{ 219{
160 int i; 220 int i;
161 221
162 for (i = 0; i < eventcnt; ++i) 222 for (i = 0; i < fdchangecnt; ++i)
163 event (events [i], type); 223 {
164} 224 int fd = fdchanges [i];
225 ANFD *anfd = anfds + fd;
226 struct ev_io *w;
165 227
166/*****************************************************************************/ 228 int events = 0;
167 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 }
272}
273
274/*****************************************************************************/
275
168static struct ev_timer **atimers; 276static struct ev_timer **timers;
169static int atimermax, atimercnt; 277static int timermax, timercnt;
170 278
171static struct ev_timer **rtimers; 279static struct ev_periodic **periodics;
172static int rtimermax, rtimercnt; 280static int periodicmax, periodiccnt;
173 281
174static void 282static void
175upheap (struct ev_timer **timers, int k) 283upheap (WT *timers, int k)
176{ 284{
177 struct ev_timer *w = timers [k]; 285 WT w = timers [k];
178 286
179 while (k && timers [k >> 1]->at > w->at) 287 while (k && timers [k >> 1]->at > w->at)
180 { 288 {
181 timers [k] = timers [k >> 1]; 289 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1; 290 timers [k]->active = k + 1;
187 timers [k]->active = k + 1; 295 timers [k]->active = k + 1;
188 296
189} 297}
190 298
191static void 299static void
192downheap (struct ev_timer **timers, int N, int k) 300downheap (WT *timers, int N, int k)
193{ 301{
194 struct ev_timer *w = timers [k]; 302 WT w = timers [k];
195 303
196 while (k < (N >> 1)) 304 while (k < (N >> 1))
197 { 305 {
198 int j = k << 1; 306 int j = k << 1;
199 307
232{ 340{
233 while (count--) 341 while (count--)
234 { 342 {
235 base->head = 0; 343 base->head = 0;
236 base->gotsig = 0; 344 base->gotsig = 0;
345
237 ++base; 346 ++base;
238 } 347 }
239} 348}
240 349
241static void 350static void
277 386
278 /* rather than sort out wether we really need nb, set it */ 387 /* rather than sort out wether we really need nb, set it */
279 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
281 390
282 evio_set (&sigev, sigpipe [0], EV_READ); 391 ev_io_set (&sigev, sigpipe [0], EV_READ);
283 evio_start (&sigev); 392 ev_io_start (&sigev);
284} 393}
285 394
286/*****************************************************************************/ 395/*****************************************************************************/
287 396
288static struct ev_idle **idles; 397static struct ev_idle **idles;
289static int idlemax, idlecnt; 398static int idlemax, idlecnt;
290 399
400static struct ev_prepare **prepares;
401static int preparemax, preparecnt;
402
291static struct ev_check **checks; 403static struct ev_check **checks;
292static int checkmax, checkcnt; 404static int checkmax, checkcnt;
293 405
294/*****************************************************************************/ 406/*****************************************************************************/
295 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
296#if HAVE_EPOLL 432#if EV_USE_EPOLL
297# include "ev_epoll.c" 433# include "ev_epoll.c"
298#endif 434#endif
299#if HAVE_SELECT 435#if EV_USE_SELECT
300# include "ev_select.c" 436# include "ev_select.c"
301#endif 437#endif
302 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
303int ev_init (int flags) 451int ev_init (int flags)
304{ 452{
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) 453 if (!ev_method)
454 {
455#if EV_USE_MONOTONIC
329 { 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 {
330 evw_init (&sigev, sigcb, 0); 480 ev_watcher_init (&sigev, sigcb);
331 siginit (); 481 siginit ();
482
483 ev_signal_init (&childev, childcb, SIGCHLD);
484 ev_signal_start (&childev);
485 }
332 } 486 }
333 487
334 return ev_method; 488 return ev_method;
335} 489}
336 490
337/*****************************************************************************/ 491/*****************************************************************************/
338 492
493void
339void ev_prefork (void) 494ev_prefork (void)
340{ 495{
496 /* nop */
341} 497}
342 498
499void
343void ev_postfork_parent (void) 500ev_postfork_parent (void)
344{ 501{
502 /* nop */
345} 503}
346 504
505void
347void ev_postfork_child (void) 506ev_postfork_child (void)
348{ 507{
349#if HAVE_EPOLL 508#if EV_USE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
351 epoll_postfork_child (); 510 epoll_postfork_child ();
352#endif 511#endif
353 512
354 evio_stop (&sigev); 513 ev_io_stop (&sigev);
355 close (sigpipe [0]); 514 close (sigpipe [0]);
356 close (sigpipe [1]); 515 close (sigpipe [1]);
357 pipe (sigpipe); 516 pipe (sigpipe);
358 siginit (); 517 siginit ();
359} 518}
360 519
361/*****************************************************************************/ 520/*****************************************************************************/
362 521
363static void 522static 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 () 523call_pending (void)
391{ 524{
392 int i; 525 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 526 {
396 ANPENDING *p = pendings + i; 527 ANPENDING *p = pendings + --pendingcnt;
397 528
398 if (p->w) 529 if (p->w)
399 { 530 {
400 p->w->pending = 0; 531 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 532 p->w->cb (p->w, p->events);
402 } 533 }
403 } 534 }
404
405 pendingcnt = 0;
406} 535}
407 536
408static void 537static void
409timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 538timers_reify (void)
410{ 539{
411 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
412 { 541 {
413 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
414 543
415 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
416 if (w->repeat) 545 if (w->repeat)
417 { 546 {
418 if (w->is_abs) 547 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; 548 w->at = now + w->repeat;
422
423 assert (w->at > now);
424
425 downheap (timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
426 } 550 }
427 else 551 else
428 {
429 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
430 --timercnt; /* maybe pass by reference instead? */
431 }
432 553
433 event ((W)w, EV_TIMEOUT); 554 event ((W)w, EV_TIMEOUT);
434 } 555 }
435} 556}
436 557
437static void 558static void
438time_update () 559periodics_reify (void)
560{
561 while (periodiccnt && periodics [0]->at <= ev_now)
562 {
563 struct ev_periodic *w = periodics [0];
564
565 /* first reschedule or stop timer */
566 if (w->interval)
567 {
568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
570 downheap ((WT *)periodics, periodiccnt, 0);
571 }
572 else
573 ev_periodic_stop (w); /* nonrepeating: stop timer */
574
575 event ((W)w, EV_PERIODIC);
576 }
577}
578
579static void
580periodics_reschedule (ev_tstamp diff)
439{ 581{
440 int i; 582 int i;
583
584 /* adjust periodics after time jump */
585 for (i = 0; i < periodiccnt; ++i)
586 {
587 struct ev_periodic *w = periodics [i];
588
589 if (w->interval)
590 {
591 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
592
593 if (fabs (diff) >= 1e-4)
594 {
595 ev_periodic_stop (w);
596 ev_periodic_start (w);
597
598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
599 }
600 }
601 }
602}
603
604static void
605time_update (void)
606{
607 int i;
608
441 ev_now = ev_time (); 609 ev_now = ev_time ();
442 610
443 if (have_monotonic) 611 if (have_monotonic)
444 { 612 {
445 ev_tstamp odiff = diff; 613 ev_tstamp odiff = diff;
446 614
447 /* detecting time jumps is much more difficult */
448 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 615 for (i = 4; --i; ) /* loop a few times, before making important decisions */
449 { 616 {
450 now = get_clock (); 617 now = get_clock ();
451 diff = ev_now - now; 618 diff = ev_now - now;
452 619
453 if (fabs (odiff - diff) < MIN_TIMEJUMP) 620 if (fabs (odiff - diff) < MIN_TIMEJUMP)
454 return; /* all is well */ 621 return; /* all is well */
455 622
456 ev_now = ev_time (); 623 ev_now = ev_time ();
457 } 624 }
458 625
459 /* time jump detected, reschedule atimers */ 626 periodics_reschedule (diff - odiff);
460 for (i = 0; i < atimercnt; ++i) 627 /* 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 } 628 }
466 else 629 else
467 { 630 {
468 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 631 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
469 /* time jump detected, adjust rtimers */ 632 {
633 periodics_reschedule (ev_now - now);
634
635 /* adjust timers. this is easy, as the offset is the same for all */
470 for (i = 0; i < rtimercnt; ++i) 636 for (i = 0; i < timercnt; ++i)
471 rtimers [i]->at += ev_now - now; 637 timers [i]->at += diff;
638 }
472 639
473 now = ev_now; 640 now = ev_now;
474 } 641 }
475} 642}
476 643
477int ev_loop_done; 644int ev_loop_done;
478 645
479void ev_loop (int flags) 646void ev_loop (int flags)
480{ 647{
481 double block; 648 double block;
482 ev_loop_done = flags & EVLOOP_ONESHOT; 649 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 650
490 do 651 do
491 { 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
492 /* update fd-related kernel structures */ 660 /* update fd-related kernel structures */
493 fd_reify (); 661 fd_reify ();
494 662
495 /* calculate blocking time */ 663 /* calculate blocking time */
664
665 /* we only need this for !monotonic clockor timers, but as we basically
666 always have timers, we just calculate it always */
667 ev_now = ev_time ();
668
496 if (flags & EVLOOP_NONBLOCK || idlecnt) 669 if (flags & EVLOOP_NONBLOCK || idlecnt)
497 block = 0.; 670 block = 0.;
498 else 671 else
499 { 672 {
500 block = MAX_BLOCKTIME; 673 block = MAX_BLOCKTIME;
501 674
502 if (rtimercnt) 675 if (timercnt)
503 { 676 {
504 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
505 if (block > to) block = to; 678 if (block > to) block = to;
506 } 679 }
507 680
508 if (atimercnt) 681 if (periodiccnt)
509 { 682 {
510 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 683 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
511 if (block > to) block = to; 684 if (block > to) block = to;
512 } 685 }
513 686
514 if (block < 0.) block = 0.; 687 if (block < 0.) block = 0.;
515 } 688 }
518 691
519 /* update ev_now, do magic */ 692 /* update ev_now, do magic */
520 time_update (); 693 time_update ();
521 694
522 /* queue pending timers and reschedule them */ 695 /* queue pending timers and reschedule them */
523 /* absolute timers first */ 696 timers_reify (); /* relative timers called last */
524 timers_reify (atimers, atimercnt, ev_now); 697 periodics_reify (); /* absolute timers called first */
525 /* relative timers second */
526 timers_reify (rtimers, rtimercnt, now);
527 698
528 /* queue idle watchers unless io or timers are pending */ 699 /* queue idle watchers unless io or timers are pending */
529 if (!pendingcnt) 700 if (!pendingcnt)
530 queue_events ((W *)idles, idlecnt, EV_IDLE); 701 queue_events ((W *)idles, idlecnt, EV_IDLE);
531 702
532 /* queue check and possibly idle watchers */ 703 /* queue check watchers, to be executed first */
704 if (checkcnt)
533 queue_events ((W *)checks, checkcnt, EV_CHECK); 705 queue_events ((W *)checks, checkcnt, EV_CHECK);
534 706
535 call_pending (); 707 call_pending ();
536 } 708 }
537 while (!ev_loop_done); 709 while (!ev_loop_done);
710
711 if (ev_loop_done != 2)
712 ev_loop_done = 0;
538} 713}
539 714
540/*****************************************************************************/ 715/*****************************************************************************/
541 716
542static void 717static void
560 head = &(*head)->next; 735 head = &(*head)->next;
561 } 736 }
562} 737}
563 738
564static 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
565ev_start (W w, int active) 750ev_start (W w, int active)
566{ 751{
567 w->pending = 0;
568 w->active = active; 752 w->active = active;
569} 753}
570 754
571static void 755static void
572ev_stop (W w) 756ev_stop (W w)
573{ 757{
574 if (w->pending)
575 pendings [w->pending - 1].w = 0;
576
577 w->active = 0; 758 w->active = 0;
578 /* nop */
579} 759}
580 760
581/*****************************************************************************/ 761/*****************************************************************************/
582 762
583void 763void
584evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
585{ 765{
586 if (ev_is_active (w)) 766 if (ev_is_active (w))
587 return; 767 return;
588 768
589 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
590 772
591 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
592 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
593 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
594 776
595 ++fdchangecnt; 777 fd_change (fd);
596 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
597 fdchanges [fdchangecnt - 1] = fd;
598} 778}
599 779
600void 780void
601evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
602{ 782{
783 ev_clear_pending ((W)w);
603 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
604 return; 785 return;
605 786
606 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
607 ev_stop ((W)w); 788 ev_stop ((W)w);
608 789
609 ++fdchangecnt; 790 fd_change (w->fd);
610 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
611 fdchanges [fdchangecnt - 1] = w->fd;
612} 791}
613 792
614void 793void
615evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
616{ 795{
796 if (ev_is_active (w))
797 return;
798
799 w->at += now;
800
801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
802
803 ev_start ((W)w, ++timercnt);
804 array_needsize (timers, timermax, timercnt, );
805 timers [timercnt - 1] = w;
806 upheap ((WT *)timers, timercnt - 1);
807}
808
809void
810ev_timer_stop (struct ev_timer *w)
811{
812 ev_clear_pending ((W)w);
617 if (ev_is_active (w)) 813 if (!ev_is_active (w))
618 return; 814 return;
619 815
620 if (w->is_abs) 816 if (w->active < timercnt--)
817 {
818 timers [w->active - 1] = timers [timercnt];
819 downheap ((WT *)timers, timercnt, w->active - 1);
621 { 820 }
622 /* this formula differs from the one in timer_reify becuse we do not round up */ 821
822 w->at = w->repeat;
823
824 ev_stop ((W)w);
825}
826
827void
828ev_timer_again (struct ev_timer *w)
829{
830 if (ev_is_active (w))
831 {
623 if (w->repeat) 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
845ev_periodic_start (struct ev_periodic *w)
846{
847 if (ev_is_active (w))
848 return;
849
850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
851
852 /* this formula differs from the one in periodic_reify because we do not always round up */
853 if (w->interval)
624 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
625 855
626 ev_start ((W)w, ++atimercnt); 856 ev_start ((W)w, ++periodiccnt);
627 array_needsize (atimers, atimermax, atimercnt, ); 857 array_needsize (periodics, periodicmax, periodiccnt, );
628 atimers [atimercnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
629 upheap (atimers, atimercnt - 1); 859 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} 860}
642 861
643void 862void
644evtimer_stop (struct ev_timer *w) 863ev_periodic_stop (struct ev_periodic *w)
645{ 864{
865 ev_clear_pending ((W)w);
646 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
647 return; 867 return;
648 868
649 if (w->is_abs)
650 {
651 if (w->active < atimercnt--) 869 if (w->active < periodiccnt--)
652 {
653 atimers [w->active - 1] = atimers [atimercnt];
654 downheap (atimers, atimercnt, w->active - 1);
655 }
656 } 870 {
657 else 871 periodics [w->active - 1] = periodics [periodiccnt];
658 { 872 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 } 873 }
665 874
666 ev_stop ((W)w); 875 ev_stop ((W)w);
667} 876}
668 877
669void 878void
670evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
671{ 880{
672 if (ev_is_active (w)) 881 if (ev_is_active (w))
673 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
674 885
675 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
676 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
677 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
678 889
685 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
686 } 897 }
687} 898}
688 899
689void 900void
690evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
691{ 902{
903 ev_clear_pending ((W)w);
692 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
693 return; 905 return;
694 906
695 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
696 ev_stop ((W)w); 908 ev_stop ((W)w);
697 909
698 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
699 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
700} 912}
701 913
914void
702void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
703{ 916{
704 if (ev_is_active (w)) 917 if (ev_is_active (w))
705 return; 918 return;
706 919
707 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
708 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
709 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
710} 923}
711 924
925void
712void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
713{ 927{
928 ev_clear_pending ((W)w);
929 if (ev_is_active (w))
930 return;
931
714 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
715 ev_stop ((W)w); 933 ev_stop ((W)w);
716} 934}
717 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
718void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
719{ 960{
720 if (ev_is_active (w)) 961 if (ev_is_active (w))
721 return; 962 return;
722 963
723 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
724 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
725 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
726} 967}
727 968
969void
728void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
729{ 971{
972 ev_clear_pending ((W)w);
973 if (ev_is_active (w))
974 return;
975
730 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
731 ev_stop ((W)w); 977 ev_stop ((W)w);
732} 978}
733 979
980void
981ev_child_start (struct ev_child *w)
982{
983 if (ev_is_active (w))
984 return;
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
734/*****************************************************************************/ 1064/*****************************************************************************/
735 1065
736#if 0 1066#if 0
737 1067
1068struct ev_io wio;
1069
738static void 1070static void
739sin_cb (struct ev_io *w, int revents) 1071sin_cb (struct ev_io *w, int revents)
740{ 1072{
741 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1073 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
742} 1074}
743 1075
744static void 1076static void
745ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
746{ 1078{
747 //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);
748 evtimer_stop (w); 1080 ev_timer_stop (w);
749 evtimer_start (w); 1081 ev_timer_start (w);
750} 1082}
751 1083
752static void 1084static void
753scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
754{ 1086{
755 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1088 ev_io_stop (&wio);
1089 ev_io_start (&wio);
756} 1090}
757 1091
758static void 1092static void
759gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
760{ 1094{
761 fprintf (stderr, "generic %x\n", revents); 1095 fprintf (stderr, "generic %x\n", revents);
1096
762} 1097}
763 1098
764int main (void) 1099int main (void)
765{ 1100{
766 struct ev_io sin;
767
768 ev_init (0); 1101 ev_init (0);
769 1102
770 evw_init (&sin, sin_cb, 55);
771 evio_set (&sin, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
772 evio_start (&sin); 1104 ev_io_start (&wio);
773 1105
774 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
775 1107
776#if 0 1108#if 0
777 int i; 1109 int i;
778 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
779 { 1111 {
780 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
781 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
782 evtimer_set_abs (w, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
783 evtimer_start (w); 1115 ev_timer_start (w);
784 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
785 evtimer_stop (w); 1117 ev_timer_stop (w);
786 } 1118 }
787#endif 1119#endif
788 1120
789 struct ev_timer t1; 1121 struct ev_timer t1;
790 evw_init (&t1, ocb, 0); 1122 ev_timer_init (&t1, ocb, 5, 10);
791 evtimer_set_abs (&t1, 5, 10);
792 evtimer_start (&t1); 1123 ev_timer_start (&t1);
793 1124
794 struct ev_signal sig; 1125 struct ev_signal sig;
795 evw_init (&sig, scb, 65535);
796 evsignal_set (&sig, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
797 evsignal_start (&sig); 1127 ev_signal_start (&sig);
798 1128
799 struct ev_check cw; 1129 struct ev_check cw;
800 evw_init (&cw, gcb, 0); 1130 ev_check_init (&cw, gcb);
801 evcheck_start (&cw); 1131 ev_check_start (&cw);
802 1132
803 struct ev_idle iw; 1133 struct ev_idle iw;
804 evw_init (&iw, gcb, 0); 1134 ev_idle_init (&iw, gcb);
805 evidle_start (&iw); 1135 ev_idle_start (&iw);
806 1136
807 ev_loop (0); 1137 ev_loop (0);
808 1138
809 return 0; 1139 return 0;
810} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines