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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines