|
|
@ -2,6 +2,7 @@
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/fiber.h"
|
|
|
|
#include "common/fiber.h"
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <windows.h>
|
|
|
|
#include <windows.h>
|
|
|
@ -18,11 +19,11 @@ struct Fiber::FiberImpl {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::start() {
|
|
|
|
void Fiber::start() {
|
|
|
|
if (previous_fiber) {
|
|
|
|
ASSERT(previous_fiber != nullptr);
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber = nullptr;
|
|
|
|
previous_fiber.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_point(start_parameter);
|
|
|
|
entry_point(start_parameter);
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void __stdcall Fiber::FiberStartFunc(void* fiber_parameter)
|
|
|
|
void __stdcall Fiber::FiberStartFunc(void* fiber_parameter)
|
|
|
@ -43,12 +44,16 @@ Fiber::Fiber() : guard{}, entry_point{}, start_parameter{}, previous_fiber{} {
|
|
|
|
|
|
|
|
|
|
|
|
Fiber::~Fiber() {
|
|
|
|
Fiber::~Fiber() {
|
|
|
|
// Make sure the Fiber is not being used
|
|
|
|
// Make sure the Fiber is not being used
|
|
|
|
guard.lock();
|
|
|
|
bool locked = guard.try_lock();
|
|
|
|
guard.unlock();
|
|
|
|
ASSERT_MSG(locked, "Destroying a fiber that's still running");
|
|
|
|
|
|
|
|
if (locked) {
|
|
|
|
|
|
|
|
guard.unlock();
|
|
|
|
|
|
|
|
}
|
|
|
|
DeleteFiber(impl->handle);
|
|
|
|
DeleteFiber(impl->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::Exit() {
|
|
|
|
void Fiber::Exit() {
|
|
|
|
|
|
|
|
ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber");
|
|
|
|
if (!is_thread_fiber) {
|
|
|
|
if (!is_thread_fiber) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -57,14 +62,15 @@ void Fiber::Exit() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) {
|
|
|
|
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!");
|
|
|
|
to->guard.lock();
|
|
|
|
to->guard.lock();
|
|
|
|
to->previous_fiber = from;
|
|
|
|
to->previous_fiber = from;
|
|
|
|
SwitchToFiber(to->impl->handle);
|
|
|
|
SwitchToFiber(to->impl->handle);
|
|
|
|
auto previous_fiber = from->previous_fiber;
|
|
|
|
auto previous_fiber = from->previous_fiber;
|
|
|
|
if (previous_fiber) {
|
|
|
|
ASSERT(previous_fiber != nullptr);
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber.reset();
|
|
|
|
previous_fiber.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
|
|
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
|
@ -85,12 +91,12 @@ struct alignas(64) Fiber::FiberImpl {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::start(boost::context::detail::transfer_t& transfer) {
|
|
|
|
void Fiber::start(boost::context::detail::transfer_t& transfer) {
|
|
|
|
if (previous_fiber) {
|
|
|
|
ASSERT(previous_fiber != nullptr);
|
|
|
|
previous_fiber->impl->context = transfer.fctx;
|
|
|
|
previous_fiber->impl->context = transfer.fctx;
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber = nullptr;
|
|
|
|
previous_fiber.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_point(start_parameter);
|
|
|
|
entry_point(start_parameter);
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::FiberStartFunc(boost::context::detail::transfer_t transfer)
|
|
|
|
void Fiber::FiberStartFunc(boost::context::detail::transfer_t transfer)
|
|
|
@ -113,11 +119,15 @@ Fiber::Fiber() : guard{}, entry_point{}, start_parameter{}, previous_fiber{} {
|
|
|
|
|
|
|
|
|
|
|
|
Fiber::~Fiber() {
|
|
|
|
Fiber::~Fiber() {
|
|
|
|
// Make sure the Fiber is not being used
|
|
|
|
// Make sure the Fiber is not being used
|
|
|
|
guard.lock();
|
|
|
|
bool locked = guard.try_lock();
|
|
|
|
guard.unlock();
|
|
|
|
ASSERT_MSG(locked, "Destroying a fiber that's still running");
|
|
|
|
|
|
|
|
if (locked) {
|
|
|
|
|
|
|
|
guard.unlock();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::Exit() {
|
|
|
|
void Fiber::Exit() {
|
|
|
|
|
|
|
|
ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber");
|
|
|
|
if (!is_thread_fiber) {
|
|
|
|
if (!is_thread_fiber) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -125,15 +135,16 @@ void Fiber::Exit() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) {
|
|
|
|
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!");
|
|
|
|
to->guard.lock();
|
|
|
|
to->guard.lock();
|
|
|
|
to->previous_fiber = from;
|
|
|
|
to->previous_fiber = from;
|
|
|
|
auto transfer = boost::context::detail::jump_fcontext(to->impl.context, nullptr);
|
|
|
|
auto transfer = boost::context::detail::jump_fcontext(to->impl.context, nullptr);
|
|
|
|
auto previous_fiber = from->previous_fiber;
|
|
|
|
auto previous_fiber = from->previous_fiber;
|
|
|
|
if (previous_fiber) {
|
|
|
|
ASSERT(previous_fiber != nullptr);
|
|
|
|
previous_fiber->impl->context = transfer.fctx;
|
|
|
|
previous_fiber->impl->context = transfer.fctx;
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber->guard.unlock();
|
|
|
|
previous_fiber.reset();
|
|
|
|
previous_fiber.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
|
|
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
|
|