--- gvpe/src/util.C 2008/08/10 14:48:57 1.24 +++ gvpe/src/util.C 2011/03/08 17:33:31 1.25 @@ -1,6 +1,6 @@ /* util.C -- process management and other utility functions - Copyright (C) 2003-2008 Marc Lehmann + Copyright (C) 2003-2011 Marc Lehmann Some of these are taken from tinc, see the AUTHORS file. @@ -37,6 +37,8 @@ #include #include +#include + #include #include #include @@ -44,6 +46,10 @@ #include #include +#if ENABLE_PTHREADS +# include +#endif + #include "netcompat.h" #include "gettext.h" @@ -139,6 +145,8 @@ return 0; } +/*****************************************************************************/ + pid_t run_script (const run_script_cb &cb, bool wait) { @@ -193,6 +201,99 @@ return pid; } +/*****************************************************************************/ + +#if 0 /* not yet used */ + +#if ENABLE_PTHREADS +struct async_cb +{ + callback work_cb; + callback done_cb; +}; + +static ev::async async_done_w; +static std::queue< callback > async_q; + +static callback work_cb; + +static void * +async_exec (void *) +{ + work_cb (); + async_done_w.send (); + + return 0; +} + +static void +async_q_next () +{ + work_cb = async_q.front (); async_q.pop (); + + sigset_t fullsigset, oldsigset; + pthread_attr_t attr; + pthread_t tid; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + //pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); + sigfillset (&fullsigset); + pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); + + if (pthread_create (&tid, &attr, async_exec, 0)) + async_exec (0); + + pthread_sigmask (SIG_SETMASK, &oldsigset, 0); + pthread_attr_destroy (&attr); +} + +namespace { + void + async_done (ev::async &w, int revents) + { + callback done_cb = async_q.front (); async_q.pop (); + + if (async_q.empty ()) + async_done_w.stop (); + else + async_q_next (); + + done_cb (); + } +}; + +void +async (callback work_cb, callback done_cb) +{ + bool was_empty = async_q.empty (); + + async_q.push (work_cb); + async_q.push (done_cb); + + if (was_empty) + { + async_done_w.set (); + async_done_w.start (); + async_q_next (); + } +} + +#else + +void +async (callback work_cb, callback done_cb) +{ + work_cb (); + done_cb (); +} + +#endif + +#endif + +/*****************************************************************************/ + #if ENABLE_HTTP_PROXY // works like strdup u8 *