diff options
Diffstat (limited to 'src/common/fiber.cpp')
| -rw-r--r-- | src/common/fiber.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp new file mode 100644 index 000000000..eb59f1aa9 --- /dev/null +++ b/src/common/fiber.cpp | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/fiber.h" | ||
| 6 | |||
| 7 | namespace Common { | ||
| 8 | |||
| 9 | #ifdef _MSC_VER | ||
| 10 | #include <windows.h> | ||
| 11 | |||
| 12 | struct Fiber::FiberImpl { | ||
| 13 | LPVOID handle = nullptr; | ||
| 14 | }; | ||
| 15 | |||
| 16 | void Fiber::_start([[maybe_unused]] void* parameter) { | ||
| 17 | guard.lock(); | ||
| 18 | if (previous_fiber) { | ||
| 19 | previous_fiber->guard.unlock(); | ||
| 20 | previous_fiber = nullptr; | ||
| 21 | } | ||
| 22 | entry_point(start_parameter); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void __stdcall FiberStartFunc(LPVOID lpFiberParameter) | ||
| 26 | { | ||
| 27 | auto fiber = static_cast<Fiber *>(lpFiberParameter); | ||
| 28 | fiber->_start(nullptr); | ||
| 29 | } | ||
| 30 | |||
| 31 | Fiber::Fiber(std::function<void(void*)>&& entry_point_func, void* start_parameter) | ||
| 32 | : guard{}, entry_point{std::move(entry_point_func)}, start_parameter{start_parameter}, previous_fiber{} { | ||
| 33 | impl = std::make_unique<FiberImpl>(); | ||
| 34 | impl->handle = CreateFiber(0, &FiberStartFunc, this); | ||
| 35 | } | ||
| 36 | |||
| 37 | Fiber::Fiber() : guard{}, entry_point{}, start_parameter{}, previous_fiber{} { | ||
| 38 | impl = std::make_unique<FiberImpl>(); | ||
| 39 | } | ||
| 40 | |||
| 41 | Fiber::~Fiber() { | ||
| 42 | // Make sure the Fiber is not being used | ||
| 43 | guard.lock(); | ||
| 44 | guard.unlock(); | ||
| 45 | DeleteFiber(impl->handle); | ||
| 46 | } | ||
| 47 | |||
| 48 | void Fiber::Exit() { | ||
| 49 | if (!is_thread_fiber) { | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | ConvertFiberToThread(); | ||
| 53 | guard.unlock(); | ||
| 54 | } | ||
| 55 | |||
| 56 | void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) { | ||
| 57 | to->guard.lock(); | ||
| 58 | to->previous_fiber = from; | ||
| 59 | SwitchToFiber(to->impl->handle); | ||
| 60 | auto previous_fiber = from->previous_fiber; | ||
| 61 | if (previous_fiber) { | ||
| 62 | previous_fiber->guard.unlock(); | ||
| 63 | previous_fiber.reset(); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { | ||
| 68 | std::shared_ptr<Fiber> fiber = std::shared_ptr<Fiber>{new Fiber()}; | ||
| 69 | fiber->guard.lock(); | ||
| 70 | fiber->impl->handle = ConvertThreadToFiber(NULL); | ||
| 71 | fiber->is_thread_fiber = true; | ||
| 72 | return fiber; | ||
| 73 | } | ||
| 74 | |||
| 75 | #else | ||
| 76 | |||
| 77 | #include <boost/context/detail/fcontext.hpp> | ||
| 78 | |||
| 79 | constexpr std::size_t default_stack_size = 1024 * 1024 * 4; // 4MB | ||
| 80 | |||
| 81 | struct Fiber::FiberImpl { | ||
| 82 | boost::context::detail::fcontext_t context; | ||
| 83 | std::array<u8, default_stack_size> stack; | ||
| 84 | }; | ||
| 85 | |||
| 86 | void Fiber::_start(void* parameter) { | ||
| 87 | guard.lock(); | ||
| 88 | boost::context::detail::transfer_t* transfer = static_cast<boost::context::detail::transfer_t*>(parameter); | ||
| 89 | if (previous_fiber) { | ||
| 90 | previous_fiber->impl->context = transfer->fctx; | ||
| 91 | previous_fiber->guard.unlock(); | ||
| 92 | previous_fiber = nullptr; | ||
| 93 | } | ||
| 94 | entry_point(start_parameter); | ||
| 95 | } | ||
| 96 | |||
| 97 | static void FiberStartFunc(boost::context::detail::transfer_t transfer) | ||
| 98 | { | ||
| 99 | auto fiber = static_cast<Fiber *>(transfer.data); | ||
| 100 | fiber->_start(&transfer); | ||
| 101 | } | ||
| 102 | |||
| 103 | Fiber::Fiber(std::function<void(void*)>&& entry_point_func, void* start_parameter) | ||
| 104 | : guard{}, entry_point{std::move(entry_point_func)}, start_parameter{start_parameter}, previous_fiber{} { | ||
| 105 | impl = std::make_unique<FiberImpl>(); | ||
| 106 | auto start_func = std::bind(&Fiber::start, this); | ||
| 107 | impl->context = | ||
| 108 | boost::context::detail::make_fcontext(impl->stack.data(), impl->stack.size(), &start_func); | ||
| 109 | } | ||
| 110 | |||
| 111 | Fiber::Fiber() : guard{}, entry_point{}, start_parameter{}, previous_fiber{} { | ||
| 112 | impl = std::make_unique<FiberImpl>(); | ||
| 113 | } | ||
| 114 | |||
| 115 | Fiber::~Fiber() { | ||
| 116 | // Make sure the Fiber is not being used | ||
| 117 | guard.lock(); | ||
| 118 | guard.unlock(); | ||
| 119 | } | ||
| 120 | |||
| 121 | void Fiber::Exit() { | ||
| 122 | if (!is_thread_fiber) { | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | guard.unlock(); | ||
| 126 | } | ||
| 127 | |||
| 128 | void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) { | ||
| 129 | to->guard.lock(); | ||
| 130 | to->previous_fiber = from; | ||
| 131 | auto transfer = boost::context::detail::jump_fcontext(to->impl.context, nullptr); | ||
| 132 | auto previous_fiber = from->previous_fiber; | ||
| 133 | if (previous_fiber) { | ||
| 134 | previous_fiber->impl->context = transfer.fctx; | ||
| 135 | previous_fiber->guard.unlock(); | ||
| 136 | previous_fiber.reset(); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { | ||
| 141 | std::shared_ptr<Fiber> fiber = std::shared_ptr<Fiber>{new Fiber()}; | ||
| 142 | fiber->is_thread_fiber = true; | ||
| 143 | return fiber; | ||
| 144 | } | ||
| 145 | |||
| 146 | #endif | ||
| 147 | } // namespace Common | ||