source: mainline/uspace/lib/cpp/src/refcount_obj.cpp@ 8624d1f

Last change on this file since 8624d1f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 1.3 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2019 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <__bits/refcount_obj.hpp>
8
9namespace std::aux
10{
11 void refcount_obj::increment()
12 {
13 __atomic_add_fetch(&refcount_, 1, __ATOMIC_ACQ_REL);
14 }
15
16 void refcount_obj::increment_weak()
17 {
18 __atomic_add_fetch(&weak_refcount_, 1, __ATOMIC_ACQ_REL);
19 }
20
21 bool refcount_obj::decrement()
22 {
23 if (__atomic_sub_fetch(&refcount_, 1, __ATOMIC_ACQ_REL) == 0)
24 {
25 /**
26 * First call to destroy() will delete the held object,
27 * so it doesn't matter what the weak_refcount_ is,
28 * but we added one and we need to remove it now.
29 */
30 decrement_weak();
31
32 return true;
33 }
34 else
35 return false;
36 }
37
38 bool refcount_obj::decrement_weak()
39 {
40 return __atomic_sub_fetch(&weak_refcount_, 1, __ATOMIC_ACQ_REL) == 0 && refs() == 0;
41 }
42
43 refcount_t refcount_obj::refs() const
44 {
45 return __atomic_load_n(&refcount_, __ATOMIC_RELAXED);
46 }
47
48 refcount_t refcount_obj::weak_refs() const
49 {
50 return __atomic_load_n(&weak_refcount_, __ATOMIC_RELAXED);
51 }
52
53 bool refcount_obj::expired() const
54 {
55 return refs() == 0;
56 }
57}
Note: See TracBrowser for help on using the repository browser.