-
Notifications
You must be signed in to change notification settings - Fork 54
/
Lock.cpp
295 lines (239 loc) · 8.84 KB
/
Lock.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Lock.h"
#include "ChunkAllocator.h"
#include "InfallibleVector.h"
#include "SpinLock.h"
#include "Thread.h"
#include <unordered_map>
namespace mozilla {
namespace recordreplay {
// The total number of locks that have been created. Each Lock is given a
// non-zero id based on this counter.
static Atomic<size_t, SequentiallyConsistent, Behavior::DontPreserve> gNumLocks;
struct LockAcquires {
// List of thread acquire orders for the lock. This is protected by the lock
// itself.
Stream* mAcquires;
// During replay, the next thread id to acquire the lock. Writes to this are
// protected by the lock itself, though reads may occur on other threads.
Atomic<size_t, SequentiallyConsistent, Behavior::DontPreserve> mNextOwner;
static const size_t NoNextOwner = 0;
void ReadAndNotifyNextOwner(Thread* aCurrentThread) {
MOZ_RELEASE_ASSERT(IsReplaying());
if (mAcquires->AtEnd()) {
mNextOwner = NoNextOwner;
} else {
mNextOwner = mAcquires->ReadScalar();
if (mNextOwner != aCurrentThread->Id()) {
Thread::Notify(mNextOwner);
}
}
}
};
// Acquires for each lock, indexed by the lock ID.
static ChunkAllocator<LockAcquires> gLockAcquires;
///////////////////////////////////////////////////////////////////////////////
// Locking Interface
///////////////////////////////////////////////////////////////////////////////
// Table mapping native lock pointers to the associated Lock structure, for
// every recorded lock in existence.
typedef std::unordered_map<void*, Lock*> LockMap;
static LockMap* gLocks;
static ReadWriteSpinLock gLocksLock;
static Lock* CreateNewLock(Thread* aThread, size_t aId) {
LockAcquires* info = gLockAcquires.Create(aId);
info->mAcquires = gRecordingFile->OpenStream(StreamName::Lock, aId);
if (IsReplaying()) {
info->ReadAndNotifyNextOwner(aThread);
}
return new Lock(aId);
}
/* static */ void Lock::New(void* aNativeLock) {
Thread* thread = Thread::Current();
RecordingEventSection res(thread);
if (!res.CanAccessEvents()) {
Destroy(aNativeLock); // Clean up any old lock, as below.
return;
}
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::CreateLock);
size_t id;
if (IsRecording()) {
id = gNumLocks++;
}
thread->Events().RecordOrReplayScalar(&id);
Lock* lock = CreateNewLock(thread, id);
// Tolerate new locks being created with identical pointers, even if there
// was no explicit Destroy() call for the old one.
Destroy(aNativeLock);
AutoWriteSpinLock ex(gLocksLock);
thread->BeginDisallowEvents();
if (!gLocks) {
gLocks = new LockMap();
}
gLocks->insert(LockMap::value_type(aNativeLock, lock));
thread->EndDisallowEvents();
}
/* static */ void Lock::Destroy(void* aNativeLock) {
Lock* lock = nullptr;
{
AutoWriteSpinLock ex(gLocksLock);
if (gLocks) {
LockMap::iterator iter = gLocks->find(aNativeLock);
if (iter != gLocks->end()) {
lock = iter->second;
gLocks->erase(iter);
}
}
}
delete lock;
}
/* static */ Lock* Lock::Find(void* aNativeLock) {
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
Maybe<AutoReadSpinLock> ex;
ex.emplace(gLocksLock);
if (gLocks) {
LockMap::iterator iter = gLocks->find(aNativeLock);
if (iter != gLocks->end()) {
// Now that we know the lock is recorded, check whether thread events
// should be generated right now. Doing things in this order avoids
// reentrancy issues when initializing the thread-local state used by
// these calls.
Lock* lock = iter->second;
if (AreThreadEventsPassedThrough()) {
return nullptr;
}
if (HasDivergedFromRecording()) {
// When diverged from the recording, don't allow uses of locks that are
// held by idling threads that have not diverged from the recording.
// This will cause the process to deadlock, so rewind instead.
if (lock->mOwner && Thread::GetById(lock->mOwner)->ShouldIdle() &&
Thread::CurrentIsMainThread()) {
ex.reset();
EnsureNotDivergedFromRecording();
Unreachable();
}
return nullptr;
}
return lock;
}
}
return nullptr;
}
void Lock::Enter() {
Thread* thread = Thread::Current();
RecordingEventSection res(thread);
if (!res.CanAccessEvents()) {
return;
}
// Include an event in each thread's record when a lock acquire begins. This
// is not required by the replay but is used to check that lock acquire order
// is consistent with the recording and that we will fail explicitly instead
// of deadlocking.
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::Lock);
thread->Events().CheckInput(mId);
LockAcquires* acquires = gLockAcquires.Get(mId);
if (IsRecording()) {
acquires->mAcquires->WriteScalar(thread->Id());
} else {
// Wait until this thread is next in line to acquire the lock, or until it
// has been instructed to diverge from the recording.
while (thread->Id() != acquires->mNextOwner &&
!thread->MaybeDivergeFromRecording()) {
Thread::Wait();
}
if (!thread->HasDivergedFromRecording()) {
mOwner = thread->Id();
}
}
}
void Lock::Exit() {
Thread* thread = Thread::Current();
if (IsReplaying() && !thread->HasDivergedFromRecording()) {
mOwner = 0;
// Notify the next owner before releasing the lock.
LockAcquires* acquires = gLockAcquires.Get(mId);
acquires->ReadAndNotifyNextOwner(thread);
}
}
/* static */ void Lock::LockAquiresUpdated(size_t aLockId) {
LockAcquires* acquires = gLockAcquires.MaybeGet(aLockId);
if (acquires && acquires->mAcquires &&
acquires->mNextOwner == LockAcquires::NoNextOwner) {
acquires->ReadAndNotifyNextOwner(Thread::Current());
}
}
// We use a set of Locks to record and replay the order in which atomic
// accesses occur. Each lock describes the acquire order for a disjoint set of
// values; this is done to reduce contention between threads, and ensures that
// when the same value pointer is used in two ordered atomic accesses, those
// accesses will replay in the same order as they did while recording.
// Instead of using platform mutexes, we manage the Locks directly to avoid
// overhead in Lock::Find. Atomics accesses are a major source of recording
// overhead, which we want to minimize.
static const size_t NumAtomicLocks = 89;
static Lock** gAtomicLocks;
// While recording, these locks prevent multiple threads from simultaneously
// owning the same atomic lock.
static SpinLock* gAtomicLockOwners;
/* static */ void Lock::InitializeLocks() {
Thread* thread = Thread::Current();
gNumLocks = 1;
gAtomicLocks = new Lock*[NumAtomicLocks];
for (size_t i = 0; i < NumAtomicLocks; i++) {
gAtomicLocks[i] = CreateNewLock(thread, gNumLocks++);
}
if (IsRecording()) {
gAtomicLockOwners = new SpinLock[NumAtomicLocks];
PodZero(gAtomicLockOwners, NumAtomicLocks);
}
}
extern "C" {
MOZ_EXPORT void RecordReplayInterface_InternalBeginOrderedAtomicAccess(
const void* aValue) {
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
Thread* thread = Thread::Current();
// Determine which atomic lock to use for this access.
size_t atomicId;
{
RecordingEventSection res(thread);
if (!res.CanAccessEvents()) {
return;
}
thread->Events().RecordOrReplayThreadEvent(ThreadEvent::AtomicAccess);
atomicId = IsRecording() ? (HashGeneric(aValue) % NumAtomicLocks) : 0;
thread->Events().RecordOrReplayScalar(&atomicId);
MOZ_RELEASE_ASSERT(atomicId < NumAtomicLocks);
}
// When recording, hold a spin lock so that no other thread can access this
// same atomic until this access ends. When replaying, we don't need to hold
// any actual lock, as the atomic access cannot race and the Lock structure
// ensures that accesses happen in the same order.
if (IsRecording()) {
gAtomicLockOwners[atomicId].Lock();
}
gAtomicLocks[atomicId]->Enter();
MOZ_RELEASE_ASSERT(thread->AtomicLockId().isNothing());
thread->AtomicLockId().emplace(atomicId);
}
MOZ_EXPORT void RecordReplayInterface_InternalEndOrderedAtomicAccess() {
MOZ_RELEASE_ASSERT(IsRecordingOrReplaying());
Thread* thread = Thread::Current();
if (!thread || thread->PassThroughEvents() ||
thread->HasDivergedFromRecording()) {
return;
}
MOZ_RELEASE_ASSERT(thread->AtomicLockId().isSome());
size_t atomicId = thread->AtomicLockId().ref();
thread->AtomicLockId().reset();
if (IsRecording()) {
gAtomicLockOwners[atomicId].Unlock();
}
gAtomicLocks[atomicId]->Exit();
}
} // extern "C"
} // namespace recordreplay
} // namespace mozilla