2016-05-02 04:01:00 +00:00
|
|
|
// Handles IO that may hang.
|
2011-12-27 05:21:12 +00:00
|
|
|
#ifndef FISH_IOTHREAD_H
|
|
|
|
#define FISH_IOTHREAD_H
|
|
|
|
|
2017-01-23 18:37:16 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Runs a command on a thread.
|
|
|
|
///
|
|
|
|
/// \param handler The function to execute on a background thread. Accepts an arbitrary context
|
|
|
|
/// pointer, and returns an int, which is passed to the completionCallback.
|
|
|
|
/// \param completionCallback The function to execute on the main thread once the background thread
|
|
|
|
/// is complete. Accepts an int (the return value of handler) and the context.
|
|
|
|
/// \param context A arbitary context pointer to pass to the handler and completion callback.
|
|
|
|
/// \return A sequence number, currently not very useful.
|
|
|
|
int iothread_perform_base(int (*handler)(void *), void (*completionCallback)(void *, int),
|
|
|
|
void *context);
|
|
|
|
|
|
|
|
/// Gets the fd on which to listen for completion callbacks.
|
|
|
|
///
|
|
|
|
/// \return A file descriptor on which to listen for completion callbacks.
|
2011-12-27 05:21:12 +00:00
|
|
|
int iothread_port(void);
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Services one iothread competion callback.
|
2011-12-27 05:21:12 +00:00
|
|
|
void iothread_service_completion(void);
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Waits for all iothreads to terminate.
|
2012-02-28 03:46:15 +00:00
|
|
|
void iothread_drain_all(void);
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Helper templates.
|
|
|
|
template <typename T>
|
2017-01-23 17:56:02 +00:00
|
|
|
int iothread_perform(int (*handler)(T *), void (*completion)(T *, int), T *context) {
|
2016-05-02 04:01:00 +00:00
|
|
|
return iothread_perform_base((int (*)(void *))handler,
|
2017-01-23 17:56:02 +00:00
|
|
|
(void (*)(void *, int))completion,
|
2016-05-02 04:01:00 +00:00
|
|
|
static_cast<void *>(context));
|
2012-02-15 19:33:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
// Variant that takes no completion callback.
|
|
|
|
template <typename T>
|
|
|
|
int iothread_perform(int (*handler)(T *), T *context) {
|
|
|
|
return iothread_perform_base((int (*)(void *))handler, (void (*)(void *, int))0,
|
|
|
|
static_cast<void *>(context));
|
2014-05-04 22:06:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:37:16 +00:00
|
|
|
/// Performs a function on the main thread, blocking until it completes.
|
|
|
|
void iothread_perform_on_main(std::function<void(void)> &&func);
|
|
|
|
|
2011-12-27 05:21:12 +00:00
|
|
|
#endif
|