aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/fork_aware_rw_spin_lock.h
blob: 8e7f9e2056287b3e67efa9c1a53e0bb884ef8ca3 (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
#pragma once

#include "rw_spin_lock.h"

namespace NYT::NThreading {

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

//! Wraps TReaderWriterSpinLock and additionally acquires a global fork lock (in read mode)
//! preventing concurrent forks from happening.
class TForkAwareReaderWriterSpinLock
{
public:
    TForkAwareReaderWriterSpinLock() = default;
    TForkAwareReaderWriterSpinLock(const TForkAwareReaderWriterSpinLock&) = delete;
    TForkAwareReaderWriterSpinLock& operator =(const TForkAwareReaderWriterSpinLock&) = delete;

    void AcquireReader() noexcept;
    void ReleaseReader() noexcept;

    void AcquireWriter() noexcept;
    void ReleaseWriter() noexcept;

    bool IsLocked() const noexcept;
    bool IsLockedByReader() const noexcept;
    bool IsLockedByWriter() const noexcept;

private:
    TReaderWriterSpinLock SpinLock_;
};

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

} // namespace NYT::NThreading