X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/469af6002e7d33bcc351c1113afb188c3c246b91..cc491ddade261fb0e1755c6da29a95f1c66e8b32:/ev.c?ds=inline diff --git a/ev.c b/ev.c index 9a6f785..b2e48f8 100644 --- a/ev.c +++ b/ev.c @@ -38,6 +38,8 @@ #include #include +#include +#include #include #include @@ -61,6 +63,7 @@ #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ #define MAX_BLOCKTIME 60. +#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ #include "ev.h" @@ -334,6 +337,30 @@ static int checkmax, checkcnt; /*****************************************************************************/ +static struct ev_child *childs [PID_HASHSIZE]; +static struct ev_signal childev; + +#ifndef WCONTINUED +# define WCONTINUED 0 +#endif + +static void +childcb (struct ev_signal *sw, int revents) +{ + struct ev_child *w; + int pid, status; + + while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) + for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) + if (w->pid == pid || w->pid == -1) + { + w->status = status; + event ((W)w, EV_CHILD); + } +} + +/*****************************************************************************/ + #if HAVE_EPOLL # include "ev_epoll.c" #endif @@ -370,6 +397,9 @@ int ev_init (int flags) { evw_init (&sigev, sigcb); siginit (); + + evsignal_init (&childev, childcb, SIGCHLD); + evsignal_start (&childev); } return ev_method; @@ -561,7 +591,7 @@ void ev_loop (int flags) do { /* queue check watchers (and execute them) */ - if (checkcnt) + if (preparecnt) { queue_events ((W *)prepares, preparecnt, EV_PREPARE); call_pending (); @@ -572,7 +602,8 @@ void ev_loop (int flags) /* calculate blocking time */ - /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ + /* we only need this for !monotonic clockor timers, but as we basically + always have timers, we just calculate it always */ ev_now = ev_time (); if (flags & EVLOOP_NONBLOCK || idlecnt) @@ -880,6 +911,25 @@ void evcheck_stop (struct ev_check *w) ev_stop ((W)w); } +void evchild_start (struct ev_child *w) +{ + if (ev_is_active (w)) + return; + + ev_start ((W)w, 1); + wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); +} + +void evchild_stop (struct ev_child *w) +{ + ev_clear ((W)w); + if (ev_is_active (w)) + return; + + wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); + ev_stop ((W)w); +} + /*****************************************************************************/ struct ev_once