aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/light_rw_lock/bench/lightrwlock_test.cpp
blob: 4faa53fc2a4ba9e7769e3f26eaa9b973ba25dd35 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <library/cpp/threading/light_rw_lock/lightrwlock.h>
#include <util/random/random.h>

#ifdef _linux_
// Light rw lock is implemented only for linux

using namespace NS_LightRWLock;

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

#define LIGHT

#ifdef RWSPINLOCK
#include <library/cpp/lwtrace/rwspinlock.h>
#endif

#define CHECK_LOGIC 1
#define LOOPCOUNT 1000000
#define RANRCOUNT 100
#define THREADCOUNT 40
#define WRITELOCKS 100

#if defined(_MSC_VER)
static int Y_FORCE_INLINE AtomicFetchAdd(volatile int& item, int value) {
    return _InterlockedExchangeAdd((&item, value);
}
#elif defined(__GNUC__)
#else
#error unsupported platform 
#endif

class TPosixRWLock {
public:
    TPosixRWLock() {
    }

    ~TPosixRWLock() {
        pthread_rwlock_destroy(&rwlock);
    }

    TPosixRWLock(const TPosixRWLock&) = delete;
    void operator=(const TPosixRWLock&) = delete;

private:
    pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
    friend class TPosixRWShareLocker;
    friend class TPosixRWExclusiveLocker;
};

#if defined(LIGHT)
TLightRWLock __attribute__((aligned(64))) rwlock;
#elif defined(POSIX)
TPosixRWLock rwlock;
#elif defined(RWSPINLOCK)
TRWSpinLock __attribute__((aligned(64))) rwlock;
#else
#error "define lock type"
#endif

volatile __attribute__((aligned(64))) int checkIt = 0;
volatile int checkExcl = 0;

class TPosixRWShareLocker {
public:
    TPosixRWShareLocker(TPosixRWLock& lock)
        : LockP_(&lock)
    {
        pthread_rwlock_rdlock(&LockP_->rwlock);
    }

    ~TPosixRWShareLocker() {
        pthread_rwlock_unlock(&LockP_->rwlock);
    }

    TPosixRWShareLocker(const TPosixRWShareLocker&) = delete;
    void operator=(const TPosixRWShareLocker&) = delete;

private:
    TPosixRWLock* LockP_;
};

class TPosixRWExclusiveLocker {
public:
    TPosixRWExclusiveLocker(TPosixRWLock& lock)
        : LockP_(&lock)
    {
        pthread_rwlock_wrlock(&LockP_->rwlock);
    }

    ~TPosixRWExclusiveLocker() {
        pthread_rwlock_unlock(&LockP_->rwlock);
    }
    TPosixRWExclusiveLocker(const TPosixRWExclusiveLocker&) = delete;
    void operator=(const TPosixRWExclusiveLocker&) = delete;

private:
    TPosixRWLock* LockP_;
};

template <typename TLocker, bool excl>
static Y_FORCE_INLINE void Run() {
    TLocker lockIt(rwlock);

#if defined(CHECK_LOGIC) && CHECK_LOGIC
    if (!excl && checkExcl == 1) {
        printf("there is a bug\n");
    }

    int result = AtomicFetchAdd(checkIt, 1);
    if (excl)
        checkExcl = 1;

    if (excl && result > 1)
        printf("there is a bug\n");
#endif

    for (unsigned w = 0; w < RANRCOUNT; ++w)
        RandomNumber<ui32>();

#if defined(CHECK_LOGIC) && CHECK_LOGIC
    if (excl)
        checkExcl = 0;

    AtomicFetchAdd(checkIt, -1);
#endif
}

#ifdef LIGHT
static void* fast_thread_start(__attribute__((unused)) void* arg) {
    for (unsigned q = 0; q < LOOPCOUNT; ++q) {
        char excl = (RandomNumber<ui32>() % WRITELOCKS) == 0;
        if (excl)
            Run<TLightWriteGuard, 1>();
        else
            Run<TLightReadGuard, 0>();
    }
    return NULL;
}
#endif

#ifdef POSIX
static void* fast_thread_start(__attribute__((unused)) void* arg) {
    for (unsigned q = 0; q < LOOPCOUNT; ++q) {
        char excl = (RandomNumber<ui32>() % WRITELOCKS) == 0;
        if (excl)
            Run<TPosixRWExclusiveLocker, 1>();
        else
            Run<TPosixRWShareLocker, 0>();
    }
    return NULL;
}
#endif

#ifdef RWSPINLOCK
static void* fast_thread_start(__attribute__((unused)) void* arg) {
    for (unsigned q = 0; q < LOOPCOUNT; ++q) {
        char excl = (RandomNumber<ui32>() % WRITELOCKS) == 0;
        if (excl)
            Run<TWriteSpinLockGuard, 1>();
        else
            Run<TReadSpinLockGuard, 0>();
    }
    return NULL;
}
#endif

int main() {
    pthread_t threads[THREADCOUNT];

    for (unsigned q = 0; q < THREADCOUNT; ++q) {
        pthread_create(&(threads[q]), NULL, &fast_thread_start, NULL);
    }

    for (unsigned q = 0; q < THREADCOUNT; ++q)
        pthread_join(threads[q], NULL);

    return 0;
}

#else // !_linux_

int main() {
    return 0;
}

#endif