aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/fork_aware_spin_lock.cpp
blob: f43411b6d8c2f9aba56ef7f22bea1bb97793af99 (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
#include "fork_aware_spin_lock.h"

#include "at_fork.h"

namespace NYT::NThreading {

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

void TForkAwareSpinLock::Acquire() noexcept
{
    GetForkLock()->AcquireReaderForkFriendly();
    SpinLock_.Acquire();
}

bool TForkAwareSpinLock::TryAcquire() noexcept
{
    if (!GetForkLock()->TryAcquireReaderForkFriendly()) {
        return false;
    }
    if (!SpinLock_.TryAcquire()) {
        GetForkLock()->ReleaseReader();
        return false;
    }
    return true;
}

void TForkAwareSpinLock::Release() noexcept
{
    SpinLock_.Release();
    GetForkLock()->ReleaseReader();
}

bool TForkAwareSpinLock::IsLocked() const noexcept
{
    return SpinLock_.IsLocked();
}

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

} // namespace NYT::NThreading