From 597984021ca00fd98a0dfe2effd742c6e7bd4190 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 14 Mar 2013 14:10:22 +0100 Subject: [Qt] Implement IncrementalSweeper and HeapTimer https://bugs.webkit.org/show_bug.cgi?id=103996 Reviewed by Simon Hausmann. Implements the incremental sweeping garbage collection for the Qt platform. * heap/HeapTimer.cpp: (JSC::HeapTimer::HeapTimer): (JSC::HeapTimer::~HeapTimer): (JSC::HeapTimer::timerEvent): (JSC::HeapTimer::synchronize): (JSC::HeapTimer::invalidate): (JSC::HeapTimer::didStartVMShutdown): * heap/HeapTimer.h: (HeapTimer): * heap/IncrementalSweeper.cpp: (JSC::IncrementalSweeper::IncrementalSweeper): (JSC::IncrementalSweeper::scheduleTimer): * heap/IncrementalSweeper.h: (IncrementalSweeper): Change-Id: I47b874c050e08519cf5e3ed5a98a98ac8785971f git-svn-id: http://svn.webkit.org/repository/webkit/trunk@141089 268f45cc-cd09-0410-ab3c-d52691b4dbfc Reviewed-by: Jocelyn Turcotte --- Source/JavaScriptCore/heap/HeapTimer.cpp | 71 ++++++++++++++++++++++- Source/JavaScriptCore/heap/HeapTimer.h | 17 +++++- Source/JavaScriptCore/heap/IncrementalSweeper.cpp | 9 ++- Source/JavaScriptCore/heap/IncrementalSweeper.h | 2 +- 4 files changed, 94 insertions(+), 5 deletions(-) (limited to 'Source') diff --git a/Source/JavaScriptCore/heap/HeapTimer.cpp b/Source/JavaScriptCore/heap/HeapTimer.cpp index 214f86757..d69ee9607 100644 --- a/Source/JavaScriptCore/heap/HeapTimer.cpp +++ b/Source/JavaScriptCore/heap/HeapTimer.cpp @@ -33,6 +33,13 @@ #include #include +#if PLATFORM(QT) +#include +#include +#include +#include +#endif + namespace JSC { #if USE(CF) @@ -132,8 +139,70 @@ void HeapTimer::didStartVMShutdown() delete this; } -#else +#elif PLATFORM(QT) + +HeapTimer::HeapTimer(JSGlobalData* globalData) + : m_globalData(globalData) + , m_newThread(0) + , m_mutex(QMutex::NonRecursive) +{ + // The HeapTimer might be created before the runLoop is started, + // but we need to ensure the thread has an eventDispatcher already. + QEventLoop fakeLoop(this); +} + +HeapTimer::~HeapTimer() +{ +} + +void HeapTimer::timerEvent(QTimerEvent*) +{ + QMutexLocker lock(&m_mutex); + if (m_newThread) { + // We need to wait with processing until we are on the right thread. + return; + } + + APIEntryShim shim(m_globalData, APIEntryShimWithoutLock::DontRefGlobalData); + doWork(); +} +void HeapTimer::customEvent(QEvent*) +{ + ASSERT(m_newThread); + QMutexLocker lock(&m_mutex); + moveToThread(m_newThread); + m_newThread = 0; +} + +void HeapTimer::synchronize() +{ + if (thread() != QThread::currentThread()) { + // We can only move from the objects own thread to another, so we fire an + // event into the owning thread to trigger the move. + // This must be processed before any timerEvents so giving it high priority. + QMutexLocker lock(&m_mutex); + m_newThread = QThread::currentThread(); + QCoreApplication::postEvent(this, new QEvent(QEvent::User), Qt::HighEventPriority); + } +} + +void HeapTimer::invalidate() +{ + QMutexLocker lock(&m_mutex); + m_timer.stop(); +} + +void HeapTimer::didStartVMShutdown() +{ + invalidate(); + if (thread() == QThread::currentThread()) + delete this; + else + deleteLater(); +} + +#else HeapTimer::HeapTimer(JSGlobalData* globalData) : m_globalData(globalData) { diff --git a/Source/JavaScriptCore/heap/HeapTimer.h b/Source/JavaScriptCore/heap/HeapTimer.h index 88715098a..e0fcda672 100644 --- a/Source/JavaScriptCore/heap/HeapTimer.h +++ b/Source/JavaScriptCore/heap/HeapTimer.h @@ -33,13 +33,22 @@ #include #elif PLATFORM(BLACKBERRY) #include +#elif PLATFORM(QT) +#include +#include +#include +#include #endif namespace JSC { class JSGlobalData; - + +#if PLATFORM(QT) +class HeapTimer : public QObject { +#else class HeapTimer { +#endif public: #if USE(CF) HeapTimer(JSGlobalData*, CFRunLoopRef); @@ -69,6 +78,12 @@ protected: void timerDidFire(); BlackBerry::Platform::Timer m_timer; +#elif PLATFORM(QT) + void timerEvent(QTimerEvent*); + void customEvent(QEvent*); + QBasicTimer m_timer; + QThread* m_newThread; + QMutex m_mutex; #endif private: diff --git a/Source/JavaScriptCore/heap/IncrementalSweeper.cpp b/Source/JavaScriptCore/heap/IncrementalSweeper.cpp index 4aec4dd51..41bc7f5e4 100644 --- a/Source/JavaScriptCore/heap/IncrementalSweeper.cpp +++ b/Source/JavaScriptCore/heap/IncrementalSweeper.cpp @@ -37,7 +37,7 @@ namespace JSC { -#if USE(CF) || PLATFORM(BLACKBERRY) +#if USE(CF) || PLATFORM(BLACKBERRY) || PLATFORM(QT) static const double sweepTimeSlice = .01; // seconds static const double sweepTimeTotal = .10; @@ -67,11 +67,12 @@ void IncrementalSweeper::cancelTimer() CFRunLoopTimerSetNextFireDate(m_timer.get(), CFAbsoluteTimeGetCurrent() + s_decade); } -#elif PLATFORM(BLACKBERRY) +#elif PLATFORM(BLACKBERRY) || PLATFORM(QT) IncrementalSweeper::IncrementalSweeper(Heap* heap) : HeapTimer(heap->globalData()) , m_currentBlockToSweepIndex(0) + , m_blocksToSweep(heap->m_blockSnapshot) { } @@ -82,7 +83,11 @@ IncrementalSweeper* IncrementalSweeper::create(Heap* heap) void IncrementalSweeper::scheduleTimer() { +#if PLATFORM(QT) + m_timer.start(sweepTimeSlice * sweepTimeMultiplier * 1000, this); +#else m_timer.start(sweepTimeSlice * sweepTimeMultiplier); +#endif } void IncrementalSweeper::cancelTimer() diff --git a/Source/JavaScriptCore/heap/IncrementalSweeper.h b/Source/JavaScriptCore/heap/IncrementalSweeper.h index 5b9267bc7..7b0ae99ab 100644 --- a/Source/JavaScriptCore/heap/IncrementalSweeper.h +++ b/Source/JavaScriptCore/heap/IncrementalSweeper.h @@ -46,7 +46,7 @@ public: void willFinishSweeping(); private: -#if USE(CF) || PLATFORM(BLACKBERRY) +#if USE(CF) || PLATFORM(BLACKBERRY) || PLATFORM(QT) #if USE(CF) IncrementalSweeper(Heap*, CFRunLoopRef); #else -- cgit v1.2.1