diff options
author | Mat <mail@mathias.is> | 2020-03-05 11:52:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-05 11:52:34 +0100 |
commit | 7d4934534e9c58a111215859ba83c32a9bc0fa8a (patch) | |
tree | cb9af9fea3de762410cb49c62a030b96e01eec6c /src/OSSupport | |
parent | Update LastSentPosition when entity has no speed (#4487) (diff) | |
download | cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar.gz cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar.bz2 cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar.lz cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar.xz cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.tar.zst cuberite-7d4934534e9c58a111215859ba83c32a9bc0fa8a.zip |
Diffstat (limited to 'src/OSSupport')
-rw-r--r-- | src/OSSupport/AtomicUniquePtr.h | 81 | ||||
-rw-r--r-- | src/OSSupport/CMakeLists.txt | 1 |
2 files changed, 82 insertions, 0 deletions
diff --git a/src/OSSupport/AtomicUniquePtr.h b/src/OSSupport/AtomicUniquePtr.h new file mode 100644 index 000000000..5b18763d3 --- /dev/null +++ b/src/OSSupport/AtomicUniquePtr.h @@ -0,0 +1,81 @@ + + +#pragma once + + +/** An RAII wrapper for std::atomic<T*>. */ +template <typename T> +class cAtomicUniquePtr +{ +public: + static_assert(!std::is_array<T>::value, "cAtomicUniquePtr does not support arrays"); + DISALLOW_COPY_AND_ASSIGN(cAtomicUniquePtr); + + cAtomicUniquePtr() NOEXCEPT: + m_Ptr(nullptr) + { + } + + + cAtomicUniquePtr(std::unique_ptr<T> a_Ptr) NOEXCEPT: + m_Ptr(a_Ptr.release()) + { + } + + cAtomicUniquePtr & operator = (std::unique_ptr<T> a_Ptr) NOEXCEPT + { + store(std::move(a_Ptr)); + return *this; + } + + ~cAtomicUniquePtr() NOEXCEPT + { + delete load(); + } + + operator T * () const NOEXCEPT + { + return load(); + } + + bool compare_exchange_weak(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT + { + bool DidExchange = m_Ptr.compare_exchange_weak(a_Expected, a_Desired.get(), a_Order); + if (DidExchange) + { + // Only release ownership from the caller if the exchange occurred + a_Desired.release(); + } + return DidExchange; + } + + bool compare_exchange_strong(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT + { + bool DidExchange = m_Ptr.compare_exchange_strong(a_Expected, a_Desired.get(), a_Order); + if (DidExchange) + { + // Only release ownership from the caller if the exchange occurred + a_Desired.release(); + } + return DidExchange; + } + + std::unique_ptr<T> exchange(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT + { + return std::unique_ptr<T>{ m_Ptr.exchange(a_Ptr.release(), a_Order) }; + } + + T * load(std::memory_order a_Order = std::memory_order_seq_cst) const NOEXCEPT + { + return m_Ptr.load(a_Order); + } + + void store(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT + { + // Store new value and delete old value + delete m_Ptr.exchange(a_Ptr.release(), a_Order); + } + +private: + std::atomic<T*> m_Ptr; +}; diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index 332b880ed..9f3fcb8a0 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -19,6 +19,7 @@ SET (SRCS ) SET (HDRS + AtomicUniquePtr.h CriticalSection.h Errors.h Event.h |