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

Comparing libev/ev.c (file contents):
Revision 1.8 by root, Wed Oct 31 00:32:33 2007 UTC vs.
Revision 1.14 by root, Wed Oct 31 11:52:12 2007 UTC

9#include <assert.h> 9#include <assert.h>
10#include <errno.h> 10#include <errno.h>
11#include <sys/time.h> 11#include <sys/time.h>
12#include <time.h> 12#include <time.h>
13 13
14#ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1
16#endif
17
18#define HAVE_REALTIME 1
19#define HAVE_EPOLL 1 14#define HAVE_EPOLL 1
15
16#ifndef HAVE_MONOTONIC
17# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1
19# endif
20#endif
21
22#ifndef HAVE_SELECT
20#define HAVE_SELECT 1 23# define HAVE_SELECT 1
24#endif
25
26#ifndef HAVE_EPOLL
27# define HAVE_EPOLL 0
28#endif
29
30#ifndef HAVE_REALTIME
31# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32#endif
21 33
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 35#define MAX_BLOCKTIME 60.
24 36
25#include "ev.h" 37#include "ev.h"
26 38
27struct ev_watcher { 39typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 40typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 41typedef struct ev_watcher_time *WT;
33};
34 42
35static ev_tstamp now, diff; /* monotonic clock */ 43static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 44ev_tstamp ev_now;
37int ev_method; 45int ev_method;
38 46
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{
123 w->pending = ++pendingcnt; 131 w->pending = ++pendingcnt;
124 array_needsize (pendings, pendingmax, pendingcnt, ); 132 array_needsize (pendings, pendingmax, pendingcnt, );
125 pendings [pendingcnt - 1].w = w; 133 pendings [pendingcnt - 1].w = w;
126 pendings [pendingcnt - 1].events = events; 134 pendings [pendingcnt - 1].events = events;
135 for (w = anfd->head; w; w = w->next) 143 for (w = anfd->head; w; w = w->next)
136 { 144 {
137 int ev = w->events & events; 145 int ev = w->events & events;
138 146
139 if (ev) 147 if (ev)
140 event ((struct ev_watcher *)w, ev); 148 event ((W)w, ev);
141 } 149 }
142} 150}
143 151
144/*****************************************************************************/ 152static void
153queue_events (W *events, int eventcnt, int type)
154{
155 int i;
145 156
157 for (i = 0; i < eventcnt; ++i)
158 event (events [i], type);
159}
160
161/*****************************************************************************/
162
146static struct ev_timer **atimers; 163static struct ev_timer **timers;
147static int atimermax, atimercnt; 164static int timermax, timercnt;
148 165
149static struct ev_timer **rtimers; 166static struct ev_periodic **periodics;
150static int rtimermax, rtimercnt; 167static int periodicmax, periodiccnt;
151 168
152static void 169static void
153upheap (struct ev_timer **timers, int k) 170upheap (WT *timers, int k)
154{ 171{
155 struct ev_timer *w = timers [k]; 172 WT w = timers [k];
156 173
157 while (k && timers [k >> 1]->at > w->at) 174 while (k && timers [k >> 1]->at > w->at)
158 { 175 {
159 timers [k] = timers [k >> 1]; 176 timers [k] = timers [k >> 1];
160 timers [k]->active = k + 1; 177 timers [k]->active = k + 1;
165 timers [k]->active = k + 1; 182 timers [k]->active = k + 1;
166 183
167} 184}
168 185
169static void 186static void
170downheap (struct ev_timer **timers, int N, int k) 187downheap (WT *timers, int N, int k)
171{ 188{
172 struct ev_timer *w = timers [k]; 189 WT w = timers [k];
173 190
174 while (k < (N >> 1)) 191 while (k < (N >> 1))
175 { 192 {
176 int j = k << 1; 193 int j = k << 1;
177 194
241 if (signals [sig].gotsig) 258 if (signals [sig].gotsig)
242 { 259 {
243 signals [sig].gotsig = 0; 260 signals [sig].gotsig = 0;
244 261
245 for (w = signals [sig].head; w; w = w->next) 262 for (w = signals [sig].head; w; w = w->next)
246 event ((struct ev_watcher *)w, EV_SIGNAL); 263 event ((W)w, EV_SIGNAL);
247 } 264 }
248} 265}
249 266
250static void 267static void
251siginit (void) 268siginit (void)
258 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 275 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
259 276
260 evio_set (&sigev, sigpipe [0], EV_READ); 277 evio_set (&sigev, sigpipe [0], EV_READ);
261 evio_start (&sigev); 278 evio_start (&sigev);
262} 279}
280
281/*****************************************************************************/
282
283static struct ev_idle **idles;
284static int idlemax, idlecnt;
285
286static struct ev_check **checks;
287static int checkmax, checkcnt;
263 288
264/*****************************************************************************/ 289/*****************************************************************************/
265 290
266#if HAVE_EPOLL 291#if HAVE_EPOLL
267# include "ev_epoll.c" 292# include "ev_epoll.c"
295 if (ev_method == EVMETHOD_NONE) select_init (flags); 320 if (ev_method == EVMETHOD_NONE) select_init (flags);
296#endif 321#endif
297 322
298 if (ev_method) 323 if (ev_method)
299 { 324 {
300 evw_init (&sigev, sigcb, 0); 325 evw_init (&sigev, sigcb);
301 siginit (); 326 siginit ();
302 } 327 }
303 328
304 return ev_method; 329 return ev_method;
305} 330}
306 331
307/*****************************************************************************/ 332/*****************************************************************************/
308 333
309void ev_prefork (void) 334void ev_prefork (void)
310{ 335{
336 /* nop */
311} 337}
312 338
313void ev_postfork_parent (void) 339void ev_postfork_parent (void)
314{ 340{
341 /* nop */
315} 342}
316 343
317void ev_postfork_child (void) 344void ev_postfork_child (void)
318{ 345{
319#if HAVE_EPOLL 346#if HAVE_EPOLL
328 siginit (); 355 siginit ();
329} 356}
330 357
331/*****************************************************************************/ 358/*****************************************************************************/
332 359
333static ev_hook hooks [EVHOOK_NUM];
334
335void
336ev_hook_register (int type, ev_hook hook)
337{
338 hooks [type] = hook;
339}
340
341void
342ev_hook_unregister (int type, ev_hook hook)
343{
344 hooks [type] = 0;
345}
346
347static void
348hook_call (int type)
349{
350 if (hooks [type])
351 hooks [type] ();
352}
353
354static void 360static void
355fd_reify (void) 361fd_reify (void)
356{ 362{
357 int i; 363 int i;
358 364
395 401
396 pendingcnt = 0; 402 pendingcnt = 0;
397} 403}
398 404
399static void 405static void
400timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 406timers_reify ()
401{ 407{
402 while (timercnt && timers [0]->at <= now) 408 while (timercnt && timers [0]->at <= now)
403 { 409 {
404 struct ev_timer *w = timers [0]; 410 struct ev_timer *w = timers [0];
405 411
406 /* first reschedule or stop timer */ 412 /* first reschedule or stop timer */
407 if (w->repeat) 413 if (w->repeat)
408 { 414 {
409 if (w->is_abs)
410 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
411 else
412 w->at = now + w->repeat; 415 w->at = now + w->repeat;
413 416 assert (("timer timeout in the past, negative repeat?", w->at > now));
414 assert (w->at > now);
415
416 downheap (timers, timercnt, 0); 417 downheap ((WT *)timers, timercnt, 0);
417 } 418 }
418 else 419 else
419 {
420 evtimer_stop (w); /* nonrepeating: stop timer */ 420 evtimer_stop (w); /* nonrepeating: stop timer */
421 --timercnt; /* maybe pass by reference instead? */ 421
422 event ((W)w, EV_TIMEOUT);
423 }
424}
425
426static void
427periodics_reify ()
428{
429 while (periodiccnt && periodics [0]->at <= ev_now)
430 {
431 struct ev_periodic *w = periodics [0];
432
433 /* first reschedule or stop timer */
434 if (w->interval)
422 } 435 {
436 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
437 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
438 downheap ((WT *)periodics, periodiccnt, 0);
439 }
440 else
441 evperiodic_stop (w); /* nonrepeating: stop timer */
423 442
424 event ((struct ev_watcher *)w, EV_TIMEOUT); 443 event ((W)w, EV_TIMEOUT);
444 }
445}
446
447static void
448periodics_reschedule (ev_tstamp diff)
449{
450 int i;
451
452 /* adjust periodics after time jump */
453 for (i = 0; i < periodiccnt; ++i)
454 {
455 struct ev_periodic *w = periodics [i];
456
457 if (w->interval)
458 {
459 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
460
461 if (fabs (diff) >= 1e-4)
462 {
463 evperiodic_stop (w);
464 evperiodic_start (w);
465
466 i = 0; /* restart loop, inefficient, but time jumps should be rare */
467 }
468 }
425 } 469 }
426} 470}
427 471
428static void 472static void
429time_update () 473time_update ()
430{ 474{
431 int i; 475 int i;
476
432 ev_now = ev_time (); 477 ev_now = ev_time ();
433 478
434 if (have_monotonic) 479 if (have_monotonic)
435 { 480 {
436 ev_tstamp odiff = diff; 481 ev_tstamp odiff = diff;
437 482
438 /* detecting time jumps is much more difficult */
439 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 483 for (i = 4; --i; ) /* loop a few times, before making important decisions */
440 { 484 {
441 now = get_clock (); 485 now = get_clock ();
442 diff = ev_now - now; 486 diff = ev_now - now;
443 487
444 if (fabs (odiff - diff) < MIN_TIMEJUMP) 488 if (fabs (odiff - diff) < MIN_TIMEJUMP)
445 return; /* all is well */ 489 return; /* all is well */
446 490
447 ev_now = ev_time (); 491 ev_now = ev_time ();
448 } 492 }
449 493
450 /* time jump detected, reschedule atimers */ 494 periodics_reschedule (diff - odiff);
451 for (i = 0; i < atimercnt; ++i) 495 /* no timer adjustment, as the monotonic clock doesn't jump */
452 {
453 struct ev_timer *w = atimers [i];
454 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
455 }
456 } 496 }
457 else 497 else
458 { 498 {
459 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 499 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
460 /* time jump detected, adjust rtimers */ 500 {
501 periodics_reschedule (ev_now - now);
502
503 /* adjust timers. this is easy, as the offset is the same for all */
461 for (i = 0; i < rtimercnt; ++i) 504 for (i = 0; i < timercnt; ++i)
462 rtimers [i]->at += ev_now - now; 505 timers [i]->at += diff;
506 }
463 507
464 now = ev_now; 508 now = ev_now;
465 } 509 }
466} 510}
467 511
468int ev_loop_done; 512int ev_loop_done;
469 513
470void ev_loop (int flags) 514void ev_loop (int flags)
471{ 515{
472 double block; 516 double block;
473 ev_loop_done = flags & EVLOOP_ONESHOT; 517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
518
519 if (checkcnt)
520 {
521 queue_events ((W *)checks, checkcnt, EV_CHECK);
522 call_pending ();
523 }
474 524
475 do 525 do
476 { 526 {
477 hook_call (EVHOOK_PREPOLL);
478
479 /* update fd-related kernel structures */ 527 /* update fd-related kernel structures */
480 fd_reify (); 528 fd_reify ();
481 529
482 /* calculate blocking time */ 530 /* calculate blocking time */
531
532 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
533 ev_now = ev_time ();
534
483 if (flags & EVLOOP_NONBLOCK) 535 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 536 block = 0.;
485 else 537 else
486 { 538 {
487 block = MAX_BLOCKTIME; 539 block = MAX_BLOCKTIME;
488 540
489 if (rtimercnt) 541 if (timercnt)
490 { 542 {
491 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 543 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
492 if (block > to) block = to; 544 if (block > to) block = to;
493 } 545 }
494 546
495 if (atimercnt) 547 if (periodiccnt)
496 { 548 {
497 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 549 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
498 if (block > to) block = to; 550 if (block > to) block = to;
499 } 551 }
500 552
501 if (block < 0.) block = 0.; 553 if (block < 0.) block = 0.;
502 } 554 }
504 method_poll (block); 556 method_poll (block);
505 557
506 /* update ev_now, do magic */ 558 /* update ev_now, do magic */
507 time_update (); 559 time_update ();
508 560
509 hook_call (EVHOOK_POSTPOLL);
510
511 /* put pending timers into pendign queue and reschedule them */ 561 /* queue pending timers and reschedule them */
512 /* absolute timers first */ 562 periodics_reify (); /* absolute timers first */
513 timers_reify (atimers, atimercnt, ev_now);
514 /* relative timers second */ 563 timers_reify (); /* relative timers second */
515 timers_reify (rtimers, rtimercnt, now); 564
565 /* queue idle watchers unless io or timers are pending */
566 if (!pendingcnt)
567 queue_events ((W *)idles, idlecnt, EV_IDLE);
568
569 /* queue check and possibly idle watchers */
570 queue_events ((W *)checks, checkcnt, EV_CHECK);
516 571
517 call_pending (); 572 call_pending ();
518 } 573 }
519 while (!ev_loop_done); 574 while (!ev_loop_done);
520}
521 575
522/*****************************************************************************/ 576 if (ev_loop_done != 2)
577 ev_loop_done = 0;
578}
523 579
580/*****************************************************************************/
581
524static void 582static void
525wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 583wlist_add (WL *head, WL elem)
526{ 584{
527 elem->next = *head; 585 elem->next = *head;
528 *head = elem; 586 *head = elem;
529} 587}
530 588
531static void 589static void
532wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 590wlist_del (WL *head, WL elem)
533{ 591{
534 while (*head) 592 while (*head)
535 { 593 {
536 if (*head == elem) 594 if (*head == elem)
537 { 595 {
542 head = &(*head)->next; 600 head = &(*head)->next;
543 } 601 }
544} 602}
545 603
546static void 604static void
547ev_start (struct ev_watcher *w, int active) 605ev_start (W w, int active)
548{ 606{
549 w->pending = 0; 607 w->pending = 0;
550 w->active = active; 608 w->active = active;
551} 609}
552 610
553static void 611static void
554ev_stop (struct ev_watcher *w) 612ev_stop (W w)
555{ 613{
556 if (w->pending) 614 if (w->pending)
557 pendings [w->pending - 1].w = 0; 615 pendings [w->pending - 1].w = 0;
558 616
559 w->active = 0; 617 w->active = 0;
560 /* nop */
561} 618}
562 619
563/*****************************************************************************/ 620/*****************************************************************************/
564 621
565void 622void
568 if (ev_is_active (w)) 625 if (ev_is_active (w))
569 return; 626 return;
570 627
571 int fd = w->fd; 628 int fd = w->fd;
572 629
573 ev_start ((struct ev_watcher *)w, 1); 630 ev_start ((W)w, 1);
574 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 631 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
575 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 632 wlist_add ((WL *)&anfds[fd].head, (WL)w);
576 633
577 ++fdchangecnt; 634 ++fdchangecnt;
578 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 635 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
579 fdchanges [fdchangecnt - 1] = fd; 636 fdchanges [fdchangecnt - 1] = fd;
580} 637}
583evio_stop (struct ev_io *w) 640evio_stop (struct ev_io *w)
584{ 641{
585 if (!ev_is_active (w)) 642 if (!ev_is_active (w))
586 return; 643 return;
587 644
588 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
589 ev_stop ((struct ev_watcher *)w); 646 ev_stop ((W)w);
590 647
591 ++fdchangecnt; 648 ++fdchangecnt;
592 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 649 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
593 fdchanges [fdchangecnt - 1] = w->fd; 650 fdchanges [fdchangecnt - 1] = w->fd;
594} 651}
595 652
653
596void 654void
597evtimer_start (struct ev_timer *w) 655evtimer_start (struct ev_timer *w)
598{ 656{
599 if (ev_is_active (w)) 657 if (ev_is_active (w))
600 return; 658 return;
601 659
602 if (w->is_abs) 660 w->at += now;
603 {
604 /* this formula differs from the one in timer_reify becuse we do not round up */
605 if (w->repeat)
606 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
607 661
608 ev_start ((struct ev_watcher *)w, ++atimercnt); 662 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
663
664 ev_start ((W)w, ++timercnt);
609 array_needsize (atimers, atimermax, atimercnt, ); 665 array_needsize (timers, timermax, timercnt, );
610 atimers [atimercnt - 1] = w; 666 timers [timercnt - 1] = w;
611 upheap (atimers, atimercnt - 1); 667 upheap ((WT *)timers, timercnt - 1);
612 }
613 else
614 {
615 w->at += now;
616
617 ev_start ((struct ev_watcher *)w, ++rtimercnt);
618 array_needsize (rtimers, rtimermax, rtimercnt, );
619 rtimers [rtimercnt - 1] = w;
620 upheap (rtimers, rtimercnt - 1);
621 }
622
623} 668}
624 669
625void 670void
626evtimer_stop (struct ev_timer *w) 671evtimer_stop (struct ev_timer *w)
627{ 672{
628 if (!ev_is_active (w)) 673 if (!ev_is_active (w))
629 return; 674 return;
630 675
631 if (w->is_abs)
632 {
633 if (w->active < atimercnt--) 676 if (w->active < timercnt--)
634 { 677 {
635 atimers [w->active - 1] = atimers [atimercnt]; 678 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1);
680 }
681
682 w->at = w->repeat;
683
684 ev_stop ((W)w);
685}
686
687void
688evtimer_again (struct ev_timer *w)
689{
690 if (ev_is_active (w))
691 {
692 if (w->repeat)
693 {
694 w->at = now + w->repeat;
636 downheap (atimers, atimercnt, w->active - 1); 695 downheap ((WT *)timers, timercnt, w->active - 1);
637 }
638 }
639 else
640 {
641 if (w->active < rtimercnt--)
642 { 696 }
643 rtimers [w->active - 1] = rtimers [rtimercnt]; 697 else
644 downheap (rtimers, rtimercnt, w->active - 1); 698 evtimer_stop (w);
645 } 699 }
700 else if (w->repeat)
701 evtimer_start (w);
702}
703
704void
705evperiodic_start (struct ev_periodic *w)
706{
707 if (ev_is_active (w))
708 return;
709
710 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
711
712 /* this formula differs from the one in periodic_reify because we do not always round up */
713 if (w->interval)
714 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
715
716 ev_start ((W)w, ++periodiccnt);
717 array_needsize (periodics, periodicmax, periodiccnt, );
718 periodics [periodiccnt - 1] = w;
719 upheap ((WT *)periodics, periodiccnt - 1);
720}
721
722void
723evperiodic_stop (struct ev_periodic *w)
724{
725 if (!ev_is_active (w))
726 return;
727
728 if (w->active < periodiccnt--)
646 } 729 {
730 periodics [w->active - 1] = periodics [periodiccnt];
731 downheap ((WT *)periodics, periodiccnt, w->active - 1);
732 }
647 733
648 ev_stop ((struct ev_watcher *)w); 734 ev_stop ((W)w);
649} 735}
650 736
651void 737void
652evsignal_start (struct ev_signal *w) 738evsignal_start (struct ev_signal *w)
653{ 739{
654 if (ev_is_active (w)) 740 if (ev_is_active (w))
655 return; 741 return;
656 742
657 ev_start ((struct ev_watcher *)w, 1); 743 ev_start ((W)w, 1);
658 array_needsize (signals, signalmax, w->signum, signals_init); 744 array_needsize (signals, signalmax, w->signum, signals_init);
659 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 745 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
660 746
661 if (!w->next) 747 if (!w->next)
662 { 748 {
663 struct sigaction sa; 749 struct sigaction sa;
664 sa.sa_handler = sighandler; 750 sa.sa_handler = sighandler;
672evsignal_stop (struct ev_signal *w) 758evsignal_stop (struct ev_signal *w)
673{ 759{
674 if (!ev_is_active (w)) 760 if (!ev_is_active (w))
675 return; 761 return;
676 762
677 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 763 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
678 ev_stop ((struct ev_watcher *)w); 764 ev_stop ((W)w);
679 765
680 if (!signals [w->signum - 1].head) 766 if (!signals [w->signum - 1].head)
681 signal (w->signum, SIG_DFL); 767 signal (w->signum, SIG_DFL);
682} 768}
683 769
770void evidle_start (struct ev_idle *w)
771{
772 if (ev_is_active (w))
773 return;
774
775 ev_start ((W)w, ++idlecnt);
776 array_needsize (idles, idlemax, idlecnt, );
777 idles [idlecnt - 1] = w;
778}
779
780void evidle_stop (struct ev_idle *w)
781{
782 idles [w->active - 1] = idles [--idlecnt];
783 ev_stop ((W)w);
784}
785
786void evcheck_start (struct ev_check *w)
787{
788 if (ev_is_active (w))
789 return;
790
791 ev_start ((W)w, ++checkcnt);
792 array_needsize (checks, checkmax, checkcnt, );
793 checks [checkcnt - 1] = w;
794}
795
796void evcheck_stop (struct ev_check *w)
797{
798 checks [w->active - 1] = checks [--checkcnt];
799 ev_stop ((W)w);
800}
801
684/*****************************************************************************/ 802/*****************************************************************************/
803
685#if 1 804#if 0
805
806struct ev_io wio;
686 807
687static void 808static void
688sin_cb (struct ev_io *w, int revents) 809sin_cb (struct ev_io *w, int revents)
689{ 810{
690 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 811 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
700 821
701static void 822static void
702scb (struct ev_signal *w, int revents) 823scb (struct ev_signal *w, int revents)
703{ 824{
704 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 825 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
826 evio_stop (&wio);
827 evio_start (&wio);
828}
829
830static void
831gcb (struct ev_signal *w, int revents)
832{
833 fprintf (stderr, "generic %x\n", revents);
834
705} 835}
706 836
707int main (void) 837int main (void)
708{ 838{
709 struct ev_io sin;
710
711 ev_init (0); 839 ev_init (0);
712 840
713 evw_init (&sin, sin_cb, 55);
714 evio_set (&sin, 0, EV_READ); 841 evio_init (&wio, sin_cb, 0, EV_READ);
715 evio_start (&sin); 842 evio_start (&wio);
716 843
717 struct ev_timer t[10000]; 844 struct ev_timer t[10000];
718 845
719#if 1 846#if 0
720 int i; 847 int i;
721 for (i = 0; i < 10000; ++i) 848 for (i = 0; i < 10000; ++i)
722 { 849 {
723 struct ev_timer *w = t + i; 850 struct ev_timer *w = t + i;
724 evw_init (w, ocb, i); 851 evw_init (w, ocb, i);
725 evtimer_set_abs (w, drand48 (), 0.99775533); 852 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
726 evtimer_start (w); 853 evtimer_start (w);
727 if (drand48 () < 0.5) 854 if (drand48 () < 0.5)
728 evtimer_stop (w); 855 evtimer_stop (w);
729 } 856 }
730#endif 857#endif
731 858
732 struct ev_timer t1; 859 struct ev_timer t1;
733 evw_init (&t1, ocb, 0); 860 evtimer_init (&t1, ocb, 5, 10);
734 evtimer_set_abs (&t1, 5, 10);
735 evtimer_start (&t1); 861 evtimer_start (&t1);
736 862
737 struct ev_signal sig; 863 struct ev_signal sig;
738 evw_init (&sig, scb, 65535);
739 evsignal_set (&sig, SIGQUIT); 864 evsignal_init (&sig, scb, SIGQUIT);
740 evsignal_start (&sig); 865 evsignal_start (&sig);
741 866
867 struct ev_check cw;
868 evcheck_init (&cw, gcb);
869 evcheck_start (&cw);
870
871 struct ev_idle iw;
872 evidle_init (&iw, gcb);
873 evidle_start (&iw);
874
742 ev_loop (0); 875 ev_loop (0);
743 876
744 return 0; 877 return 0;
745} 878}
746 879

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines