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.21 by root, Wed Oct 31 18:37:38 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
35#include <stddef.h>
6 36
7#include <stdio.h> 37#include <stdio.h>
8 38
9#include <assert.h> 39#include <assert.h>
10#include <errno.h> 40#include <errno.h>
11#include <sys/time.h> 41#include <sys/time.h>
12#include <time.h> 42#include <time.h>
13 43
44#ifndef HAVE_MONOTONIC
14#ifdef CLOCK_MONOTONIC 45# ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1 46# define HAVE_MONOTONIC 1
16#endif 47# endif
48#endif
17 49
18#define HAVE_REALTIME 1 50#ifndef HAVE_SELECT
19#define HAVE_EPOLL 1
20#define HAVE_SELECT 1 51# define HAVE_SELECT 1
52#endif
53
54#ifndef HAVE_EPOLL
55# define HAVE_EPOLL 0
56#endif
57
58#ifndef HAVE_REALTIME
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
60#endif
21 61
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 62#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 63#define MAX_BLOCKTIME 60.
24 64
25#include "ev.h" 65#include "ev.h"
26 66
27struct ev_watcher { 67typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 68typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 69typedef struct ev_watcher_time *WT;
33};
34 70
35static ev_tstamp now, diff; /* monotonic clock */ 71static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 72ev_tstamp ev_now;
37int ev_method; 73int ev_method;
38 74
39static int have_monotonic; /* runtime */ 75static int have_monotonic; /* runtime */
40 76
41static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 77static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42static void (*method_modify)(int fd, int oev, int nev); 78static void (*method_modify)(int fd, int oev, int nev);
43static void (*method_poll)(ev_tstamp timeout); 79static void (*method_poll)(ev_tstamp timeout);
80
81/*****************************************************************************/
44 82
45ev_tstamp 83ev_tstamp
46ev_time (void) 84ev_time (void)
47{ 85{
48#if HAVE_REALTIME 86#if HAVE_REALTIME
73 111
74#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
75 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
76 { \ 114 { \
77 int newcnt = cur ? cur << 1 : 16; \ 115 int newcnt = cur ? cur << 1 : 16; \
78 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
79 base = realloc (base, sizeof (*base) * (newcnt)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
81 cur = newcnt; \ 118 cur = newcnt; \
82 } 119 }
83 120
121/*****************************************************************************/
122
84typedef struct 123typedef struct
85{ 124{
86 struct ev_io *head; 125 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */ 126 unsigned char wev, rev; /* want, received event set */
88} ANFD; 127} ANFD;
104 } 143 }
105} 144}
106 145
107typedef struct 146typedef struct
108{ 147{
109 struct ev_watcher *w; 148 W w;
110 int events; 149 int events;
111} ANPENDING; 150} ANPENDING;
112 151
113static ANPENDING *pendings; 152static ANPENDING *pendings;
114static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
115 154
116static void 155static void
117event (struct ev_watcher *w, int events) 156event (W w, int events)
118{ 157{
158 if (w->active)
159 {
119 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
123} 165}
124 166
125static void 167static void
126fd_event (int fd, int events) 168fd_event (int fd, int events)
127{ 169{
131 for (w = anfd->head; w; w = w->next) 173 for (w = anfd->head; w; w = w->next)
132 { 174 {
133 int ev = w->events & events; 175 int ev = w->events & events;
134 176
135 if (ev) 177 if (ev)
136 event ((struct ev_watcher *)w, ev); 178 event ((W)w, ev);
137 } 179 }
138} 180}
139 181
182static void
183queue_events (W *events, int eventcnt, int type)
184{
185 int i;
186
187 for (i = 0; i < eventcnt; ++i)
188 event (events [i], type);
189}
190
191/* called on EBADF to verify fds */
192static void
193fd_recheck ()
194{
195 int fd;
196
197 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head)
201 evio_stop (anfds [fd].head);
202}
203
204/*****************************************************************************/
205
140static struct ev_timer **atimers; 206static struct ev_timer **timers;
141static int atimermax, atimercnt; 207static int timermax, timercnt;
142 208
143static struct ev_timer **rtimers; 209static struct ev_periodic **periodics;
144static int rtimermax, rtimercnt; 210static int periodicmax, periodiccnt;
145 211
146static void 212static void
147upheap (struct ev_timer **timers, int k) 213upheap (WT *timers, int k)
148{ 214{
149 struct ev_timer *w = timers [k]; 215 WT w = timers [k];
150 216
151 while (k && timers [k >> 1]->at > w->at) 217 while (k && timers [k >> 1]->at > w->at)
152 { 218 {
153 timers [k] = timers [k >> 1]; 219 timers [k] = timers [k >> 1];
154 timers [k]->active = k + 1; 220 timers [k]->active = k + 1;
159 timers [k]->active = k + 1; 225 timers [k]->active = k + 1;
160 226
161} 227}
162 228
163static void 229static void
164downheap (struct ev_timer **timers, int N, int k) 230downheap (WT *timers, int N, int k)
165{ 231{
166 struct ev_timer *w = timers [k]; 232 WT w = timers [k];
167 233
168 while (k < (N >> 1)) 234 while (k < (N >> 1))
169 { 235 {
170 int j = k << 1; 236 int j = k << 1;
171 237
181 } 247 }
182 248
183 timers [k] = w; 249 timers [k] = w;
184 timers [k]->active = k + 1; 250 timers [k]->active = k + 1;
185} 251}
252
253/*****************************************************************************/
186 254
187typedef struct 255typedef struct
188{ 256{
189 struct ev_signal *head; 257 struct ev_signal *head;
190 sig_atomic_t gotsig; 258 sig_atomic_t gotsig;
233 if (signals [sig].gotsig) 301 if (signals [sig].gotsig)
234 { 302 {
235 signals [sig].gotsig = 0; 303 signals [sig].gotsig = 0;
236 304
237 for (w = signals [sig].head; w; w = w->next) 305 for (w = signals [sig].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL); 306 event ((W)w, EV_SIGNAL);
239 } 307 }
240} 308}
241 309
242static void 310static void
243siginit (void) 311siginit (void)
250 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 318 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
251 319
252 evio_set (&sigev, sigpipe [0], EV_READ); 320 evio_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev); 321 evio_start (&sigev);
254} 322}
323
324/*****************************************************************************/
325
326static struct ev_idle **idles;
327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
331
332static struct ev_check **checks;
333static int checkmax, checkcnt;
334
335/*****************************************************************************/
255 336
256#if HAVE_EPOLL 337#if HAVE_EPOLL
257# include "ev_epoll.c" 338# include "ev_epoll.c"
258#endif 339#endif
259#if HAVE_SELECT 340#if HAVE_SELECT
285 if (ev_method == EVMETHOD_NONE) select_init (flags); 366 if (ev_method == EVMETHOD_NONE) select_init (flags);
286#endif 367#endif
287 368
288 if (ev_method) 369 if (ev_method)
289 { 370 {
290 evw_init (&sigev, sigcb, 0); 371 evw_init (&sigev, sigcb);
291 siginit (); 372 siginit ();
292 } 373 }
293 374
294 return ev_method; 375 return ev_method;
295} 376}
296 377
378/*****************************************************************************/
379
297void ev_prefork (void) 380void ev_prefork (void)
298{ 381{
382 /* nop */
299} 383}
300 384
301void ev_postfork_parent (void) 385void ev_postfork_parent (void)
302{ 386{
387 /* nop */
303} 388}
304 389
305void ev_postfork_child (void) 390void ev_postfork_child (void)
306{ 391{
307#if HAVE_EPOLL 392#if HAVE_EPOLL
314 close (sigpipe [1]); 399 close (sigpipe [1]);
315 pipe (sigpipe); 400 pipe (sigpipe);
316 siginit (); 401 siginit ();
317} 402}
318 403
404/*****************************************************************************/
405
319static void 406static void
320fd_reify (void) 407fd_reify (void)
321{ 408{
322 int i; 409 int i;
323 410
343} 430}
344 431
345static void 432static void
346call_pending () 433call_pending ()
347{ 434{
348 int i; 435 while (pendingcnt)
349
350 for (i = 0; i < pendingcnt; ++i)
351 { 436 {
352 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
353 438
354 if (p->w) 439 if (p->w)
355 { 440 {
356 p->w->pending = 0; 441 p->w->pending = 0;
357 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
358 } 443 }
359 } 444 }
360
361 pendingcnt = 0;
362} 445}
363 446
364static void 447static void
365timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 448timers_reify ()
366{ 449{
367 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
368 { 451 {
369 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
370 455
371 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
372 if (w->repeat) 457 if (w->repeat)
373 { 458 {
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; 459 w->at = now + w->repeat;
378 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
379 assert (w->at > now);
380
381 downheap (timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
382 } 462 }
383 else 463 else
384 {
385 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
386 --timercnt; /* maybe pass by reference instead? */ 465 }
466}
467
468static void
469periodics_reify ()
470{
471 while (periodiccnt && periodics [0]->at <= ev_now)
472 {
473 struct ev_periodic *w = periodics [0];
474
475 /* first reschedule or stop timer */
476 if (w->interval)
387 } 477 {
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
479 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0);
481 }
482 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */
388 484
389 event ((struct ev_watcher *)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
486 }
487}
488
489static void
490periodics_reschedule (ev_tstamp diff)
491{
492 int i;
493
494 /* adjust periodics after time jump */
495 for (i = 0; i < periodiccnt; ++i)
496 {
497 struct ev_periodic *w = periodics [i];
498
499 if (w->interval)
500 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502
503 if (fabs (diff) >= 1e-4)
504 {
505 evperiodic_stop (w);
506 evperiodic_start (w);
507
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 }
510 }
390 } 511 }
391} 512}
392 513
393static void 514static void
394time_update () 515time_update ()
395{ 516{
396 int i; 517 int i;
518
397 ev_now = ev_time (); 519 ev_now = ev_time ();
398 520
399 if (have_monotonic) 521 if (have_monotonic)
400 { 522 {
401 ev_tstamp odiff = diff; 523 ev_tstamp odiff = diff;
402 524
403 /* detecting time jumps is much more difficult */
404 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 525 for (i = 4; --i; ) /* loop a few times, before making important decisions */
405 { 526 {
406 now = get_clock (); 527 now = get_clock ();
407 diff = ev_now - now; 528 diff = ev_now - now;
408 529
409 if (fabs (odiff - diff) < MIN_TIMEJUMP) 530 if (fabs (odiff - diff) < MIN_TIMEJUMP)
410 return; /* all is well */ 531 return; /* all is well */
411 532
412 ev_now = ev_time (); 533 ev_now = ev_time ();
413 } 534 }
414 535
415 /* time jump detected, reschedule atimers */ 536 periodics_reschedule (diff - odiff);
416 for (i = 0; i < atimercnt; ++i) 537 /* 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 } 538 }
422 else 539 else
423 { 540 {
424 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
425 /* time jump detected, adjust rtimers */ 542 {
543 periodics_reschedule (ev_now - now);
544
545 /* adjust timers. this is easy, as the offset is the same for all */
426 for (i = 0; i < rtimercnt; ++i) 546 for (i = 0; i < timercnt; ++i)
427 rtimers [i]->at += ev_now - now; 547 timers [i]->at += diff;
548 }
428 549
429 now = ev_now; 550 now = ev_now;
430 } 551 }
431} 552}
432 553
433int ev_loop_done; 554int ev_loop_done;
434 555
435void ev_loop (int flags) 556void ev_loop (int flags)
436{ 557{
437 double block; 558 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
439 560
440 do 561 do
441 { 562 {
563 /* queue check watchers (and execute them) */
564 if (preparecnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
442 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
443 fd_reify (); 571 fd_reify ();
444 572
445 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clockor timers, but as we basically
576 always have timers, we just calculate it always */
577 ev_now = ev_time ();
578
446 if (flags & EVLOOP_NONBLOCK) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
447 block = 0.; 580 block = 0.;
448 else 581 else
449 { 582 {
450 block = MAX_BLOCKTIME; 583 block = MAX_BLOCKTIME;
451 584
452 if (rtimercnt) 585 if (timercnt)
453 { 586 {
454 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 587 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
455 if (block > to) block = to; 588 if (block > to) block = to;
456 } 589 }
457 590
458 if (atimercnt) 591 if (periodiccnt)
459 { 592 {
460 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 593 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
461 if (block > to) block = to; 594 if (block > to) block = to;
462 } 595 }
463 596
464 if (block < 0.) block = 0.; 597 if (block < 0.) block = 0.;
465 } 598 }
467 method_poll (block); 600 method_poll (block);
468 601
469 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
470 time_update (); 603 time_update ();
471 604
472 /* put pending timers into pendign queue and reschedule them */ 605 /* queue pending timers and reschedule them */
473 /* absolute timers first */ 606 timers_reify (); /* relative timers called last */
474 timers_reify (atimers, atimercnt, ev_now); 607 periodics_reify (); /* absolute timers called first */
475 /* relative timers second */ 608
476 timers_reify (rtimers, rtimercnt, now); 609 /* queue idle watchers unless io or timers are pending */
610 if (!pendingcnt)
611 queue_events ((W *)idles, idlecnt, EV_IDLE);
612
613 /* queue check watchers, to be executed first */
614 if (checkcnt)
615 queue_events ((W *)checks, checkcnt, EV_CHECK);
477 616
478 call_pending (); 617 call_pending ();
479 } 618 }
480 while (!ev_loop_done); 619 while (!ev_loop_done);
481}
482 620
621 if (ev_loop_done != 2)
622 ev_loop_done = 0;
623}
624
625/*****************************************************************************/
626
483static void 627static void
484wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 628wlist_add (WL *head, WL elem)
485{ 629{
486 elem->next = *head; 630 elem->next = *head;
487 *head = elem; 631 *head = elem;
488} 632}
489 633
490static void 634static void
491wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 635wlist_del (WL *head, WL elem)
492{ 636{
493 while (*head) 637 while (*head)
494 { 638 {
495 if (*head == elem) 639 if (*head == elem)
496 { 640 {
501 head = &(*head)->next; 645 head = &(*head)->next;
502 } 646 }
503} 647}
504 648
505static void 649static void
506ev_start (struct ev_watcher *w, int active) 650ev_clear (W w)
507{ 651{
652 if (w->pending)
653 {
654 pendings [w->pending - 1].w = 0;
508 w->pending = 0; 655 w->pending = 0;
656 }
657}
658
659static void
660ev_start (W w, int active)
661{
509 w->active = active; 662 w->active = active;
510} 663}
511 664
512static void 665static void
513ev_stop (struct ev_watcher *w) 666ev_stop (W w)
514{ 667{
515 if (w->pending)
516 pendings [w->pending - 1].w = 0;
517
518 w->active = 0; 668 w->active = 0;
519 /* nop */
520} 669}
670
671/*****************************************************************************/
521 672
522void 673void
523evio_start (struct ev_io *w) 674evio_start (struct ev_io *w)
524{ 675{
525 if (ev_is_active (w)) 676 if (ev_is_active (w))
526 return; 677 return;
527 678
528 int fd = w->fd; 679 int fd = w->fd;
529 680
530 ev_start ((struct ev_watcher *)w, 1); 681 ev_start ((W)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 682 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 683 wlist_add ((WL *)&anfds[fd].head, (WL)w);
533 684
534 ++fdchangecnt; 685 ++fdchangecnt;
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 686 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd; 687 fdchanges [fdchangecnt - 1] = fd;
537} 688}
538 689
539void 690void
540evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
541{ 692{
693 ev_clear ((W)w);
542 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
543 return; 695 return;
544 696
545 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
546 ev_stop ((struct ev_watcher *)w); 698 ev_stop ((W)w);
547 699
548 ++fdchangecnt; 700 ++fdchangecnt;
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
551} 703}
554evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
555{ 707{
556 if (ev_is_active (w)) 708 if (ev_is_active (w))
557 return; 709 return;
558 710
559 if (w->is_abs) 711 w->at += now;
560 {
561 /* this formula differs from the one in timer_reify becuse we do not round up */
562 if (w->repeat)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
564 712
565 ev_start ((struct ev_watcher *)w, ++atimercnt); 713 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
714
715 ev_start ((W)w, ++timercnt);
566 array_needsize (atimers, atimermax, atimercnt, ); 716 array_needsize (timers, timermax, timercnt, );
567 atimers [atimercnt - 1] = w; 717 timers [timercnt - 1] = w;
568 upheap (atimers, atimercnt - 1); 718 upheap ((WT *)timers, timercnt - 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} 719}
581 720
582void 721void
583evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
584{ 723{
724 ev_clear ((W)w);
585 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
586 return; 726 return;
587 727
588 if (w->is_abs)
589 {
590 if (w->active < atimercnt--) 728 if (w->active < timercnt--)
591 { 729 {
592 atimers [w->active - 1] = atimers [atimercnt]; 730 timers [w->active - 1] = timers [timercnt];
731 downheap ((WT *)timers, timercnt, w->active - 1);
732 }
733
734 w->at = w->repeat;
735
736 ev_stop ((W)w);
737}
738
739void
740evtimer_again (struct ev_timer *w)
741{
742 if (ev_is_active (w))
743 {
744 if (w->repeat)
745 {
746 w->at = now + w->repeat;
593 downheap (atimers, atimercnt, w->active - 1); 747 downheap ((WT *)timers, timercnt, w->active - 1);
594 }
595 }
596 else
597 {
598 if (w->active < rtimercnt--)
599 { 748 }
600 rtimers [w->active - 1] = rtimers [rtimercnt]; 749 else
601 downheap (rtimers, rtimercnt, w->active - 1); 750 evtimer_stop (w);
602 } 751 }
752 else if (w->repeat)
753 evtimer_start (w);
754}
755
756void
757evperiodic_start (struct ev_periodic *w)
758{
759 if (ev_is_active (w))
760 return;
761
762 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
763
764 /* this formula differs from the one in periodic_reify because we do not always round up */
765 if (w->interval)
766 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
767
768 ev_start ((W)w, ++periodiccnt);
769 array_needsize (periodics, periodicmax, periodiccnt, );
770 periodics [periodiccnt - 1] = w;
771 upheap ((WT *)periodics, periodiccnt - 1);
772}
773
774void
775evperiodic_stop (struct ev_periodic *w)
776{
777 ev_clear ((W)w);
778 if (!ev_is_active (w))
779 return;
780
781 if (w->active < periodiccnt--)
603 } 782 {
783 periodics [w->active - 1] = periodics [periodiccnt];
784 downheap ((WT *)periodics, periodiccnt, w->active - 1);
785 }
604 786
605 ev_stop ((struct ev_watcher *)w); 787 ev_stop ((W)w);
606} 788}
607 789
608void 790void
609evsignal_start (struct ev_signal *w) 791evsignal_start (struct ev_signal *w)
610{ 792{
611 if (ev_is_active (w)) 793 if (ev_is_active (w))
612 return; 794 return;
613 795
614 ev_start ((struct ev_watcher *)w, 1); 796 ev_start ((W)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init); 797 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); 798 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
617 799
618 if (!w->next) 800 if (!w->next)
619 { 801 {
620 struct sigaction sa; 802 struct sigaction sa;
621 sa.sa_handler = sighandler; 803 sa.sa_handler = sighandler;
626} 808}
627 809
628void 810void
629evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
630{ 812{
813 ev_clear ((W)w);
631 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
632 return; 815 return;
633 816
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
635 ev_stop ((struct ev_watcher *)w); 818 ev_stop ((W)w);
636 819
637 if (!signals [w->signum - 1].head) 820 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL); 821 signal (w->signum, SIG_DFL);
639} 822}
640 823
824void evidle_start (struct ev_idle *w)
825{
826 if (ev_is_active (w))
827 return;
828
829 ev_start ((W)w, ++idlecnt);
830 array_needsize (idles, idlemax, idlecnt, );
831 idles [idlecnt - 1] = w;
832}
833
834void evidle_stop (struct ev_idle *w)
835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
840 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w);
842}
843
844void evprepare_start (struct ev_prepare *w)
845{
846 if (ev_is_active (w))
847 return;
848
849 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w;
852}
853
854void evprepare_stop (struct ev_prepare *w)
855{
856 ev_clear ((W)w);
857 if (ev_is_active (w))
858 return;
859
860 prepares [w->active - 1] = prepares [--preparecnt];
861 ev_stop ((W)w);
862}
863
864void evcheck_start (struct ev_check *w)
865{
866 if (ev_is_active (w))
867 return;
868
869 ev_start ((W)w, ++checkcnt);
870 array_needsize (checks, checkmax, checkcnt, );
871 checks [checkcnt - 1] = w;
872}
873
874void evcheck_stop (struct ev_check *w)
875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
880 checks [w->active - 1] = checks [--checkcnt];
881 ev_stop ((W)w);
882}
883
641/*****************************************************************************/ 884/*****************************************************************************/
885
886struct ev_once
887{
888 struct ev_io io;
889 struct ev_timer to;
890 void (*cb)(int revents, void *arg);
891 void *arg;
892};
893
894static void
895once_cb (struct ev_once *once, int revents)
896{
897 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg;
899
900 evio_stop (&once->io);
901 evtimer_stop (&once->to);
902 free (once);
903
904 cb (revents, arg);
905}
906
907static void
908once_cb_io (struct ev_io *w, int revents)
909{
910 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
911}
912
913static void
914once_cb_to (struct ev_timer *w, int revents)
915{
916 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
917}
918
919void
920ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
921{
922 struct ev_once *once = malloc (sizeof (struct ev_once));
923
924 if (!once)
925 cb (EV_ERROR, arg);
926 else
927 {
928 once->cb = cb;
929 once->arg = arg;
930
931 evw_init (&once->io, once_cb_io);
932
933 if (fd >= 0)
934 {
935 evio_set (&once->io, fd, events);
936 evio_start (&once->io);
937 }
938
939 evw_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.)
942 {
943 evtimer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to);
945 }
946 }
947}
948
949/*****************************************************************************/
950
642#if 1 951#if 0
952
953struct ev_io wio;
643 954
644static void 955static void
645sin_cb (struct ev_io *w, int revents) 956sin_cb (struct ev_io *w, int revents)
646{ 957{
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 958 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
657 968
658static void 969static void
659scb (struct ev_signal *w, int revents) 970scb (struct ev_signal *w, int revents)
660{ 971{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 972 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
973 evio_stop (&wio);
974 evio_start (&wio);
975}
976
977static void
978gcb (struct ev_signal *w, int revents)
979{
980 fprintf (stderr, "generic %x\n", revents);
981
662} 982}
663 983
664int main (void) 984int main (void)
665{ 985{
666 struct ev_io sin;
667
668 ev_init (0); 986 ev_init (0);
669 987
670 evw_init (&sin, sin_cb, 55);
671 evio_set (&sin, 0, EV_READ); 988 evio_init (&wio, sin_cb, 0, EV_READ);
672 evio_start (&sin); 989 evio_start (&wio);
673 990
674 struct ev_timer t[10000]; 991 struct ev_timer t[10000];
675 992
676#if 0 993#if 0
677 int i; 994 int i;
678 for (i = 0; i < 10000; ++i) 995 for (i = 0; i < 10000; ++i)
679 { 996 {
680 struct ev_timer *w = t + i; 997 struct ev_timer *w = t + i;
681 evw_init (w, ocb, i); 998 evw_init (w, ocb, i);
682 evtimer_set_abs (w, drand48 (), 0.99775533); 999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
683 evtimer_start (w); 1000 evtimer_start (w);
684 if (drand48 () < 0.5) 1001 if (drand48 () < 0.5)
685 evtimer_stop (w); 1002 evtimer_stop (w);
686 } 1003 }
687#endif 1004#endif
688 1005
689 struct ev_timer t1; 1006 struct ev_timer t1;
690 evw_init (&t1, ocb, 0); 1007 evtimer_init (&t1, ocb, 5, 10);
691 evtimer_set_abs (&t1, 5, 10);
692 evtimer_start (&t1); 1008 evtimer_start (&t1);
693 1009
694 struct ev_signal sig; 1010 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT); 1011 evsignal_init (&sig, scb, SIGQUIT);
697 evsignal_start (&sig); 1012 evsignal_start (&sig);
698 1013
1014 struct ev_check cw;
1015 evcheck_init (&cw, gcb);
1016 evcheck_start (&cw);
1017
1018 struct ev_idle iw;
1019 evidle_init (&iw, gcb);
1020 evidle_start (&iw);
1021
699 ev_loop (0); 1022 ev_loop (0);
700 1023
701 return 0; 1024 return 0;
702} 1025}
703 1026

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines