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

Comparing libev/ev.c (file contents):
Revision 1.18 by root, Wed Oct 31 16:29:52 2007 UTC vs.
Revision 1.22 by root, Wed Oct 31 19:07:43 2007 UTC

36 36
37#include <stdio.h> 37#include <stdio.h>
38 38
39#include <assert.h> 39#include <assert.h>
40#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
41#include <sys/time.h> 43#include <sys/time.h>
42#include <time.h> 44#include <time.h>
43
44#define HAVE_EPOLL 1
45 45
46#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
49# endif 49# endif
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 62#endif
63 63
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
66 67
67#include "ev.h" 68#include "ev.h"
68 69
69typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
70typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
188 189
189 for (i = 0; i < eventcnt; ++i) 190 for (i = 0; i < eventcnt; ++i)
190 event (events [i], type); 191 event (events [i], type);
191} 192}
192 193
194/* called on EBADF to verify fds */
195static void
196fd_recheck ()
197{
198 int fd;
199
200 for (fd = 0; fd < anfdmax; ++fd)
201 if (anfds [fd].wev)
202 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
203 while (anfds [fd].head)
204 evio_stop (anfds [fd].head);
205}
206
193/*****************************************************************************/ 207/*****************************************************************************/
194 208
195static struct ev_timer **timers; 209static struct ev_timer **timers;
196static int timermax, timercnt; 210static int timermax, timercnt;
197 211
313/*****************************************************************************/ 327/*****************************************************************************/
314 328
315static struct ev_idle **idles; 329static struct ev_idle **idles;
316static int idlemax, idlecnt; 330static int idlemax, idlecnt;
317 331
332static struct ev_prepare **prepares;
333static int preparemax, preparecnt;
334
318static struct ev_check **checks; 335static struct ev_check **checks;
319static int checkmax, checkcnt; 336static int checkmax, checkcnt;
337
338/*****************************************************************************/
339
340static struct ev_child *childs [PID_HASHSIZE];
341static struct ev_signal childev;
342
343#ifndef WCONTINUED
344# define WCONTINUED 0
345#endif
346
347static void
348childcb (struct ev_signal *sw, int revents)
349{
350 struct ev_child *w;
351 int pid, status;
352
353 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
354 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
355 if (w->pid == pid || w->pid == -1)
356 {
357 w->status = status;
358 event ((W)w, EV_CHILD);
359 }
360}
320 361
321/*****************************************************************************/ 362/*****************************************************************************/
322 363
323#if HAVE_EPOLL 364#if HAVE_EPOLL
324# include "ev_epoll.c" 365# include "ev_epoll.c"
354 395
355 if (ev_method) 396 if (ev_method)
356 { 397 {
357 evw_init (&sigev, sigcb); 398 evw_init (&sigev, sigcb);
358 siginit (); 399 siginit ();
400
401 evsignal_init (&childev, childcb, SIGCHLD);
402 evsignal_start (&childev);
359 } 403 }
360 404
361 return ev_method; 405 return ev_method;
362} 406}
363 407
542void ev_loop (int flags) 586void ev_loop (int flags)
543{ 587{
544 double block; 588 double block;
545 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 589 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
546 590
547 if (checkcnt)
548 {
549 queue_events ((W *)checks, checkcnt, EV_CHECK);
550 call_pending ();
551 }
552
553 do 591 do
554 { 592 {
593 /* queue check watchers (and execute them) */
594 if (preparecnt)
595 {
596 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
597 call_pending ();
598 }
599
555 /* update fd-related kernel structures */ 600 /* update fd-related kernel structures */
556 fd_reify (); 601 fd_reify ();
557 602
558 /* calculate blocking time */ 603 /* calculate blocking time */
559 604
560 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 605 /* we only need this for !monotonic clockor timers, but as we basically
606 always have timers, we just calculate it always */
561 ev_now = ev_time (); 607 ev_now = ev_time ();
562 608
563 if (flags & EVLOOP_NONBLOCK || idlecnt) 609 if (flags & EVLOOP_NONBLOCK || idlecnt)
564 block = 0.; 610 block = 0.;
565 else 611 else
585 631
586 /* update ev_now, do magic */ 632 /* update ev_now, do magic */
587 time_update (); 633 time_update ();
588 634
589 /* queue pending timers and reschedule them */ 635 /* queue pending timers and reschedule them */
636 timers_reify (); /* relative timers called last */
590 periodics_reify (); /* absolute timers first */ 637 periodics_reify (); /* absolute timers called first */
591 timers_reify (); /* relative timers second */
592 638
593 /* queue idle watchers unless io or timers are pending */ 639 /* queue idle watchers unless io or timers are pending */
594 if (!pendingcnt) 640 if (!pendingcnt)
595 queue_events ((W *)idles, idlecnt, EV_IDLE); 641 queue_events ((W *)idles, idlecnt, EV_IDLE);
596 642
597 /* queue check and possibly idle watchers */ 643 /* queue check watchers, to be executed first */
644 if (checkcnt)
598 queue_events ((W *)checks, checkcnt, EV_CHECK); 645 queue_events ((W *)checks, checkcnt, EV_CHECK);
599 646
600 call_pending (); 647 call_pending ();
601 } 648 }
602 while (!ev_loop_done); 649 while (!ev_loop_done);
603 650
822 869
823 idles [w->active - 1] = idles [--idlecnt]; 870 idles [w->active - 1] = idles [--idlecnt];
824 ev_stop ((W)w); 871 ev_stop ((W)w);
825} 872}
826 873
874void evprepare_start (struct ev_prepare *w)
875{
876 if (ev_is_active (w))
877 return;
878
879 ev_start ((W)w, ++preparecnt);
880 array_needsize (prepares, preparemax, preparecnt, );
881 prepares [preparecnt - 1] = w;
882}
883
884void evprepare_stop (struct ev_prepare *w)
885{
886 ev_clear ((W)w);
887 if (ev_is_active (w))
888 return;
889
890 prepares [w->active - 1] = prepares [--preparecnt];
891 ev_stop ((W)w);
892}
893
827void evcheck_start (struct ev_check *w) 894void evcheck_start (struct ev_check *w)
828{ 895{
829 if (ev_is_active (w)) 896 if (ev_is_active (w))
830 return; 897 return;
831 898
839 ev_clear ((W)w); 906 ev_clear ((W)w);
840 if (ev_is_active (w)) 907 if (ev_is_active (w))
841 return; 908 return;
842 909
843 checks [w->active - 1] = checks [--checkcnt]; 910 checks [w->active - 1] = checks [--checkcnt];
911 ev_stop ((W)w);
912}
913
914void evchild_start (struct ev_child *w)
915{
916 if (ev_is_active (w))
917 return;
918
919 ev_start ((W)w, 1);
920 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
921}
922
923void evchild_stop (struct ev_child *w)
924{
925 ev_clear ((W)w);
926 if (ev_is_active (w))
927 return;
928
929 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
844 ev_stop ((W)w); 930 ev_stop ((W)w);
845} 931}
846 932
847/*****************************************************************************/ 933/*****************************************************************************/
848 934

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines