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

Comparing libev/ev.c (file contents):
Revision 1.10 by root, Wed Oct 31 07:36:03 2007 UTC vs.
Revision 1.29 by root, Thu Nov 1 08:10:03 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
32
1#include <math.h> 33#include <math.h>
2#include <stdlib.h> 34#include <stdlib.h>
3#include <unistd.h> 35#include <unistd.h>
4#include <fcntl.h> 36#include <fcntl.h>
5#include <signal.h> 37#include <signal.h>
38#include <stddef.h>
6 39
7#include <stdio.h> 40#include <stdio.h>
8 41
9#include <assert.h> 42#include <assert.h>
10#include <errno.h> 43#include <errno.h>
44#include <sys/types.h>
45#include <sys/wait.h>
11#include <sys/time.h> 46#include <sys/time.h>
12#include <time.h> 47#include <time.h>
13 48
14#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
15# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
16# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
17# endif 52# endif
18#endif 53#endif
19 54
20#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
21# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
22#endif 57#endif
23 58
24#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
25# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
26#endif 61#endif
27 62
28#ifndef HAVE_REALTIME 63#ifndef EV_USE_REALTIME
29# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
30#endif 65#endif
31 66
32#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) */
33#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 */
34 70
35#include "ev.h" 71#include "ev.h"
36
37struct ev_watcher {
38 EV_WATCHER (ev_watcher);
39};
40
41struct ev_watcher_list {
42 EV_WATCHER_LIST (ev_watcher_list);
43};
44 72
45typedef struct ev_watcher *W; 73typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL; 74typedef struct ev_watcher_list *WL;
75typedef struct ev_watcher_time *WT;
47 76
48static ev_tstamp now, diff; /* monotonic clock */ 77static ev_tstamp now, diff; /* monotonic clock */
49ev_tstamp ev_now; 78ev_tstamp ev_now;
50int ev_method; 79int ev_method;
51 80
58/*****************************************************************************/ 87/*****************************************************************************/
59 88
60ev_tstamp 89ev_tstamp
61ev_time (void) 90ev_time (void)
62{ 91{
63#if HAVE_REALTIME 92#if EV_USE_REALTIME
64 struct timespec ts; 93 struct timespec ts;
65 clock_gettime (CLOCK_REALTIME, &ts); 94 clock_gettime (CLOCK_REALTIME, &ts);
66 return ts.tv_sec + ts.tv_nsec * 1e-9; 95 return ts.tv_sec + ts.tv_nsec * 1e-9;
67#else 96#else
68 struct timeval tv; 97 struct timeval tv;
72} 101}
73 102
74static ev_tstamp 103static ev_tstamp
75get_clock (void) 104get_clock (void)
76{ 105{
77#if HAVE_MONOTONIC 106#if EV_USE_MONOTONIC
78 if (have_monotonic) 107 if (have_monotonic)
79 { 108 {
80 struct timespec ts; 109 struct timespec ts;
81 clock_gettime (CLOCK_MONOTONIC, &ts); 110 clock_gettime (CLOCK_MONOTONIC, &ts);
82 return ts.tv_sec + ts.tv_nsec * 1e-9; 111 return ts.tv_sec + ts.tv_nsec * 1e-9;
84#endif 113#endif
85 114
86 return ev_time (); 115 return ev_time ();
87} 116}
88 117
118#define array_nextsize(n) (((n) << 1) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120
89#define array_needsize(base,cur,cnt,init) \ 121#define array_needsize(base,cur,cnt,init) \
90 if ((cnt) > cur) \ 122 if ((cnt) > cur) \
91 { \ 123 { \
92 int newcnt = cur ? cur << 1 : 16; \ 124 int newcnt = cur; \
93 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ 125 do \
126 { \
127 newcnt = array_nextsize (newcnt); \
128 } \
129 while ((cnt) > newcnt); \
130 \
94 base = realloc (base, sizeof (*base) * (newcnt)); \ 131 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \ 132 init (base + cur, newcnt - cur); \
96 cur = newcnt; \ 133 cur = newcnt; \
97 } 134 }
98 135
99/*****************************************************************************/ 136/*****************************************************************************/
100 137
101typedef struct 138typedef struct
102{ 139{
103 struct ev_io *head; 140 struct ev_io *head;
104 unsigned char wev, rev; /* want, received event set */ 141 int events;
105} ANFD; 142} ANFD;
106 143
107static ANFD *anfds; 144static ANFD *anfds;
108static int anfdmax; 145static int anfdmax;
109 146
110static int *fdchanges;
111static int fdchangemax, fdchangecnt;
112
113static void 147static void
114anfds_init (ANFD *base, int count) 148anfds_init (ANFD *base, int count)
115{ 149{
116 while (count--) 150 while (count--)
117 { 151 {
118 base->head = 0; 152 base->head = 0;
119 base->wev = base->rev = EV_NONE; 153 base->events = EV_NONE;
120 ++base; 154 ++base;
121 } 155 }
122} 156}
123 157
124typedef struct 158typedef struct
131static int pendingmax, pendingcnt; 165static int pendingmax, pendingcnt;
132 166
133static void 167static void
134event (W w, int events) 168event (W w, int events)
135{ 169{
170 if (w->active)
171 {
136 w->pending = ++pendingcnt; 172 w->pending = ++pendingcnt;
137 array_needsize (pendings, pendingmax, pendingcnt, ); 173 array_needsize (pendings, pendingmax, pendingcnt, );
138 pendings [pendingcnt - 1].w = w; 174 pendings [pendingcnt - 1].w = w;
139 pendings [pendingcnt - 1].events = events; 175 pendings [pendingcnt - 1].events = events;
176 }
177}
178
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);
140} 186}
141 187
142static void 188static void
143fd_event (int fd, int events) 189fd_event (int fd, int events)
144{ 190{
152 if (ev) 198 if (ev)
153 event ((W)w, ev); 199 event ((W)w, ev);
154 } 200 }
155} 201}
156 202
203/*****************************************************************************/
204
205static int *fdchanges;
206static int fdchangemax, fdchangecnt;
207
157static void 208static void
158queue_events (W *events, int eventcnt, int type) 209fd_reify (void)
159{ 210{
160 int i; 211 int i;
161 212
162 for (i = 0; i < eventcnt; ++i) 213 for (i = 0; i < fdchangecnt; ++i)
163 event (events [i], type); 214 {
164} 215 int fd = fdchanges [i];
216 ANFD *anfd = anfds + fd;
217 struct ev_io *w;
165 218
166/*****************************************************************************/ 219 int events = 0;
167 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;
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/*****************************************************************************/
266
168static struct ev_timer **atimers; 267static struct ev_timer **timers;
169static int atimermax, atimercnt; 268static int timermax, timercnt;
170 269
171static struct ev_timer **rtimers; 270static struct ev_periodic **periodics;
172static int rtimermax, rtimercnt; 271static int periodicmax, periodiccnt;
173 272
174static void 273static void
175upheap (struct ev_timer **timers, int k) 274upheap (WT *timers, int k)
176{ 275{
177 struct ev_timer *w = timers [k]; 276 WT w = timers [k];
178 277
179 while (k && timers [k >> 1]->at > w->at) 278 while (k && timers [k >> 1]->at > w->at)
180 { 279 {
181 timers [k] = timers [k >> 1]; 280 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1; 281 timers [k]->active = k + 1;
187 timers [k]->active = k + 1; 286 timers [k]->active = k + 1;
188 287
189} 288}
190 289
191static void 290static void
192downheap (struct ev_timer **timers, int N, int k) 291downheap (WT *timers, int N, int k)
193{ 292{
194 struct ev_timer *w = timers [k]; 293 WT w = timers [k];
195 294
196 while (k < (N >> 1)) 295 while (k < (N >> 1))
197 { 296 {
198 int j = k << 1; 297 int j = k << 1;
199 298
277 376
278 /* rather than sort out wether we really need nb, set it */ 377 /* rather than sort out wether we really need nb, set it */
279 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
281 380
282 evio_set (&sigev, sigpipe [0], EV_READ); 381 ev_io_set (&sigev, sigpipe [0], EV_READ);
283 evio_start (&sigev); 382 ev_io_start (&sigev);
284} 383}
285 384
286/*****************************************************************************/ 385/*****************************************************************************/
287 386
288static struct ev_idle **idles; 387static struct ev_idle **idles;
289static int idlemax, idlecnt; 388static int idlemax, idlecnt;
290 389
390static struct ev_prepare **prepares;
391static int preparemax, preparecnt;
392
291static struct ev_check **checks; 393static struct ev_check **checks;
292static int checkmax, checkcnt; 394static int checkmax, checkcnt;
293 395
294/*****************************************************************************/ 396/*****************************************************************************/
295 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
296#if HAVE_EPOLL 422#if EV_USE_EPOLL
297# include "ev_epoll.c" 423# include "ev_epoll.c"
298#endif 424#endif
299#if HAVE_SELECT 425#if EV_USE_SELECT
300# include "ev_select.c" 426# include "ev_select.c"
301#endif 427#endif
302 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
303int ev_init (int flags) 441int ev_init (int flags)
304{ 442{
305#if HAVE_MONOTONIC
306 {
307 struct timespec ts;
308 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
309 have_monotonic = 1;
310 }
311#endif
312
313 ev_now = ev_time ();
314 now = get_clock ();
315 diff = ev_now - now;
316
317 if (pipe (sigpipe))
318 return 0;
319
320 ev_method = EVMETHOD_NONE;
321#if HAVE_EPOLL
322 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
323#endif
324#if HAVE_SELECT
325 if (ev_method == EVMETHOD_NONE) select_init (flags);
326#endif
327
328 if (ev_method) 443 if (!ev_method)
444 {
445#if EV_USE_MONOTONIC
329 { 446 {
447 struct timespec ts;
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1;
450 }
451#endif
452
453 ev_now = ev_time ();
454 now = get_clock ();
455 diff = ev_now - now;
456
457 if (pipe (sigpipe))
458 return 0;
459
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 {
330 evw_init (&sigev, sigcb, 0); 470 ev_watcher_init (&sigev, sigcb);
331 siginit (); 471 siginit ();
472
473 ev_signal_init (&childev, childcb, SIGCHLD);
474 ev_signal_start (&childev);
475 }
332 } 476 }
333 477
334 return ev_method; 478 return ev_method;
335} 479}
336 480
337/*****************************************************************************/ 481/*****************************************************************************/
338 482
483void
339void ev_prefork (void) 484ev_prefork (void)
340{ 485{
486 /* nop */
341} 487}
342 488
489void
343void ev_postfork_parent (void) 490ev_postfork_parent (void)
344{ 491{
492 /* nop */
345} 493}
346 494
495void
347void ev_postfork_child (void) 496ev_postfork_child (void)
348{ 497{
349#if HAVE_EPOLL 498#if EV_USE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
351 epoll_postfork_child (); 500 epoll_postfork_child ();
352#endif 501#endif
353 502
354 evio_stop (&sigev); 503 ev_io_stop (&sigev);
355 close (sigpipe [0]); 504 close (sigpipe [0]);
356 close (sigpipe [1]); 505 close (sigpipe [1]);
357 pipe (sigpipe); 506 pipe (sigpipe);
358 siginit (); 507 siginit ();
359} 508}
360 509
361/*****************************************************************************/ 510/*****************************************************************************/
362 511
363static void 512static void
364fd_reify (void)
365{
366 int i;
367
368 for (i = 0; i < fdchangecnt; ++i)
369 {
370 int fd = fdchanges [i];
371 ANFD *anfd = anfds + fd;
372 struct ev_io *w;
373
374 int wev = 0;
375
376 for (w = anfd->head; w; w = w->next)
377 wev |= w->events;
378
379 if (anfd->wev != wev)
380 {
381 method_modify (fd, anfd->wev, wev);
382 anfd->wev = wev;
383 }
384 }
385
386 fdchangecnt = 0;
387}
388
389static void
390call_pending () 513call_pending (void)
391{ 514{
392 int i; 515 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 516 {
396 ANPENDING *p = pendings + i; 517 ANPENDING *p = pendings + --pendingcnt;
397 518
398 if (p->w) 519 if (p->w)
399 { 520 {
400 p->w->pending = 0; 521 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 522 p->w->cb (p->w, p->events);
402 } 523 }
403 } 524 }
404
405 pendingcnt = 0;
406} 525}
407 526
408static void 527static void
409timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 528timers_reify (void)
410{ 529{
411 while (timercnt && timers [0]->at <= now) 530 while (timercnt && timers [0]->at <= now)
412 { 531 {
413 struct ev_timer *w = timers [0]; 532 struct ev_timer *w = timers [0];
533
534 event ((W)w, EV_TIMEOUT);
414 535
415 /* first reschedule or stop timer */ 536 /* first reschedule or stop timer */
416 if (w->repeat) 537 if (w->repeat)
417 { 538 {
418 if (w->is_abs)
419 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
420 else
421 w->at = now + w->repeat; 539 w->at = now + w->repeat;
422 540 assert (("timer timeout in the past, negative repeat?", w->at > now));
423 assert (w->at > now);
424
425 downheap (timers, timercnt, 0); 541 downheap ((WT *)timers, timercnt, 0);
426 } 542 }
427 else 543 else
428 {
429 evtimer_stop (w); /* nonrepeating: stop timer */ 544 ev_timer_stop (w); /* nonrepeating: stop timer */
430 --timercnt; /* maybe pass by reference instead? */ 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)
431 } 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 */
432 564
433 event ((W)w, EV_TIMEOUT); 565 event ((W)w, EV_TIMEOUT);
434 } 566 }
435} 567}
436 568
437static void 569static void
438time_update () 570periodics_reschedule (ev_tstamp diff)
439{ 571{
440 int i; 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
441 ev_now = ev_time (); 599 ev_now = ev_time ();
442 600
443 if (have_monotonic) 601 if (have_monotonic)
444 { 602 {
445 ev_tstamp odiff = diff; 603 ev_tstamp odiff = diff;
446 604
447 /* detecting time jumps is much more difficult */
448 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 605 for (i = 4; --i; ) /* loop a few times, before making important decisions */
449 { 606 {
450 now = get_clock (); 607 now = get_clock ();
451 diff = ev_now - now; 608 diff = ev_now - now;
452 609
453 if (fabs (odiff - diff) < MIN_TIMEJUMP) 610 if (fabs (odiff - diff) < MIN_TIMEJUMP)
454 return; /* all is well */ 611 return; /* all is well */
455 612
456 ev_now = ev_time (); 613 ev_now = ev_time ();
457 } 614 }
458 615
459 /* time jump detected, reschedule atimers */ 616 periodics_reschedule (diff - odiff);
460 for (i = 0; i < atimercnt; ++i) 617 /* no timer adjustment, as the monotonic clock doesn't jump */
461 {
462 struct ev_timer *w = atimers [i];
463 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
464 }
465 } 618 }
466 else 619 else
467 { 620 {
468 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 621 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
469 /* time jump detected, adjust rtimers */ 622 {
623 periodics_reschedule (ev_now - now);
624
625 /* adjust timers. this is easy, as the offset is the same for all */
470 for (i = 0; i < rtimercnt; ++i) 626 for (i = 0; i < timercnt; ++i)
471 rtimers [i]->at += ev_now - now; 627 timers [i]->at += diff;
628 }
472 629
473 now = ev_now; 630 now = ev_now;
474 } 631 }
475} 632}
476 633
477int ev_loop_done; 634int ev_loop_done;
478 635
479void ev_loop (int flags) 636void ev_loop (int flags)
480{ 637{
481 double block; 638 double block;
482 ev_loop_done = flags & EVLOOP_ONESHOT; 639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
483
484 if (checkcnt)
485 {
486 queue_events ((W *)checks, checkcnt, EV_CHECK);
487 call_pending ();
488 }
489 640
490 do 641 do
491 { 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
492 /* update fd-related kernel structures */ 650 /* update fd-related kernel structures */
493 fd_reify (); 651 fd_reify ();
494 652
495 /* 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 */
657 ev_now = ev_time ();
658
496 if (flags & EVLOOP_NONBLOCK || idlecnt) 659 if (flags & EVLOOP_NONBLOCK || idlecnt)
497 block = 0.; 660 block = 0.;
498 else 661 else
499 { 662 {
500 block = MAX_BLOCKTIME; 663 block = MAX_BLOCKTIME;
501 664
502 if (rtimercnt) 665 if (timercnt)
503 { 666 {
504 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 667 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
505 if (block > to) block = to; 668 if (block > to) block = to;
506 } 669 }
507 670
508 if (atimercnt) 671 if (periodiccnt)
509 { 672 {
510 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 673 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
511 if (block > to) block = to; 674 if (block > to) block = to;
512 } 675 }
513 676
514 if (block < 0.) block = 0.; 677 if (block < 0.) block = 0.;
515 } 678 }
518 681
519 /* update ev_now, do magic */ 682 /* update ev_now, do magic */
520 time_update (); 683 time_update ();
521 684
522 /* queue pending timers and reschedule them */ 685 /* queue pending timers and reschedule them */
523 /* absolute timers first */ 686 timers_reify (); /* relative timers called last */
524 timers_reify (atimers, atimercnt, ev_now); 687 periodics_reify (); /* absolute timers called first */
525 /* relative timers second */
526 timers_reify (rtimers, rtimercnt, now);
527 688
528 /* queue idle watchers unless io or timers are pending */ 689 /* queue idle watchers unless io or timers are pending */
529 if (!pendingcnt) 690 if (!pendingcnt)
530 queue_events ((W *)idles, idlecnt, EV_IDLE); 691 queue_events ((W *)idles, idlecnt, EV_IDLE);
531 692
532 /* queue check and possibly idle watchers */ 693 /* queue check watchers, to be executed first */
694 if (checkcnt)
533 queue_events ((W *)checks, checkcnt, EV_CHECK); 695 queue_events ((W *)checks, checkcnt, EV_CHECK);
534 696
535 call_pending (); 697 call_pending ();
536 } 698 }
537 while (!ev_loop_done); 699 while (!ev_loop_done);
700
701 if (ev_loop_done != 2)
702 ev_loop_done = 0;
538} 703}
539 704
540/*****************************************************************************/ 705/*****************************************************************************/
541 706
542static void 707static void
560 head = &(*head)->next; 725 head = &(*head)->next;
561 } 726 }
562} 727}
563 728
564static void 729static void
730ev_clear (W w)
731{
732 if (w->pending)
733 {
734 pendings [w->pending - 1].w = 0;
735 w->pending = 0;
736 }
737}
738
739static void
565ev_start (W w, int active) 740ev_start (W w, int active)
566{ 741{
567 w->pending = 0;
568 w->active = active; 742 w->active = active;
569} 743}
570 744
571static void 745static void
572ev_stop (W w) 746ev_stop (W w)
573{ 747{
574 if (w->pending)
575 pendings [w->pending - 1].w = 0;
576
577 w->active = 0; 748 w->active = 0;
578 /* nop */
579} 749}
580 750
581/*****************************************************************************/ 751/*****************************************************************************/
582 752
583void 753void
584evio_start (struct ev_io *w) 754ev_io_start (struct ev_io *w)
585{ 755{
586 if (ev_is_active (w)) 756 if (ev_is_active (w))
587 return; 757 return;
588 758
589 int fd = w->fd; 759 int fd = w->fd;
590 760
591 ev_start ((W)w, 1); 761 ev_start ((W)w, 1);
592 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 762 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
593 wlist_add ((WL *)&anfds[fd].head, (WL)w); 763 wlist_add ((WL *)&anfds[fd].head, (WL)w);
594 764
595 ++fdchangecnt; 765 fd_change (fd);
596 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
597 fdchanges [fdchangecnt - 1] = fd;
598} 766}
599 767
600void 768void
601evio_stop (struct ev_io *w) 769ev_io_stop (struct ev_io *w)
602{ 770{
771 ev_clear ((W)w);
603 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
604 return; 773 return;
605 774
606 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
607 ev_stop ((W)w); 776 ev_stop ((W)w);
608 777
609 ++fdchangecnt; 778 fd_change (w->fd);
610 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
611 fdchanges [fdchangecnt - 1] = w->fd;
612} 779}
613 780
614void 781void
615evtimer_start (struct ev_timer *w) 782ev_timer_start (struct ev_timer *w)
616{ 783{
784 if (ev_is_active (w))
785 return;
786
787 w->at += now;
788
789 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
790
791 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1);
795}
796
797void
798ev_timer_stop (struct ev_timer *w)
799{
800 ev_clear ((W)w);
617 if (ev_is_active (w)) 801 if (!ev_is_active (w))
618 return; 802 return;
619 803
620 if (w->is_abs) 804 if (w->active < timercnt--)
805 {
806 timers [w->active - 1] = timers [timercnt];
807 downheap ((WT *)timers, timercnt, w->active - 1);
621 { 808 }
622 /* this formula differs from the one in timer_reify becuse we do not round up */ 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))
819 {
623 if (w->repeat) 820 if (w->repeat)
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);
830}
831
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)
624 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
625 843
626 ev_start ((W)w, ++atimercnt); 844 ev_start ((W)w, ++periodiccnt);
627 array_needsize (atimers, atimermax, atimercnt, ); 845 array_needsize (periodics, periodicmax, periodiccnt, );
628 atimers [atimercnt - 1] = w; 846 periodics [periodiccnt - 1] = w;
629 upheap (atimers, atimercnt - 1); 847 upheap ((WT *)periodics, periodiccnt - 1);
630 }
631 else
632 {
633 w->at += now;
634
635 ev_start ((W)w, ++rtimercnt);
636 array_needsize (rtimers, rtimermax, rtimercnt, );
637 rtimers [rtimercnt - 1] = w;
638 upheap (rtimers, rtimercnt - 1);
639 }
640
641} 848}
642 849
643void 850void
644evtimer_stop (struct ev_timer *w) 851ev_periodic_stop (struct ev_periodic *w)
645{ 852{
853 ev_clear ((W)w);
646 if (!ev_is_active (w)) 854 if (!ev_is_active (w))
647 return; 855 return;
648 856
649 if (w->is_abs)
650 {
651 if (w->active < atimercnt--) 857 if (w->active < periodiccnt--)
652 {
653 atimers [w->active - 1] = atimers [atimercnt];
654 downheap (atimers, atimercnt, w->active - 1);
655 }
656 } 858 {
657 else 859 periodics [w->active - 1] = periodics [periodiccnt];
658 { 860 downheap ((WT *)periodics, periodiccnt, w->active - 1);
659 if (w->active < rtimercnt--)
660 {
661 rtimers [w->active - 1] = rtimers [rtimercnt];
662 downheap (rtimers, rtimercnt, w->active - 1);
663 }
664 } 861 }
665 862
666 ev_stop ((W)w); 863 ev_stop ((W)w);
667} 864}
668 865
669void 866void
670evsignal_start (struct ev_signal *w) 867ev_signal_start (struct ev_signal *w)
671{ 868{
672 if (ev_is_active (w)) 869 if (ev_is_active (w))
673 return; 870 return;
674 871
675 ev_start ((W)w, 1); 872 ev_start ((W)w, 1);
685 sigaction (w->signum, &sa, 0); 882 sigaction (w->signum, &sa, 0);
686 } 883 }
687} 884}
688 885
689void 886void
690evsignal_stop (struct ev_signal *w) 887ev_signal_stop (struct ev_signal *w)
691{ 888{
889 ev_clear ((W)w);
692 if (!ev_is_active (w)) 890 if (!ev_is_active (w))
693 return; 891 return;
694 892
695 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
696 ev_stop ((W)w); 894 ev_stop ((W)w);
697 895
698 if (!signals [w->signum - 1].head) 896 if (!signals [w->signum - 1].head)
699 signal (w->signum, SIG_DFL); 897 signal (w->signum, SIG_DFL);
700} 898}
701 899
900void
702void evidle_start (struct ev_idle *w) 901ev_idle_start (struct ev_idle *w)
703{ 902{
704 if (ev_is_active (w)) 903 if (ev_is_active (w))
705 return; 904 return;
706 905
707 ev_start ((W)w, ++idlecnt); 906 ev_start ((W)w, ++idlecnt);
708 array_needsize (idles, idlemax, idlecnt, ); 907 array_needsize (idles, idlemax, idlecnt, );
709 idles [idlecnt - 1] = w; 908 idles [idlecnt - 1] = w;
710} 909}
711 910
911void
712void evidle_stop (struct ev_idle *w) 912ev_idle_stop (struct ev_idle *w)
713{ 913{
914 ev_clear ((W)w);
915 if (ev_is_active (w))
916 return;
917
714 idles [w->active - 1] = idles [--idlecnt]; 918 idles [w->active - 1] = idles [--idlecnt];
715 ev_stop ((W)w); 919 ev_stop ((W)w);
716} 920}
717 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
718void evcheck_start (struct ev_check *w) 945ev_check_start (struct ev_check *w)
719{ 946{
720 if (ev_is_active (w)) 947 if (ev_is_active (w))
721 return; 948 return;
722 949
723 ev_start ((W)w, ++checkcnt); 950 ev_start ((W)w, ++checkcnt);
724 array_needsize (checks, checkmax, checkcnt, ); 951 array_needsize (checks, checkmax, checkcnt, );
725 checks [checkcnt - 1] = w; 952 checks [checkcnt - 1] = w;
726} 953}
727 954
955void
728void evcheck_stop (struct ev_check *w) 956ev_check_stop (struct ev_check *w)
729{ 957{
958 ev_clear ((W)w);
959 if (ev_is_active (w))
960 return;
961
730 checks [w->active - 1] = checks [--checkcnt]; 962 checks [w->active - 1] = checks [--checkcnt];
731 ev_stop ((W)w); 963 ev_stop ((W)w);
732} 964}
733 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
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
734/*****************************************************************************/ 1050/*****************************************************************************/
735 1051
736#if 0 1052#if 0
737 1053
1054struct ev_io wio;
1055
738static void 1056static void
739sin_cb (struct ev_io *w, int revents) 1057sin_cb (struct ev_io *w, int revents)
740{ 1058{
741 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1059 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
742} 1060}
743 1061
744static void 1062static void
745ocb (struct ev_timer *w, int revents) 1063ocb (struct ev_timer *w, int revents)
746{ 1064{
747 //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);
748 evtimer_stop (w); 1066 ev_timer_stop (w);
749 evtimer_start (w); 1067 ev_timer_start (w);
750} 1068}
751 1069
752static void 1070static void
753scb (struct ev_signal *w, int revents) 1071scb (struct ev_signal *w, int revents)
754{ 1072{
755 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1073 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1074 ev_io_stop (&wio);
1075 ev_io_start (&wio);
756} 1076}
757 1077
758static void 1078static void
759gcb (struct ev_signal *w, int revents) 1079gcb (struct ev_signal *w, int revents)
760{ 1080{
761 fprintf (stderr, "generic %x\n", revents); 1081 fprintf (stderr, "generic %x\n", revents);
1082
762} 1083}
763 1084
764int main (void) 1085int main (void)
765{ 1086{
766 struct ev_io sin;
767
768 ev_init (0); 1087 ev_init (0);
769 1088
770 evw_init (&sin, sin_cb, 55);
771 evio_set (&sin, 0, EV_READ); 1089 ev_io_init (&wio, sin_cb, 0, EV_READ);
772 evio_start (&sin); 1090 ev_io_start (&wio);
773 1091
774 struct ev_timer t[10000]; 1092 struct ev_timer t[10000];
775 1093
776#if 0 1094#if 0
777 int i; 1095 int i;
778 for (i = 0; i < 10000; ++i) 1096 for (i = 0; i < 10000; ++i)
779 { 1097 {
780 struct ev_timer *w = t + i; 1098 struct ev_timer *w = t + i;
781 evw_init (w, ocb, i); 1099 ev_watcher_init (w, ocb, i);
782 evtimer_set_abs (w, drand48 (), 0.99775533); 1100 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
783 evtimer_start (w); 1101 ev_timer_start (w);
784 if (drand48 () < 0.5) 1102 if (drand48 () < 0.5)
785 evtimer_stop (w); 1103 ev_timer_stop (w);
786 } 1104 }
787#endif 1105#endif
788 1106
789 struct ev_timer t1; 1107 struct ev_timer t1;
790 evw_init (&t1, ocb, 0); 1108 ev_timer_init (&t1, ocb, 5, 10);
791 evtimer_set_abs (&t1, 5, 10);
792 evtimer_start (&t1); 1109 ev_timer_start (&t1);
793 1110
794 struct ev_signal sig; 1111 struct ev_signal sig;
795 evw_init (&sig, scb, 65535);
796 evsignal_set (&sig, SIGQUIT); 1112 ev_signal_init (&sig, scb, SIGQUIT);
797 evsignal_start (&sig); 1113 ev_signal_start (&sig);
798 1114
799 struct ev_check cw; 1115 struct ev_check cw;
800 evw_init (&cw, gcb, 0); 1116 ev_check_init (&cw, gcb);
801 evcheck_start (&cw); 1117 ev_check_start (&cw);
802 1118
803 struct ev_idle iw; 1119 struct ev_idle iw;
804 evw_init (&iw, gcb, 0); 1120 ev_idle_init (&iw, gcb);
805 evidle_start (&iw); 1121 ev_idle_start (&iw);
806 1122
807 ev_loop (0); 1123 ev_loop (0);
808 1124
809 return 0; 1125 return 0;
810} 1126}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines