--- libev/ev.h 2010/10/22 05:57:55 1.143 +++ libev/ev.h 2010/10/24 18:05:53 1.150 @@ -41,9 +41,13 @@ #define EV_H_ #ifdef __cplusplus -extern "C" { +# define EV_CPP(x) x +#else +# define EV_CPP(x) #endif +EV_CPP(extern "C" {) + /*****************************************************************************/ /* pre-4.0 compatibility */ @@ -96,7 +100,11 @@ #endif #ifndef EV_FORK_ENABLE -# define EV_FORK_ENABLE EV_FEATURE_WATCHERS +# define EV_FORK_ENABLE 0 /* not implemented */ +#endif + +#ifndef EV_CLEANUP_ENABLE +# define EV_CLEANUP_ENABLE EV_FEATURE_WATCHERS #endif #ifndef EV_SIGNAL_ENABLE @@ -147,23 +155,17 @@ # include #endif -#ifdef __cplusplus -# define EV_DEFARG(x) = x -#else -# define EV_DEFARG(x) -#endif - /* support multiple event loops? */ #if EV_MULTIPLICITY struct ev_loop; -# define EV_P struct ev_loop *loop -# define EV_P_ EV_P, -# define EV_A loop -# define EV_A_ EV_A, -# define EV_DEFAULT_UC ev_default_loop_uc () -# define EV_DEFAULT_UC_ EV_DEFAULT_UC, -# define EV_DEFAULT ev_default_loop (0) -# define EV_DEFAULT_ EV_DEFAULT, +# define EV_P struct ev_loop *loop /* a loop as sole parameter in a declaration */ +# define EV_P_ EV_P, /* a loop as first of multiple parameters */ +# define EV_A loop /* a loop as sole argument to a function call */ +# define EV_A_ EV_A, /* a loop as first of multiple arguments */ +# define EV_DEFAULT_UC ev_default_loop_uc_ () /* the default loop, if initialised, as sole arg */ +# define EV_DEFAULT_UC_ EV_DEFAULT_UC, /* the default loop as first of multiple arguments */ +# define EV_DEFAULT ev_default_loop (0) /* the default loop as sole arg */ +# define EV_DEFAULT_ EV_DEFAULT, /* the default loop as first of multiple arguments */ #else # define EV_P void # define EV_P_ @@ -176,12 +178,14 @@ # undef EV_EMBED_ENABLE #endif +/* EV_INLINE is used for functions in header files */ #if __STDC_VERSION__ >= 199901L || __GNUC__ >= 3 # define EV_INLINE static inline #else # define EV_INLINE static #endif +/* EV_PROTOTYPES can be sued to switch of prototype declarations */ #ifndef EV_PROTOTYPES # define EV_PROTOTYPES 1 #endif @@ -212,7 +216,8 @@ EV_CHECK = 0x00008000, /* event loop finished poll */ EV_EMBED = 0x00010000, /* embedded event loop needs sweep */ EV_FORK = 0x00020000, /* event loop resumed in child */ - EV_ASYNC = 0x00040000, /* async intra-loop signal */ + EV_CLEANUP = 0x00040000, /* event loop resumed in child */ + EV_ASYNC = 0x00080000, /* async intra-loop signal */ EV_CUSTOM = 0x01000000, /* for use by user code */ EV_ERROR = 0x80000000 /* sent when an error occurs */ }; @@ -391,12 +396,22 @@ #if EV_FORK_ENABLE /* the callback gets invoked before check in the child process when a fork was detected */ +/* revent EV_FORK */ typedef struct ev_fork { EV_WATCHER (ev_fork) } ev_fork; #endif +#if EV_CLEANUP_ENABLE +/* is invoked just before the loop gets destroyed */ +/* revent EV_CLEANUP */ +typedef struct ev_cleanup +{ + EV_WATCHER (ev_cleanup) +} ev_cleanup; +#endif + #if EV_EMBED_ENABLE /* used to embed an event loop inside another */ /* the callback gets invoked when the event loop has handled events, and can be 0 */ @@ -412,6 +427,7 @@ ev_periodic periodic; /* unused */ ev_idle idle; /* unused */ ev_fork fork; /* private */ + ev_cleanup cleanup; /* unused */ } ev_embed; #endif @@ -450,6 +466,9 @@ #if EV_FORK_ENABLE struct ev_fork fork; #endif +#if EV_CLEANUP_ENABLE + struct ev_cleanup cleanup; +#endif #if EV_EMBED_ENABLE struct ev_embed embed; #endif @@ -510,41 +529,35 @@ void ev_set_syserr_cb (void (*cb)(const char *msg)); #if EV_MULTIPLICITY + +/* the default loop is the only one that handles signals and child watchers */ +/* you can call this as often as you like */ +struct ev_loop *ev_default_loop (unsigned int flags EV_CPP (= 0)); + EV_INLINE struct ev_loop * -ev_default_loop_uc (void) +ev_default_loop_uc_ (void) { extern struct ev_loop *ev_default_loop_ptr; return ev_default_loop_ptr; } -/* the default loop is the only one that handles signals and child watchers */ -/* you can call this as often as you like */ -EV_INLINE struct ev_loop * -ev_default_loop (unsigned int flags EV_DEFARG (0)) +EV_INLINE int +ev_is_default_loop (EV_P) { - struct ev_loop *loop = ev_default_loop_uc (); - - if (!loop) - { - extern struct ev_loop *ev_default_loop_init (unsigned int flags); - - loop = ev_default_loop_init (flags); - } - - return loop; + return EV_A == EV_DEFAULT_UC; } /* create and destroy alternative loops that don't handle signals */ -struct ev_loop *ev_loop_new (unsigned int flags EV_DEFARG (0)); +struct ev_loop *ev_loop_new (unsigned int flags EV_CPP (= 0)); +/* destroy event loops, also works for the default loop */ void ev_loop_destroy (EV_P); -void ev_loop_fork (EV_P); ev_tstamp ev_now (EV_P); /* time w.r.t. timers and the eventloop, updated after each poll */ #else -int ev_default_loop (unsigned int flags EV_DEFARG (0)); /* returns true when successful */ +int ev_default_loop (unsigned int flags EV_CPP (= 0)); /* returns true when successful */ EV_INLINE ev_tstamp ev_now (void) @@ -553,26 +566,21 @@ return ev_rt_now; } -#endif /* multiplicity */ +/* looks weird, but ev_is_default_loop (EV_A) still works if this exists */ EV_INLINE int -ev_is_default_loop (EV_P) +ev_is_default_loop (void) { -#if EV_MULTIPLICITY - extern struct ev_loop *ev_default_loop_ptr; - - return !!(EV_A == ev_default_loop_ptr); -#else return 1; -#endif } -void ev_default_destroy (void); /* destroy the default loop */ -/* this needs to be called after fork, to duplicate the default loop */ -/* if you create alternative loops you have to call ev_loop_fork on them */ +#endif /* multiplicity */ + +/* this needs to be called after fork, to duplicate the loop */ +/* when you want to re-use it in the child */ /* you can call it in either the parent or the child */ /* you can actually call it at any time, anywhere :) */ -void ev_default_fork (void); +void ev_loop_fork (EV_P); unsigned int ev_backend (EV_P); /* backend in use by loop */ @@ -601,8 +609,8 @@ }; #if EV_PROTOTYPES -void ev_run (EV_P_ int flags EV_DEFARG (0)); -void ev_break (EV_P_ int how EV_DEFARG (EVBREAK_ONE)); /* break out of the loop */ +void ev_run (EV_P_ int flags EV_CPP (= 0)); +void ev_break (EV_P_ int how EV_CPP (= EVBREAK_ONE)); /* break out of the loop */ /* * ref/unref can be used to add or remove a refcount on the mainloop. every watcher @@ -664,6 +672,7 @@ #define ev_check_set(ev) /* nop, yes, this is a serious in-joke */ #define ev_embed_set(ev,other_) do { (ev)->other = (other_); } while (0) #define ev_fork_set(ev) /* nop, yes, this is a serious in-joke */ +#define ev_cleanup_set(ev) /* nop, yes, this is a serious in-joke */ #define ev_async_set(ev) /* nop, yes, this is a serious in-joke */ #define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0) @@ -677,6 +686,7 @@ #define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0) #define ev_embed_init(ev,cb,other) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0) #define ev_fork_init(ev,cb) do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0) +#define ev_cleanup_init(ev,cb) do { ev_init ((ev), (cb)); ev_cleanup_set ((ev)); } while (0) #define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0) #define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */ @@ -766,6 +776,11 @@ void ev_fork_stop (EV_P_ ev_fork *w); # endif +# if EV_CLEANUP_ENABLE +void ev_cleanup_start (EV_P_ ev_cleanup *w); +void ev_cleanup_stop (EV_P_ ev_cleanup *w); +# endif + # if EV_EMBED_ENABLE /* only supported when loop to be embedded is in fact embeddable */ void ev_embed_start (EV_P_ ev_embed *w); @@ -788,6 +803,8 @@ #if EV_PROTOTYPES EV_INLINE void ev_loop (EV_P_ int flags) { ev_run (EV_A_ flags); } EV_INLINE void ev_unloop (EV_P_ int how ) { ev_break (EV_A_ how ); } + EV_INLINE void ev_default_destroy (void) { ev_loop_destroy (EV_DEFAULT); } + EV_INLINE void ev_default_fork (void) { ev_loop_fork (EV_DEFAULT); } #if EV_FEATURE_API EV_INLINE void ev_loop_count (EV_P) { ev_iteration (EV_A); } EV_INLINE void ev_loop_depth (EV_P) { ev_depth (EV_A); } @@ -800,9 +817,7 @@ #endif -#ifdef __cplusplus -} -#endif +EV_CPP(}) #endif