1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_ATOMIC_SHARED_PTR_HPP
20 #define REALM_ATOMIC_SHARED_PTR_HPP
29 // Check if std::atomic_load has an overload taking a std::shared_ptr, and set
30 // HasAtomicPtrOps to either true_type or false_type
32 template<typename... Ts> struct make_void { typedef void type; };
33 template<typename... Ts> using void_t = typename make_void<Ts...>::type;
35 template<typename, typename = void_t<>>
36 struct HasAtomicPtrOps : std::false_type { };
39 struct HasAtomicPtrOps<T, void_t<decltype(std::atomic_load(std::declval<T*>()))>> : std::true_type { };
43 // A wrapper for std::shared_ptr that enables sharing a shared_ptr instance
44 // (and not just a thing *pointed to* by a shared_ptr) between threads. Is
45 // lock-free iff the underlying shared_ptr implementation supports atomic
46 // operations. Currently the only implemented operation other than copy/move
47 // construction/assignment is exchange().
48 template<typename T, bool = _impl::HasAtomicPtrOps<std::shared_ptr<T>>::value>
49 class AtomicSharedPtr;
52 class AtomicSharedPtr<T, true> {
54 AtomicSharedPtr() = default;
55 AtomicSharedPtr(std::shared_ptr<T> ptr) : m_ptr(std::move(ptr)) { }
57 AtomicSharedPtr(AtomicSharedPtr const& ptr) : m_ptr(std::atomic_load(&ptr.m_ptr)) { }
58 AtomicSharedPtr(AtomicSharedPtr&& ptr) : m_ptr(std::atomic_exchange(&ptr.m_ptr, {})) { }
60 AtomicSharedPtr& operator=(AtomicSharedPtr const& ptr)
63 std::atomic_store(&m_ptr, std::atomic_load(&ptr.m_ptr));
68 AtomicSharedPtr& operator=(AtomicSharedPtr&& ptr)
70 std::atomic_store(&m_ptr, std::atomic_exchange(&ptr.m_ptr, {}));
74 std::shared_ptr<T> exchange(std::shared_ptr<T> ptr)
76 return std::atomic_exchange(&m_ptr, std::move(ptr));
79 std::shared_ptr<T> load() const noexcept
81 return std::atomic_load(&m_ptr);
85 std::shared_ptr<T> m_ptr = nullptr;
89 class AtomicSharedPtr<T, false> {
91 AtomicSharedPtr() = default;
92 AtomicSharedPtr(std::shared_ptr<T> ptr) : m_ptr(std::move(ptr)) { }
94 AtomicSharedPtr(AtomicSharedPtr const& ptr)
96 std::lock_guard<std::mutex> lock(ptr.m_mutex);
99 AtomicSharedPtr(AtomicSharedPtr&& ptr)
101 std::lock_guard<std::mutex> lock(ptr.m_mutex);
102 m_ptr = std::move(ptr.m_ptr);
105 AtomicSharedPtr& operator=(AtomicSharedPtr const& ptr)
108 // std::lock() ensures that these are locked in a consistent order
110 std::lock(m_mutex, ptr.m_mutex);
113 ptr.m_mutex.unlock();
118 AtomicSharedPtr& operator=(AtomicSharedPtr&& ptr)
120 std::lock(m_mutex, ptr.m_mutex);
121 m_ptr = std::move(ptr.m_ptr);
123 ptr.m_mutex.unlock();
127 std::shared_ptr<T> exchange(std::shared_ptr<T> ptr)
129 std::lock_guard<std::mutex> lock(m_mutex);
134 std::shared_ptr<T> load() const noexcept
136 std::lock_guard<std::mutex> lock(m_mutex);
141 mutable std::mutex m_mutex;
142 std::shared_ptr<T> m_ptr = nullptr;
148 #endif // REALM_ATOMIC_SHARED_PTR_HPP