From 62889adfde94710f280868c1b7dc4be4cc8cc229 Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:04:28 +0200 Subject: [PATCH] Use memory barriers in Linux fiber implementation Prevent compilers from caching TLS variables across swapcontext calls --- src/util/Fiber/FiberUnix.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util/Fiber/FiberUnix.cpp b/src/util/Fiber/FiberUnix.cpp index 7d3bf05a..0d527069 100644 --- a/src/util/Fiber/FiberUnix.cpp +++ b/src/util/Fiber/FiberUnix.cpp @@ -1,5 +1,6 @@ #include "Fiber.h" #include +#include thread_local Fiber* sCurrentFiber{}; @@ -44,7 +45,9 @@ void Fiber::Switch(Fiber& targetFiber) { Fiber* leavingFiber = sCurrentFiber; sCurrentFiber = &targetFiber; + std::atomic_thread_fence(std::memory_order_seq_cst); swapcontext((ucontext_t*)(leavingFiber->m_implData), (ucontext_t*)(targetFiber.m_implData)); + std::atomic_thread_fence(std::memory_order_seq_cst); } void* Fiber::GetFiberPrivateData()