diff options
Diffstat (limited to 'src/common/fiber.h')
| -rw-r--r-- | src/common/fiber.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/common/fiber.h b/src/common/fiber.h new file mode 100644 index 000000000..ab44905cf --- /dev/null +++ b/src/common/fiber.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <memory> | ||
| 9 | |||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "common/spin_lock.h" | ||
| 12 | |||
| 13 | namespace Common { | ||
| 14 | |||
| 15 | class Fiber { | ||
| 16 | public: | ||
| 17 | Fiber(std::function<void(void*)>&& entry_point_func, void* start_parameter); | ||
| 18 | ~Fiber(); | ||
| 19 | |||
| 20 | Fiber(const Fiber&) = delete; | ||
| 21 | Fiber& operator=(const Fiber&) = delete; | ||
| 22 | |||
| 23 | Fiber(Fiber&&) = default; | ||
| 24 | Fiber& operator=(Fiber&&) = default; | ||
| 25 | |||
| 26 | /// Yields control from Fiber 'from' to Fiber 'to' | ||
| 27 | /// Fiber 'from' must be the currently running fiber. | ||
| 28 | static void YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to); | ||
| 29 | static std::shared_ptr<Fiber> ThreadToFiber(); | ||
| 30 | |||
| 31 | /// Only call from main thread's fiber | ||
| 32 | void Exit(); | ||
| 33 | |||
| 34 | /// Used internally but required to be public, Shall not be used | ||
| 35 | void _start(void* parameter); | ||
| 36 | |||
| 37 | /// Changes the start parameter of the fiber. Has no effect if the fiber already started | ||
| 38 | void SetStartParameter(void* new_parameter) { | ||
| 39 | start_parameter = new_parameter; | ||
| 40 | } | ||
| 41 | |||
| 42 | private: | ||
| 43 | Fiber(); | ||
| 44 | |||
| 45 | struct FiberImpl; | ||
| 46 | |||
| 47 | SpinLock guard; | ||
| 48 | std::function<void(void*)> entry_point; | ||
| 49 | void* start_parameter; | ||
| 50 | std::shared_ptr<Fiber> previous_fiber; | ||
| 51 | std::unique_ptr<FiberImpl> impl; | ||
| 52 | bool is_thread_fiber{}; | ||
| 53 | }; | ||
| 54 | |||
| 55 | } // namespace Common | ||