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.23 by root, Wed Oct 31 20:10:17 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;
112} 113}
113 114
114#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
115 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
116 { \ 117 { \
117 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
118 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
119 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
120 cur = newcnt; \ 127 cur = newcnt; \
121 } 128 }
122 129
188 195
189 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
190 event (events [i], type); 197 event (events [i], type);
191} 198}
192 199
200/* called on EBADF to verify fds */
201static void
202fd_recheck ()
203{
204 int fd;
205
206 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head)
210 evio_stop (anfds [fd].head);
211}
212
193/*****************************************************************************/ 213/*****************************************************************************/
194 214
195static struct ev_timer **timers; 215static struct ev_timer **timers;
196static int timermax, timercnt; 216static int timermax, timercnt;
197 217
313/*****************************************************************************/ 333/*****************************************************************************/
314 334
315static struct ev_idle **idles; 335static struct ev_idle **idles;
316static int idlemax, idlecnt; 336static int idlemax, idlecnt;
317 337
338static struct ev_prepare **prepares;
339static int preparemax, preparecnt;
340
318static struct ev_check **checks; 341static struct ev_check **checks;
319static int checkmax, checkcnt; 342static int checkmax, checkcnt;
320 343
321/*****************************************************************************/ 344/*****************************************************************************/
322 345
346static struct ev_child *childs [PID_HASHSIZE];
347static struct ev_signal childev;
348
349#ifndef WCONTINUED
350# define WCONTINUED 0
351#endif
352
353static void
354childcb (struct ev_signal *sw, int revents)
355{
356 struct ev_child *w;
357 int pid, status;
358
359 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
360 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
361 if (w->pid == pid || w->pid == -1)
362 {
363 w->status = status;
364 event ((W)w, EV_CHILD);
365 }
366}
367
368/*****************************************************************************/
369
323#if HAVE_EPOLL 370#if HAVE_EPOLL
324# include "ev_epoll.c" 371# include "ev_epoll.c"
325#endif 372#endif
326#if HAVE_SELECT 373#if HAVE_SELECT
327# include "ev_select.c" 374# include "ev_select.c"
328#endif 375#endif
329 376
330int ev_init (int flags) 377int ev_init (int flags)
331{ 378{
379 if (!ev_method)
380 {
332#if HAVE_MONOTONIC 381#if HAVE_MONOTONIC
333 { 382 {
334 struct timespec ts; 383 struct timespec ts;
335 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 384 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
336 have_monotonic = 1; 385 have_monotonic = 1;
337 } 386 }
338#endif 387#endif
339 388
340 ev_now = ev_time (); 389 ev_now = ev_time ();
341 now = get_clock (); 390 now = get_clock ();
342 diff = ev_now - now; 391 diff = ev_now - now;
343 392
344 if (pipe (sigpipe)) 393 if (pipe (sigpipe))
345 return 0; 394 return 0;
346 395
347 ev_method = EVMETHOD_NONE; 396 ev_method = EVMETHOD_NONE;
348#if HAVE_EPOLL 397#if HAVE_EPOLL
349 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 398 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
350#endif 399#endif
351#if HAVE_SELECT 400#if HAVE_SELECT
352 if (ev_method == EVMETHOD_NONE) select_init (flags); 401 if (ev_method == EVMETHOD_NONE) select_init (flags);
353#endif 402#endif
354 403
355 if (ev_method) 404 if (ev_method)
356 { 405 {
357 evw_init (&sigev, sigcb); 406 evw_init (&sigev, sigcb);
358 siginit (); 407 siginit ();
408
409 evsignal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev);
411 }
359 } 412 }
360 413
361 return ev_method; 414 return ev_method;
362} 415}
363 416
542void ev_loop (int flags) 595void ev_loop (int flags)
543{ 596{
544 double block; 597 double block;
545 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
546 599
547 if (checkcnt)
548 {
549 queue_events ((W *)checks, checkcnt, EV_CHECK);
550 call_pending ();
551 }
552
553 do 600 do
554 { 601 {
602 /* queue check watchers (and execute them) */
603 if (preparecnt)
604 {
605 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
606 call_pending ();
607 }
608
555 /* update fd-related kernel structures */ 609 /* update fd-related kernel structures */
556 fd_reify (); 610 fd_reify ();
557 611
558 /* calculate blocking time */ 612 /* calculate blocking time */
559 613
560 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 614 /* we only need this for !monotonic clockor timers, but as we basically
615 always have timers, we just calculate it always */
561 ev_now = ev_time (); 616 ev_now = ev_time ();
562 617
563 if (flags & EVLOOP_NONBLOCK || idlecnt) 618 if (flags & EVLOOP_NONBLOCK || idlecnt)
564 block = 0.; 619 block = 0.;
565 else 620 else
585 640
586 /* update ev_now, do magic */ 641 /* update ev_now, do magic */
587 time_update (); 642 time_update ();
588 643
589 /* queue pending timers and reschedule them */ 644 /* queue pending timers and reschedule them */
645 timers_reify (); /* relative timers called last */
590 periodics_reify (); /* absolute timers first */ 646 periodics_reify (); /* absolute timers called first */
591 timers_reify (); /* relative timers second */
592 647
593 /* queue idle watchers unless io or timers are pending */ 648 /* queue idle watchers unless io or timers are pending */
594 if (!pendingcnt) 649 if (!pendingcnt)
595 queue_events ((W *)idles, idlecnt, EV_IDLE); 650 queue_events ((W *)idles, idlecnt, EV_IDLE);
596 651
597 /* queue check and possibly idle watchers */ 652 /* queue check watchers, to be executed first */
653 if (checkcnt)
598 queue_events ((W *)checks, checkcnt, EV_CHECK); 654 queue_events ((W *)checks, checkcnt, EV_CHECK);
599 655
600 call_pending (); 656 call_pending ();
601 } 657 }
602 while (!ev_loop_done); 658 while (!ev_loop_done);
603 659
822 878
823 idles [w->active - 1] = idles [--idlecnt]; 879 idles [w->active - 1] = idles [--idlecnt];
824 ev_stop ((W)w); 880 ev_stop ((W)w);
825} 881}
826 882
883void evprepare_start (struct ev_prepare *w)
884{
885 if (ev_is_active (w))
886 return;
887
888 ev_start ((W)w, ++preparecnt);
889 array_needsize (prepares, preparemax, preparecnt, );
890 prepares [preparecnt - 1] = w;
891}
892
893void evprepare_stop (struct ev_prepare *w)
894{
895 ev_clear ((W)w);
896 if (ev_is_active (w))
897 return;
898
899 prepares [w->active - 1] = prepares [--preparecnt];
900 ev_stop ((W)w);
901}
902
827void evcheck_start (struct ev_check *w) 903void evcheck_start (struct ev_check *w)
828{ 904{
829 if (ev_is_active (w)) 905 if (ev_is_active (w))
830 return; 906 return;
831 907
839 ev_clear ((W)w); 915 ev_clear ((W)w);
840 if (ev_is_active (w)) 916 if (ev_is_active (w))
841 return; 917 return;
842 918
843 checks [w->active - 1] = checks [--checkcnt]; 919 checks [w->active - 1] = checks [--checkcnt];
920 ev_stop ((W)w);
921}
922
923void evchild_start (struct ev_child *w)
924{
925 if (ev_is_active (w))
926 return;
927
928 ev_start ((W)w, 1);
929 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930}
931
932void evchild_stop (struct ev_child *w)
933{
934 ev_clear ((W)w);
935 if (ev_is_active (w))
936 return;
937
938 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
844 ev_stop ((W)w); 939 ev_stop ((W)w);
845} 940}
846 941
847/*****************************************************************************/ 942/*****************************************************************************/
848 943

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines