File: shared_mutex_tests.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (238 lines) | stat: -rw-r--r-- 5,598 bytes parent folder | download | duplicates (2)
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
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <future>
#include <mutex>
#include <thread>

#include <gtest/gtest.h>

#include <mega/common/lock.h>
#include <mega/common/shared_mutex.h>

namespace mega
{
namespace testing
{

// Convenience.
using Clock = std::chrono::steady_clock;
using Milliseconds = std::chrono::milliseconds;
using TimePoint = Clock::time_point;

class SharedMutexTests: public ::testing::Test
{
    // Signaled when a function has completed execution.
    std::condition_variable mCV;

    // Serializes access to instance members.
    std::mutex mLock;

    // How many functions are currently being executed?
    std::uint64_t mNumFunctions;

public:
    SharedMutexTests():
        Test(),
        mCV(),
        mLock(),
        mNumFunctions(0)
    {
    }

    ~SharedMutexTests()
    {
        std::unique_lock<std::mutex> lock(mLock);

        // Wait for queued functions to complete.
        mCV.wait(lock, [&]() { return !mNumFunctions; });
    }

    // Queue a function for execution on another thread.
    template<typename Ret>
    std::future<Ret> execute(std::function<Ret()> function)
    {
        std::lock_guard<std::mutex> guard(mLock);

        // Wrap the caller's function.
        auto wrapper = [this](std::function<Ret()>& function) {
            // Execute the caller's function.
            auto result = function();

            std::lock_guard<std::mutex> guard(mLock);

            // Notify the fixture that the function's completed.
            --mNumFunctions;

            // Wake the fixture if necessary.
            mCV.notify_one();

            // Return result to caller.
            return result;
        }; // wrapper

        function = std::bind(std::move(wrapper), std::move(function));

        // Package the task for execution.
        auto task = std::packaged_task<Ret()>(std::move(function));

        // Retrieve future so the caller can wait for the function's result.
        auto future = task.get_future();

        // Remember that we've queued a function for execution.
        ++mNumFunctions;

        // Spawn a thread to execute the task.
        std::thread thread(std::move(task));

        // Detach the thread so we can return immediately.
        thread.detach();

        // Return the future to the caller.
        return future;
    }
}; // SharedMutexTests

using namespace common;

TEST_F(SharedMutexTests, lock_fails)
{
    SharedMutex mutex;

    {
        UniqueLock<SharedMutex> lock0(mutex, std::try_to_lock);
        ASSERT_TRUE(lock0);

        auto result = execute(std::function<bool()>([&]() {
            return !UniqueLock<SharedMutex>(mutex, std::try_to_lock);
        }));

        ASSERT_TRUE(result.get());
    }

    SharedLock<SharedMutex> lock0(mutex, std::try_to_lock);
    ASSERT_TRUE(lock0);

    auto result = execute(std::function<bool()>([&]() {
        return !UniqueLock<SharedMutex>(mutex, std::try_to_lock);
    }));

    ASSERT_TRUE(result.get());
}

TEST_F(SharedMutexTests, lock_succeeds)
{
    SharedMutex mutex;

    UniqueLock<SharedMutex> lock0(mutex, std::try_to_lock);
    ASSERT_TRUE(lock0);

    UniqueLock<SharedMutex> lock1(mutex, std::try_to_lock);
    ASSERT_TRUE(lock1);

    auto result = execute(std::function<TimePoint()>([&]() {
        UniqueLock<SharedMutex> lock(mutex, std::defer_lock);

        if (lock.try_lock_for(Milliseconds(256)))
            return Clock::now();

        return TimePoint::min();
    }));

    std::this_thread::sleep_for(Milliseconds(32));

    auto released = Clock::now();

    lock0.unlock();
    lock1.unlock();

    auto acquired = result.get();
    ASSERT_GT(acquired, released);
}

TEST_F(SharedMutexTests, shared_lock_fails)
{
    SharedMutex mutex;

    UniqueLock<SharedMutex> lock(mutex, std::try_to_lock);
    ASSERT_TRUE(lock);

    auto result = execute(std::function<bool()>([&]() {
        return !SharedLock<SharedMutex>(mutex, std::try_to_lock);
    }));

    ASSERT_TRUE(result.get());

    ASSERT_FALSE(SharedLock<SharedMutex>(mutex, std::try_to_lock));
}

TEST_F(SharedMutexTests, shared_lock_recursive_succeeds)
{
    SharedMutex mutex;

    SharedLock<SharedMutex> lock0(mutex, std::try_to_lock);
    ASSERT_TRUE(lock0);

    SharedLock<SharedMutex> lock1(mutex, std::try_to_lock);
    ASSERT_TRUE(lock1);
}

TEST_F(SharedMutexTests, shared_lock_succeeds)
{
    SharedMutex mutex;

    SharedLock<SharedMutex> lock(mutex, std::try_to_lock);
    ASSERT_TRUE(lock);

    auto result = execute(std::function<TimePoint()>([&]() {
        SharedLock<SharedMutex> lock(mutex, std::try_to_lock);

        if (lock)
            return Clock::now();

        return TimePoint::max();
    }));

    std::this_thread::sleep_for(Milliseconds(32));

    auto acquired = result.get();

    ASSERT_LE(acquired, Clock::now());
}

TEST_F(SharedMutexTests, to_shared_lock_succeeds)
{
    SharedMutex mutex;

    UniqueLock<SharedMutex> lock0(mutex, std::try_to_lock);
    ASSERT_TRUE(lock0);

    auto result = execute(std::function<TimePoint()>(
        [&]()
        {
            SharedLock<SharedMutex> lock(mutex, std::defer_lock);

            if (lock.try_lock_for(Milliseconds(256)))
                return Clock::now();

            return TimePoint::min();
        }));

    std::this_thread::sleep_for(Milliseconds(32));

    auto released = Clock::now();

    auto lock1 = lock0.to_shared_lock();
    ASSERT_TRUE(lock1);

    auto acquired = result.get();
    ASSERT_GE(acquired, released);

    lock1.unlock();

    ASSERT_TRUE(lock0.try_lock());
}

} // testing
} // mega