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.16 by root, Wed Oct 31 13:57:34 2007 UTC

1#include <math.h> 1#include <math.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <unistd.h> 3#include <unistd.h>
4#include <fcntl.h> 4#include <fcntl.h>
5#include <signal.h> 5#include <signal.h>
6#include <stddef.h>
6 7
7#include <stdio.h> 8#include <stdio.h>
8 9
9#include <assert.h> 10#include <assert.h>
10#include <errno.h> 11#include <errno.h>
11#include <sys/time.h> 12#include <sys/time.h>
12#include <time.h> 13#include <time.h>
13 14
14#ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1
16#endif
17
18#define HAVE_REALTIME 1
19#define HAVE_EPOLL 1 15#define HAVE_EPOLL 1
16
17#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1
20# endif
21#endif
22
23#ifndef HAVE_SELECT
20#define HAVE_SELECT 1 24# define HAVE_SELECT 1
25#endif
26
27#ifndef HAVE_EPOLL
28# define HAVE_EPOLL 0
29#endif
30
31#ifndef HAVE_REALTIME
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif
21 34
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 35#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 36#define MAX_BLOCKTIME 60.
24 37
25#include "ev.h" 38#include "ev.h"
26 39
27struct ev_watcher { 40typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 41typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 42typedef struct ev_watcher_time *WT;
33};
34 43
35static ev_tstamp now, diff; /* monotonic clock */ 44static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 45ev_tstamp ev_now;
37int ev_method; 46int ev_method;
38 47
75 84
76#define array_needsize(base,cur,cnt,init) \ 85#define array_needsize(base,cur,cnt,init) \
77 if ((cnt) > cur) \ 86 if ((cnt) > cur) \
78 { \ 87 { \
79 int newcnt = cur ? cur << 1 : 16; \ 88 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)); \ 89 base = realloc (base, sizeof (*base) * (newcnt)); \
82 init (base + cur, newcnt - cur); \ 90 init (base + cur, newcnt - cur); \
83 cur = newcnt; \ 91 cur = newcnt; \
84 } 92 }
85 93
108 } 116 }
109} 117}
110 118
111typedef struct 119typedef struct
112{ 120{
113 struct ev_watcher *w; 121 W w;
114 int events; 122 int events;
115} ANPENDING; 123} ANPENDING;
116 124
117static ANPENDING *pendings; 125static ANPENDING *pendings;
118static int pendingmax, pendingcnt; 126static int pendingmax, pendingcnt;
119 127
120static void 128static void
121event (struct ev_watcher *w, int events) 129event (W w, int events)
122{ 130{
131 if (w->active)
132 {
123 w->pending = ++pendingcnt; 133 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, ); 134 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w; 135 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events; 136 pendings [pendingcnt - 1].events = events;
137 }
127} 138}
128 139
129static void 140static void
130fd_event (int fd, int events) 141fd_event (int fd, int events)
131{ 142{
135 for (w = anfd->head; w; w = w->next) 146 for (w = anfd->head; w; w = w->next)
136 { 147 {
137 int ev = w->events & events; 148 int ev = w->events & events;
138 149
139 if (ev) 150 if (ev)
140 event ((struct ev_watcher *)w, ev); 151 event ((W)w, ev);
141 } 152 }
142} 153}
143 154
144static void 155static void
145queue_events (struct ev_watcher **events, int eventcnt, int type) 156queue_events (W *events, int eventcnt, int type)
146{ 157{
147 int i; 158 int i;
148 159
149 for (i = 0; i < eventcnt; ++i) 160 for (i = 0; i < eventcnt; ++i)
150 event (events [i], type); 161 event (events [i], type);
151} 162}
152 163
153/*****************************************************************************/ 164/*****************************************************************************/
154 165
155static struct ev_timer **atimers; 166static struct ev_timer **timers;
156static int atimermax, atimercnt; 167static int timermax, timercnt;
157 168
158static struct ev_timer **rtimers; 169static struct ev_periodic **periodics;
159static int rtimermax, rtimercnt; 170static int periodicmax, periodiccnt;
160 171
161static void 172static void
162upheap (struct ev_timer **timers, int k) 173upheap (WT *timers, int k)
163{ 174{
164 struct ev_timer *w = timers [k]; 175 WT w = timers [k];
165 176
166 while (k && timers [k >> 1]->at > w->at) 177 while (k && timers [k >> 1]->at > w->at)
167 { 178 {
168 timers [k] = timers [k >> 1]; 179 timers [k] = timers [k >> 1];
169 timers [k]->active = k + 1; 180 timers [k]->active = k + 1;
174 timers [k]->active = k + 1; 185 timers [k]->active = k + 1;
175 186
176} 187}
177 188
178static void 189static void
179downheap (struct ev_timer **timers, int N, int k) 190downheap (WT *timers, int N, int k)
180{ 191{
181 struct ev_timer *w = timers [k]; 192 WT w = timers [k];
182 193
183 while (k < (N >> 1)) 194 while (k < (N >> 1))
184 { 195 {
185 int j = k << 1; 196 int j = k << 1;
186 197
250 if (signals [sig].gotsig) 261 if (signals [sig].gotsig)
251 { 262 {
252 signals [sig].gotsig = 0; 263 signals [sig].gotsig = 0;
253 264
254 for (w = signals [sig].head; w; w = w->next) 265 for (w = signals [sig].head; w; w = w->next)
255 event ((struct ev_watcher *)w, EV_SIGNAL); 266 event ((W)w, EV_SIGNAL);
256 } 267 }
257} 268}
258 269
259static void 270static void
260siginit (void) 271siginit (void)
312 if (ev_method == EVMETHOD_NONE) select_init (flags); 323 if (ev_method == EVMETHOD_NONE) select_init (flags);
313#endif 324#endif
314 325
315 if (ev_method) 326 if (ev_method)
316 { 327 {
317 evw_init (&sigev, sigcb, 0); 328 evw_init (&sigev, sigcb);
318 siginit (); 329 siginit ();
319 } 330 }
320 331
321 return ev_method; 332 return ev_method;
322} 333}
323 334
324/*****************************************************************************/ 335/*****************************************************************************/
325 336
326void ev_prefork (void) 337void ev_prefork (void)
327{ 338{
339 /* nop */
328} 340}
329 341
330void ev_postfork_parent (void) 342void ev_postfork_parent (void)
331{ 343{
344 /* nop */
332} 345}
333 346
334void ev_postfork_child (void) 347void ev_postfork_child (void)
335{ 348{
336#if HAVE_EPOLL 349#if HAVE_EPOLL
391 404
392 pendingcnt = 0; 405 pendingcnt = 0;
393} 406}
394 407
395static void 408static void
396timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 409timers_reify ()
397{ 410{
398 while (timercnt && timers [0]->at <= now) 411 while (timercnt && timers [0]->at <= now)
399 { 412 {
400 struct ev_timer *w = timers [0]; 413 struct ev_timer *w = timers [0];
414
415 event ((W)w, EV_TIMEOUT);
401 416
402 /* first reschedule or stop timer */ 417 /* first reschedule or stop timer */
403 if (w->repeat) 418 if (w->repeat)
404 { 419 {
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; 420 w->at = now + w->repeat;
409 421 assert (("timer timeout in the past, negative repeat?", w->at > now));
410 assert (w->at > now);
411
412 downheap (timers, timercnt, 0); 422 downheap ((WT *)timers, timercnt, 0);
413 } 423 }
414 else 424 else
415 {
416 evtimer_stop (w); /* nonrepeating: stop timer */ 425 evtimer_stop (w); /* nonrepeating: stop timer */
417 --timercnt; /* maybe pass by reference instead? */ 426 }
427}
428
429static void
430periodics_reify ()
431{
432 while (periodiccnt && periodics [0]->at <= ev_now)
433 {
434 struct ev_periodic *w = periodics [0];
435
436 /* first reschedule or stop timer */
437 if (w->interval)
418 } 438 {
439 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
440 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
441 downheap ((WT *)periodics, periodiccnt, 0);
442 }
443 else
444 evperiodic_stop (w); /* nonrepeating: stop timer */
419 445
420 event ((struct ev_watcher *)w, EV_TIMEOUT); 446 event ((W)w, EV_TIMEOUT);
447 }
448}
449
450static void
451periodics_reschedule (ev_tstamp diff)
452{
453 int i;
454
455 /* adjust periodics after time jump */
456 for (i = 0; i < periodiccnt; ++i)
457 {
458 struct ev_periodic *w = periodics [i];
459
460 if (w->interval)
461 {
462 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
463
464 if (fabs (diff) >= 1e-4)
465 {
466 evperiodic_stop (w);
467 evperiodic_start (w);
468
469 i = 0; /* restart loop, inefficient, but time jumps should be rare */
470 }
471 }
421 } 472 }
422} 473}
423 474
424static void 475static void
425time_update () 476time_update ()
426{ 477{
427 int i; 478 int i;
479
428 ev_now = ev_time (); 480 ev_now = ev_time ();
429 481
430 if (have_monotonic) 482 if (have_monotonic)
431 { 483 {
432 ev_tstamp odiff = diff; 484 ev_tstamp odiff = diff;
433 485
434 /* detecting time jumps is much more difficult */
435 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 486 for (i = 4; --i; ) /* loop a few times, before making important decisions */
436 { 487 {
437 now = get_clock (); 488 now = get_clock ();
438 diff = ev_now - now; 489 diff = ev_now - now;
439 490
440 if (fabs (odiff - diff) < MIN_TIMEJUMP) 491 if (fabs (odiff - diff) < MIN_TIMEJUMP)
441 return; /* all is well */ 492 return; /* all is well */
442 493
443 ev_now = ev_time (); 494 ev_now = ev_time ();
444 } 495 }
445 496
446 /* time jump detected, reschedule atimers */ 497 periodics_reschedule (diff - odiff);
447 for (i = 0; i < atimercnt; ++i) 498 /* 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 } 499 }
453 else 500 else
454 { 501 {
455 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 502 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
456 /* time jump detected, adjust rtimers */ 503 {
504 periodics_reschedule (ev_now - now);
505
506 /* adjust timers. this is easy, as the offset is the same for all */
457 for (i = 0; i < rtimercnt; ++i) 507 for (i = 0; i < timercnt; ++i)
458 rtimers [i]->at += ev_now - now; 508 timers [i]->at += diff;
509 }
459 510
460 now = ev_now; 511 now = ev_now;
461 } 512 }
462} 513}
463 514
464int ev_loop_done; 515int ev_loop_done;
465 516
466void ev_loop (int flags) 517void ev_loop (int flags)
467{ 518{
468 double block; 519 double block;
469 ev_loop_done = flags & EVLOOP_ONESHOT; 520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
470 521
471 if (checkcnt) 522 if (checkcnt)
472 { 523 {
473 queue_events (checks, checkcnt, EV_CHECK); 524 queue_events ((W *)checks, checkcnt, EV_CHECK);
474 call_pending (); 525 call_pending ();
475 } 526 }
476 527
477 do 528 do
478 { 529 {
479 /* update fd-related kernel structures */ 530 /* update fd-related kernel structures */
480 fd_reify (); 531 fd_reify ();
481 532
482 /* calculate blocking time */ 533 /* calculate blocking time */
534
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
536 ev_now = ev_time ();
537
483 if (flags & EVLOOP_NONBLOCK || idlecnt) 538 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 539 block = 0.;
485 else 540 else
486 { 541 {
487 block = MAX_BLOCKTIME; 542 block = MAX_BLOCKTIME;
488 543
489 if (rtimercnt) 544 if (timercnt)
490 { 545 {
491 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 546 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
492 if (block > to) block = to; 547 if (block > to) block = to;
493 } 548 }
494 549
495 if (atimercnt) 550 if (periodiccnt)
496 { 551 {
497 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 552 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
498 if (block > to) block = to; 553 if (block > to) block = to;
499 } 554 }
500 555
501 if (block < 0.) block = 0.; 556 if (block < 0.) block = 0.;
502 } 557 }
505 560
506 /* update ev_now, do magic */ 561 /* update ev_now, do magic */
507 time_update (); 562 time_update ();
508 563
509 /* queue pending timers and reschedule them */ 564 /* queue pending timers and reschedule them */
510 /* absolute timers first */ 565 periodics_reify (); /* absolute timers first */
511 timers_reify (atimers, atimercnt, ev_now);
512 /* relative timers second */ 566 timers_reify (); /* relative timers second */
513 timers_reify (rtimers, rtimercnt, now);
514 567
515 /* queue idle watchers unless io or timers are pending */ 568 /* queue idle watchers unless io or timers are pending */
516 if (!pendingcnt) 569 if (!pendingcnt)
517 queue_events (idles, idlecnt, EV_IDLE); 570 queue_events ((W *)idles, idlecnt, EV_IDLE);
518 571
519 /* queue check and possibly idle watchers */ 572 /* queue check and possibly idle watchers */
520 queue_events (checks, checkcnt, EV_CHECK); 573 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 574
522 call_pending (); 575 call_pending ();
523 } 576 }
524 while (!ev_loop_done); 577 while (!ev_loop_done);
525}
526 578
527/*****************************************************************************/ 579 if (ev_loop_done != 2)
580 ev_loop_done = 0;
581}
528 582
583/*****************************************************************************/
584
529static void 585static void
530wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 586wlist_add (WL *head, WL elem)
531{ 587{
532 elem->next = *head; 588 elem->next = *head;
533 *head = elem; 589 *head = elem;
534} 590}
535 591
536static void 592static void
537wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 593wlist_del (WL *head, WL elem)
538{ 594{
539 while (*head) 595 while (*head)
540 { 596 {
541 if (*head == elem) 597 if (*head == elem)
542 { 598 {
547 head = &(*head)->next; 603 head = &(*head)->next;
548 } 604 }
549} 605}
550 606
551static void 607static void
552ev_start (struct ev_watcher *w, int active) 608ev_clear (W w)
553{ 609{
610 if (w->pending)
611 {
612 pendings [w->pending - 1].w = 0;
554 w->pending = 0; 613 w->pending = 0;
614 }
615}
616
617static void
618ev_start (W w, int active)
619{
555 w->active = active; 620 w->active = active;
556} 621}
557 622
558static void 623static void
559ev_stop (struct ev_watcher *w) 624ev_stop (W w)
560{ 625{
561 if (w->pending)
562 pendings [w->pending - 1].w = 0;
563
564 w->active = 0; 626 w->active = 0;
565 /* nop */
566} 627}
567 628
568/*****************************************************************************/ 629/*****************************************************************************/
569 630
570void 631void
573 if (ev_is_active (w)) 634 if (ev_is_active (w))
574 return; 635 return;
575 636
576 int fd = w->fd; 637 int fd = w->fd;
577 638
578 ev_start ((struct ev_watcher *)w, 1); 639 ev_start ((W)w, 1);
579 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 640 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
580 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 641 wlist_add ((WL *)&anfds[fd].head, (WL)w);
581 642
582 ++fdchangecnt; 643 ++fdchangecnt;
583 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 644 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
584 fdchanges [fdchangecnt - 1] = fd; 645 fdchanges [fdchangecnt - 1] = fd;
585} 646}
586 647
587void 648void
588evio_stop (struct ev_io *w) 649evio_stop (struct ev_io *w)
589{ 650{
651 ev_clear ((W)w);
590 if (!ev_is_active (w)) 652 if (!ev_is_active (w))
591 return; 653 return;
592 654
593 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 655 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
594 ev_stop ((struct ev_watcher *)w); 656 ev_stop ((W)w);
595 657
596 ++fdchangecnt; 658 ++fdchangecnt;
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 659 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = w->fd; 660 fdchanges [fdchangecnt - 1] = w->fd;
599} 661}
602evtimer_start (struct ev_timer *w) 664evtimer_start (struct ev_timer *w)
603{ 665{
604 if (ev_is_active (w)) 666 if (ev_is_active (w))
605 return; 667 return;
606 668
607 if (w->is_abs) 669 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 670
613 ev_start ((struct ev_watcher *)w, ++atimercnt); 671 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
672
673 ev_start ((W)w, ++timercnt);
614 array_needsize (atimers, atimermax, atimercnt, ); 674 array_needsize (timers, timermax, timercnt, );
615 atimers [atimercnt - 1] = w; 675 timers [timercnt - 1] = w;
616 upheap (atimers, atimercnt - 1); 676 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} 677}
629 678
630void 679void
631evtimer_stop (struct ev_timer *w) 680evtimer_stop (struct ev_timer *w)
632{ 681{
682 ev_clear ((W)w);
633 if (!ev_is_active (w)) 683 if (!ev_is_active (w))
634 return; 684 return;
635 685
636 if (w->is_abs)
637 {
638 if (w->active < atimercnt--) 686 if (w->active < timercnt--)
639 { 687 {
640 atimers [w->active - 1] = atimers [atimercnt]; 688 timers [w->active - 1] = timers [timercnt];
689 downheap ((WT *)timers, timercnt, w->active - 1);
690 }
691
692 w->at = w->repeat;
693
694 ev_stop ((W)w);
695}
696
697void
698evtimer_again (struct ev_timer *w)
699{
700 if (ev_is_active (w))
701 {
702 if (w->repeat)
703 {
704 w->at = now + w->repeat;
641 downheap (atimers, atimercnt, w->active - 1); 705 downheap ((WT *)timers, timercnt, w->active - 1);
642 }
643 }
644 else
645 {
646 if (w->active < rtimercnt--)
647 { 706 }
648 rtimers [w->active - 1] = rtimers [rtimercnt]; 707 else
649 downheap (rtimers, rtimercnt, w->active - 1); 708 evtimer_stop (w);
650 } 709 }
710 else if (w->repeat)
711 evtimer_start (w);
712}
713
714void
715evperiodic_start (struct ev_periodic *w)
716{
717 if (ev_is_active (w))
718 return;
719
720 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
721
722 /* this formula differs from the one in periodic_reify because we do not always round up */
723 if (w->interval)
724 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
725
726 ev_start ((W)w, ++periodiccnt);
727 array_needsize (periodics, periodicmax, periodiccnt, );
728 periodics [periodiccnt - 1] = w;
729 upheap ((WT *)periodics, periodiccnt - 1);
730}
731
732void
733evperiodic_stop (struct ev_periodic *w)
734{
735 ev_clear ((W)w);
736 if (!ev_is_active (w))
737 return;
738
739 if (w->active < periodiccnt--)
651 } 740 {
741 periodics [w->active - 1] = periodics [periodiccnt];
742 downheap ((WT *)periodics, periodiccnt, w->active - 1);
743 }
652 744
653 ev_stop ((struct ev_watcher *)w); 745 ev_stop ((W)w);
654} 746}
655 747
656void 748void
657evsignal_start (struct ev_signal *w) 749evsignal_start (struct ev_signal *w)
658{ 750{
659 if (ev_is_active (w)) 751 if (ev_is_active (w))
660 return; 752 return;
661 753
662 ev_start ((struct ev_watcher *)w, 1); 754 ev_start ((W)w, 1);
663 array_needsize (signals, signalmax, w->signum, signals_init); 755 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); 756 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
665 757
666 if (!w->next) 758 if (!w->next)
667 { 759 {
668 struct sigaction sa; 760 struct sigaction sa;
669 sa.sa_handler = sighandler; 761 sa.sa_handler = sighandler;
674} 766}
675 767
676void 768void
677evsignal_stop (struct ev_signal *w) 769evsignal_stop (struct ev_signal *w)
678{ 770{
771 ev_clear ((W)w);
679 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
680 return; 773 return;
681 774
682 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
683 ev_stop ((struct ev_watcher *)w); 776 ev_stop ((W)w);
684 777
685 if (!signals [w->signum - 1].head) 778 if (!signals [w->signum - 1].head)
686 signal (w->signum, SIG_DFL); 779 signal (w->signum, SIG_DFL);
687} 780}
688 781
689void evidle_start (struct ev_idle *w) 782void evidle_start (struct ev_idle *w)
690{ 783{
691 if (ev_is_active (w)) 784 if (ev_is_active (w))
692 return; 785 return;
693 786
694 ev_start ((struct ev_watcher *)w, ++idlecnt); 787 ev_start ((W)w, ++idlecnt);
695 array_needsize (idles, idlemax, idlecnt, ); 788 array_needsize (idles, idlemax, idlecnt, );
696 idles [idlecnt - 1] = w; 789 idles [idlecnt - 1] = w;
697} 790}
698 791
699void evidle_stop (struct ev_idle *w) 792void evidle_stop (struct ev_idle *w)
700{ 793{
794 ev_clear ((W)w);
795 if (ev_is_active (w))
796 return;
797
701 idles [w->active - 1] = idles [--idlecnt]; 798 idles [w->active - 1] = idles [--idlecnt];
702 ev_stop ((struct ev_watcher *)w); 799 ev_stop ((W)w);
703} 800}
704 801
705void evcheck_start (struct ev_check *w) 802void evcheck_start (struct ev_check *w)
706{ 803{
707 if (ev_is_active (w)) 804 if (ev_is_active (w))
708 return; 805 return;
709 806
710 ev_start ((struct ev_watcher *)w, ++checkcnt); 807 ev_start ((W)w, ++checkcnt);
711 array_needsize (checks, checkmax, checkcnt, ); 808 array_needsize (checks, checkmax, checkcnt, );
712 checks [checkcnt - 1] = w; 809 checks [checkcnt - 1] = w;
713} 810}
714 811
715void evcheck_stop (struct ev_check *w) 812void evcheck_stop (struct ev_check *w)
716{ 813{
814 ev_clear ((W)w);
815 if (ev_is_active (w))
816 return;
817
717 checks [w->active - 1] = checks [--checkcnt]; 818 checks [w->active - 1] = checks [--checkcnt];
718 ev_stop ((struct ev_watcher *)w); 819 ev_stop ((W)w);
719} 820}
720 821
721/*****************************************************************************/ 822/*****************************************************************************/
823
824struct ev_once
825{
826 struct ev_io io;
827 struct ev_timer to;
828 void (*cb)(int revents, void *arg);
829 void *arg;
830};
831
832static void
833once_cb (struct ev_once *once, int revents)
834{
835 void (*cb)(int revents, void *arg) = once->cb;
836 void *arg = once->arg;
837
838 evio_stop (&once->io);
839 evtimer_stop (&once->to);
840 free (once);
841
842 cb (revents, arg);
843}
844
845static void
846once_cb_io (struct ev_io *w, int revents)
847{
848 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
849}
850
851static void
852once_cb_to (struct ev_timer *w, int revents)
853{
854 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
855}
856
857void
858ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
859{
860 struct ev_once *once = malloc (sizeof (struct ev_once));
861
862 if (!once)
863 cb (EV_ERROR, arg);
864 else
865 {
866 once->cb = cb;
867 once->arg = arg;
868
869 evw_init (&once->io, once_cb_io);
870
871 if (fd >= 0)
872 {
873 evio_set (&once->io, fd, events);
874 evio_start (&once->io);
875 }
876
877 evw_init (&once->to, once_cb_to);
878
879 if (timeout >= 0.)
880 {
881 evtimer_set (&once->to, timeout, 0.);
882 evtimer_start (&once->to);
883 }
884 }
885}
886
887/*****************************************************************************/
888
722#if 1 889#if 0
890
891struct ev_io wio;
723 892
724static void 893static void
725sin_cb (struct ev_io *w, int revents) 894sin_cb (struct ev_io *w, int revents)
726{ 895{
727 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 896 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
737 906
738static void 907static void
739scb (struct ev_signal *w, int revents) 908scb (struct ev_signal *w, int revents)
740{ 909{
741 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 910 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
911 evio_stop (&wio);
912 evio_start (&wio);
742} 913}
743 914
744static void 915static void
745gcb (struct ev_signal *w, int revents) 916gcb (struct ev_signal *w, int revents)
746{ 917{
747 fprintf (stderr, "generic %x\n", revents); 918 fprintf (stderr, "generic %x\n", revents);
919
748} 920}
749 921
750int main (void) 922int main (void)
751{ 923{
752 struct ev_io sin;
753
754 ev_init (0); 924 ev_init (0);
755 925
756 evw_init (&sin, sin_cb, 55);
757 evio_set (&sin, 0, EV_READ); 926 evio_init (&wio, sin_cb, 0, EV_READ);
758 evio_start (&sin); 927 evio_start (&wio);
759 928
760 struct ev_timer t[10000]; 929 struct ev_timer t[10000];
761 930
762#if 0 931#if 0
763 int i; 932 int i;
764 for (i = 0; i < 10000; ++i) 933 for (i = 0; i < 10000; ++i)
765 { 934 {
766 struct ev_timer *w = t + i; 935 struct ev_timer *w = t + i;
767 evw_init (w, ocb, i); 936 evw_init (w, ocb, i);
768 evtimer_set_abs (w, drand48 (), 0.99775533); 937 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
769 evtimer_start (w); 938 evtimer_start (w);
770 if (drand48 () < 0.5) 939 if (drand48 () < 0.5)
771 evtimer_stop (w); 940 evtimer_stop (w);
772 } 941 }
773#endif 942#endif
774 943
775 struct ev_timer t1; 944 struct ev_timer t1;
776 evw_init (&t1, ocb, 0); 945 evtimer_init (&t1, ocb, 5, 10);
777 evtimer_set_abs (&t1, 5, 10);
778 evtimer_start (&t1); 946 evtimer_start (&t1);
779 947
780 struct ev_signal sig; 948 struct ev_signal sig;
781 evw_init (&sig, scb, 65535);
782 evsignal_set (&sig, SIGQUIT); 949 evsignal_init (&sig, scb, SIGQUIT);
783 evsignal_start (&sig); 950 evsignal_start (&sig);
784 951
785 struct ev_check cw; 952 struct ev_check cw;
786 evw_init (&cw, gcb, 0); 953 evcheck_init (&cw, gcb);
787 evcheck_start (&cw); 954 evcheck_start (&cw);
788 955
789 struct ev_idle iw; 956 struct ev_idle iw;
790 evw_init (&iw, gcb, 0); 957 evidle_init (&iw, gcb);
791 evidle_start (&iw); 958 evidle_start (&iw);
792 959
793 ev_loop (0); 960 ev_loop (0);
794 961
795 return 0; 962 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines