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.9 by root, Wed Oct 31 07:24:17 2007 UTC

139 if (ev) 139 if (ev)
140 event ((struct ev_watcher *)w, ev); 140 event ((struct ev_watcher *)w, ev);
141 } 141 }
142} 142}
143 143
144static void
145queue_events (struct ev_watcher **events, int eventcnt, int type)
146{
147 int i;
148
149 for (i = 0; i < eventcnt; ++i)
150 event (events [i], type);
151}
152
144/*****************************************************************************/ 153/*****************************************************************************/
145 154
146static struct ev_timer **atimers; 155static struct ev_timer **atimers;
147static int atimermax, atimercnt; 156static int atimermax, atimercnt;
148 157
258 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 267 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
259 268
260 evio_set (&sigev, sigpipe [0], EV_READ); 269 evio_set (&sigev, sigpipe [0], EV_READ);
261 evio_start (&sigev); 270 evio_start (&sigev);
262} 271}
272
273/*****************************************************************************/
274
275static struct ev_idle **idles;
276static int idlemax, idlecnt;
277
278static struct ev_check **checks;
279static int checkmax, checkcnt;
263 280
264/*****************************************************************************/ 281/*****************************************************************************/
265 282
266#if HAVE_EPOLL 283#if HAVE_EPOLL
267# include "ev_epoll.c" 284# include "ev_epoll.c"
328 siginit (); 345 siginit ();
329} 346}
330 347
331/*****************************************************************************/ 348/*****************************************************************************/
332 349
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 350static void
355fd_reify (void) 351fd_reify (void)
356{ 352{
357 int i; 353 int i;
358 354
470void ev_loop (int flags) 466void ev_loop (int flags)
471{ 467{
472 double block; 468 double block;
473 ev_loop_done = flags & EVLOOP_ONESHOT; 469 ev_loop_done = flags & EVLOOP_ONESHOT;
474 470
471 if (checkcnt)
472 {
473 queue_events (checks, checkcnt, EV_CHECK);
474 call_pending ();
475 }
476
475 do 477 do
476 { 478 {
477 hook_call (EVHOOK_PREPOLL);
478
479 /* update fd-related kernel structures */ 479 /* update fd-related kernel structures */
480 fd_reify (); 480 fd_reify ();
481 481
482 /* calculate blocking time */ 482 /* calculate blocking time */
483 if (flags & EVLOOP_NONBLOCK) 483 if (flags & EVLOOP_NONBLOCK || idlecnt)
484 block = 0.; 484 block = 0.;
485 else 485 else
486 { 486 {
487 block = MAX_BLOCKTIME; 487 block = MAX_BLOCKTIME;
488 488
504 method_poll (block); 504 method_poll (block);
505 505
506 /* update ev_now, do magic */ 506 /* update ev_now, do magic */
507 time_update (); 507 time_update ();
508 508
509 hook_call (EVHOOK_POSTPOLL);
510
511 /* put pending timers into pendign queue and reschedule them */ 509 /* queue pending timers and reschedule them */
512 /* absolute timers first */ 510 /* absolute timers first */
513 timers_reify (atimers, atimercnt, ev_now); 511 timers_reify (atimers, atimercnt, ev_now);
514 /* relative timers second */ 512 /* relative timers second */
515 timers_reify (rtimers, rtimercnt, now); 513 timers_reify (rtimers, rtimercnt, now);
514
515 /* queue idle watchers unless io or timers are pending */
516 if (!pendingcnt)
517 queue_events (idles, idlecnt, EV_IDLE);
518
519 /* queue check and possibly idle watchers */
520 queue_events (checks, checkcnt, EV_CHECK);
516 521
517 call_pending (); 522 call_pending ();
518 } 523 }
519 while (!ev_loop_done); 524 while (!ev_loop_done);
520} 525}
679 684
680 if (!signals [w->signum - 1].head) 685 if (!signals [w->signum - 1].head)
681 signal (w->signum, SIG_DFL); 686 signal (w->signum, SIG_DFL);
682} 687}
683 688
689void evidle_start (struct ev_idle *w)
690{
691 if (ev_is_active (w))
692 return;
693
694 ev_start ((struct ev_watcher *)w, ++idlecnt);
695 array_needsize (idles, idlemax, idlecnt, );
696 idles [idlecnt - 1] = w;
697}
698
699void evidle_stop (struct ev_idle *w)
700{
701 idles [w->active - 1] = idles [--idlecnt];
702 ev_stop ((struct ev_watcher *)w);
703}
704
705void evcheck_start (struct ev_check *w)
706{
707 if (ev_is_active (w))
708 return;
709
710 ev_start ((struct ev_watcher *)w, ++checkcnt);
711 array_needsize (checks, checkmax, checkcnt, );
712 checks [checkcnt - 1] = w;
713}
714
715void evcheck_stop (struct ev_check *w)
716{
717 checks [w->active - 1] = checks [--checkcnt];
718 ev_stop ((struct ev_watcher *)w);
719}
720
684/*****************************************************************************/ 721/*****************************************************************************/
685#if 1 722#if 1
686 723
687static void 724static void
688sin_cb (struct ev_io *w, int revents) 725sin_cb (struct ev_io *w, int revents)
702scb (struct ev_signal *w, int revents) 739scb (struct ev_signal *w, int revents)
703{ 740{
704 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 741 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
705} 742}
706 743
744static void
745gcb (struct ev_signal *w, int revents)
746{
747 fprintf (stderr, "generic %x\n", revents);
748}
749
707int main (void) 750int main (void)
708{ 751{
709 struct ev_io sin; 752 struct ev_io sin;
710 753
711 ev_init (0); 754 ev_init (0);
714 evio_set (&sin, 0, EV_READ); 757 evio_set (&sin, 0, EV_READ);
715 evio_start (&sin); 758 evio_start (&sin);
716 759
717 struct ev_timer t[10000]; 760 struct ev_timer t[10000];
718 761
719#if 1 762#if 0
720 int i; 763 int i;
721 for (i = 0; i < 10000; ++i) 764 for (i = 0; i < 10000; ++i)
722 { 765 {
723 struct ev_timer *w = t + i; 766 struct ev_timer *w = t + i;
724 evw_init (w, ocb, i); 767 evw_init (w, ocb, i);
737 struct ev_signal sig; 780 struct ev_signal sig;
738 evw_init (&sig, scb, 65535); 781 evw_init (&sig, scb, 65535);
739 evsignal_set (&sig, SIGQUIT); 782 evsignal_set (&sig, SIGQUIT);
740 evsignal_start (&sig); 783 evsignal_start (&sig);
741 784
785 struct ev_check cw;
786 evw_init (&cw, gcb, 0);
787 evcheck_start (&cw);
788
789 struct ev_idle iw;
790 evw_init (&iw, gcb, 0);
791 evidle_start (&iw);
792
742 ev_loop (0); 793 ev_loop (0);
743 794
744 return 0; 795 return 0;
745} 796}
746 797

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines