1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <Kernel/Locking/ContendedResource.h>
#include <Kernel/Locking/LockLocation.h>
#include <Kernel/Locking/Mutex.h>
namespace Kernel {
template<typename T>
class RefCountedContended : public ContendedResource
, public AK::RefCountedBase {
AK_MAKE_NONCOPYABLE(RefCountedContended);
AK_MAKE_NONMOVABLE(RefCountedContended);
protected:
using LockedShared = LockedResource<T const, LockMode::Shared>;
using LockedExclusive = LockedResource<T, LockMode::Exclusive>;
LockedShared lock_shared(LockLocation const& location) const { return LockedShared(static_cast<T const*>(this), this->ContendedResource::m_mutex, location); }
LockedExclusive lock_exclusive(LockLocation const& location) { return LockedExclusive(static_cast<T*>(this), this->ContendedResource::m_mutex, location); }
public:
RefCountedContended() = default;
bool unref() const
{
auto new_ref_count = deref_base();
if (new_ref_count == 0) {
AK::call_will_be_destroyed_if_present(static_cast<T*>(const_cast<RefCountedContended*>(this)->lock_exclusive().get()));
delete static_cast<const T*>(this);
return true;
} else if (new_ref_count == 1) {
AK::call_one_ref_left_if_present(static_cast<T*>(const_cast<RefCountedContended*>(this)->lock_exclusive().get()));
}
return false;
}
template<typename Callback>
decltype(auto) with_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{
auto lock = lock_shared(location);
return callback(*lock);
}
template<typename Callback>
decltype(auto) with_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{
auto lock = lock_exclusive(location);
return callback(*lock);
}
template<typename Callback>
void for_each_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{
with_shared([&](const auto& value) {
for (auto& item : value)
callback(item);
},
location);
}
template<typename Callback>
void for_each_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{
with_exclusive([&](auto& value) {
for (auto& item : value)
callback(item);
},
location);
}
};
}
|