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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines