-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspin_lock
92 lines (71 loc) · 2.85 KB
/
spin_lock
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_EXECUTION_SPIN_LOCK_HPP
#define GTL_EXECUTION_SPIN_LOCK_HPP
// Summary: Spin lock implemented using an atomic flag.
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the spin_lock is misused.
# define GTL_SPIN_LOCK_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_SPIN_LOCK_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
#if defined(_MSC_VER)
# pragma warning(push, 0)
#endif
#include <atomic>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
namespace gtl {
/// @brief The spin_lock is a simple mutex structure that doesn't put threads to sleep.
class spin_lock final {
/// @brief The lock flag determines if the spin_lock is locked or not.
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
/// @brief Destructor asserts that the spin_lock is unlocked.
~spin_lock() {
GTL_SPIN_LOCK_ASSERT(this->try_lock(), "Ensure that the lock is lockable when it is destructed.");
}
/// @brief Default constructor.
spin_lock() = default;
/// @brief Deleted copy constructor.
spin_lock(spin_lock&) = delete;
/// @brief Deleted move constructor.
spin_lock(spin_lock&&) = delete;
/// @brief Deleted copy assignment operator.
spin_lock& operator=(const spin_lock&) = delete;
/// @brief Deleted move assignment operator.
spin_lock& operator=(spin_lock&&) = delete;
public:
/// @brief Block until the current thread has the lock.
void lock() {
while (!this->try_lock());
}
/// @brief Unlock the lock.
void unlock() {
this->flag.clear(std::memory_order_release);
}
/// @brief Try and lock the spin_lock.
/// @return True if the spin_lock is successfully locked, false otherwise.
bool try_lock() {
return !this->flag.test_and_set(std::memory_order_acquire);
}
};
}
#undef GTL_SPIN_LOCK_ASSERT
#endif // GTL_EXECUTION_SPIN_LOCK_HPP