Repository navigation
Expand file tree
/
Copy pathWaiterListManager.cpp
More file actions
466 lines (404 loc) · 18.1 KB
/
Copy pathWaiterListManager.cpp
File metadata and controls
466 lines (404 loc) · 18.1 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (C) 2022-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WaiterListManager.h"
#include "HeapCellInlines.h"
#include "JSGlobalObject.h"
#include "JSLock.h"
#include "JSObjectInlines.h"
#include "ObjectConstructor.h"
#include "VMManager.h"
#include "VMTrapsInlines.h"
#include <wtf/DataLog.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/RawPointer.h>
#include <wtf/TZoneMallocInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
namespace WaiterListsManagerInternal {
static constexpr bool verbose = false;
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(Waiter);
WTF_MAKE_TZONE_ALLOCATED_IMPL(WaiterList);
Waiter::Waiter(VM* vm)
: m_vm(vm)
, m_isAsync(false)
{
}
Waiter::Waiter(JSPromise* promise)
: m_vm(&promise->vm())
, m_globalObject(promise->realm())
, m_ticket(m_vm->deferredWorkTimer->addPendingWork(DeferredWorkTimer::WorkType::AtSomePoint, *m_vm, promise, { }))
, m_isAsync(true)
{
}
Waiter::~Waiter() = default;
void Waiter::setParkedList(RefPtr<WaiterList>&& list)
{
ASSERT(!m_isAsync);
Locker locker { m_parkedListLock };
m_parkedList = WTF::move(list);
}
void Waiter::notifyOfTerminationRequest()
{
ASSERT(!m_isAsync);
RefPtr<WaiterList> parkedList;
{
Locker locker { m_parkedListLock };
parkedList = m_parkedList;
}
// Not parked (yet): waitSyncImpl publishes the list before it tests for the request, so a wait
// that starts after this sees the NeedTermination trap our caller has already fired.
if (!parkedList)
return;
// Notify under the list's lock, which the waiter holds from testing for the request until it is
// parked, so that the notification cannot fall in between.
Locker listLocker { parkedList->lock };
m_condition.notifyOne();
}
// A termination is only ever established (VM::hasTerminationRequest) by the mutator thread itself,
// which cannot do that while it is parked in a wait. Another thread asking this VM to terminate
// (VM::notifyNeedTermination) is visible as the pending NeedTermination trap. Neither interrupts the
// wait while termination is being deferred (DeferTermination); it takes effect when the deferral ends.
static ALWAYS_INLINE bool shouldStopWaitingForTermination(VM& vm)
{
if (vm.traps().isDeferringTermination()) [[unlikely]]
return false;
return vm.hasTerminationRequest() || vm.traps().needHandling(VMTraps::NeedTermination);
}
WaiterListManager& WaiterListManager::singleton()
{
static LazyNeverDestroyed<WaiterListManager> manager;
static std::once_flag onceKey;
std::call_once(onceKey, [&] {
manager.construct();
});
return manager;
}
template <typename ValueType>
WaiterListManager::WaitSyncResult WaiterListManager::waitSyncImpl(VM& vm, ValueType* ptr, ValueType expectedValue, Seconds timeout)
{
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> waitSyncImpl starts totalWaiterCount=", totalWaiterCount());
Ref<Waiter> syncWaiter = vm.syncWaiter();
Ref<WaiterList> list = findOrCreateList(ptr);
MonotonicTime time = MonotonicTime::timePointFromNow(timeout);
{
VMBlockingScope waitScope(vm, StopTheWorldEvent::AtomicsWaitBlocked);
Locker listLocker { list->lock };
if (WTF::atomicLoad(ptr) != expectedValue)
return WaitSyncResult::NotEqual;
list->addLast(listLocker, syncWaiter);
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> added a new SyncWaiter=", syncWaiter.get(), " to a waiterList for ptr ", RawPointer(ptr));
syncWaiter->setParkedList(list.copyRef());
#if USE(MIMALLOC)
// Only the owning thread can return the memory its thread-local heap holds, so do that in a wait that takes a
// while. A wait that is notified soon pays nothing. One release does not return the large blocks that the
// thread used a moment ago, so the thread releases again an interval later while one is due.
static constexpr Seconds releaseFreeMemoryInterval = Seconds::fromMilliseconds(WTF::IdleFreeMemoryRelease::intervalInMilliseconds);
WTF::IdleFreeMemoryRelease freeMemoryRelease;
MonotonicTime releaseFreeMemoryTime = MonotonicTime::now() + releaseFreeMemoryInterval;
#endif
while (syncWaiter->isOnList() && time.now() < time && !shouldStopWaitingForTermination(vm)) {
#if USE(MIMALLOC)
if (freeMemoryRelease.isDue() && releaseFreeMemoryTime < time) {
if (MonotonicTime::now() < releaseFreeMemoryTime) {
syncWaiter->condition().waitUntil(list->lock, releaseFreeMemoryTime.approximate<WallTime>());
continue;
}
// A notification in the meantime takes us off the list, and the loop tests for that (and for a
// termination request) under the lock again before it waits.
{
DropLockForScope dropLock { listLocker };
freeMemoryRelease.release();
}
releaseFreeMemoryTime = MonotonicTime::now() + releaseFreeMemoryInterval;
continue;
}
#endif
syncWaiter->condition().waitUntil(list->lock, time.approximate<WallTime>());
}
syncWaiter->setParkedList(nullptr);
// At this point, syncWaiter should be either notified (dequeued) or timeout (not dequeued).
bool didGetDequeued = !syncWaiter->isOnList();
if (didGetDequeued)
return WaitSyncResult::OK;
didGetDequeued = list->findAndRemove(listLocker, syncWaiter);
ASSERT(didGetDequeued);
if (!shouldStopWaitingForTermination(vm))
return WaitSyncResult::TimedOut;
}
// The wait was cut short by a termination request: leave with it established and the
// TerminationException thrown, consuming the trap if another thread's request is what woke us
// (as VMTraps::handleTraps() would, minus jettisoning the code blocks that have trap breakpoints
// installed, which throwing from here does not need).
ASSERT(!vm.traps().isDeferringTermination());
if (vm.traps().clearTrap(VMTraps::NeedTermination))
vm.setHasTerminationRequest();
if (!vm.hasPendingTerminationException())
vm.throwTerminationException();
return WaitSyncResult::Terminated;
}
template <typename ValueType>
JSValue WaiterListManager::waitAsyncImpl(JSGlobalObject* globalObject, VM& vm, ValueType* ptr, ValueType expectedValue, Seconds timeout)
{
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> waitAsyncImpl starts totalWaiterCount=", totalWaiterCount());
JSObject* object = constructEmptyObject(globalObject);
bool isAsync = false;
JSValue value;
Ref<WaiterList> list = findOrCreateList(ptr);
JSPromise* promise = JSPromise::create(vm, globalObject->promiseStructure());
{
Locker listLocker { list->lock };
if (WTF::atomicLoad(ptr) != expectedValue)
value = vm.smallStrings.notEqualString();
else if (!timeout)
value = vm.smallStrings.timedOutString();
else {
isAsync = true;
Ref<Waiter> waiter = adoptRef(*new Waiter(promise));
list->addLast(listLocker, waiter);
if (timeout != Seconds::infinity()) {
Ref<RunLoop::DispatchTimer> timer = RunLoop::currentSingleton().scheduleTimer(timeout, [this, ptr, waiter = waiter.copyRef()]() mutable {
timeoutAsyncWaiter(ptr, WTF::move(waiter));
});
waiter->setTimer(listLocker, WTF::move(timer));
}
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> added a new AsyncWaiter=", *waiter.ptr(), " to a waiterList for ptr ", RawPointer(ptr));
value = promise;
}
}
object->putDirect(vm, vm.propertyNames->async, jsBoolean(isAsync));
object->putDirect(vm, vm.propertyNames->value, value);
return object;
}
JSValue WaiterListManager::waitAsync(JSGlobalObject* globalObject, VM& vm, int32_t* ptr, int32_t expected, Seconds timeout)
{
return waitAsyncImpl(globalObject, vm, ptr, expected, timeout);
}
JSValue WaiterListManager::waitAsync(JSGlobalObject* globalObject, VM& vm, int64_t* ptr, int64_t expected, Seconds timeout)
{
return waitAsyncImpl(globalObject, vm, ptr, expected, timeout);
}
WaiterListManager::WaitSyncResult WaiterListManager::waitSync(VM& vm, int32_t* ptr, int32_t expected, Seconds timeout)
{
return waitSyncImpl(vm, ptr, expected, timeout);
}
WaiterListManager::WaitSyncResult WaiterListManager::waitSync(VM& vm, int64_t* ptr, int64_t expected, Seconds timeout)
{
return waitSyncImpl(vm, ptr, expected, timeout);
}
void WaiterListManager::timeoutAsyncWaiter(void* ptr, Ref<Waiter>&& waiter)
{
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> timeoutAsyncWaiter ", waiter.get(), ") for ptr ", RawPointer(ptr));
if (RefPtr<WaiterList> list = findList(ptr)) {
Locker listLocker { list->lock };
if (waiter->isOnList()) {
bool didGetDequeued = list->findAndRemove(listLocker, waiter);
ASSERT_UNUSED(didGetDequeued, didGetDequeued);
}
notifyWaiterImpl(listLocker, WTF::move(waiter), ResolveResult::Timeout);
return;
}
ASSERT(!waiter->isOnList());
notifyWaiterImpl(NoLockingNecessary, WTF::move(waiter), ResolveResult::Timeout);
}
unsigned WaiterListManager::notifyWaiter(void* ptr, unsigned count)
{
ASSERT(ptr);
unsigned notified = 0;
RefPtr<WaiterList> list = findList(ptr);
if (list) {
Locker listLocker { list->lock };
while (notified < count && list->size()) {
notifyWaiterImpl(listLocker, list->takeFirst(listLocker), ResolveResult::Ok);
notified++;
}
}
dataLogLnIf(WaiterListsManagerInternal::verbose, "<WaiterListManager> <Thread:", Thread::currentSingleton(), "> notified waiters (count ", notified, ") for ptr ", RawPointer(ptr));
return notified;
}
void WaiterListManager::notifyWaiterImpl(const AbstractLocker& listLocker, Ref<Waiter>&& waiter, const ResolveResult resolveResult)
{
ASSERT(!waiter->isOnList());
if (waiter->isAsync()) {
waiter->scheduleWorkAndClear(listLocker, [resolveResult](DeferredWorkTimer::Ticket& ticket) {
JSPromise* promise = uncheckedDowncast<JSPromise>(ticket.target());
JSGlobalObject* globalObject = promise->realm();
VM& vm = promise->vm();
JSValue result = resolveResult == ResolveResult::Ok ? vm.smallStrings.okString() : vm.smallStrings.timedOutString();
promise->resolve(globalObject, vm, result);
});
return;
}
waiter->condition().notifyOne();
}
size_t WaiterListManager::waiterListSize(void* ptr)
{
RefPtr<WaiterList> list = findList(ptr);
size_t size = 0;
if (list) {
Locker listLocker { list->lock };
size = list->size();
}
return size;
}
size_t WaiterListManager::totalWaiterCount()
{
Locker waiterListsLocker { m_waiterListsLock };
size_t totalCount = 0;
for (auto& entry : m_waiterLists) {
Ref<WaiterList> list = entry.value;
Locker listLocker { list->lock };
totalCount += list->size();
}
return totalCount;
}
void Waiter::scheduleWorkAndClear(const AbstractLocker& listLocker, DeferredWorkTimer::Task&& task)
{
ASSERT(m_isAsync && m_vm && !isOnList());
if (m_vm->deferredWorkTimer->scheduleWorkSoonIfActive(m_ticket, WTF::move(task)))
clearTicket(listLocker);
clearTimer(listLocker);
}
void Waiter::cancelAndClear(const AbstractLocker& listLocker)
{
ASSERT(m_isAsync);
if (auto ticket = this->ticket(listLocker)) {
m_vm->deferredWorkTimer->cancelPendingWork(*ticket);
clearTicket(listLocker);
}
clearTimer(listLocker);
}
void WaiterListManager::unregister(VM* vm)
{
Locker waiterListsLocker { m_waiterListsLock };
for (auto& entry : m_waiterLists) {
Ref<WaiterList> list = entry.value;
Locker listLocker { list->lock };
list->removeIf(listLocker, [&](Waiter* waiter) {
if (waiter->vm() == vm) {
dataLogLnIf(WaiterListsManagerInternal::verbose,
"<WaiterListManager> <Thread:", Thread::currentSingleton(),
"> unregister VM is cancelling waiter=", *waiter,
" in WaiterList for ptr ", RawPointer(entry.key));
// If the vm is about destructing, then it shouldn't
// been blocked. That means we shouldn't find any SyncWaiter.
ASSERT(waiter->isAsync());
waiter->cancelAndClear(listLocker);
return true;
}
return false;
});
}
}
void WaiterListManager::unregister(JSGlobalObject* globalObject)
{
Locker waiterListsLocker { m_waiterListsLock };
for (auto& entry : m_waiterLists) {
Ref<WaiterList> list = entry.value;
Locker listLocker { list->lock };
list->removeIf(listLocker, [&](Waiter* waiter) {
if (waiter->isAsync() && waiter->globalObject() == globalObject) {
dataLogLnIf(WaiterListsManagerInternal::verbose,
"<WaiterListManager> <Thread:", Thread::currentSingleton(),
"> unregister JSGlobalObject is cancelling waiter=", *waiter,
" in WaiterList for ptr ", RawPointer(entry.key));
waiter->cancelAndClear(listLocker);
return true;
}
return false;
});
}
}
void WaiterListManager::unregister(uint8_t* arrayPtr, size_t size)
{
Locker waiterListsLocker { m_waiterListsLock };
m_waiterLists.removeIf([&](auto& entry) {
if (entry.key >= arrayPtr && entry.key < arrayPtr + size) {
Ref<WaiterList> list = entry.value;
Locker listLocker { list->lock };
list->removeIf(listLocker, [&](Waiter* waiter) {
dataLogLnIf(WaiterListsManagerInternal::verbose,
"<WaiterListManager> <Thread:", Thread::currentSingleton(),
"> unregister SAB is cancelling waiter=", *waiter,
" in WaiterList for ptr ", RawPointer(entry.key));
// If the SharedArrayBuffer is about destructing, then no VM is
// referencing the buffer. That means no blocking SyncWaiter
// on the buffer for any VM.
ASSERT(waiter->isAsync());
// If the AsyncWaiter has a valid timer, then let it timeout. Otherwise un-task it.
// See example, waitasync-timeout-finite-gc.js.
//
// OK, let's say if the ticket has a valid timer and its globalObject is about being
// destructed later but before the timeout. Then, we cannot cancel the work from
// `unregister(JSGlobalObject* globalObject)` since the waiter is already removed
// from the lists by this code. So, should we keep it in the list? No, in either
// case, we have to remove it since all lists associating to the SAB (about destructing)
// must be removed. This is because there may be a new SAB with a waiter at the same address.
// Therefore, we will let `clearWeakTickets` to handle this special case.
if (!waiter->hasTimer(listLocker))
waiter->cancelAndClear(listLocker);
return true;
});
ASSERT(!list->size());
return true;
}
return false;
});
}
Ref<WaiterList> WaiterListManager::findOrCreateList(void* ptr)
{
Locker waiterListsLocker { m_waiterListsLock };
return m_waiterLists.ensure(ptr, [] {
return adoptRef(*new WaiterList());
}).iterator->value.get();
}
RefPtr<WaiterList> WaiterListManager::findList(void* ptr)
{
Locker waiterListsLocker { m_waiterListsLock };
return m_waiterLists.get(ptr);
}
void Waiter::dump(PrintStream& out) const
{
out.print("[this=");
out.print(RawPointer(this));
out.print(", vm=", RawPointer(m_vm));
out.print(", isAsync=", m_isAsync);
if (!m_isAsync) {
out.print("]");
return;
}
auto ticket = this->ticket(NoLockingNecessary);
out.print(", ticket=", RawPointer(ticket.get()));
out.print(", globalObject=", RawPointer(m_globalObject));
if (ticket && !ticket->isCancelled()) {
out.print(", m_ticket->target=", RawPointer(uncheckedDowncast<JSObject>(ticket->dependencies().last())));
out.print(", m_ticket->scriptExecutionOwner=", RawPointer(ticket->scriptExecutionOwner()));
}
out.print(", m_timer=", RawPointer(m_timer.get()));
out.print("]");
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END