-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlockers.cc
227 lines (207 loc) · 6.34 KB
/
lockers.cc
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
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/lockers.h"
#include "platform/assert.h"
#include "vm/heap/safepoint.h"
#include "vm/isolate.h"
namespace dart {
Monitor::WaitResult MonitorLocker::WaitWithSafepointCheck(Thread* thread,
int64_t millis) {
ASSERT(thread == Thread::Current());
ASSERT(thread->execution_state() == Thread::kThreadInVM);
#if defined(DEBUG)
if (no_safepoint_scope_) {
thread->DecrementNoSafepointScopeDepth();
}
#endif
thread->set_execution_state(Thread::kThreadInBlockedState);
thread->EnterSafepoint();
Monitor::WaitResult result = monitor_->Wait(millis);
// First try a fast update of the thread state to indicate it is not at a
// safepoint anymore.
if (!thread->TryExitSafepoint()) {
// Fast update failed which means we could potentially be in the middle
// of a safepoint operation and need to block for it.
monitor_->Exit();
thread->ExitSafepointUsingLock();
monitor_->Enter();
}
thread->set_execution_state(Thread::kThreadInVM);
#if defined(DEBUG)
if (no_safepoint_scope_) {
thread->IncrementNoSafepointScopeDepth();
}
#endif
return result;
}
SafepointMutexLocker::SafepointMutexLocker(ThreadState* thread, Mutex* mutex)
: StackResource(thread), mutex_(mutex) {
ASSERT(mutex != nullptr);
if (!mutex_->TryLock()) {
// We did not get the lock and could potentially block, so transition
// accordingly.
Thread* thread = Thread::Current();
if (thread != nullptr) {
TransitionVMToBlocked transition(thread);
mutex->Lock();
} else {
mutex->Lock();
}
}
}
void SafepointMonitorLocker::AcquireLock() {
ASSERT(monitor_ != nullptr);
if (!monitor_->TryEnter()) {
// We did not get the lock and could potentially block, so transition
// accordingly.
Thread* thread = Thread::Current();
if (thread != nullptr) {
TransitionVMToBlocked transition(thread);
monitor_->Enter();
} else {
monitor_->Enter();
}
}
}
void SafepointMonitorLocker::ReleaseLock() {
monitor_->Exit();
}
Monitor::WaitResult SafepointMonitorLocker::Wait(int64_t millis) {
Thread* thread = Thread::Current();
if (thread != nullptr) {
Monitor::WaitResult result;
{
TransitionVMToBlocked transition(thread);
result = monitor_->Wait(millis);
}
return result;
} else {
return monitor_->Wait(millis);
}
}
#if defined(DEBUG)
bool SafepointRwLock::IsCurrentThreadReader() {
ThreadId id = OSThread::GetCurrentThreadId();
if (IsCurrentThreadWriter()) {
return true;
}
MonitorLocker ml(&monitor_);
for (intptr_t i = readers_ids_.length() - 1; i >= 0; i--) {
if (readers_ids_.At(i) == id) {
return true;
}
}
return false;
}
#endif // defined(DEBUG)
bool SafepointRwLock::EnterRead() {
// No need to safepoint if the current thread is not attached.
auto thread = Thread::Current();
// Attempt to acquire a lock while owning a safepoint could lead to a deadlock
// (some other thread might be forced to a safepoint while holding this lock).
//
// Though if the lock was already acquired by this thread before entering a
// safepoint, we do allow the nested acquire (which is a NOP).
DEBUG_ASSERT(thread == nullptr || thread->CanAcquireSafepointLocks() ||
IsCurrentThreadReader());
const bool can_block_without_safepoint = thread == nullptr;
bool acquired_read_lock = false;
if (!TryEnterRead(can_block_without_safepoint, &acquired_read_lock)) {
// Important: must never hold monitor_ when blocking for safepoint.
TransitionVMToBlocked transition(thread);
const bool ok = TryEnterRead(/*can_block=*/true, &acquired_read_lock);
RELEASE_ASSERT(ok);
RELEASE_ASSERT(acquired_read_lock);
}
return acquired_read_lock;
}
bool SafepointRwLock::TryEnterRead(bool can_block, bool* acquired_read_lock) {
MonitorLocker ml(&monitor_);
if (IsCurrentThreadWriter()) {
*acquired_read_lock = false;
return true;
}
if (can_block) {
while (state_ < 0) {
ml.Wait();
}
}
if (state_ >= 0) {
++state_;
DEBUG_ONLY(readers_ids_.Add(OSThread::GetCurrentThreadId()));
*acquired_read_lock = true;
return true;
}
return false;
}
void SafepointRwLock::LeaveRead() {
MonitorLocker ml(&monitor_);
ASSERT(state_ > 0);
#if defined(DEBUG)
{
intptr_t i = readers_ids_.length() - 1;
ThreadId id = OSThread::GetCurrentThreadId();
while (i >= 0) {
if (readers_ids_.At(i) == id) {
readers_ids_.RemoveAt(i);
break;
}
i--;
}
ASSERT(i >= 0);
}
#endif
if (--state_ == 0) {
ml.NotifyAll();
}
}
void SafepointRwLock::EnterWrite() {
// No need to safepoint if the current thread is not attached.
auto thread = Thread::Current();
// Attempt to acquire a lock while owning a safepoint could lead to a deadlock
// (some other thread might be forced to a safepoint while holding this lock).
//
// Though if the lock was already acquired by this thread before entering a
// safepoint, we do allow the nested acquire (which is a NOP).
DEBUG_ASSERT(thread == nullptr || thread->CanAcquireSafepointLocks() ||
IsCurrentThreadWriter());
const bool can_block_without_safepoint = thread == nullptr;
RELEASE_ASSERT(can_block_without_safepoint ||
thread->current_safepoint_level() >=
expected_safepoint_level_);
if (!TryEnterWrite(can_block_without_safepoint)) {
// Important: must never hold monitor_ when blocking for safepoint.
TransitionVMToBlocked transition(thread);
const bool ok = TryEnterWrite(/*can_block=*/true);
RELEASE_ASSERT(ok);
}
}
bool SafepointRwLock::TryEnterWrite(bool can_block) {
MonitorLocker ml(&monitor_);
if (IsCurrentThreadWriter()) {
state_--;
return true;
}
if (can_block) {
while (state_ != 0) {
ml.Wait();
}
}
if (state_ == 0) {
writer_id_ = OSThread::GetCurrentThreadId();
state_ = -1;
return true;
}
return false;
}
void SafepointRwLock::LeaveWrite() {
MonitorLocker ml(&monitor_);
ASSERT(state_ < 0);
if (++state_ < 0) {
return;
}
writer_id_ = OSThread::kInvalidThreadId;
ml.NotifyAll();
}
} // namespace dart