#ifndef UC_IOMUX_WAKE_H_ #define UC_IOMUX_WAKE_H_ #include #include "iomux.h" #ifdef __cplusplus extern "C" { #endif struct IOMuxWaker; typedef void (*uc_waker_cb)(struct IOMuxWaker *wk); /** These are primitives to wake up an IOMux. * An IOMux event loop can be woken up from e.g. an unix signal handler * or another thread. The IOMuxWaker itself is not thread safe, it's members * must not be accessed from anything other than the loop it's connected to. * Only uc_iomux_waker_signal() can be called from another thread/signal handler * than the IOMux it's connected to. * * Note that several signals to wake up a loop can get merged, the callback * handler can be called only once for several signals. */ struct IOMuxWaker { //private fields int fds[2]; struct IOMuxFD read_fd; uc_waker_cb wake_cb; //public fields struct IOMux *mux; void *cookie; }; /** Initialize an IOMuxWaker. * Must be called from the thread running the IOMux. * * @mux the IOMux to be woken up. * @wk the waker to initialize * @cb callback to invoke in the mux when signalled * @return 0 on success, errno value if initializing fails */ int uc_iomux_waker_init(struct IOMux *mux, struct IOMuxWaker *wk, uc_waker_cb cb); /** Destroy an IOMuxWaker. * Must previously been successfully initialized. * Must be called from the thread running the IOMux. * Care must be taken so the IOMuxWaker is destroyed * without race conditions with a thread that wants to _signal the * waker. * * @param wk * @return 0 on success, errno on failure (Things are seriously * wrong if this fails) */ int uc_iomux_waker_destroy(struct IOMuxWaker *wk); /** Signal an IOMux to wake up and run its callback handler. * Is async signal safe. * Several signals in a short time may result in the callback * being run only once. * * @param wk * @return 0 on success, errno on failure (Things are seriously * wrong if this fails) */ int uc_iomux_waker_signal(struct IOMuxWaker *wk); #ifdef __cplusplus } #endif #endif