aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/traceless_guard-inl.h
blob: e03d5196c43a3caffa01e1ca0e2ee548e1b73699 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once

#ifndef TRACELESS_GUARD_INL_H_
#error "Direct inclusion of this file is not allowed, include traceless_guard.h"
#endif
#undef TRACELESS_GUARD_INL_H_

#include "rw_spin_lock.h"
#include "spin_lock_count.h"

#include <util/system/guard.h>

#include <concepts>

namespace NYT::NThreading::NPrivate {

////////////////////////////////////////////////////////////////////////////////

#ifdef NDEBUG

template <class T, class TOps = TCommonLockOps<T>>
using TTracelessGuard = TGuard<T, TOps>;

template <class T, class TOps = TCommonLockOps<T>>
using TTracelessInverseGuard = TInverseGuard<T, TOps>;

template <class T, class TOps = TTryLockOps<T>>
using TTracelessTryGuard = TTryGuard<T, TOps>;

template <class T>
using TTracelessReaderGuard = TReaderGuard<T>;

template <class T>
using TTracelessWriterGuard = TWriterGuard<T>;

#else

template <class TOps>
struct TTracelessOps
    : public TOps
{
    template <class T>
    static void Acquire(T* t) noexcept
    {
        TOps::Acquire(t);
        NDetail::RecordSpinLockReleased();
    }

    template <class T>
    static void Release(T* t) noexcept
    {
        NDetail::RecordSpinLockAcquired();
        TOps::Release(t);
    }

    template <class T>
        requires requires (T* t) { { TOps::TryAcquire(t) } -> std::same_as<bool>; }
    static bool TryAcquire(T* t) noexcept
    {
        bool isAcquired = TOps::TryAcquire(t);

        if (isAcquired) {
            NDetail::RecordSpinLockReleased();
        }

        return isAcquired;
    }
};

template <CTrackedSpinLock T, class TOps = TCommonLockOps<T>>
using TTracelessGuard = TGuard<T, TTracelessOps<TOps>>;

template <CTrackedSpinLock T, class TOps = TCommonLockOps<T>>
using TTracelessInverseGuard = TInverseGuard<T, TTracelessOps<TOps>>;

template <CTrackedSpinLock T, class TOps = TTryLockOps<T>>
using TTracelessTryGuard = TTryGuard<T, TTracelessOps<TOps>>;

template <CTrackedSpinLock T>
using TTracelessReaderGuard = TGuard<T, TTracelessOps<TReaderSpinlockTraits<T>>>;

template <CTrackedSpinLock T>
using TTracelessWriterGuard = TGuard<T, TTracelessOps<TWriterSpinlockTraits<T>>>;

#endif

////////////////////////////////////////////////////////////////////////////////

template <CTrackedSpinLock T>
TTracelessGuard<T> TracelessGuard(const T& mutex)
{
    return {&mutex};
}

template <CTrackedSpinLock T>
TTracelessTryGuard<T> TracelessTryGuard(const T& mutex)
{
    return {&mutex};
}

template <CTrackedSpinLock T>
TTracelessReaderGuard<T> TracelessReaderGuard(const T& mutex)
{
    return {&mutex};
}

template <CTrackedSpinLock T>
TTracelessWriterGuard<T> TracelessWriterGuard(const T& mutex)
{
    return {&mutex};
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NThreading::NPrivate