GCThreadSharedData.cpp [plain text]
#include "config.h"
#include "GCThreadSharedData.h"
#include "CopyVisitor.h"
#include "CopyVisitorInlines.h"
#include "GCThread.h"
#include "MarkStack.h"
#include "JSCInlines.h"
#include "SlotVisitor.h"
#include "SlotVisitorInlines.h"
#include "VM.h"
namespace JSC {
#if ENABLE(PARALLEL_GC)
void GCThreadSharedData::resetChildren()
{
for (size_t i = 0; i < m_gcThreads.size(); ++i)
m_gcThreads[i]->slotVisitor()->reset();
}
size_t GCThreadSharedData::childVisitCount()
{
unsigned long result = 0;
for (unsigned i = 0; i < m_gcThreads.size(); ++i)
result += m_gcThreads[i]->slotVisitor()->visitCount();
return result;
}
size_t GCThreadSharedData::childBytesVisited()
{
size_t result = 0;
for (unsigned i = 0; i < m_gcThreads.size(); ++i)
result += m_gcThreads[i]->slotVisitor()->bytesVisited();
return result;
}
size_t GCThreadSharedData::childBytesCopied()
{
size_t result = 0;
for (unsigned i = 0; i < m_gcThreads.size(); ++i)
result += m_gcThreads[i]->slotVisitor()->bytesCopied();
return result;
}
#endif
GCThreadSharedData::GCThreadSharedData(VM* vm)
: m_vm(vm)
, m_copiedSpace(&vm->heap.m_storageSpace)
, m_shouldHashCons(false)
, m_sharedMarkStack(vm->heap.blockAllocator())
, m_numberOfActiveParallelMarkers(0)
, m_parallelMarkersShouldExit(false)
, m_copyIndex(0)
, m_numberOfActiveGCThreads(0)
, m_gcThreadsShouldWait(false)
, m_currentPhase(NoPhase)
{
m_copyLock.Init();
#if ENABLE(PARALLEL_GC)
std::unique_lock<std::mutex> lock(m_phaseMutex);
for (unsigned i = 1; i < Options::numberOfGCMarkers(); ++i) {
m_numberOfActiveGCThreads++;
SlotVisitor* slotVisitor = new SlotVisitor(*this);
CopyVisitor* copyVisitor = new CopyVisitor(*this);
GCThread* newThread = new GCThread(*this, slotVisitor, copyVisitor);
ThreadIdentifier threadID = createThread(GCThread::gcThreadStartFunc, newThread, "JavaScriptCore::Marking");
newThread->initializeThreadID(threadID);
m_gcThreads.append(newThread);
}
m_activityConditionVariable.wait(lock, [this] { return !m_numberOfActiveGCThreads; });
#endif
}
GCThreadSharedData::~GCThreadSharedData()
{
#if ENABLE(PARALLEL_GC)
{
std::lock_guard<std::mutex> markingLock(m_markingMutex);
std::lock_guard<std::mutex> phaseLock(m_phaseMutex);
ASSERT(m_currentPhase == NoPhase);
m_parallelMarkersShouldExit = true;
m_gcThreadsShouldWait = false;
m_currentPhase = Exit;
m_phaseConditionVariable.notify_all();
}
for (unsigned i = 0; i < m_gcThreads.size(); ++i) {
waitForThreadCompletion(m_gcThreads[i]->threadID());
delete m_gcThreads[i];
}
#endif
}
void GCThreadSharedData::reset()
{
ASSERT(m_sharedMarkStack.isEmpty());
m_weakReferenceHarvesters.removeAll();
if (m_shouldHashCons) {
m_vm->resetNewStringsSinceLastHashCons();
m_shouldHashCons = false;
}
}
void GCThreadSharedData::startNextPhase(GCPhase phase)
{
std::lock_guard<std::mutex> lock(m_phaseMutex);
ASSERT(!m_gcThreadsShouldWait);
ASSERT(m_currentPhase == NoPhase);
m_gcThreadsShouldWait = true;
m_currentPhase = phase;
m_phaseConditionVariable.notify_all();
}
void GCThreadSharedData::endCurrentPhase()
{
ASSERT(m_gcThreadsShouldWait);
std::unique_lock<std::mutex> lock(m_phaseMutex);
m_currentPhase = NoPhase;
m_gcThreadsShouldWait = false;
m_phaseConditionVariable.notify_all();
m_activityConditionVariable.wait(lock, [this] { return !m_numberOfActiveGCThreads; });
}
void GCThreadSharedData::didStartMarking()
{
if (m_vm->heap.operationInProgress() == FullCollection) {
#if ENABLE(PARALLEL_GC)
m_opaqueRoots.clear();
#else
ASSERT(m_opaqueRoots.isEmpty());
#endif
}
std::lock_guard<std::mutex> lock(m_markingMutex);
m_parallelMarkersShouldExit = false;
startNextPhase(Mark);
}
void GCThreadSharedData::didFinishMarking()
{
{
std::lock_guard<std::mutex> lock(m_markingMutex);
m_parallelMarkersShouldExit = true;
m_markingConditionVariable.notify_all();
}
ASSERT(m_currentPhase == Mark);
endCurrentPhase();
}
void GCThreadSharedData::didStartCopying()
{
{
SpinLockHolder locker(&m_copyLock);
if (m_vm->heap.operationInProgress() == EdenCollection) {
m_blocksToCopy.shrink(0);
for (CopiedBlock* block = m_copiedSpace->m_newGen.fromSpace->head(); block; block = block->next())
m_blocksToCopy.append(block);
} else {
ASSERT(m_vm->heap.operationInProgress() == FullCollection);
WTF::copyToVector(m_copiedSpace->m_blockSet, m_blocksToCopy);
}
m_copyIndex = 0;
}
for (size_t i = 0; i < m_gcThreads.size(); i++)
m_gcThreads[i]->copyVisitor()->startCopying();
startNextPhase(Copy);
}
void GCThreadSharedData::didFinishCopying()
{
ASSERT(m_currentPhase == Copy);
endCurrentPhase();
}
}