summaryrefslogtreecommitdiff
path: root/Kernel/Locking/SpinLockProtected.h
blob: 143eb98133a52ae1bd6ace283891abf6c78257c4 (plain)
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
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <Kernel/Locking/SpinLockResource.h>

namespace Kernel {

template<typename T>
class SpinLockProtected
    : private T
    , public SpinLockContendedResource {
    AK_MAKE_NONCOPYABLE(SpinLockProtected);
    AK_MAKE_NONMOVABLE(SpinLockProtected);

protected:
    using LockedConst = SpinLockLockedResource<T const>;
    using LockedMutable = SpinLockLockedResource<T>;

    LockedConst lock_const() const { return LockedConst(static_cast<T const*>(this), this->SpinLockContendedResource::m_spinlock); }
    LockedMutable lock_mutable() { return LockedMutable(static_cast<T*>(this), this->SpinLockContendedResource::m_spinlock); }

public:
    using T::T;

    SpinLockProtected() = default;

    template<typename Callback>
    decltype(auto) with(Callback callback) const
    {
        auto lock = lock_const();
        return callback(*lock);
    }

    template<typename Callback>
    decltype(auto) with(Callback callback)
    {
        auto lock = lock_mutable();
        return callback(*lock);
    }

    template<typename Callback>
    void for_each_const(Callback callback) const
    {
        with([&](const auto& value) {
            for (auto& item : value)
                callback(item);
        });
    }

    template<typename Callback>
    void for_each(Callback callback)
    {
        with([&](auto& value) {
            for (auto& item : value)
                callback(item);
        });
    }
};

}