-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOneThreadAtATime.h
More file actions
67 lines (60 loc) · 1.42 KB
/
OneThreadAtATime.h
File metadata and controls
67 lines (60 loc) · 1.42 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
#pragma once
#include "debug.h"
// Usage:
//
// Define the global atomic boolean wrapper:
//
// OneThreadAtATime critical_area_01;
//
// Also include utils::threading inside utils somewhere
// (or use utils::threading::unlock_guard below):
//
// namespace utils { using namespace threading; }
//
// Then around each block of this critical area, use:
//
// {
// #if CW_DEBUG
// std::lock_guard<OneThreadAtATime> lk(critical_area_01);
// #endif
// ... critical area here ...
// }
//
// Or, for example
//
// {
// #if CW_DEBUG
// std::unique_lock<OneThreadAtATime> ul(critical_area_01);
// #endif
// ... critical area here ...
// {
// #if CW_DEBUG
// utils::unlock_guard<OneThreadAtATime> unlk(ul);
// #endif
// ... non-critical area (for example a callback function) ...
// }
// ... critical area here ...
// }
#if CW_DEBUG
#include <atomic>
#include <thread>
class OneThreadAtATime
{
private:
std::atomic<std::thread::id> m_owner;
int m_recursive;
public:
OneThreadAtATime() : m_recursive(0) { }
void lock()
{
std::thread::id previous_owner = m_owner.exchange(std::this_thread::get_id(), std::memory_order_relaxed);
ASSERT(previous_owner == std::thread::id() || previous_owner == std::this_thread::get_id());
++m_recursive;
}
void unlock()
{
if (--m_recursive == 0)
m_owner.store(std::thread::id(), std::memory_order_relaxed);
}
};
#endif // CW_DEBUG