/src/immer/immer/refcount/refcount_policy.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // immer: immutable data structures for C++ |
3 | | // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente |
4 | | // |
5 | | // This software is distributed under the Boost Software License, Version 1.0. |
6 | | // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt |
7 | | // |
8 | | |
9 | | #pragma once |
10 | | |
11 | | #include <immer/refcount/no_refcount_policy.hpp> |
12 | | |
13 | | #include <atomic> |
14 | | #include <cassert> |
15 | | #include <utility> |
16 | | |
17 | | namespace immer { |
18 | | |
19 | | /*! |
20 | | * A reference counting policy implemented using an *atomic* `int` |
21 | | * count. It is **thread-safe**. |
22 | | */ |
23 | | struct refcount_policy |
24 | | { |
25 | | mutable std::atomic<int> refcount; |
26 | | |
27 | | refcount_policy() |
28 | 251k | : refcount{1} {}; |
29 | | refcount_policy(disowned) |
30 | | : refcount{0} |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | 1.40M | void inc() { refcount.fetch_add(1, std::memory_order_relaxed); } |
35 | | |
36 | 1.65M | bool dec() { return 1 == refcount.fetch_sub(1, std::memory_order_acq_rel); } |
37 | | |
38 | 0 | bool unique() { return refcount == 1; } |
39 | | }; |
40 | | |
41 | | } // namespace immer |