6#ifndef SWIFT_MISC_LOCKFREE_H
7#define SWIFT_MISC_LOCKFREE_H
36 const T &
get()
const {
return *m_ptr; }
39 operator const T &()
const {
return *m_ptr; }
49 friend class LockFree<std::remove_const_t<T>>;
52 std::shared_ptr<const T> m_ptr;
65 T &
get() {
return *m_ptr; }
68 operator T &() {
return *m_ptr; }
81 *m_ptr = std::move(other);
93 : m_old(std::move(other.m_old)), m_now(std::move(other.m_now)), m_ptr(std::move(other.m_ptr))
99 std::tie(m_old, m_now, m_ptr) =
100 std::forward_as_tuple(std::move(other.m_old), std::move(other.m_now), std::move(other.m_ptr));
107 if (m_ptr.use_count() == 0)
112#ifdef __cpp_lib_atomic_shared_ptr
113 bool success = m_now->compare_exchange_strong(m_old, std::shared_ptr<const T>(m_ptr));
115 bool success = std::atomic_compare_exchange_strong(m_now, &m_old, std::shared_ptr<const T>(m_ptr));
118 Q_ASSERT_X(success, Q_FUNC_INFO,
"UniqueWriter detected simultaneous writes");
125#ifdef __cpp_lib_atomic_shared_ptr
130 : m_old(ptr), m_now(now), m_ptr(std::make_shared<T>(*m_old))
132 std::shared_ptr<const T> m_old;
133#ifdef __cpp_lib_atomic_shared_ptr
134 std::atomic<std::shared_ptr<const T>> *m_now;
136 std::shared_ptr<const T> *m_now;
138 std::shared_ptr<T> m_ptr;
146 template <
typename T>
154 LockFree(
const T &other) : m_ptr(std::make_shared<const T>(other)) {}
157 LockFree(T &&other)
noexcept(std::is_nothrow_move_assignable_v<T>)
158 : m_ptr(std::make_shared<const T>(std::move(other)))
170#ifdef __cpp_lib_atomic_shared_ptr
177#ifdef __cpp_lib_atomic_shared_ptr
184 template <
typename F>
187 return std::forward<F>(inspector)(
read().get());
191 template <
typename F>
198#ifdef __cpp_lib_atomic_shared_ptr
199 std::atomic<std::shared_ptr<const T>> m_ptr = std::make_shared<const T>();
201 std::shared_ptr<const T> m_ptr = std::make_shared<const T>();
208 template <
template <
typename>
class T,
typename... Ts>
219 template <
typename F>
222 return call(std::forward<F>(function), std::make_index_sequence<
sizeof...(Ts)>());
226 template <
typename F,
size_t... Is>
227 auto call(F &&function, std::index_sequence<Is...>)
229 return std::forward<F>(function)(std::get<Is>(m_tup).get()...);
232 const std::tuple<T<Ts>...> m_tup;
238 template <
typename... Ts>
241 return { std::forward_as_tuple(vs.
read()...) };
247 template <
typename... Ts>
250 return { std::forward_as_tuple(vs.
uniqueWrite()...) };
257 template <
typename T>
260 return reader->begin();
263 template <
typename T>
266 return reader->end();
269 template <
typename T>
272 return writer->begin();
275 template <
typename T>
278 return writer->end();
288 template <
typename T>
291 template <
typename T>
294 template <
typename T>
297 template <
typename T>
Lock-free wrapper for synchronizing multi-threaded access to an object.
LockFreeUniqueWriter< T > uniqueWrite()
Return an object which can write a new value, as long as there are no other writes.
LockFreeReader< const T > read() const
Return an object which can read the current value.
LockFree()=default
Default constructor. Object will contain a default-constructed T.
LockFree & operator=(LockFree &&)=delete
LockFree cannot be copied or moved.
auto read(F &&inspector)
Pass the current value to the functor inspector, and return whatever inspector returns.
LockFree(LockFree &&)=delete
LockFree cannot be copied or moved.
LockFree(const T &other)
Construct by copying from a T.
LockFree & operator=(const LockFree &)=delete
LockFree cannot be copied or moved.
LockFree(const LockFree &)=delete
LockFree cannot be copied or moved.
LockFree(T &&other) noexcept(std::is_nothrow_move_assignable_v< T >)
Construct by moving from a T.
void uniqueWrite(F &&mutator)
Pass a modifiable reference to the functor mutator. Unsafe if there are multiple writers.
Compose multiple LockFreeReader or LockFreeUniqueWriter instances.
auto operator()(F &&function) &&
Function call operator.
LockFreeMulti(std::tuple< T< Ts > &&... > &&tup)
Construct from a forwarded tuple. Prefer to construct via swift::misc::multiRead or swift::misc::mult...
Return value of LockFree::read().
LockFreeReader & operator=(const LockFreeReader &)=default
Copy assignment operator.
const T & get() const
Return the value that was present when the reader was created.
const T & operator*() const
Return the value that was present when the reader was created.
LockFreeReader(const LockFreeReader &)=default
Copy constructor.
const T * operator->() const
Return the value that was present when the reader was created.
Return value of LockFree::uniqueWrite().
LockFreeUniqueWriter(LockFreeUniqueWriter &&other) noexcept
Move constructor.
LockFreeUniqueWriter & operator=(const LockFreeUniqueWriter &)=delete
LockFreeUniqueWriter cannot be copied.
~LockFreeUniqueWriter()
Destructor. The original object will be overwritten by the new one stored in the writer.
T & operator*()
The value can be modified through the returned reference. The modification is applied in the destruct...
T & get()
The value can be modified through the returned reference. The modification is applied in the destruct...
LockFreeUniqueWriter(const LockFreeUniqueWriter &)=delete
LockFreeUniqueWriter cannot be copied.
LockFreeUniqueWriter & operator=(LockFreeUniqueWriter &&other) noexcept
Move assignment operator.
LockFreeUniqueWriter & operator=(const T &other)
Replace the stored value by copying from a T. The change is applied in the destructor.
T * operator->()
The value can be modified through the returned reference. The modification is applied in the destruct...
LockFreeUniqueWriter & operator=(T &&other) noexcept(std::is_nothrow_move_assignable_v< T >)
Replace the stored value by moving from a T. The change is applied in the destructor.
Free functions in swift::misc.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
LockFreeMulti< LockFreeUniqueWriter, Ts... > multiUniqueWrite(LockFree< Ts > &...vs)
Return a callable object for writing to multiple LockFree instances simultaneously.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
LockFreeMulti< LockFreeReader, const Ts... > multiRead(const LockFree< Ts > &...vs)
Return a callable object for reading from multiple LockFree instances simultaneously.