diff options
| author | Stefan Boberg <[email protected]> | 2023-11-16 15:34:25 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-16 15:34:25 +0100 |
| commit | 3d16e8a64e9ef76f407270ef092671a1be7bf346 (patch) | |
| tree | a354559a0a3e857b8a6f3e292c9c9cd25ba87bc5 | |
| parent | 0.2.34 (diff) | |
| download | zen-3d16e8a64e9ef76f407270ef092671a1be7bf346.tar.xz zen-3d16e8a64e9ef76f407270ef092671a1be7bf346.zip | |
changed posix event implementation to use std::atomic instead of volatile (#547)
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | src/zencore/include/zencore/thread.h | 4 | ||||
| -rw-r--r-- | src/zencore/thread.cpp | 18 |
3 files changed, 14 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c2079397..091a7393d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ ## - Bumped zenserver data schema (to '5') to wipe corrupted state caused by version 0.2.31/0.2.32 - Bugfix: Fix hang on ZenServerInstance shutdown on Mac/Linux +- Improvement: Event implementation now uses `std::atomic_bool` instead of `volatile bool` for correctness - Improvement: We now set Sentry to `<username>@<hostname>` if personal information is allowed to be sent - Improvement: Removed dependency on cxxopts exception types, to enable use of library versions >3.0.0 + ## 0.2.33 - Bugfix: Fix index out of bounds in CacheBucket::CompactState - Bugfix: Implement != operator for DiskLocation to avoid comparing uninitialized data diff --git a/src/zencore/include/zencore/thread.h b/src/zencore/include/zencore/thread.h index 9f2671610..47f37c9a3 100644 --- a/src/zencore/include/zencore/thread.h +++ b/src/zencore/include/zencore/thread.h @@ -10,6 +10,8 @@ #include <string_view> #include <vector> +#define ZEN_USE_WINDOWS_EVENTS ZEN_PLATFORM_WINDOWS + namespace zen { void SetCurrentThreadName(std::string_view ThreadName); @@ -107,7 +109,7 @@ public: ZENCORE_API bool Wait(int TimeoutMs = -1); ZENCORE_API void Close(); -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS inline void* GetWindowsHandle() { return m_EventHandle; } #endif diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index 758e88350..27a5ec1ae 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -152,12 +152,12 @@ RwLock::ReleaseExclusive() noexcept ////////////////////////////////////////////////////////////////////////// -#if !ZEN_PLATFORM_WINDOWS +#if !ZEN_USE_WINDOWS_EVENTS struct EventInner { std::mutex Mutex; std::condition_variable CondVar; - bool volatile bSet = false; + std::atomic_bool bSet{false}; }; #endif // !ZEN_PLATFORM_WINDOWS @@ -166,7 +166,7 @@ Event::Event() bool bManualReset = true; bool bInitialState = false; -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS m_EventHandle = CreateEvent(nullptr, bManualReset, bInitialState, nullptr); #else ZEN_UNUSED(bManualReset); @@ -184,7 +184,7 @@ Event::~Event() void Event::Set() { -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS SetEvent(m_EventHandle); #else auto* Inner = (EventInner*)m_EventHandle; @@ -199,7 +199,7 @@ Event::Set() void Event::Reset() { -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS ResetEvent(m_EventHandle); #else auto* Inner = (EventInner*)m_EventHandle; @@ -213,7 +213,7 @@ Event::Reset() void Event::Close() { -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS CloseHandle(m_EventHandle); #else auto* Inner = (EventInner*)m_EventHandle; @@ -225,7 +225,7 @@ Event::Close() bool Event::Wait(int TimeoutMs) { -#if ZEN_PLATFORM_WINDOWS +#if ZEN_USE_WINDOWS_EVENTS using namespace std::literals; const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs; @@ -250,14 +250,14 @@ Event::Wait(int TimeoutMs) return true; } - return Inner->CondVar.wait_for(Lock, std::chrono::milliseconds(TimeoutMs), [&] { return Inner->bSet; }); + return Inner->CondVar.wait_for(Lock, std::chrono::milliseconds(TimeoutMs), [&] { return Inner->bSet.load(); }); } std::unique_lock Lock(Inner->Mutex); if (!Inner->bSet) { - Inner->CondVar.wait(Lock, [&] { return Inner->bSet; }); + Inner->CondVar.wait(Lock, [&] { return Inner->bSet.load(); }); } return true; |