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