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

Comparing libev/ev.c (file contents):
Revision 1.19 by root, Wed Oct 31 17:55:55 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 45
44#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
45# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
60#endif 62#endif
61 63
62#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) */
63#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
64 67
65#include "ev.h" 68#include "ev.h"
66 69
67typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
68typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
110} 113}
111 114
112#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
113 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
114 { \ 117 { \
115 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 \
116 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
117 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
118 cur = newcnt; \ 127 cur = newcnt; \
119 } 128 }
120 129
324/*****************************************************************************/ 333/*****************************************************************************/
325 334
326static struct ev_idle **idles; 335static struct ev_idle **idles;
327static int idlemax, idlecnt; 336static int idlemax, idlecnt;
328 337
338static struct ev_prepare **prepares;
339static int preparemax, preparecnt;
340
329static struct ev_check **checks; 341static struct ev_check **checks;
330static int checkmax, checkcnt; 342static int checkmax, checkcnt;
331 343
332/*****************************************************************************/ 344/*****************************************************************************/
333 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
334#if HAVE_EPOLL 370#if HAVE_EPOLL
335# include "ev_epoll.c" 371# include "ev_epoll.c"
336#endif 372#endif
337#if HAVE_SELECT 373#if HAVE_SELECT
338# include "ev_select.c" 374# include "ev_select.c"
339#endif 375#endif
340 376
341int ev_init (int flags) 377int ev_init (int flags)
342{ 378{
379 if (!ev_method)
380 {
343#if HAVE_MONOTONIC 381#if HAVE_MONOTONIC
344 { 382 {
345 struct timespec ts; 383 struct timespec ts;
346 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 384 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
347 have_monotonic = 1; 385 have_monotonic = 1;
348 } 386 }
349#endif 387#endif
350 388
351 ev_now = ev_time (); 389 ev_now = ev_time ();
352 now = get_clock (); 390 now = get_clock ();
353 diff = ev_now - now; 391 diff = ev_now - now;
354 392
355 if (pipe (sigpipe)) 393 if (pipe (sigpipe))
356 return 0; 394 return 0;
357 395
358 ev_method = EVMETHOD_NONE; 396 ev_method = EVMETHOD_NONE;
359#if HAVE_EPOLL 397#if HAVE_EPOLL
360 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 398 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
361#endif 399#endif
362#if HAVE_SELECT 400#if HAVE_SELECT
363 if (ev_method == EVMETHOD_NONE) select_init (flags); 401 if (ev_method == EVMETHOD_NONE) select_init (flags);
364#endif 402#endif
365 403
366 if (ev_method) 404 if (ev_method)
367 { 405 {
368 evw_init (&sigev, sigcb); 406 evw_init (&sigev, sigcb);
369 siginit (); 407 siginit ();
408
409 evsignal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev);
411 }
370 } 412 }
371 413
372 return ev_method; 414 return ev_method;
373} 415}
374 416
553void ev_loop (int flags) 595void ev_loop (int flags)
554{ 596{
555 double block; 597 double block;
556 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
557 599
558 if (checkcnt)
559 {
560 queue_events ((W *)checks, checkcnt, EV_CHECK);
561 call_pending ();
562 }
563
564 do 600 do
565 { 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
566 /* update fd-related kernel structures */ 609 /* update fd-related kernel structures */
567 fd_reify (); 610 fd_reify ();
568 611
569 /* calculate blocking time */ 612 /* calculate blocking time */
570 613
571 /* 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 */
572 ev_now = ev_time (); 616 ev_now = ev_time ();
573 617
574 if (flags & EVLOOP_NONBLOCK || idlecnt) 618 if (flags & EVLOOP_NONBLOCK || idlecnt)
575 block = 0.; 619 block = 0.;
576 else 620 else
596 640
597 /* update ev_now, do magic */ 641 /* update ev_now, do magic */
598 time_update (); 642 time_update ();
599 643
600 /* queue pending timers and reschedule them */ 644 /* queue pending timers and reschedule them */
645 timers_reify (); /* relative timers called last */
601 periodics_reify (); /* absolute timers first */ 646 periodics_reify (); /* absolute timers called first */
602 timers_reify (); /* relative timers second */
603 647
604 /* queue idle watchers unless io or timers are pending */ 648 /* queue idle watchers unless io or timers are pending */
605 if (!pendingcnt) 649 if (!pendingcnt)
606 queue_events ((W *)idles, idlecnt, EV_IDLE); 650 queue_events ((W *)idles, idlecnt, EV_IDLE);
607 651
608 /* queue check and possibly idle watchers */ 652 /* queue check watchers, to be executed first */
653 if (checkcnt)
609 queue_events ((W *)checks, checkcnt, EV_CHECK); 654 queue_events ((W *)checks, checkcnt, EV_CHECK);
610 655
611 call_pending (); 656 call_pending ();
612 } 657 }
613 while (!ev_loop_done); 658 while (!ev_loop_done);
614 659
833 878
834 idles [w->active - 1] = idles [--idlecnt]; 879 idles [w->active - 1] = idles [--idlecnt];
835 ev_stop ((W)w); 880 ev_stop ((W)w);
836} 881}
837 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
838void evcheck_start (struct ev_check *w) 903void evcheck_start (struct ev_check *w)
839{ 904{
840 if (ev_is_active (w)) 905 if (ev_is_active (w))
841 return; 906 return;
842 907
850 ev_clear ((W)w); 915 ev_clear ((W)w);
851 if (ev_is_active (w)) 916 if (ev_is_active (w))
852 return; 917 return;
853 918
854 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);
855 ev_stop ((W)w); 939 ev_stop ((W)w);
856} 940}
857 941
858/*****************************************************************************/ 942/*****************************************************************************/
859 943

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines