]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/atomic_count.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
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) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP | |
12 | #define BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if !defined(BOOST_ASIO_HAS_THREADS) | |
21 | // Nothing to include. | |
22 | #elif defined(BOOST_ASIO_HAS_STD_ATOMIC) | |
23 | # include <atomic> | |
24 | #else // defined(BOOST_ASIO_HAS_STD_ATOMIC) | |
25 | # include <boost/detail/atomic_count.hpp> | |
26 | #endif // defined(BOOST_ASIO_HAS_STD_ATOMIC) | |
27 | ||
28 | namespace boost { | |
29 | namespace asio { | |
30 | namespace detail { | |
31 | ||
32 | #if !defined(BOOST_ASIO_HAS_THREADS) | |
33 | typedef long atomic_count; | |
34 | inline void increment(atomic_count& a, long b) { a += b; } | |
20effc67 TL |
35 | inline void ref_count_up(atomic_count& a) { ++a; } |
36 | inline bool ref_count_down(atomic_count& a) { return --a == 0; } | |
7c673cae FG |
37 | #elif defined(BOOST_ASIO_HAS_STD_ATOMIC) |
38 | typedef std::atomic<long> atomic_count; | |
39 | inline void increment(atomic_count& a, long b) { a += b; } | |
20effc67 TL |
40 | |
41 | inline void ref_count_up(atomic_count& a) | |
42 | { | |
43 | a.fetch_add(1, std::memory_order_relaxed); | |
44 | } | |
45 | ||
46 | inline bool ref_count_down(atomic_count& a) | |
47 | { | |
48 | if (a.fetch_sub(1, std::memory_order_release) == 1) | |
49 | { | |
50 | std::atomic_thread_fence(std::memory_order_acquire); | |
51 | return true; | |
52 | } | |
53 | return false; | |
54 | } | |
7c673cae FG |
55 | #else // defined(BOOST_ASIO_HAS_STD_ATOMIC) |
56 | typedef boost::detail::atomic_count atomic_count; | |
57 | inline void increment(atomic_count& a, long b) { while (b > 0) ++a, --b; } | |
20effc67 TL |
58 | inline void ref_count_up(atomic_count& a) { ++a; } |
59 | inline bool ref_count_down(atomic_count& a) { return --a == 0; } | |
7c673cae FG |
60 | #endif // defined(BOOST_ASIO_HAS_STD_ATOMIC) |
61 | ||
62 | } // namespace detail | |
63 | } // namespace asio | |
64 | } // namespace boost | |
65 | ||
66 | #endif // BOOST_ASIO_DETAIL_ATOMIC_COUNT_HPP |