2011-12-27 13:21:12 +08:00
|
|
|
/** \file iothread.h
|
|
|
|
Handles IO that may hang.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FISH_IOTHREAD_H
|
|
|
|
#define FISH_IOTHREAD_H
|
|
|
|
|
|
|
|
/**
|
|
|
|
Runs a command on a thread.
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2011-12-27 13:21:12 +08:00
|
|
|
\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.
|
|
|
|
*/
|
2012-02-16 03:33:41 +08:00
|
|
|
int iothread_perform_base(int (*handler)(void *), void (*completionCallback)(void *, int), void *context);
|
2011-12-27 13:21:12 +08:00
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
/**
|
2011-12-27 13:21:12 +08:00
|
|
|
Gets the fd on which to listen for completion callbacks.
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2011-12-27 13:21:12 +08:00
|
|
|
\return A file descriptor on which to listen for completion callbacks.
|
|
|
|
*/
|
|
|
|
int iothread_port(void);
|
|
|
|
|
2012-02-28 11:46:15 +08:00
|
|
|
/** Services one iothread competion callback. */
|
2011-12-27 13:21:12 +08:00
|
|
|
void iothread_service_completion(void);
|
|
|
|
|
2012-03-01 09:55:50 +08:00
|
|
|
/** Waits for all iothreads to terminate. */
|
2012-02-28 11:46:15 +08:00
|
|
|
void iothread_drain_all(void);
|
|
|
|
|
2013-11-28 08:04:12 +08:00
|
|
|
/** Performs a function on the main thread, blocking until it completes */
|
|
|
|
int iothread_perform_on_main_base(int (*handler)(void *), void *context);
|
|
|
|
|
|
|
|
/** Helper templates */
|
2012-02-16 03:33:41 +08:00
|
|
|
template<typename T>
|
2012-11-19 08:30:30 +08:00
|
|
|
int iothread_perform(int (*handler)(T *), void (*completionCallback)(T *, int), T *context)
|
|
|
|
{
|
2012-02-16 03:33:41 +08:00
|
|
|
return iothread_perform_base((int (*)(void *))handler, (void (*)(void *, int))completionCallback, static_cast<void *>(context));
|
|
|
|
}
|
|
|
|
|
2015-01-09 03:02:40 +08:00
|
|
|
/* Variant that takes no completion callback */
|
2014-05-05 06:06:40 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-11-28 08:04:12 +08:00
|
|
|
template<typename T>
|
|
|
|
int iothread_perform_on_main(int (*handler)(T *), T *context)
|
|
|
|
{
|
2013-11-30 05:31:18 +08:00
|
|
|
return iothread_perform_on_main_base((int (*)(void *))handler, (void *)(context));
|
2013-11-28 08:04:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-27 13:21:12 +08:00
|
|
|
#endif
|