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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines