84 lines
2.3 KiB
C
84 lines
2.3 KiB
C
#ifndef UC_IOMUX_WAKE_H_
|
|
#define UC_IOMUX_WAKE_H_
|
|
|
|
#include <pthread.h>
|
|
#include "iomux.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
struct IOMuxWaker;
|
|
typedef void (*uc_waker_cb)(struct IOMuxWaker *wk);
|
|
|
|
/** Usef for signalling an IOMux */
|
|
struct IOMuxSignaller {
|
|
int fd;
|
|
};
|
|
|
|
/** @file
|
|
* 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
|
|
* except for the signaller must not be accessed from anything other than the
|
|
* IOMux 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
|
|
struct IOMuxFD read_fd;
|
|
uc_waker_cb wake_cb;
|
|
|
|
//public fields
|
|
struct IOMuxSignaller signaller;
|
|
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.
|
|
* The embedded IOMuxSignaller of the IOMuxWaker can be used
|
|
* by someone else in the uc_iomux_waker_signal() function
|
|
*
|
|
* @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 IOMuxSignaller *signaller);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|
|
|