From 7d4934534e9c58a111215859ba83c32a9bc0fa8a Mon Sep 17 00:00:00 2001 From: Mat Date: Thu, 5 Mar 2020 12:52:34 +0200 Subject: Stabilise MoveToWorld (#4004) * Stabilise MoveToWorld * Fix comments and deprecate ScheduleMoveToWorld * Enhanced thread safety for m_WorldChangeInfo * Return unique_ptr from cAtomicUniquePtr::exchange * cWorld now calls entity cEntity::OnAddToWorld and cEntity::OnRemoveFromWorld. Allows broadcasting entities added to the world from the world's tick thread. This also factors out some common code from cEntity::DoMoveToWorld and cEntity::Initialize. As a consequence, cEntity::Destroy(false) (i.e. Destroying the entity without broadcasting) is impossible. This isn't used anywhere in Cuberite so it's now deprecated. * Update entity position after removing it from the world. Fixes broadcasts being sent to the wrong chunk. * Fix style * cEntity: Update LastSentPosition when sending spawn packet * Add Wno-deprecated-declarations to the lua bindings * Kill uses of ScheduleMoveToWorld --- src/OSSupport/AtomicUniquePtr.h | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/OSSupport/AtomicUniquePtr.h (limited to 'src/OSSupport/AtomicUniquePtr.h') 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. */ +template +class cAtomicUniquePtr +{ +public: + static_assert(!std::is_array::value, "cAtomicUniquePtr does not support arrays"); + DISALLOW_COPY_AND_ASSIGN(cAtomicUniquePtr); + + cAtomicUniquePtr() NOEXCEPT: + m_Ptr(nullptr) + { + } + + + cAtomicUniquePtr(std::unique_ptr a_Ptr) NOEXCEPT: + m_Ptr(a_Ptr.release()) + { + } + + cAtomicUniquePtr & operator = (std::unique_ptr 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 && 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 && 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 exchange(std::unique_ptr a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT + { + return std::unique_ptr{ 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 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 m_Ptr; +}; -- cgit v1.2.3