-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththread_sync.hpp
More file actions
88 lines (68 loc) · 2.31 KB
/
thread_sync.hpp
File metadata and controls
88 lines (68 loc) · 2.31 KB
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
// Copyright 2020-2025 UnoDB contributors
#ifndef UNODB_DETAIL_THREAD_SYNC_HPP
#define UNODB_DETAIL_THREAD_SYNC_HPP
/// \file
/// Thread synchronization for concurrent tests.
// Should be the first include
#include "global.hpp"
#include <array>
#include <condition_variable>
#include <mutex>
#include "assert.hpp"
namespace unodb::detail {
/// A simple one-way synchronization mechanism to make one thread wait until
/// another one signals it.
///
/// Used in concurrent Google Test and fuzzer tests.
class [[nodiscard]] thread_sync final {
public:
/// Default constructor creates the synchronization primitive in reset state.
thread_sync() noexcept = default;
UNODB_DETAIL_DISABLE_MSVC_WARNING(26447)
/// Destructor, asserting that the sync object, if signaled, was actually
/// waited-for.
~thread_sync() noexcept { UNODB_DETAIL_ASSERT(is_reset()); }
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
/// Check if the synchronization primitive is in reset state.
[[nodiscard]] bool is_reset() const {
const std::lock_guard lock{sync_mutex};
return !flag;
}
/// Signal to allow a waiting thread to proceed.
void notify() {
{
const std::lock_guard lock{sync_mutex};
flag = true;
}
sync.notify_one();
}
/// Wait until notified, then reset.
void wait() {
std::unique_lock lock{sync_mutex};
sync.wait(lock, [&inner_flag = flag] { return inner_flag; });
flag = false;
lock.unlock();
}
/// Copy construction is disabled.
thread_sync(const thread_sync&) = delete;
/// Move construction is disabled.
thread_sync(thread_sync&&) = delete;
/// Copy assignment is disabled.
thread_sync& operator=(const thread_sync&) = delete;
/// Move assignment is disabled.
thread_sync& operator=(thread_sync&&) = delete;
private:
/// Underlying condition variable.
std::condition_variable sync;
/// Mutex for protecting the flag.
mutable std::mutex sync_mutex;
/// Flag indicating whether the thread_sync has been notified.
bool flag{false};
};
/// Global array of thread synchronization objects.
///
/// The array size is determined by test needs.
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,fuchsia-statically-constructed-objects)
inline std::array<thread_sync, 6> thread_syncs;
} // namespace unodb::detail
#endif // UNODB_DETAIL_THREAD_SYNC_HPP