summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/rw_spin_lock.cpp
blob: efc0e90167a5d2964e3e9506cbefe63c0ea9571a (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
#include "rw_spin_lock.h"
#include "util/system/backtrace.h"

#include <library/cpp/yt/assert/assert.h>

#include <library/cpp/yt/misc/tls.h>

#include <util/generic/hash_set.h>

#include <thread>

namespace NYT::NThreading::NDetail {

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

void TUncheckedReaderWriterSpinLock::AcquireReaderSlow() noexcept
{
    TSpinWait spinWait(Location_, ESpinLockActivityKind::Read);
    while (!TryAndTryAcquireReader()) {
        spinWait.Wait();
    }
}

void TUncheckedReaderWriterSpinLock::AcquireReaderForkFriendlySlow() noexcept
{
    TSpinWait spinWait(Location_, ESpinLockActivityKind::Read);
    while (!TryAcquireReaderForkFriendly()) {
        spinWait.Wait();
    }
}

void TUncheckedReaderWriterSpinLock::AcquireWriterSlow() noexcept
{
    TSpinWait spinWait(Location_, ESpinLockActivityKind::Write);
    while (!TryAndTryAcquireWriter()) {
        spinWait.Wait();
    }
}

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

namespace {

// NB: Reading this after destruction is UB, but we can't deal with Static Initialization Order Fiasco in any other feasible way :(
YT_DEFINE_THREAD_LOCAL(bool, ThreadLockTrackerDestroyed, false);

class TThreadLockTracker
{
public:
    void RecordThreadLockAcquisition(TCheckedReaderWriterSpinLock* lock) noexcept
    {
        auto [_, inserted] = ThreadLocksAcquired_.insert(lock);
        YT_VERIFY(inserted, "Non-reentrant RWSpinLock detected two acquisitions in one thread");
    }

    void RecordThreadLockRelease(TCheckedReaderWriterSpinLock* lock) noexcept
    {
        YT_VERIFY(ThreadLocksAcquired_.erase(lock) == 1, "Released RWSpinLock that has never been acquired");
    }

    ~TThreadLockTracker()
    {
        ThreadLockTrackerDestroyed() = true;
    };

private:
    THashSet<TCheckedReaderWriterSpinLock*> ThreadLocksAcquired_;
};

YT_DEFINE_THREAD_LOCAL(TThreadLockTracker, ThreadLockTracker);

} // namespace

void TCheckedReaderWriterSpinLock::RecordThreadAcquisition(bool acquired)
{
    if (acquired) {
        // We might be called from destructor of another static variable after the destruction of actual TThreadLockTracker.
        if (ThreadLockTrackerDestroyed()) {
            return;
        }
        ThreadLockTracker().RecordThreadLockAcquisition(this);
    }
}

void TCheckedReaderWriterSpinLock::RecordThreadRelease()
{
    // We might be called from destructor of another static variable after the destruction of actual TThreadLockTracker.
    if (ThreadLockTrackerDestroyed()) {
        return;
    }
    ThreadLockTracker().RecordThreadLockRelease(this);
}

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

} // namespace NYT::NThreading::NDetail