ASThread.h
1
2
3
4
ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL ASDisplayNodeThreadIsMain()
{
return 0 != pthread_main_np();
}
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
namespace AS {
// Set once in Mutex constructor. Linker fails if this is a member variable. ??
static bool gMutex_unfair;
// Silence unguarded availability warnings in here, because
// perf is critical and we will check availability once
// and not again.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
class Mutex
{
public:
/// Constructs a plain mutex (the default).
Mutex () : Mutex (false) {}
~Mutex () {
// Manually destroy since unions can't do it.
switch (_type) {
case Plain:
_plain.~mutex();
break;
case Recursive:
_recursive.~recursive_mutex();
break;
case Unfair:
// nop
break;
case RecursiveUnfair:
// nop
break;
}
}
Mutex (const Mutex&) = delete;
Mutex &operator=(const Mutex&) = delete;
bool try_lock() {
bool success = false;
switch (_type) {
case Plain:
success = _plain.try_lock();
break;
case Recursive:
success = _recursive.try_lock();
break;
case Unfair:
success = os_unfair_lock_trylock(&_unfair);
break;
case RecursiveUnfair:
success = ASRecursiveUnfairLockTryLock(&_runfair);
break;
}
if (success) {
DidLock();
}
return success;
}
void lock() {
switch (_type) {
case Plain:
_plain.lock();
break;
case Recursive:
_recursive.lock();
break;
case Unfair:
os_unfair_lock_lock(&_unfair);
break;
case RecursiveUnfair:
ASRecursiveUnfairLockLock(&_runfair);
break;
}
DidLock();
}
void unlock() {
WillUnlock();
switch (_type) {
case Plain:
_plain.unlock();
break;
case Recursive:
_recursive.unlock();
break;
case Unfair:
os_unfair_lock_unlock(&_unfair);
break;
case RecursiveUnfair:
ASRecursiveUnfairLockUnlock(&_runfair);
break;
}
}
void AssertHeld() {
ASDisplayNodeCAssert(_owner == std::this_thread::get_id(), @"Thread should hold lock");
}
void AssertNotHeld() {
ASDisplayNodeCAssert(_owner != std::this_thread::get_id(), @"Thread should not hold lock");
}
explicit Mutex (bool recursive) {
// Check if we can use unfair lock and store in static var.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (AS_AVAILABLE_IOS_TVOS(10, 10)) {
gMutex_unfair = ASActivateExperimentalFeature(ASExperimentalUnfairLock);
}
});
if (recursive) {
if (gMutex_unfair) {
_type = RecursiveUnfair;
_runfair = AS_RECURSIVE_UNFAIR_LOCK_INIT;
} else {
_type = Recursive;
new (&_recursive) std::recursive_mutex();
}
} else {
if (gMutex_unfair) {
_type = Unfair;
_unfair = OS_UNFAIR_LOCK_INIT;
} else {
_type = Plain;
new (&_plain) std::mutex();
}
}
}
private:
enum Type {
Plain,
Recursive,
Unfair,
RecursiveUnfair
};
void WillUnlock() {
#if ASDISPLAYNODE_ASSERTIONS_ENABLED
if (--_count == 0) {
_owner = std::thread::id();
}
#endif
}
void DidLock() {
#if ASDISPLAYNODE_ASSERTIONS_ENABLED
if (++_count == 1) {
// New owner.
_owner = std::this_thread::get_id();
}
#endif
}
Type _type;
union {
os_unfair_lock _unfair;
ASRecursiveUnfairLock _runfair;
std::mutex _plain;
std::recursive_mutex _recursive;
};
#if ASDISPLAYNODE_ASSERTIONS_ENABLED
std::thread::id _owner = std::thread::id();
int _count = 0;
#endif
};
#pragma clang diagnostic pop // ignored "-Wunguarded-availability"
/**
Obj-C doesn't allow you to pass parameters to C++ ivar constructors.
Provide a convenience to change the default from non-recursive to recursive.
But wait! Recursive mutexes are a bad idea. Think twice before using one:
http://www.zaval.org/resources/library/butenhof1.html
http://www.fieryrobot.com/blog/2008/10/14/recursive-locks-will-kill-you/
*/
class RecursiveMutex : public Mutex
{
public:
RecursiveMutex () : Mutex (true) {}
};
typedef std::lock_guard<Mutex> MutexLocker;
typedef std::unique_lock<Mutex> UniqueLock;
}