Use memory barriers in Linux fiber implementation

Prevent compilers from caching TLS variables across swapcontext calls
This commit is contained in:
Exzap 2023-09-08 07:04:28 +02:00
parent 524188bb7a
commit 62889adfde

View File

@ -1,5 +1,6 @@
#include "Fiber.h" #include "Fiber.h"
#include <ucontext.h> #include <ucontext.h>
#include <atomic>
thread_local Fiber* sCurrentFiber{}; thread_local Fiber* sCurrentFiber{};
@ -44,7 +45,9 @@ void Fiber::Switch(Fiber& targetFiber)
{ {
Fiber* leavingFiber = sCurrentFiber; Fiber* leavingFiber = sCurrentFiber;
sCurrentFiber = &targetFiber; sCurrentFiber = &targetFiber;
std::atomic_thread_fence(std::memory_order_seq_cst);
swapcontext((ucontext_t*)(leavingFiber->m_implData), (ucontext_t*)(targetFiber.m_implData)); swapcontext((ucontext_t*)(leavingFiber->m_implData), (ucontext_t*)(targetFiber.m_implData));
std::atomic_thread_fence(std::memory_order_seq_cst);
} }
void* Fiber::GetFiberPrivateData() void* Fiber::GetFiberPrivateData()