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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines