Skip to content

Commit 76a4aa2

Browse files
committed
Implement timers by posting delayed tasks
This patch refactors TimerBase to post tasks delayed tasks and deletes the now-obsolete timer heap and shared timer mechanism. ATTN Sheriffs: If there are weird layout test flakes all of a sudden, this patch may be the cause since the interleaving of timers with other posted tasks will change. Original patch by Alex Clarke <alexclarke@chromium.org>. BUG=463143,416362,480522 Review URL: https://codereview.chromium.org/1134523002 git-svn-id: svn://svn.chromium.org/blink/trunk@195706 bbb929c8-8fbe-4397-9dbb-9b2b20218538
1 parent 50c4cd2 commit 76a4aa2

20 files changed

+238
-998
lines changed

third_party/WebKit/Source/core/Init.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include "core/workers/WorkerThread.h"
5555
#include "platform/EventTracer.h"
5656
#include "platform/FontFamilyNames.h"
57-
#include "platform/PlatformThreadData.h"
5857
#include "platform/weborigin/KURL.h"
5958
#include "platform/weborigin/SecurityPolicy.h"
6059
#include "wtf/Partitions.h"
@@ -110,10 +109,6 @@ void CoreInitializer::init()
110109

111110
registerEventFactory();
112111

113-
// Ensure that the main thread's thread-local data is initialized before
114-
// starting any worker threads.
115-
PlatformThreadData::current();
116-
117112
StringImpl::freezeStaticStrings();
118113

119114
// Creates HTMLParserThread::shared and ScriptStreamerThread::shared, but

third_party/WebKit/Source/core/animation/CompositorAnimationsTestHelper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class AnimationCompositorAnimationsTestBase : public ::testing::Test {
142142
private:
143143
class PlatformProxy : public Platform {
144144
public:
145-
PlatformProxy(WebCompositorSupportMock** compositor) : m_compositor(compositor) { }
145+
PlatformProxy(WebCompositorSupportMock** compositor) : m_oldPlatform(blink::Platform::current()), m_compositor(compositor) { }
146146

147147
virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t length) { ASSERT_NOT_REACHED(); }
148148
const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) override
@@ -151,7 +151,13 @@ class AnimationCompositorAnimationsTestBase : public ::testing::Test {
151151
return &tracingIsDisabled;
152152
}
153153

154+
WebThread* currentThread() override
155+
{
156+
return m_oldPlatform->currentThread();
157+
}
158+
154159
private:
160+
blink::Platform* m_oldPlatform; // Not owned.
155161
WebCompositorSupportMock** m_compositor;
156162
virtual WebCompositorSupport* compositorSupport() override { return *m_compositor; }
157163
};

