Skip to content

Commit d0f5aa9

Browse files
committed
[JSC] Decouple Heap's world state from the Collector's thread lock
https://bugs.webkit.org/show_bug.cgi?id=329379 rdar://191261786 Reviewed by Mark Lam. Heap used the Collector's thread lock and condition variable directly. Both should be implementation details of the Collector, so that how the Collector serves requests and how a mutator waits and is woken can change independently, as both will for global collections. - waitForCollector held the lock to check its wait and set the waiting bit together. It now sets the bit first, then checks through Collector queries that take the lock themselves. If what the mutator waits for completes after the check, the Collector still clears the bit and wakes the mutator. - finishRelinquishingConn held the lock while it notified the collector thread and unparked the world state. Only the notify needs the lock, so it moves into Collector::didRelinquishConn(), and the unpark stays in Heap. So the world state and the Collector's lock no longer depend on each other: Heap waits and wakes only through its world state, and only the Collector takes its lock. Also rewrite waitForCollector's comments, which described an older protocol. Canonical link: https://commits.webkit.org/325143@main
1 parent 39d8ce5 commit d0f5aa9

4 files changed

Lines changed: 49 additions & 47 deletions

File tree

‎Source/JavaScriptCore/heap/Collector.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ GCRequest::Ticket Collector::requestCollection(Heap& requester, GCRequest reques
251251
return m_lastGrantedTicket;
252252
}
253253

