129 lines
3.2 KiB
C++
129 lines
3.2 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)
|
|
|
|
#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)
|
|
|
|
#else
|
|
|
|
#error Not supported on this platform
|
|
|
|
#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_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)
|