1 //  Copyright 2013 Peter Dimov
2 //  Copyright (c) 2016 Agustin Berge
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
5 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef HPX_UTIL_ATOMIC_COUNT_HPP
8 #define HPX_UTIL_ATOMIC_COUNT_HPP
9 
10 #include <hpx/config.hpp>
11 
12 #include <atomic>
13 
14 namespace hpx { namespace util
15 {
16     class atomic_count
17     {
18     public:
19         HPX_NON_COPYABLE(atomic_count);
20 
21     public:
atomic_count(long value)22         explicit atomic_count(long value)
23           : value_(value)
24         {}
25 
operator =(long value)26         atomic_count& operator=(long value)
27         {
28             value_.store(value, std::memory_order_relaxed);
29             return *this;
30         }
31 
operator ++()32         long operator++()
33         {
34             return value_.fetch_add(1, std::memory_order_acq_rel) + 1;
35         }
36 
operator --()37         long operator--()
38         {
39             return value_.fetch_sub(1, std::memory_order_acq_rel) - 1;
40         }
41 
operator +=(long n)42         atomic_count& operator+=(long n)
43         {
44             value_.fetch_add(n, std::memory_order_acq_rel);
45             return *this;
46         }
47 
operator -=(long n)48         atomic_count& operator-=(long n)
49         {
50             value_.fetch_sub(n, std::memory_order_acq_rel);
51             return *this;
52         }
53 
operator long() const54         operator long() const
55         {
56             return value_.load(std::memory_order_acquire);
57         }
58 
59     private:
60         std::atomic<long> value_;
61     };
62 }}
63 
64 #endif /*HPX_UTIL_ATOMIC_COUNT_HPP*/
65