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

Comparing libev/ev.c (file contents):
Revision 1.9 by root, Wed Oct 31 07:24:17 2007 UTC vs.
Revision 1.20 by root, Wed Oct 31 18:28:00 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
75 111
76#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
77 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
78 { \ 114 { \
79 int newcnt = cur ? cur << 1 : 16; \ 115 int newcnt = cur ? cur << 1 : 16; \
80 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
81 base = realloc (base, sizeof (*base) * (newcnt)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
82 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
83 cur = newcnt; \ 118 cur = newcnt; \
84 } 119 }
85 120
108 } 143 }
109} 144}
110 145
111typedef struct 146typedef struct
112{ 147{
113 struct ev_watcher *w; 148 W w;
114 int events; 149 int events;
115} ANPENDING; 150} ANPENDING;
116 151
117static ANPENDING *pendings; 152static ANPENDING *pendings;
118static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
119 154
120static void 155static void
121event (struct ev_watcher *w, int events) 156event (W w, int events)
122{ 157{
158 if (w->active)
159 {
123 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
127} 165}
128 166
129static void 167static void
130fd_event (int fd, int events) 168fd_event (int fd, int events)
131{ 169{
135 for (w = anfd->head; w; w = w->next) 173 for (w = anfd->head; w; w = w->next)
136 { 174 {
137 int ev = w->events & events; 175 int ev = w->events & events;
138 176
139 if (ev) 177 if (ev)
140 event ((struct ev_watcher *)w, ev); 178 event ((W)w, ev);
141 } 179 }
142} 180}
143 181
144static void 182static void
145queue_events (struct ev_watcher **events, int eventcnt, int type) 183queue_events (W *events, int eventcnt, int type)
146{ 184{
147 int i; 185 int i;
148 186
149 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
150 event (events [i], type); 188 event (events [i], type);
151} 189}
152 190
153/*****************************************************************************/ 191/* called on EBADF to verify fds */
192static void
193fd_recheck ()
194{
195 int fd;
154 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
155static struct ev_timer **atimers; 206static struct ev_timer **timers;
156static int atimermax, atimercnt; 207static int timermax, timercnt;
157 208
158static struct ev_timer **rtimers; 209static struct ev_periodic **periodics;
159static int rtimermax, rtimercnt; 210static int periodicmax, periodiccnt;
160 211
161static void 212static void
162upheap (struct ev_timer **timers, int k) 213upheap (WT *timers, int k)
163{ 214{
164 struct ev_timer *w = timers [k]; 215 WT w = timers [k];
165 216
166 while (k && timers [k >> 1]->at > w->at) 217 while (k && timers [k >> 1]->at > w->at)
167 { 218 {
168 timers [k] = timers [k >> 1]; 219 timers [k] = timers [k >> 1];
169 timers [k]->active = k + 1; 220 timers [k]->active = k + 1;
174 timers [k]->active = k + 1; 225 timers [k]->active = k + 1;
175 226
176} 227}
177 228
178static void 229static void
179downheap (struct ev_timer **timers, int N, int k) 230downheap (WT *timers, int N, int k)
180{ 231{
181 struct ev_timer *w = timers [k]; 232 WT w = timers [k];
182 233
183 while (k < (N >> 1)) 234 while (k < (N >> 1))
184 { 235 {
185 int j = k << 1; 236 int j = k << 1;
186 237
250 if (signals [sig].gotsig) 301 if (signals [sig].gotsig)
251 { 302 {
252 signals [sig].gotsig = 0; 303 signals [sig].gotsig = 0;
253 304
254 for (w = signals [sig].head; w; w = w->next) 305 for (w = signals [sig].head; w; w = w->next)
255 event ((struct ev_watcher *)w, EV_SIGNAL); 306 event ((W)w, EV_SIGNAL);
256 } 307 }
257} 308}
258 309
259static void 310static void
260siginit (void) 311siginit (void)
272 323
273/*****************************************************************************/ 324/*****************************************************************************/
274 325
275static struct ev_idle **idles; 326static struct ev_idle **idles;
276static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
277 331
278static struct ev_check **checks; 332static struct ev_check **checks;
279static int checkmax, checkcnt; 333static int checkmax, checkcnt;
280 334
281/*****************************************************************************/ 335/*****************************************************************************/
312 if (ev_method == EVMETHOD_NONE) select_init (flags); 366 if (ev_method == EVMETHOD_NONE) select_init (flags);
313#endif 367#endif
314 368
315 if (ev_method) 369 if (ev_method)
316 { 370 {
317 evw_init (&sigev, sigcb, 0); 371 evw_init (&sigev, sigcb);
318 siginit (); 372 siginit ();
319 } 373 }
320 374
321 return ev_method; 375 return ev_method;
322} 376}
323 377
324/*****************************************************************************/ 378/*****************************************************************************/
325 379
326void ev_prefork (void) 380void ev_prefork (void)
327{ 381{
382 /* nop */
328} 383}
329 384
330void ev_postfork_parent (void) 385void ev_postfork_parent (void)
331{ 386{
387 /* nop */
332} 388}
333 389
334void ev_postfork_child (void) 390void ev_postfork_child (void)
335{ 391{
336#if HAVE_EPOLL 392#if HAVE_EPOLL
374} 430}
375 431
376static void 432static void
377call_pending () 433call_pending ()
378{ 434{
379 int i; 435 while (pendingcnt)
380
381 for (i = 0; i < pendingcnt; ++i)
382 { 436 {
383 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
384 438
385 if (p->w) 439 if (p->w)
386 { 440 {
387 p->w->pending = 0; 441 p->w->pending = 0;
388 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
389 } 443 }
390 } 444 }
391
392 pendingcnt = 0;
393} 445}
394 446
395static void 447static void
396timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 448timers_reify ()
397{ 449{
398 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
399 { 451 {
400 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
401 455
402 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
403 if (w->repeat) 457 if (w->repeat)
404 { 458 {
405 if (w->is_abs)
406 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
407 else
408 w->at = now + w->repeat; 459 w->at = now + w->repeat;
409 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
410 assert (w->at > now);
411
412 downheap (timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
413 } 462 }
414 else 463 else
415 {
416 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
417 --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)
418 } 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 */
419 484
420 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 }
421 } 511 }
422} 512}
423 513
424static void 514static void
425time_update () 515time_update ()
426{ 516{
427 int i; 517 int i;
518
428 ev_now = ev_time (); 519 ev_now = ev_time ();
429 520
430 if (have_monotonic) 521 if (have_monotonic)
431 { 522 {
432 ev_tstamp odiff = diff; 523 ev_tstamp odiff = diff;
433 524
434 /* detecting time jumps is much more difficult */
435 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 */
436 { 526 {
437 now = get_clock (); 527 now = get_clock ();
438 diff = ev_now - now; 528 diff = ev_now - now;
439 529
440 if (fabs (odiff - diff) < MIN_TIMEJUMP) 530 if (fabs (odiff - diff) < MIN_TIMEJUMP)
441 return; /* all is well */ 531 return; /* all is well */
442 532
443 ev_now = ev_time (); 533 ev_now = ev_time ();
444 } 534 }
445 535
446 /* time jump detected, reschedule atimers */ 536 periodics_reschedule (diff - odiff);
447 for (i = 0; i < atimercnt; ++i) 537 /* no timer adjustment, as the monotonic clock doesn't jump */
448 {
449 struct ev_timer *w = atimers [i];
450 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
451 }
452 } 538 }
453 else 539 else
454 { 540 {
455 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
456 /* 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 */
457 for (i = 0; i < rtimercnt; ++i) 546 for (i = 0; i < timercnt; ++i)
458 rtimers [i]->at += ev_now - now; 547 timers [i]->at += diff;
548 }
459 549
460 now = ev_now; 550 now = ev_now;
461 } 551 }
462} 552}
463 553
464int ev_loop_done; 554int ev_loop_done;
465 555
466void ev_loop (int flags) 556void ev_loop (int flags)
467{ 557{
468 double block; 558 double block;
469 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
470
471 if (checkcnt)
472 {
473 queue_events (checks, checkcnt, EV_CHECK);
474 call_pending ();
475 }
476 560
477 do 561 do
478 { 562 {
563 /* queue check watchers (and execute them) */
564 if (checkcnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
479 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
480 fd_reify (); 571 fd_reify ();
481 572
482 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
576 ev_now = ev_time ();
577
483 if (flags & EVLOOP_NONBLOCK || idlecnt) 578 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 579 block = 0.;
485 else 580 else
486 { 581 {
487 block = MAX_BLOCKTIME; 582 block = MAX_BLOCKTIME;
488 583
489 if (rtimercnt) 584 if (timercnt)
490 { 585 {
491 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 586 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
492 if (block > to) block = to; 587 if (block > to) block = to;
493 } 588 }
494 589
495 if (atimercnt) 590 if (periodiccnt)
496 { 591 {
497 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 592 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
498 if (block > to) block = to; 593 if (block > to) block = to;
499 } 594 }
500 595
501 if (block < 0.) block = 0.; 596 if (block < 0.) block = 0.;
502 } 597 }
505 600
506 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
507 time_update (); 602 time_update ();
508 603
509 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
510 /* absolute timers first */ 605 timers_reify (); /* relative timers called last */
511 timers_reify (atimers, atimercnt, ev_now); 606 periodics_reify (); /* absolute timers called first */
512 /* relative timers second */
513 timers_reify (rtimers, rtimercnt, now);
514 607
515 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
516 if (!pendingcnt) 609 if (!pendingcnt)
517 queue_events (idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
518 611
519 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
520 queue_events (checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 615
522 call_pending (); 616 call_pending ();
523 } 617 }
524 while (!ev_loop_done); 618 while (!ev_loop_done);
525}
526 619
527/*****************************************************************************/ 620 if (ev_loop_done != 2)
621 ev_loop_done = 0;
622}
528 623
624/*****************************************************************************/
625
529static void 626static void
530wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 627wlist_add (WL *head, WL elem)
531{ 628{
532 elem->next = *head; 629 elem->next = *head;
533 *head = elem; 630 *head = elem;
534} 631}
535 632
536static void 633static void
537wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 634wlist_del (WL *head, WL elem)
538{ 635{
539 while (*head) 636 while (*head)
540 { 637 {
541 if (*head == elem) 638 if (*head == elem)
542 { 639 {
547 head = &(*head)->next; 644 head = &(*head)->next;
548 } 645 }
549} 646}
550 647
551static void 648static void
552ev_start (struct ev_watcher *w, int active) 649ev_clear (W w)
553{ 650{
651 if (w->pending)
652 {
653 pendings [w->pending - 1].w = 0;
554 w->pending = 0; 654 w->pending = 0;
655 }
656}
657
658static void
659ev_start (W w, int active)
660{
555 w->active = active; 661 w->active = active;
556} 662}
557 663
558static void 664static void
559ev_stop (struct ev_watcher *w) 665ev_stop (W w)
560{ 666{
561 if (w->pending)
562 pendings [w->pending - 1].w = 0;
563
564 w->active = 0; 667 w->active = 0;
565 /* nop */
566} 668}
567 669
568/*****************************************************************************/ 670/*****************************************************************************/
569 671
570void 672void
573 if (ev_is_active (w)) 675 if (ev_is_active (w))
574 return; 676 return;
575 677
576 int fd = w->fd; 678 int fd = w->fd;
577 679
578 ev_start ((struct ev_watcher *)w, 1); 680 ev_start ((W)w, 1);
579 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 681 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
580 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 682 wlist_add ((WL *)&anfds[fd].head, (WL)w);
581 683
582 ++fdchangecnt; 684 ++fdchangecnt;
583 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
584 fdchanges [fdchangecnt - 1] = fd; 686 fdchanges [fdchangecnt - 1] = fd;
585} 687}
586 688
587void 689void
588evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
589{ 691{
692 ev_clear ((W)w);
590 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
591 return; 694 return;
592 695
593 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
594 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
595 698
596 ++fdchangecnt; 699 ++fdchangecnt;
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = w->fd; 701 fdchanges [fdchangecnt - 1] = w->fd;
599} 702}
602evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
603{ 706{
604 if (ev_is_active (w)) 707 if (ev_is_active (w))
605 return; 708 return;
606 709
607 if (w->is_abs) 710 w->at += now;
608 {
609 /* this formula differs from the one in timer_reify becuse we do not round up */
610 if (w->repeat)
611 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
612 711
613 ev_start ((struct ev_watcher *)w, ++atimercnt); 712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
713
714 ev_start ((W)w, ++timercnt);
614 array_needsize (atimers, atimermax, atimercnt, ); 715 array_needsize (timers, timermax, timercnt, );
615 atimers [atimercnt - 1] = w; 716 timers [timercnt - 1] = w;
616 upheap (atimers, atimercnt - 1); 717 upheap ((WT *)timers, timercnt - 1);
617 }
618 else
619 {
620 w->at += now;
621
622 ev_start ((struct ev_watcher *)w, ++rtimercnt);
623 array_needsize (rtimers, rtimermax, rtimercnt, );
624 rtimers [rtimercnt - 1] = w;
625 upheap (rtimers, rtimercnt - 1);
626 }
627
628} 718}
629 719
630void 720void
631evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
632{ 722{
723 ev_clear ((W)w);
633 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
634 return; 725 return;
635 726
636 if (w->is_abs)
637 {
638 if (w->active < atimercnt--) 727 if (w->active < timercnt--)
639 { 728 {
640 atimers [w->active - 1] = atimers [atimercnt]; 729 timers [w->active - 1] = timers [timercnt];
730 downheap ((WT *)timers, timercnt, w->active - 1);
731 }
732
733 w->at = w->repeat;
734
735 ev_stop ((W)w);
736}
737
738void
739evtimer_again (struct ev_timer *w)
740{
741 if (ev_is_active (w))
742 {
743 if (w->repeat)
744 {
745 w->at = now + w->repeat;
641 downheap (atimers, atimercnt, w->active - 1); 746 downheap ((WT *)timers, timercnt, w->active - 1);
642 }
643 }
644 else
645 {
646 if (w->active < rtimercnt--)
647 { 747 }
648 rtimers [w->active - 1] = rtimers [rtimercnt]; 748 else
649 downheap (rtimers, rtimercnt, w->active - 1); 749 evtimer_stop (w);
650 } 750 }
751 else if (w->repeat)
752 evtimer_start (w);
753}
754
755void
756evperiodic_start (struct ev_periodic *w)
757{
758 if (ev_is_active (w))
759 return;
760
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
762
763 /* this formula differs from the one in periodic_reify because we do not always round up */
764 if (w->interval)
765 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
766
767 ev_start ((W)w, ++periodiccnt);
768 array_needsize (periodics, periodicmax, periodiccnt, );
769 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1);
771}
772
773void
774evperiodic_stop (struct ev_periodic *w)
775{
776 ev_clear ((W)w);
777 if (!ev_is_active (w))
778 return;
779
780 if (w->active < periodiccnt--)
651 } 781 {
782 periodics [w->active - 1] = periodics [periodiccnt];
783 downheap ((WT *)periodics, periodiccnt, w->active - 1);
784 }
652 785
653 ev_stop ((struct ev_watcher *)w); 786 ev_stop ((W)w);
654} 787}
655 788
656void 789void
657evsignal_start (struct ev_signal *w) 790evsignal_start (struct ev_signal *w)
658{ 791{
659 if (ev_is_active (w)) 792 if (ev_is_active (w))
660 return; 793 return;
661 794
662 ev_start ((struct ev_watcher *)w, 1); 795 ev_start ((W)w, 1);
663 array_needsize (signals, signalmax, w->signum, signals_init); 796 array_needsize (signals, signalmax, w->signum, signals_init);
664 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 797 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
665 798
666 if (!w->next) 799 if (!w->next)
667 { 800 {
668 struct sigaction sa; 801 struct sigaction sa;
669 sa.sa_handler = sighandler; 802 sa.sa_handler = sighandler;
674} 807}
675 808
676void 809void
677evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
678{ 811{
812 ev_clear ((W)w);
679 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
680 return; 814 return;
681 815
682 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
683 ev_stop ((struct ev_watcher *)w); 817 ev_stop ((W)w);
684 818
685 if (!signals [w->signum - 1].head) 819 if (!signals [w->signum - 1].head)
686 signal (w->signum, SIG_DFL); 820 signal (w->signum, SIG_DFL);
687} 821}
688 822
689void evidle_start (struct ev_idle *w) 823void evidle_start (struct ev_idle *w)
690{ 824{
691 if (ev_is_active (w)) 825 if (ev_is_active (w))
692 return; 826 return;
693 827
694 ev_start ((struct ev_watcher *)w, ++idlecnt); 828 ev_start ((W)w, ++idlecnt);
695 array_needsize (idles, idlemax, idlecnt, ); 829 array_needsize (idles, idlemax, idlecnt, );
696 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
697} 831}
698 832
699void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
700{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
701 idles [w->active - 1] = idles [--idlecnt]; 839 idles [w->active - 1] = idles [--idlecnt];
702 ev_stop ((struct ev_watcher *)w); 840 ev_stop ((W)w);
841}
842
843void evprepare_start (struct ev_prepare *w)
844{
845 if (ev_is_active (w))
846 return;
847
848 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w;
851}
852
853void evprepare_stop (struct ev_prepare *w)
854{
855 ev_clear ((W)w);
856 if (ev_is_active (w))
857 return;
858
859 prepares [w->active - 1] = prepares [--preparecnt];
860 ev_stop ((W)w);
703} 861}
704 862
705void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
706{ 864{
707 if (ev_is_active (w)) 865 if (ev_is_active (w))
708 return; 866 return;
709 867
710 ev_start ((struct ev_watcher *)w, ++checkcnt); 868 ev_start ((W)w, ++checkcnt);
711 array_needsize (checks, checkmax, checkcnt, ); 869 array_needsize (checks, checkmax, checkcnt, );
712 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
713} 871}
714 872
715void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
716{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
717 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
718 ev_stop ((struct ev_watcher *)w); 880 ev_stop ((W)w);
719} 881}
720 882
721/*****************************************************************************/ 883/*****************************************************************************/
884
885struct ev_once
886{
887 struct ev_io io;
888 struct ev_timer to;
889 void (*cb)(int revents, void *arg);
890 void *arg;
891};
892
893static void
894once_cb (struct ev_once *once, int revents)
895{
896 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg;
898
899 evio_stop (&once->io);
900 evtimer_stop (&once->to);
901 free (once);
902
903 cb (revents, arg);
904}
905
906static void
907once_cb_io (struct ev_io *w, int revents)
908{
909 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
910}
911
912static void
913once_cb_to (struct ev_timer *w, int revents)
914{
915 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
916}
917
918void
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{
921 struct ev_once *once = malloc (sizeof (struct ev_once));
922
923 if (!once)
924 cb (EV_ERROR, arg);
925 else
926 {
927 once->cb = cb;
928 once->arg = arg;
929
930 evw_init (&once->io, once_cb_io);
931
932 if (fd >= 0)
933 {
934 evio_set (&once->io, fd, events);
935 evio_start (&once->io);
936 }
937
938 evw_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.)
941 {
942 evtimer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to);
944 }
945 }
946}
947
948/*****************************************************************************/
949
722#if 1 950#if 0
951
952struct ev_io wio;
723 953
724static void 954static void
725sin_cb (struct ev_io *w, int revents) 955sin_cb (struct ev_io *w, int revents)
726{ 956{
727 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 957 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
737 967
738static void 968static void
739scb (struct ev_signal *w, int revents) 969scb (struct ev_signal *w, int revents)
740{ 970{
741 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 971 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio);
973 evio_start (&wio);
742} 974}
743 975
744static void 976static void
745gcb (struct ev_signal *w, int revents) 977gcb (struct ev_signal *w, int revents)
746{ 978{
747 fprintf (stderr, "generic %x\n", revents); 979 fprintf (stderr, "generic %x\n", revents);
980
748} 981}
749 982
750int main (void) 983int main (void)
751{ 984{
752 struct ev_io sin;
753
754 ev_init (0); 985 ev_init (0);
755 986
756 evw_init (&sin, sin_cb, 55);
757 evio_set (&sin, 0, EV_READ); 987 evio_init (&wio, sin_cb, 0, EV_READ);
758 evio_start (&sin); 988 evio_start (&wio);
759 989
760 struct ev_timer t[10000]; 990 struct ev_timer t[10000];
761 991
762#if 0 992#if 0
763 int i; 993 int i;
764 for (i = 0; i < 10000; ++i) 994 for (i = 0; i < 10000; ++i)
765 { 995 {
766 struct ev_timer *w = t + i; 996 struct ev_timer *w = t + i;
767 evw_init (w, ocb, i); 997 evw_init (w, ocb, i);
768 evtimer_set_abs (w, drand48 (), 0.99775533); 998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
769 evtimer_start (w); 999 evtimer_start (w);
770 if (drand48 () < 0.5) 1000 if (drand48 () < 0.5)
771 evtimer_stop (w); 1001 evtimer_stop (w);
772 } 1002 }
773#endif 1003#endif
774 1004
775 struct ev_timer t1; 1005 struct ev_timer t1;
776 evw_init (&t1, ocb, 0); 1006 evtimer_init (&t1, ocb, 5, 10);
777 evtimer_set_abs (&t1, 5, 10);
778 evtimer_start (&t1); 1007 evtimer_start (&t1);
779 1008
780 struct ev_signal sig; 1009 struct ev_signal sig;
781 evw_init (&sig, scb, 65535);
782 evsignal_set (&sig, SIGQUIT); 1010 evsignal_init (&sig, scb, SIGQUIT);
783 evsignal_start (&sig); 1011 evsignal_start (&sig);
784 1012
785 struct ev_check cw; 1013 struct ev_check cw;
786 evw_init (&cw, gcb, 0); 1014 evcheck_init (&cw, gcb);
787 evcheck_start (&cw); 1015 evcheck_start (&cw);
788 1016
789 struct ev_idle iw; 1017 struct ev_idle iw;
790 evw_init (&iw, gcb, 0); 1018 evidle_init (&iw, gcb);
791 evidle_start (&iw); 1019 evidle_start (&iw);
792 1020
793 ev_loop (0); 1021 ev_loop (0);
794 1022
795 return 0; 1023 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines