From febfafcf7f0e4d1cbe878e7dfffd9c9b78036aed Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Dec 2017 17:34:00 +0200 Subject: Add support for first-access value typification during non-load phases --- build2/types.hxx | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'build2/types.hxx') diff --git a/build2/types.hxx b/build2/types.hxx index cf95a9d..2f07029 100644 --- a/build2/types.hxx +++ b/build2/types.hxx @@ -98,6 +98,41 @@ namespace build2 using atomic_count = atomic; // Matches scheduler::atomic_count. + // Like std::atomic except implicit conversion and assignment use relaxed + // memory ordering. + // + template + struct relaxed_atomic: atomic + { + using atomic::atomic; // Delegate. + relaxed_atomic (const relaxed_atomic& a) noexcept + : atomic (a.load (memory_order_relaxed)) {} + + operator T () const noexcept {return this->load (memory_order_relaxed);} + + T operator= (T v) noexcept { + this->store (v, memory_order_relaxed); return v;} + T operator= (const relaxed_atomic& a) noexcept { + return *this = a.load (memory_order_relaxed);} + }; + + template + struct relaxed_atomic: atomic + { + using atomic::atomic; // Delegate. + relaxed_atomic (const relaxed_atomic& a) noexcept + : atomic (a.load (memory_order_relaxed)) {} + + operator T* () const noexcept {return this->load (memory_order_relaxed);} + T& operator* () const noexcept {return *this->load (memory_order_relaxed);} + T* operator-> () const noexcept {return this->load (memory_order_relaxed);} + + T* operator= (T* v) noexcept { + this->store (v, memory_order_relaxed); return v;} + T* operator= (const relaxed_atomic& a) noexcept { + return *this = a.load (memory_order_relaxed);} + }; + using std::mutex; using mlock = std::unique_lock; -- cgit v1.1