1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #ifndef BOOST_HISTOGRAM_DETAIL_ATOMIC_NUMBER_HPP
- #define BOOST_HISTOGRAM_DETAIL_ATOMIC_NUMBER_HPP
- #include <atomic>
- #include <boost/histogram/detail/priority.hpp>
- #include <type_traits>
- namespace boost {
- namespace histogram {
- namespace detail {
- template <class T>
- struct atomic_number : std::atomic<T> {
- static_assert(std::is_arithmetic<T>(), "");
- using base_t = std::atomic<T>;
- using std::atomic<T>::atomic;
- atomic_number() noexcept = default;
- atomic_number(const atomic_number& o) noexcept : std::atomic<T>{o.load()} {}
- atomic_number& operator=(const atomic_number& o) noexcept {
- this->store(o.load());
- return *this;
- }
-
- atomic_number& operator++() noexcept {
- increment_impl(static_cast<base_t&>(*this), priority<1>{});
- return *this;
- }
-
- atomic_number& operator+=(const T& x) noexcept {
- add_impl(static_cast<base_t&>(*this), x, priority<1>{});
- return *this;
- }
-
- atomic_number& operator*=(const T& x) noexcept {
- this->store(this->load() * x);
- return *this;
- }
-
- atomic_number& operator/=(const T& x) noexcept {
- this->store(this->load() / x);
- return *this;
- }
- private:
-
- template <class U = T>
- auto increment_impl(std::atomic<U>& a, priority<1>) noexcept -> decltype(++a) {
- return ++a;
- }
-
- template <class U = T>
- void increment_impl(std::atomic<U>&, priority<0>) noexcept {
- this->operator+=(static_cast<U>(1));
- }
-
- template <class U = T>
- static auto add_impl(std::atomic<U>& a, const U& x, priority<1>) noexcept
- -> decltype(a += x) {
- return a += x;
- }
-
- template <class U = T>
- static void add_impl(std::atomic<U>& a, const U& x, priority<0>) noexcept {
- U expected = a.load();
-
-
-
- while (!a.compare_exchange_weak(expected, expected + x))
- ;
- }
- };
- }
- }
- }
- #endif
|