2 // detail/atomic_count.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP
12 #define BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_HAS_THREADS)
21 // Nothing to include.
22 #elif defined(BOOST_ASIO_HAS_STD_ATOMIC)
24 #else // defined(BOOST_ASIO_HAS_STD_ATOMIC)
25 # include <boost/detail/atomic_count.hpp>
26 #endif // defined(BOOST_ASIO_HAS_STD_ATOMIC)
32 #if !defined(BOOST_ASIO_HAS_THREADS)
33 typedef long atomic_count;
34 inline void increment(atomic_count& a, long b) { a += b; }
35 inline void decrement(atomic_count& a, long b) { a -= b; }
36 inline void ref_count_up(atomic_count& a) { ++a; }
37 inline bool ref_count_down(atomic_count& a) { return --a == 0; }
38 #elif defined(BOOST_ASIO_HAS_STD_ATOMIC)
39 typedef std::atomic<long> atomic_count;
40 inline void increment(atomic_count& a, long b) { a += b; }
41 inline void decrement(atomic_count& a, long b) { a -= b; }
43 inline void ref_count_up(atomic_count& a)
45 a.fetch_add(1, std::memory_order_relaxed);
48 inline bool ref_count_down(atomic_count& a)
50 if (a.fetch_sub(1, std::memory_order_release) == 1)
52 std::atomic_thread_fence(std::memory_order_acquire);
57 #else // defined(BOOST_ASIO_HAS_STD_ATOMIC)
58 typedef boost::detail::atomic_count atomic_count;
59 inline void increment(atomic_count& a, long b) { while (b > 0) ++a, --b; }
60 inline void decrement(atomic_count& a, long b) { while (b > 0) --a, --b; }
61 inline void ref_count_up(atomic_count& a) { ++a; }
62 inline bool ref_count_down(atomic_count& a) { return --a == 0; }
63 #endif // defined(BOOST_ASIO_HAS_STD_ATOMIC)
69 #endif // BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP