WebKit Bugzilla
Attachment 359317 Details for
Bug 193417
: Use MonotonicTime in WorkerRunLoop
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch
bug-193417-20190116150616.patch (text/plain), 16.96 KB, created by
youenn fablet
on 2019-01-16 15:06:17 PST
(
hide
)
Description:
Patch
Filename:
MIME Type:
Creator:
youenn fablet
Created:
2019-01-16 15:06:17 PST
Size:
16.96 KB
patch
obsolete
>Subversion Revision: 240051 >diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog >index 121e6bfeea9fb9d01bb1fd785ebb2cbcdec10c72..eb0f430a5000085c48195f52bc742fb3a76007fc 100644 >--- a/Source/WTF/ChangeLog >+++ b/Source/WTF/ChangeLog >@@ -1,3 +1,14 @@ >+2019-01-16 Youenn Fablet <youenn@apple.com> >+ >+ Use MonotonicTime in WorkerRunLoop >+ https://bugs.webkit.org/show_bug.cgi?id=193417 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ * wtf/MessageQueue.h: >+ (WTF::MessageQueue<DataType>::waitForMessage): >+ (WTF::MessageQueue<DataType>::waitForMessageFilteredWithTimeout): >+ > 2019-01-16 Keith Miller <keith_miller@apple.com> > > bmalloc should use JSC VM tag for gigacage >diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog >index 8b3cef233bd6b951eccb0e2ca85b4a10071b436b..edb874282b1b2681c9f68c59f993aba2f9cf8dc4 100644 >--- a/Source/WebCore/ChangeLog >+++ b/Source/WebCore/ChangeLog >@@ -1,3 +1,17 @@ >+2019-01-16 Youenn Fablet <youenn@apple.com> >+ >+ Use MonotonicTime in WorkerRunLoop >+ https://bugs.webkit.org/show_bug.cgi?id=193417 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ Condition is based on MonotonicTime so MessageQueue should also be based on MonotonicTime. >+ Ditto for WorkerRunLoop. >+ No easy way to test the change which should not be easily observable. >+ >+ * workers/WorkerRunLoop.cpp: >+ (WebCore::WorkerRunLoop::runInMode): >+ > 2019-01-16 Youenn Fablet <youenn@apple.com> > > ServiceWorkerContainer is leaking due to a ref cycle >diff --git a/Source/WTF/wtf/MessageQueue.h b/Source/WTF/wtf/MessageQueue.h >index 04362a33861f5f0bf4e0def9175536a948d0009b..9d5950d4e04c99502ce04a363917989273e5e364 100644 >--- a/Source/WTF/wtf/MessageQueue.h >+++ b/Source/WTF/wtf/MessageQueue.h >@@ -34,8 +34,8 @@ > #include <wtf/Condition.h> > #include <wtf/Deque.h> > #include <wtf/Lock.h> >+#include <wtf/MonotonicTime.h> > #include <wtf/Noncopyable.h> >-#include <wtf/WallTime.h> > > namespace WTF { > >@@ -65,7 +65,7 @@ namespace WTF { > Deque<std::unique_ptr<DataType>> takeAllMessages(); > std::unique_ptr<DataType> tryGetMessageIgnoringKilled(); > template<typename Predicate> >- std::unique_ptr<DataType> waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, WallTime absoluteTime); >+ std::unique_ptr<DataType> waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, Seconds relativeTimeout); > > template<typename Predicate> > void removeIf(Predicate&&); >@@ -128,18 +128,19 @@ namespace WTF { > inline auto MessageQueue<DataType>::waitForMessage() -> std::unique_ptr<DataType> > { > MessageQueueWaitResult exitReason; >- std::unique_ptr<DataType> result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, WallTime::infinity()); >+ std::unique_ptr<DataType> result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, Seconds::infinity()); > ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueueMessageReceived); > return result; > } > > template<typename DataType> > template<typename Predicate> >- inline auto MessageQueue<DataType>::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, WallTime absoluteTime) -> std::unique_ptr<DataType> >+ inline auto MessageQueue<DataType>::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, Seconds relativeTimeout) -> std::unique_ptr<DataType> > { > LockHolder lock(m_mutex); > bool timedOut = false; > >+ MonotonicTime absoluteTimeout = MonotonicTime::now() + relativeTimeout; > auto found = m_queue.end(); > while (!m_killed && !timedOut) { > found = m_queue.findIf([&predicate](const std::unique_ptr<DataType>& ptr) -> bool { >@@ -149,10 +150,10 @@ namespace WTF { > if (found != m_queue.end()) > break; > >- timedOut = !m_condition.waitUntil(m_mutex, absoluteTime); >+ timedOut = !m_condition.waitUntil(m_mutex, absoluteTimeout); > } > >- ASSERT(!timedOut || absoluteTime != WallTime::infinity()); >+ ASSERT(!timedOut || absoluteTimeout != MonotonicTime::infinity()); > > if (m_killed) { > result = MessageQueueTerminated; >diff --git a/Source/WebCore/workers/WorkerRunLoop.cpp b/Source/WebCore/workers/WorkerRunLoop.cpp >index 8f7d2c6bdb04cc14d63bd42cf2ac135bcd2b1ffa..bcd94e6ac0403acd6a54bac41fab41e5bf5527da 100644 >--- a/Source/WebCore/workers/WorkerRunLoop.cpp >+++ b/Source/WebCore/workers/WorkerRunLoop.cpp >@@ -49,17 +49,17 @@ namespace WebCore { > class WorkerSharedTimer final : public SharedTimer { > public: > // SharedTimer interface. >- void setFiredFunction(WTF::Function<void()>&& function) override { m_sharedTimerFunction = WTFMove(function); } >- void setFireInterval(Seconds interval) override { m_nextFireTime = interval + WallTime::now(); } >- void stop() override { m_nextFireTime = WallTime(); } >+ void setFiredFunction(WTF::Function<void()>&& function) final { m_sharedTimerFunction = WTFMove(function); } >+ void setFireInterval(Seconds interval) final { m_nextFireTime = MonotonicTime::now() + interval; } >+ void stop() final { m_nextFireTime = MonotonicTime { }; } > > bool isActive() { return m_sharedTimerFunction && m_nextFireTime; } >- WallTime fireTime() { return m_nextFireTime; } >+ Seconds fireTimeDelay() { return std::max(0_s, m_nextFireTime - MonotonicTime::now()); } > void fire() { m_sharedTimerFunction(); } > > private: > WTF::Function<void()> m_sharedTimerFunction; >- WallTime m_nextFireTime; >+ MonotonicTime m_nextFireTime; > }; > > class ModePredicate { >@@ -176,28 +176,23 @@ MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerGlobalScope* context, cons > g_main_context_iteration(mainContext, FALSE); > #endif > >- WallTime deadline = WallTime::infinity(); >+ Seconds timeoutDelay = Seconds::infinity(); > > #if USE(CF) > CFAbsoluteTime nextCFRunLoopTimerFireDate = CFRunLoopGetNextTimerFireDate(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); > double timeUntilNextCFRunLoopTimerInSeconds = nextCFRunLoopTimerFireDate - CFAbsoluteTimeGetCurrent(); >- deadline = WallTime::now() + std::max(0_s, Seconds(timeUntilNextCFRunLoopTimerInSeconds)); >+ timeoutDelay = std::max(0_s, Seconds(timeUntilNextCFRunLoopTimerInSeconds)); > #endif > >- WallTime absoluteTime; >- if (waitMode == WaitForMessage) { >- if (predicate.isDefaultMode() && m_sharedTimer->isActive()) >- absoluteTime = std::min(deadline, m_sharedTimer->fireTime()); >- else >- absoluteTime = deadline; >- } >+ if (waitMode == WaitForMessage && predicate.isDefaultMode() && m_sharedTimer->isActive()) >+ timeoutDelay = std::min(timeoutDelay, m_sharedTimer->fireTimeDelay()); > > if (WorkerScriptController* script = context->script()) { > script->releaseHeapAccess(); > script->addTimerSetNotification(timerAddedTask); > } > MessageQueueWaitResult result; >- auto task = m_messageQueue.waitForMessageFilteredWithTimeout(result, predicate, absoluteTime); >+ auto task = m_messageQueue.waitForMessageFilteredWithTimeout(result, predicate, timeoutDelay); > if (WorkerScriptController* script = context->script()) { > script->acquireHeapAccess(); > script->removeTimerSetNotification(timerAddedTask); >diff --git a/Source/WebCore/workers/WorkerRunLoop.cpp.orig b/Source/WebCore/workers/WorkerRunLoop.cpp.orig >new file mode 100644 >index 0000000000000000000000000000000000000000..8f7d2c6bdb04cc14d63bd42cf2ac135bcd2b1ffa >--- /dev/null >+++ b/Source/WebCore/workers/WorkerRunLoop.cpp.orig >@@ -0,0 +1,283 @@ >+/* >+ * Copyright (C) 2009 Google Inc. All rights reserved. >+ * Copyright (C) 2016-2017 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: >+ * >+ * * Redistributions of source code must retain the above copyright >+ * notice, this list of conditions and the following disclaimer. >+ * * 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. >+ * * Neither the name of Google Inc. nor the names of its >+ * contributors may be used to endorse or promote products derived from >+ * this software without specific prior written permission. >+ * >+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS >+ * "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 THE COPYRIGHT >+ * OWNER 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 "ScriptExecutionContext.h" >+#include "SharedTimer.h" >+#include "ThreadGlobalData.h" >+#include "ThreadTimers.h" >+#include "WorkerRunLoop.h" >+#include "WorkerGlobalScope.h" >+#include "WorkerThread.h" >+#include <JavaScriptCore/PromiseDeferredTimer.h> >+ >+#if USE(GLIB) >+#include <glib.h> >+#endif >+ >+namespace WebCore { >+ >+class WorkerSharedTimer final : public SharedTimer { >+public: >+ // SharedTimer interface. >+ void setFiredFunction(WTF::Function<void()>&& function) override { m_sharedTimerFunction = WTFMove(function); } >+ void setFireInterval(Seconds interval) override { m_nextFireTime = interval + WallTime::now(); } >+ void stop() override { m_nextFireTime = WallTime(); } >+ >+ bool isActive() { return m_sharedTimerFunction && m_nextFireTime; } >+ WallTime fireTime() { return m_nextFireTime; } >+ void fire() { m_sharedTimerFunction(); } >+ >+private: >+ WTF::Function<void()> m_sharedTimerFunction; >+ WallTime m_nextFireTime; >+}; >+ >+class ModePredicate { >+public: >+ explicit ModePredicate(String&& mode) >+ : m_mode(WTFMove(mode)) >+ , m_defaultMode(m_mode == WorkerRunLoop::defaultMode()) >+ { >+ } >+ >+ bool isDefaultMode() const >+ { >+ return m_defaultMode; >+ } >+ >+ bool operator()(const WorkerRunLoop::Task& task) const >+ { >+ return m_defaultMode || m_mode == task.mode(); >+ } >+ >+private: >+ String m_mode; >+ bool m_defaultMode; >+}; >+ >+WorkerRunLoop::WorkerRunLoop() >+ : m_sharedTimer(std::make_unique<WorkerSharedTimer>()) >+{ >+} >+ >+WorkerRunLoop::~WorkerRunLoop() >+{ >+ ASSERT(!m_nestedCount); >+} >+ >+String WorkerRunLoop::defaultMode() >+{ >+ return String(); >+} >+ >+static String debuggerMode() >+{ >+ return "debugger"_s; >+} >+ >+class RunLoopSetup { >+ WTF_MAKE_NONCOPYABLE(RunLoopSetup); >+public: >+ enum class IsForDebugging { No, Yes }; >+ RunLoopSetup(WorkerRunLoop& runLoop, IsForDebugging isForDebugging) >+ : m_runLoop(runLoop) >+ , m_isForDebugging(isForDebugging) >+ { >+ if (!m_runLoop.m_nestedCount) >+ threadGlobalData().threadTimers().setSharedTimer(m_runLoop.m_sharedTimer.get()); >+ m_runLoop.m_nestedCount++; >+ if (m_isForDebugging == IsForDebugging::Yes) >+ m_runLoop.m_debugCount++; >+ } >+ >+ ~RunLoopSetup() >+ { >+ m_runLoop.m_nestedCount--; >+ if (!m_runLoop.m_nestedCount) >+ threadGlobalData().threadTimers().setSharedTimer(nullptr); >+ if (m_isForDebugging == IsForDebugging::Yes) >+ m_runLoop.m_debugCount--; >+ } >+private: >+ WorkerRunLoop& m_runLoop; >+ IsForDebugging m_isForDebugging { IsForDebugging::No }; >+}; >+ >+void WorkerRunLoop::run(WorkerGlobalScope* context) >+{ >+ RunLoopSetup setup(*this, RunLoopSetup::IsForDebugging::No); >+ ModePredicate modePredicate(defaultMode()); >+ MessageQueueWaitResult result; >+ do { >+ result = runInMode(context, modePredicate, WaitForMessage); >+ } while (result != MessageQueueTerminated); >+ runCleanupTasks(context); >+} >+ >+MessageQueueWaitResult WorkerRunLoop::runInDebuggerMode(WorkerGlobalScope& context) >+{ >+ RunLoopSetup setup(*this, RunLoopSetup::IsForDebugging::Yes); >+ return runInMode(&context, ModePredicate { debuggerMode() }, WaitForMessage); >+} >+ >+MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerGlobalScope* context, const String& mode, WaitMode waitMode) >+{ >+ ASSERT(mode != debuggerMode()); >+ RunLoopSetup setup(*this, RunLoopSetup::IsForDebugging::No); >+ ModePredicate modePredicate(String { mode }); >+ MessageQueueWaitResult result = runInMode(context, modePredicate, waitMode); >+ return result; >+} >+ >+MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerGlobalScope* context, const ModePredicate& predicate, WaitMode waitMode) >+{ >+ ASSERT(context); >+ ASSERT(context->thread().thread() == &Thread::current()); >+ >+ JSC::JSRunLoopTimer::TimerNotificationCallback timerAddedTask = WTF::createSharedTask<JSC::JSRunLoopTimer::TimerNotificationType>([this] { >+ // We don't actually do anything here, we just want to loop around runInMode >+ // to both recalculate our deadline and to potentially run the run loop. >+ this->postTask([](ScriptExecutionContext&) { }); >+ }); >+ >+#if USE(GLIB) >+ GMainContext* mainContext = g_main_context_get_thread_default(); >+ if (g_main_context_pending(mainContext)) >+ g_main_context_iteration(mainContext, FALSE); >+#endif >+ >+ WallTime deadline = WallTime::infinity(); >+ >+#if USE(CF) >+ CFAbsoluteTime nextCFRunLoopTimerFireDate = CFRunLoopGetNextTimerFireDate(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); >+ double timeUntilNextCFRunLoopTimerInSeconds = nextCFRunLoopTimerFireDate - CFAbsoluteTimeGetCurrent(); >+ deadline = WallTime::now() + std::max(0_s, Seconds(timeUntilNextCFRunLoopTimerInSeconds)); >+#endif >+ >+ WallTime absoluteTime; >+ if (waitMode == WaitForMessage) { >+ if (predicate.isDefaultMode() && m_sharedTimer->isActive()) >+ absoluteTime = std::min(deadline, m_sharedTimer->fireTime()); >+ else >+ absoluteTime = deadline; >+ } >+ >+ if (WorkerScriptController* script = context->script()) { >+ script->releaseHeapAccess(); >+ script->addTimerSetNotification(timerAddedTask); >+ } >+ MessageQueueWaitResult result; >+ auto task = m_messageQueue.waitForMessageFilteredWithTimeout(result, predicate, absoluteTime); >+ if (WorkerScriptController* script = context->script()) { >+ script->acquireHeapAccess(); >+ script->removeTimerSetNotification(timerAddedTask); >+ } >+ >+ // If the context is closing, don't execute any further JavaScript tasks (per section 4.1.1 of the Web Workers spec). However, there may be implementation cleanup tasks in the queue, so keep running through it. >+ >+ switch (result) { >+ case MessageQueueTerminated: >+ break; >+ >+ case MessageQueueMessageReceived: >+ task->performTask(context); >+ break; >+ >+ case MessageQueueTimeout: >+ if (!context->isClosing() && !isBeingDebugged()) >+ m_sharedTimer->fire(); >+ break; >+ } >+ >+#if USE(CF) >+ if (result != MessageQueueTerminated) { >+ if (nextCFRunLoopTimerFireDate <= CFAbsoluteTimeGetCurrent()) >+ CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, /*returnAfterSourceHandled*/ false); >+ } >+#endif >+ >+ return result; >+} >+ >+void WorkerRunLoop::runCleanupTasks(WorkerGlobalScope* context) >+{ >+ ASSERT(context); >+ ASSERT(context->thread().thread() == &Thread::current()); >+ ASSERT(m_messageQueue.killed()); >+ >+ while (true) { >+ auto task = m_messageQueue.tryGetMessageIgnoringKilled(); >+ if (!task) >+ return; >+ task->performTask(context); >+ } >+} >+ >+void WorkerRunLoop::terminate() >+{ >+ m_messageQueue.kill(); >+} >+ >+void WorkerRunLoop::postTask(ScriptExecutionContext::Task&& task) >+{ >+ postTaskForMode(WTFMove(task), defaultMode()); >+} >+ >+void WorkerRunLoop::postTaskAndTerminate(ScriptExecutionContext::Task&& task) >+{ >+ m_messageQueue.appendAndKill(std::make_unique<Task>(WTFMove(task), defaultMode())); >+} >+ >+void WorkerRunLoop::postTaskForMode(ScriptExecutionContext::Task&& task, const String& mode) >+{ >+ m_messageQueue.append(std::make_unique<Task>(WTFMove(task), mode)); >+} >+ >+void WorkerRunLoop::postDebuggerTask(ScriptExecutionContext::Task&& task) >+{ >+ postTaskForMode(WTFMove(task), debuggerMode()); >+} >+ >+void WorkerRunLoop::Task::performTask(WorkerGlobalScope* context) >+{ >+ if ((!context->isClosing() && context->script() && !context->script()->isTerminatingExecution()) || m_task.isCleanupTask()) >+ m_task.performTask(*context); >+} >+ >+WorkerRunLoop::Task::Task(ScriptExecutionContext::Task&& task, const String& mode) >+ : m_task(WTFMove(task)) >+ , m_mode(mode.isolatedCopy()) >+{ >+} >+ >+} // namespace WebCore
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 193417
:
359109
|
359317
|
359330