Files

152 lines
4.0 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Fiber.h
*
* @brief Header file for the fiber class.
*/
#if (!defined ACTIV_BASE_FIBER_H)
#define ACTIV_BASE_FIBER_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/CxxYZ/functional.h"
#if (defined ACTIV_FIBER_USE_THREAD)
#include "ActivMiddleware/CxxYZ/condition_variable.h"
#include "ActivMiddleware/CxxYZ/thread.h"
#include "ActivMiddleware/CxxYZ/mutex.h"
#elif (defined ACTIV_PLATFORM_WINDOWS) && (!defined ACTIV_COMPILER_GCC)
// Native Fibers currently broken with GCC due to setjmp-longjmp implementation of exceptions.
// May be fixed in mingw-w64 4.6 which might use SEH.
#define ACTIV_FIBER_USE_WINDOWS_FIBER
#elif (defined ACTIV_POSIX_HAS_CONTEXT_API)
#define ACTIV_FIBER_USE_UCONTEXT
#include <ucontext.h>
#include ACTIV_BOOST_INCLUDE(scoped_array.hpp)
#elif (defined ACTIV_PLATFORM_WASM)
// NB emscripten coroutines require building with "-s ASYNCIFY=1", which causes bloat and seems to be deprecated.
// Hopefully once thread support makes it to wasm, we can enable ACTIV_FIBER_USE_THREAD.
#define ACTIV_FIBER_USE_EMSCRIPTEN
#include <emscripten.h>
#else
#define ACTIV_FIBER_USE_BOOST_FCONTEXT
#include ACTIV_BOOST_INCLUDE(context/all.hpp)
#endif
//! Activ namespace.
namespace Activ
{
/**
* @brief Fiber class.
*/
class Fiber : private Boost::noncopyable
{
public:
typedef CxxYZ::function<void ()> FunctionType;
/**
* @brief Constructor.
*
* @param function function to start on first call to @a Continue(). Copy is taken. If this function exits, the fiber
* implicitly Cede()s back to the calling fiber.
* @param stackSize size of stack to commit. 0 will use the current stack size setting for the process, unless the
* environment variable ACTIV_FIBER_STACK_SIZE is set to a numeric value.
*
* @throw Exception.
*/
ACTIV_BASE_API Fiber(const FunctionType &function, const size_t stackSize = 0);
//! Destructor.
ACTIV_BASE_API ~Fiber();
//! Start or continue to run this fiber.
ACTIV_BASE_API void Continue();
/**
* @brief Yield execution of this fiber and return to the calling fiber.
*
* This call just returns if this object has not been Continue() at all.
* This would be called Yield() except for nonsense in WinBase.h: "#define Yield()". If only inline functions were a thing!
*/
ACTIV_BASE_API void Cede();
//! Is the fiber currently running?
bool IsRunning() const;
private:
enum State
{
STATE_NOT_RUNNING, ///< Fiber isn't running.
STATE_RUNNING, ///< Fiber has been Continue()d.
STATE_EXITED ///< Fiber function has exited.
};
State m_state; ///< Current state of fiber.
const FunctionType m_function;
#if (defined ACTIV_FIBER_USE_WINDOWS_FIBER)
typedef void *FiberId;
static void CALLBACK StaticFiberProc(void *pData);
#elif (defined ACTIV_FIBER_USE_UCONTEXT)
typedef ucontext_t FiberId;
static void StaticFiberProc(const uint32_t highFiberAddress, const uint32_t lowFiberAddress);
const size_t m_stackSize;
char * const m_pStack; ///< Stack for fiber.
#elif (defined ACTIV_FIBER_USE_BOOST_FCONTEXT)
typedef Boost::context::fcontext_t FiberId;
static void StaticFiberProc(intptr_t pData);
const size_t m_stackSize;
void * const m_pStack;
#elif (defined ACTIV_FIBER_USE_EMSCRIPTEN)
typedef emscripten_coroutine FiberId;
static void StaticFiberProc(void *pData);
#elif (defined ACTIV_FIBER_USE_THREAD)
void ThreadFunction();
void WaitForState(const State state, CxxYZ::unique_lock<CxxYZ::mutex> &uniqueLock);
CxxYZ::thread m_thread;
CxxYZ::mutex m_mutex;
CxxYZ::condition_variable m_conditionVariable;
#else
#error Write me
#endif
#if (!defined ACTIV_FIBER_USE_THREAD)
FiberId m_id; ///< This fiber id.
FiberId m_returnId; ///< Fiber to return to.
#endif
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Fiber::IsRunning() const
{
return (STATE_RUNNING == m_state);
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_FIBER_H)