third_party/WebKit/Source/core/fetch/CachingCorrectnessTest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class CachingCorrectnessTest : public ::testing::Test {
124124
// A simple platform that mocks out the clock, for cache freshness testing.
125125
class ProxyPlatform : public blink::Platform {
126126
public:
127-
ProxyPlatform() : m_elapsedSeconds(0.) { }
127+
ProxyPlatform() : m_oldPlatform(blink::Platform::current()), m_elapsedSeconds(0.) { }
128128

129129
void advanceClock(double seconds)
130130
{
@@ -145,6 +145,12 @@ class CachingCorrectnessTest : public ::testing::Test {
145145
return &kAConstUnsignedCharZero;
146146
}
147147

148+
WebThread* currentThread() override
149+
{
150+
return m_oldPlatform->currentThread();
151+
}
152+
153+
blink::Platform* m_oldPlatform; // Not owned.
148154
double m_elapsedSeconds;
149155
};
150156

third_party/WebKit/Source/core/fetch/ResourceTest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace {
2222

2323
class MockPlatform final : public Platform {
2424
public:
25-
MockPlatform() { }
25+
MockPlatform() : m_oldPlatform(Platform::current()) { }
2626
~MockPlatform() override { }
2727

2828
// From blink::Platform:
@@ -40,7 +40,13 @@ class MockPlatform final : public Platform {
4040
return m_cachedURLs;
4141
}
4242

43+
WebThread* currentThread() override
44+
{
45+
return m_oldPlatform->currentThread();
46+
}
47+
4348
private:
49+
Platform* m_oldPlatform; // Not owned.
4450
Vector<WebURL> m_cachedURLs;
4551
};
4652

third_party/WebKit/Source/core/frame/DOMTimerCoordinator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ void DOMTimerCoordinator::removeTimeoutByID(int timeoutID)
4343

4444
void DOMTimerCoordinator::didChangeTimerAlignmentInterval()
4545
{
46+
double now = monotonicallyIncreasingTime();
4647
for (TimeoutMap::iterator iter = m_timers.begin(); iter != m_timers.end(); ++iter)
47-
iter->value->didChangeAlignmentInterval();
48+
iter->value->didChangeAlignmentInterval(now);
4849
}
4950

5051
DEFINE_TRACE(DOMTimerCoordinator)

third_party/WebKit/Source/core/workers/WorkerThread.cpp

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@
3838
#include "core/workers/WorkerClients.h"
3939
#include "core/workers/WorkerReportingProxy.h"
4040
#include "core/workers/WorkerThreadStartupData.h"
41-
#include "platform/PlatformThreadData.h"
4241
#include "platform/Task.h"
4342
#include "platform/ThreadSafeFunctional.h"
44-
#include "platform/ThreadTimers.h"
4543
#include "platform/heap/SafePoint.h"
4644
#include "platform/heap/ThreadState.h"
4745
#include "platform/weborigin/KURL.h"
@@ -111,100 +109,6 @@ unsigned WorkerThread::workerThreadCount()
111109
return workerThreads().size();
112110
}
113111

114-
class WorkerThreadCancelableTask final : public ExecutionContextTask {
115-
WTF_MAKE_NONCOPYABLE(WorkerThreadCancelableTask); WTF_MAKE_FAST_ALLOCATED(WorkerThreadCancelableTask);
116-
public:
117-
static PassOwnPtr<WorkerThreadCancelableTask> create(PassOwnPtr<Closure> closure)
118-
{
119-
return adoptPtr(new WorkerThreadCancelableTask(closure));
120-
}
121-
122-
virtual ~WorkerThreadCancelableTask() { }
123-
124-
virtual void performTask(ExecutionContext*) override
125-
{
126-
if (!m_taskCanceled)
127-
(*m_closure)();
128-
}
129-
130-
WeakPtr<WorkerThreadCancelableTask> createWeakPtr() { return m_weakFactory.createWeakPtr(); }
131-
void cancelTask() { m_taskCanceled = true; }
132-
133-
private:
134-
explicit WorkerThreadCancelableTask(PassOwnPtr<Closure> closure)
135-
: m_closure(closure)
136-
, m_weakFactory(this)
137-
, m_taskCanceled(false)
138-
{ }
139-
140-
OwnPtr<Closure> m_closure;
141-
WeakPtrFactory<WorkerThreadCancelableTask> m_weakFactory;
142-
bool m_taskCanceled;
143-
};
144-
145-
class WorkerSharedTimer : public SharedTimer {
146-
public:
147-
explicit WorkerSharedTimer(WorkerThread* workerThread)
148-
: m_workerThread(workerThread)
149-
, m_running(false)
150-
{ }
151-
152-
typedef void (*SharedTimerFunction)();
153-
virtual void setFiredFunction(SharedTimerFunction func)
154-
{
155-
m_sharedTimerFunction = func;
156-
}
157-
158-
virtual void setFireInterval(double interval)
159-
{
160-
ASSERT(m_sharedTimerFunction);
161-
162-
// See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of
163-
// why ceil is used in the interval calculation.
164-
int64_t delay = static_cast<int64_t>(ceil(interval * 1000));
165-
166-
if (delay < 0) {
167-
delay = 0;
168-
}
169-
170-
m_running = true;
171-
172-
if (m_lastQueuedTask.get())
173-
m_lastQueuedTask->cancelTask();
174-
175-
// Now queue the task as a cancellable one.
176-
OwnPtr<WorkerThreadCancelableTask> task = WorkerThreadCancelableTask::create(bind(&WorkerSharedTimer::OnTimeout, this));
177-
m_lastQueuedTask = task->createWeakPtr();
178-
m_workerThread->postDelayedTask(FROM_HERE, task.release(), delay);
179-
}
180-
181-
virtual void stop()
182-
{
183-
m_running = false;
184-
m_lastQueuedTask = nullptr;
185-
}
186-
187-
private:
188-
void OnTimeout()
189-
{
190-
ASSERT(m_workerThread->workerGlobalScope());
191-
192-
m_lastQueuedTask = nullptr;
193-
194-
if (m_sharedTimerFunction && m_running && !m_workerThread->workerGlobalScope()->isClosing())
195-
m_sharedTimerFunction();
196-
}
197-
198-
WorkerThread* m_workerThread;
199-
SharedTimerFunction m_sharedTimerFunction;
200-
bool m_running;
201-
202-
// The task to run OnTimeout, if any. While OnTimeout resets
203-
// m_lastQueuedTask, this must be a weak pointer because the
204-
// worker runloop may delete the task as it is shutting down.
205-
WeakPtr<WorkerThreadCancelableTask> m_lastQueuedTask;
206-
};
207-
208112
class WorkerThreadTask : public WebThread::Task {
209113
WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED(WorkerThreadTask);
210114
public:
@@ -321,8 +225,6 @@ void WorkerThread::initialize(PassOwnPtr<WorkerThreadStartupData> startupData)
321225
m_isolate = initializeIsolate();
322226
m_workerGlobalScope = createWorkerGlobalScope(startupData);
323227
m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.get() ? cachedMetaData->size() : 0);
324-
325-
PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this)));
326228
}
327229

328230
// The corresponding call to stopRunLoop() is in ~WorkerScriptController().
@@ -356,7 +258,6 @@ void WorkerThread::shutdown()
356258
m_shutdown = true;
357259
}
358260

359-
PlatformThreadData::current().threadTimers().setSharedTimer(nullptr);
360261
workerGlobalScope()->dispose();
361262
willDestroyIsolate();
362263

@@ -383,9 +284,6 @@ void WorkerThread::shutdown()
383284
workerReportingProxy().workerThreadTerminated();
384285

385286
m_terminationEvent->signal();
386-
387-
// Clean up PlatformThreadData before WTF::WTFThreadData goes away!
388-
PlatformThreadData::current().destroy();
389287
}
390288

391289

third_party/WebKit/Source/core/workers/WorkerThread.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class WorkerGlobalScope;
4848
class WorkerInspectorController;
4949
class WorkerMicrotaskRunner;
5050
class WorkerReportingProxy;
51-
class WorkerSharedTimer;
5251
class WorkerThreadStartupData;
5352

5453
enum WorkerThreadStartMode {
@@ -131,7 +130,6 @@ class CORE_EXPORT WorkerThread : public RefCounted<WorkerThread> {
131130
virtual bool doIdleGc(double deadlineSeconds);
132131

133132
private:
134-
friend class WorkerSharedTimer;
135133
friend class WorkerMicrotaskRunner;
136134

137135
void stopInShutdownSequence();

third_party/WebKit/Source/platform/PlatformThreadData.cpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

third_party/WebKit/Source/platform/PlatformThreadData.h

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)