summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/sync/sync_test.cpp
blob: 0925ce060a094a2273448afeee39c506fcd15505 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/sync.h>

namespace vespalib {
class TryLock
{
private:
    friend class LockGuard;
    friend class MonitorGuard;

    std::unique_lock<std::mutex> _guard;
    std::condition_variable     *_cond;

public:
    TryLock(const Lock &lock)
        : _guard(*lock._mutex, std::try_to_lock), _cond(nullptr)
    {}
    TryLock(const Monitor &mon)
        : _guard(*mon._mutex, std::try_to_lock),
          _cond(_guard ? mon._cond.get() : nullptr)
    {}
    ~TryLock() = default;

    TryLock(const TryLock &) = delete;
    TryLock &operator=(const TryLock &) = delete;

    /**
     * @brief Check whether this object holds a lock
     *
     * @return true if this object holds a lock
     **/
    bool hasLock() const { return static_cast<bool>(_guard); }
    void unlock() {
        if (_guard) {
            _guard.unlock();
            _cond = nullptr;
        }
    }
};

}
using namespace vespalib;

#define CHECK_LOCKED(m) { TryLock tl(m); EXPECT_TRUE(!tl.hasLock()); }
#define CHECK_UNLOCKED(m) { TryLock tl(m); EXPECT_TRUE(tl.hasLock()); }

class Test : public TestApp
{
private:
    Lock    _lock;
    Monitor _monitor;

    LockGuard    lockLock()      { return LockGuard(_lock); }
    LockGuard    lockMonitor()   { return LockGuard(_monitor); }
    MonitorGuard obtainMonitor() { return MonitorGuard(_monitor); }
public:
    ~Test() override;
    void testCountDownLatch();
    int Main() override;
};

Test::~Test() {}
void
Test::testCountDownLatch() {
    {
        CountDownLatch latch(5);
        EXPECT_EQUAL(latch.getCount(), 5u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 4u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 3u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 2u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 1u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 0u);
        latch.countDown();
        EXPECT_EQUAL(latch.getCount(), 0u);
        latch.await(); // should not block
        latch.await(); // should not block
    }
    {
        Gate gate;
        EXPECT_EQUAL(gate.getCount(), 1u);
        gate.countDown();
        EXPECT_EQUAL(gate.getCount(), 0u);
        gate.countDown();
        EXPECT_EQUAL(gate.getCount(), 0u);
        gate.await(); // should not block
        gate.await(); // should not block
    }
    {
        Gate gate;
        EXPECT_EQUAL(gate.getCount(), 1u);
        EXPECT_EQUAL(gate.await(0), false);
        EXPECT_EQUAL(gate.await(10), false);
        gate.countDown();
        EXPECT_EQUAL(gate.getCount(), 0u);
        EXPECT_EQUAL(gate.await(0), true);
        EXPECT_EQUAL(gate.await(10), true);
    }
}

int
Test::Main()
{
    TEST_INIT("sync_test");
    {
        Lock lock;
        {
            CHECK_UNLOCKED(lock);
            LockGuard guard(lock);
            CHECK_LOCKED(lock);
        }
        CHECK_UNLOCKED(lock);
        {
            LockGuard guard(lock);
            CHECK_LOCKED(lock);
            guard.unlock();
            CHECK_UNLOCKED(lock);
        }
    }
    // you can use a LockGuard to lock a Monitor
    {
        Monitor monitor;
        {
            CHECK_UNLOCKED(monitor);
            LockGuard guard(monitor);
            CHECK_LOCKED(monitor);
        }
        CHECK_UNLOCKED(monitor);
        {
            LockGuard guard(monitor);
            CHECK_LOCKED(monitor);
            guard.unlock();
            CHECK_UNLOCKED(monitor);
        }
    }
    {
        Monitor monitor;
        {
            CHECK_UNLOCKED(monitor);
            MonitorGuard guard(monitor);
            guard.signal();
            guard.broadcast();
            guard.wait(10);
            CHECK_LOCKED(monitor);
        }
        CHECK_UNLOCKED(monitor);
        {
            MonitorGuard guard(monitor);
            CHECK_LOCKED(monitor);
            guard.unlock();
            CHECK_UNLOCKED(monitor);
        }
    }

    // you can lock const objects
    {
        const Lock lock;
        CHECK_UNLOCKED(lock);
        LockGuard guard(lock);
        CHECK_LOCKED(lock);
    }
    {
        const Monitor lock;
        CHECK_UNLOCKED(lock);
        LockGuard guard(lock);
        CHECK_LOCKED(lock);
    }
    {
        const Monitor monitor;
        CHECK_UNLOCKED(monitor);
        MonitorGuard guard(monitor);
        CHECK_LOCKED(monitor);
    }
    // LockGuard/MonitorGuard have destructive move
    {
        Lock lock;
        CHECK_UNLOCKED(lock);
        LockGuard a(lock);
        CHECK_LOCKED(lock);
        {
            CHECK_LOCKED(lock);
            LockGuard b(std::move(a));
            CHECK_LOCKED(lock);
        }
        CHECK_UNLOCKED(lock);
    }
    {
        Monitor mon;
        CHECK_UNLOCKED(mon);
        MonitorGuard a(mon);
        CHECK_LOCKED(mon);
        {
            CHECK_LOCKED(mon);
            MonitorGuard b(std::move(a));
            CHECK_LOCKED(mon);
        }
        CHECK_UNLOCKED(mon);
    }
    // Destructive copy also works for return value handover
    {
        CHECK_UNLOCKED(_lock);
        CHECK_UNLOCKED(_monitor);
        {
            CHECK_UNLOCKED(_lock);
            CHECK_UNLOCKED(_monitor);
            LockGuard a(lockLock());
            CHECK_LOCKED(_lock);
            CHECK_UNLOCKED(_monitor);
            LockGuard b = lockMonitor(); // copy, not assign
            CHECK_LOCKED(_lock);
            CHECK_LOCKED(_monitor);
        }
        CHECK_UNLOCKED(_lock);
        CHECK_UNLOCKED(_monitor);
    }
    {
        CHECK_UNLOCKED(_monitor);
        {
            CHECK_UNLOCKED(_monitor);
            MonitorGuard guard(obtainMonitor());
            CHECK_LOCKED(_monitor);
        }
        CHECK_UNLOCKED(_monitor);
    }
    // Test that guards can be matched to locks/monitors
    {
        Lock lock1;
        Lock lock2;
        LockGuard lockGuard1(lock1);
        LockGuard lockGuard2(lock2);
        EXPECT_TRUE(lockGuard1.locks(lock1));
        EXPECT_FALSE(lockGuard1.locks(lock2));
        EXPECT_TRUE(lockGuard2.locks(lock2));
        EXPECT_FALSE(lockGuard2.locks(lock1));
        lockGuard1.unlock();
        EXPECT_FALSE(lockGuard1.locks(lock1));
    }
    {
        Monitor lock1;
        Monitor lock2;
        MonitorGuard lockGuard1(lock1);
        MonitorGuard lockGuard2(lock2);
        EXPECT_TRUE(lockGuard1.monitors(lock1));
        EXPECT_FALSE(lockGuard1.monitors(lock2));
        EXPECT_TRUE(lockGuard2.monitors(lock2));
        EXPECT_FALSE(lockGuard2.monitors(lock1));
        lockGuard1.unlock();
        EXPECT_FALSE(lockGuard1.monitors(lock1));
    }
    testCountDownLatch();
    TEST_DONE();
}

TEST_APPHOOK(Test)