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

Comparing libev/ev.c (file contents):
Revision 1.17 by root, Wed Oct 31 14:44:15 2007 UTC vs.
Revision 1.21 by root, Wed Oct 31 18:37:38 2007 UTC

39#include <assert.h> 39#include <assert.h>
40#include <errno.h> 40#include <errno.h>
41#include <sys/time.h> 41#include <sys/time.h>
42#include <time.h> 42#include <time.h>
43 43
44#define HAVE_EPOLL 1
45
46#ifndef HAVE_MONOTONIC 44#ifndef HAVE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 45# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 46# define HAVE_MONOTONIC 1
49# endif 47# endif
50#endif 48#endif
188 186
189 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
190 event (events [i], type); 188 event (events [i], type);
191} 189}
192 190
191/* called on EBADF to verify fds */
192static void
193fd_recheck ()
194{
195 int fd;
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
193/*****************************************************************************/ 204/*****************************************************************************/
194 205
195static struct ev_timer **timers; 206static struct ev_timer **timers;
196static int timermax, timercnt; 207static int timermax, timercnt;
197 208
312 323
313/*****************************************************************************/ 324/*****************************************************************************/
314 325
315static struct ev_idle **idles; 326static struct ev_idle **idles;
316static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
317 331
318static struct ev_check **checks; 332static struct ev_check **checks;
319static int checkmax, checkcnt; 333static int checkmax, checkcnt;
320 334
321/*****************************************************************************/ 335/*****************************************************************************/
416} 430}
417 431
418static void 432static void
419call_pending () 433call_pending ()
420{ 434{
421 int i; 435 while (pendingcnt)
422
423 for (i = 0; i < pendingcnt; ++i)
424 { 436 {
425 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
426 438
427 if (p->w) 439 if (p->w)
428 { 440 {
429 p->w->pending = 0; 441 p->w->pending = 0;
430 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
431 } 443 }
432 } 444 }
433
434 pendingcnt = 0;
435} 445}
436 446
437static void 447static void
438timers_reify () 448timers_reify ()
439{ 449{
546void ev_loop (int flags) 556void ev_loop (int flags)
547{ 557{
548 double block; 558 double block;
549 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
550 560
551 if (checkcnt)
552 {
553 queue_events ((W *)checks, checkcnt, EV_CHECK);
554 call_pending ();
555 }
556
557 do 561 do
558 { 562 {
563 /* queue check watchers (and execute them) */
564 if (preparecnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
559 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
560 fd_reify (); 571 fd_reify ();
561 572
562 /* calculate blocking time */ 573 /* calculate blocking time */
563 574
564 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 575 /* we only need this for !monotonic clockor timers, but as we basically
576 always have timers, we just calculate it always */
565 ev_now = ev_time (); 577 ev_now = ev_time ();
566 578
567 if (flags & EVLOOP_NONBLOCK || idlecnt) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
568 block = 0.; 580 block = 0.;
569 else 581 else
589 601
590 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
591 time_update (); 603 time_update ();
592 604
593 /* queue pending timers and reschedule them */ 605 /* queue pending timers and reschedule them */
606 timers_reify (); /* relative timers called last */
594 periodics_reify (); /* absolute timers first */ 607 periodics_reify (); /* absolute timers called first */
595 timers_reify (); /* relative timers second */
596 608
597 /* queue idle watchers unless io or timers are pending */ 609 /* queue idle watchers unless io or timers are pending */
598 if (!pendingcnt) 610 if (!pendingcnt)
599 queue_events ((W *)idles, idlecnt, EV_IDLE); 611 queue_events ((W *)idles, idlecnt, EV_IDLE);
600 612
601 /* queue check and possibly idle watchers */ 613 /* queue check watchers, to be executed first */
614 if (checkcnt)
602 queue_events ((W *)checks, checkcnt, EV_CHECK); 615 queue_events ((W *)checks, checkcnt, EV_CHECK);
603 616
604 call_pending (); 617 call_pending ();
605 } 618 }
606 while (!ev_loop_done); 619 while (!ev_loop_done);
607 620
823 ev_clear ((W)w); 836 ev_clear ((W)w);
824 if (ev_is_active (w)) 837 if (ev_is_active (w))
825 return; 838 return;
826 839
827 idles [w->active - 1] = idles [--idlecnt]; 840 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w);
842}
843
844void evprepare_start (struct ev_prepare *w)
845{
846 if (ev_is_active (w))
847 return;
848
849 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w;
852}
853
854void evprepare_stop (struct ev_prepare *w)
855{
856 ev_clear ((W)w);
857 if (ev_is_active (w))
858 return;
859
860 prepares [w->active - 1] = prepares [--preparecnt];
828 ev_stop ((W)w); 861 ev_stop ((W)w);
829} 862}
830 863
831void evcheck_start (struct ev_check *w) 864void evcheck_start (struct ev_check *w)
832{ 865{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines