|
|
|
@ -12,10 +12,13 @@
|
|
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
|
|
constexpr std::size_t default_stack_size = 256 * 1024; // 256kb
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(WIN32)
|
|
|
|
|
|
|
|
|
|
struct Fiber::FiberImpl {
|
|
|
|
|
LPVOID handle = nullptr;
|
|
|
|
|
LPVOID rewind_handle = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void Fiber::start() {
|
|
|
|
@ -26,15 +29,29 @@ void Fiber::start() {
|
|
|
|
|
UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fiber::onRewind() {
|
|
|
|
|
ASSERT(impl->handle != nullptr);
|
|
|
|
|
DeleteFiber(impl->handle);
|
|
|
|
|
impl->handle = impl->rewind_handle;
|
|
|
|
|
impl->rewind_handle = nullptr;
|
|
|
|
|
rewind_point(rewind_parameter);
|
|
|
|
|
UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void __stdcall Fiber::FiberStartFunc(void* fiber_parameter) {
|
|
|
|
|
auto fiber = static_cast<Fiber*>(fiber_parameter);
|
|
|
|
|
fiber->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void __stdcall Fiber::RewindStartFunc(void* fiber_parameter) {
|
|
|
|
|
auto fiber = static_cast<Fiber*>(fiber_parameter);
|
|
|
|
|
fiber->onRewind();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Fiber::Fiber(std::function<void(void*)>&& entry_point_func, void* start_parameter)
|
|
|
|
|
: entry_point{std::move(entry_point_func)}, start_parameter{start_parameter} {
|
|
|
|
|
impl = std::make_unique<FiberImpl>();
|
|
|
|
|
impl->handle = CreateFiber(0, &FiberStartFunc, this);
|
|
|
|
|
impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Fiber::Fiber() {
|
|
|
|
@ -60,6 +77,18 @@ void Fiber::Exit() {
|
|
|
|
|
guard.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fiber::SetRewindPoint(std::function<void(void*)>&& rewind_func, void* start_parameter) {
|
|
|
|
|
rewind_point = std::move(rewind_func);
|
|
|
|
|
rewind_parameter = start_parameter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fiber::Rewind() {
|
|
|
|
|
ASSERT(rewind_point);
|
|
|
|
|
ASSERT(impl->rewind_handle == nullptr);
|
|
|
|
|
impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this);
|
|
|
|
|
SwitchToFiber(impl->rewind_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) {
|
|
|
|
|
ASSERT_MSG(from != nullptr, "Yielding fiber is null!");
|
|
|
|
|
ASSERT_MSG(to != nullptr, "Next fiber is null!");
|
|
|
|
@ -81,7 +110,6 @@ std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
constexpr std::size_t default_stack_size = 1024 * 1024; // 1MB
|
|
|
|
|
|
|
|
|
|
struct Fiber::FiberImpl {
|
|
|
|
|
alignas(64) std::array<u8, default_stack_size> stack;
|
|
|
|
|