254+
void Collector::didRelinquishConn()
255+
{
256+
Locker locker { *m_threadLock };
257+
if (!hasWorkWithLock())
258+
return;
259+
RELEASE_ASSERT(!m_threadShouldStop);
260+
m_threadCondition->notifyOne(locker);
261+
}
262+
254263
bool Collector::shouldCollectInCollectorThread()
255264
{
256265
dataLogLnIf(CollectorInternal::verbose, "Mutator has the conn = ", anyMutatorHasConnWithLock());

‎Source/JavaScriptCore/heap/Collector.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,33 @@ class Collector {
129129
GCRequest::Ticket requestCollection(Heap& requester, GCRequest);
130130
bool isSubsumedByQueuedRequest(const GCRequest&);
131131

132+
// Call after a mutator gives up the conn, so that any work waiting for the conn can proceed.
133+
void didRelinquishConn();
134+
132135
bool hasPendingGCRequest()
133136
{
134137
Locker locker { *m_threadLock };
135138
return hasPendingGCRequestWithLock();
136139
}
140+
bool hasServedTicket(GCRequest::Ticket ticket)
141+
{
142+
Locker locker { *m_threadLock };
143+
return m_lastServedTicket >= ticket;
144+
}
145+
bool hasAppliedMembershipChange(MembershipChange::Ticket ticket)
146+
{
147+
Locker locker { *m_threadLock };
148+
return m_lastAppliedMembershipChange >= ticket;
149+
}
137150
bool hasPendingGCRequestWithLock() const WTF_REQUIRES_LOCK(*m_threadLock)
138151
{
139152
RELEASE_ASSERT(m_lastServedTicket <= m_lastGrantedTicket);
140153
// A request stays queued until it is served.
141154
ASSERT(m_requests.size() == m_lastGrantedTicket - m_lastServedTicket);
142155
return m_lastServedTicket < m_lastGrantedTicket;
143156
}
144-
bool hasServedTicketWithLock(GCRequest::Ticket ticket) const WTF_REQUIRES_LOCK(*m_threadLock) { return m_lastServedTicket >= ticket; }
145157

146158
bool hasPendingMembershipChangeWithLock() const WTF_REQUIRES_LOCK(*m_threadLock) { return !m_membershipChanges.isEmpty(); }
147-
bool hasAppliedMembershipChangeWithLock(MembershipChange::Ticket ticket) const WTF_REQUIRES_LOCK(*m_threadLock) { return m_lastAppliedMembershipChange >= ticket; }
148159
void applyAllMembershipChangesWithLock(Vector<Heap*, 8>& changedHeaps) WTF_REQUIRES_LOCK(*m_threadLock);
149160
void applyMembershipChangeWithLock(MembershipChange&) WTF_REQUIRES_LOCK(*m_threadLock);
150161

‎Source/JavaScriptCore/heap/Heap.cpp‎

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,39 +1486,29 @@ NEVER_INLINE void Heap::collectInMutatorThread()
14861486
m_collector->collectInMutatorThread(*this);
14871487
}
14881488

1489-
template<typename Func>
1490-
void Heap::waitForCollector(NOESCAPE const Func& func)
1489+
void Heap::waitForCollector(NOESCAPE const ConstInvocable<bool()> auto& isDone)
14911490
{
14921491
for (;;) {
1493-
bool done;
1494-
{
1495-
Locker locker { *m_collector->m_threadLock };
1496-
done = func(locker);
1497-
if (!done) {
1498-
setMutatorWaiting();
1499-
1500-
// At this point, the collector knows that we intend to wait, and he will clear the
1501-
// waiting bit and then unparkAll when the GC cycle finishes. Clearing the bit
1502-
// prevents us from parking except if there is also stop-the-world. Unparking after
1503-
// clearing means that if the clearing happens after we park, then we will unpark.
1504-
}
1505-
}
1506-
1507-
// If we're in a stop-the-world scenario, we need to wait for that even if done is true.
1492+
// Set the waiting bit before checking, so that if what we wait for completes after the check, the Collector
1493+
// always sees that we're waiting, and clears the bit and wakes us.
1494+
setMutatorWaiting();
1495+
bool done = isDone();
1496+
if (done)
1497+
clearMutatorWaiting();
1498+
1499+
// We hold access, so the collector cannot stop us. If it handed us the conn, we must run the phase ourselves.
15081500
unsigned oldState = m_worldState.load();
15091501
stopIfNecessarySlow(oldState);
15101502

15111503
m_mutatorDidRun = true;
1512-
// FIXME: We wouldn't need this if stopIfNecessarySlow() had a mode where it knew to just
1513-
// do the collection.
1504+
// Give the conn back so the collector thread can carry the collection forward, since we won't poll while parked.
1505+
// FIXME: Could conduct the whole collection here instead, since this thread has nothing else to do.
15141506
relinquishConn();
15151507

1516-
if (done) {
1517-
clearMutatorWaiting(); // Clean up just in case.
1508+
if (done)
15181509
return;
1519-
}
1520-
1521-
// If mutatorWaitingBit is still set then we want to wait.
1510+
1511+
// Park only if the waiting bit is still set. The Collector clears it before unparking, so we cannot miss the unpark.
15221512
ParkingLot::compareAndPark(&m_worldState, oldState | mutatorWaitingBit);
15231513
}
15241514
}
@@ -1598,14 +1588,10 @@ bool Heap::relinquishConn(unsigned oldState)
15981588
void Heap::finishRelinquishingConn()
15991589
{
16001590
dataLogLnIf(HeapInternal::verbose, "Relinquished the conn.");
1601-
1591+
16021592
sanitizeStackForVM(vm());
1603-
1604-
Locker locker { *m_collector->m_threadLock };
1605-
if (m_collector->hasWorkWithLock()) {
1606-
RELEASE_ASSERT(!m_collector->m_threadShouldStop);
1607-
m_collector->m_threadCondition->notifyOne(locker);
1608-
}
1593+
1594+
m_collector->didRelinquishConn();
16091595
ParkingLot::unparkAll(&m_worldState);
16101596
}
16111597

@@ -1734,26 +1720,23 @@ void Heap::runCollectionEpilogue()
17341720

17351721
void Heap::waitForMembershipChange(Collector::MembershipChange::Ticket ticket)
17361722
{
1737-
waitForCollector(
1738-
[&](const AbstractLocker&) WTF_REQUIRES_LOCK(*m_collector->m_threadLock) -> bool {
1739-
return m_collector->hasAppliedMembershipChangeWithLock(ticket);
1740-
});
1723+
waitForCollector([&] {
1724+
return m_collector->hasAppliedMembershipChange(ticket);
1725+
});
17411726
}
17421727

17431728
void Heap::waitForCollection(GCRequest::Ticket ticket)
17441729
{
1745-
waitForCollector(
1746-
[&](const AbstractLocker&) WTF_REQUIRES_LOCK(*m_collector->m_threadLock) -> bool {
1747-
return m_collector->hasServedTicketWithLock(ticket);
1748-
});
1730+
waitForCollector([&] {
1731+
return m_collector->hasServedTicket(ticket);
1732+
});
17491733
}
17501734

17511735
void Heap::waitForAllCollections()
17521736
{
1753-
waitForCollector(
1754-
[&](const AbstractLocker&) WTF_REQUIRES_LOCK(*m_collector->m_threadLock) -> bool {
1755-
return !m_collector->hasPendingGCRequestWithLock();
1756-
});
1737+
waitForCollector([&] {
1738+
return !m_collector->hasPendingGCRequest();
1739+
});
17571740
}
17581741

17591742
void Heap::sweepEagerlyInEpilogue()

‎Source/JavaScriptCore/heap/Heap.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,7 @@ class Heap {
680680
JS_EXPORT_PRIVATE void stopIfNecessarySlow();
681681
void stopIfNecessarySlow(unsigned extraStateBits);
682682

683-
template<typename Func>
684-
void waitForCollector(NOESCAPE const Func&);
683+
void waitForCollector(NOESCAPE const ConstInvocable<bool()> auto& isDone);
685684

686685
JS_EXPORT_PRIVATE void acquireAccessSlow();
687686
JS_EXPORT_PRIVATE void releaseAccessSlow();

0 commit comments

Comments
 (0)