aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/rwspinlock.h
blob: 1effd5fe0e74b244de5338877ee4b39182923ee9 (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
#pragma once

#include <atomic>

#include <util/system/spinlock.h>

// State can be one of:
// 0) [NOT_USED] State = 0:
//    * any reader can acquire lock (State += 2: -> READING)
//    * one writer can acquire lock (State = -1: -> WRITING)
// 1) [READING] even number:
//    * State/2 = number of active readers
//    * no writers are waiting
//    * any reader can aqcuire lock (State += 2: -> READING)
//    * active readers can release lock (State -= 2)
//    * one writer can acquire lock (State += 1: -> WAITING)
// 2) [WAITING] odd number > 0:
//    * (State-1)/2 = number of active readers
//    * no more readers/writers can acquire lock
//    * active readers can release lock (State -= 2: -> WAITING)
//    * exactly one writer is waiting for active readers to release lock
//    * if no more active readers left (State == 1) waiting writer acquires lock (State = -1: -> WRITING)
// 3) [WRITING] State = -1
//    * exactly one active writer
//    * no active readers
//    * no more readers/writers can acquire lock
//    * writer can release lock (State = 0: -> READING)

struct TRWSpinLock {
    std::atomic_signed_lock_free State;

    void Init() noexcept {
        State.store(0, std::memory_order_relaxed);
    }

    void AcquireRead() noexcept {
        while (true) {
            std::atomic_signed_lock_free::value_type a = State.load(std::memory_order_acquire);
            if ((a & 1) == 0 && State.compare_exchange_strong(a, a + 2, std::memory_order_acquire)) {
                break;
            }
            SpinLockPause();
        }
    }

    void ReleaseRead() noexcept {
        State.fetch_add(-2, std::memory_order_release);
    }

    void AcquireWrite() noexcept {
        while (true) {
            std::atomic_signed_lock_free::value_type a = State.load(std::memory_order_acquire);
            if ((a & 1) == 0 && State.compare_exchange_strong(a, a + 1, std::memory_order_acquire)) {
                break;
            }
            SpinLockPause();
        }

        while (true) {
            std::atomic_signed_lock_free::value_type a = 1;
            if (State.compare_exchange_strong(a, -1, std::memory_order_acquire)) {
                break;
            }
            SpinLockPause();
        }
    }

    void ReleaseWrite() noexcept {
        State.store(0, std::memory_order_release);
    }
};

struct TRWSpinLockReadOps {
    static inline void Acquire(TRWSpinLock* t) noexcept {
        t->AcquireRead();
    }

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

struct TRWSpinLockWriteOps {
    static inline void Acquire(TRWSpinLock* t) noexcept {
        t->AcquireWrite();
    }

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

using TReadSpinLockGuard = TGuard<TRWSpinLock, TRWSpinLockReadOps>;
using TWriteSpinLockGuard = TGuard<TRWSpinLock, TRWSpinLockWriteOps>;