aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/rwlock.h
blob: 0bb9b3fe1c814081a884c554d9d91e97af3bbcb3 (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
#pragma once

#include "guard.h"
#include "defaults.h"

#include <util/generic/ptr.h>

class TRWMutex {
public:
    TRWMutex();
    ~TRWMutex();

    void AcquireRead() noexcept;
    bool TryAcquireRead() noexcept;
    void ReleaseRead() noexcept;

    void AcquireWrite() noexcept;
    bool TryAcquireWrite() noexcept;
    void ReleaseWrite() noexcept;

    void Release() noexcept;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};

template <class T>
struct TReadGuardOps {
    static inline void Acquire(T* t) noexcept {
        t->AcquireRead();
    }

    static inline void Release(T* t) noexcept {
        t->ReleaseRead();
    }
};

template <class T>
struct TTryReadGuardOps: public TReadGuardOps<T> {
    static inline bool TryAcquire(T* t) noexcept {
        return t->TryAcquireRead();
    }
};

template <class T>
struct TWriteGuardOps {
    static inline void Acquire(T* t) noexcept {
        t->AcquireWrite();
    }

    static inline void Release(T* t) noexcept {
        t->ReleaseWrite();
    }
};

template <class T>
struct TTryWriteGuardOps: public TWriteGuardOps<T> {
    static inline bool TryAcquire(T* t) noexcept {
        return t->TryAcquireWrite();
    }
};

template <class T>
using TReadGuardBase = TGuard<T, TReadGuardOps<T>>;
template <class T>
using TTryReadGuardBase = TTryGuard<T, TTryReadGuardOps<T>>;

template <class T>
using TWriteGuardBase = TGuard<T, TWriteGuardOps<T>>;
template <class T>
using TTryWriteGuardBase = TTryGuard<T, TTryWriteGuardOps<T>>;

using TReadGuard = TReadGuardBase<TRWMutex>;
using TTryReadGuard = TTryReadGuardBase<TRWMutex>;

using TWriteGuard = TWriteGuardBase<TRWMutex>;
using TTryWriteGuard = TTryWriteGuardBase<TRWMutex>;