| Index: third_party/WebKit/Source/core/workers/WorkerThread.cpp
|
| diff --git a/third_party/WebKit/Source/core/workers/WorkerThread.cpp b/third_party/WebKit/Source/core/workers/WorkerThread.cpp
|
| index f572f098ff5184736bc4801bc5a96fe60a40ef74..8db289779653866c92671a68b4002e81ee1051b8 100644
|
| --- a/third_party/WebKit/Source/core/workers/WorkerThread.cpp
|
| +++ b/third_party/WebKit/Source/core/workers/WorkerThread.cpp
|
| @@ -47,7 +47,6 @@
|
| #include "platform/WebThreadSupportingGC.h"
|
| #include "platform/heap/SafePoint.h"
|
| #include "platform/heap/ThreadState.h"
|
| -#include "platform/scheduler/CancellableTaskFactory.h"
|
| #include "platform/weborigin/KURL.h"
|
| #include "wtf/Functional.h"
|
| #include "wtf/Noncopyable.h"
|
| @@ -62,55 +61,7 @@ namespace blink {
|
| using ExitCode = WorkerThread::ExitCode;
|
|
|
| // TODO(nhiroki): Adjust the delay based on UMA.
|
| -const long long kForceTerminationDelayInMs = 2000; // 2 secs
|
| -
|
| -// ForceTerminationTask is used for posting a delayed task to terminate the
|
| -// worker execution from the main thread. This task is expected to run when the
|
| -// shutdown sequence does not start in a certain time period because of an
|
| -// inifite loop in the JS execution context etc. When the shutdown sequence is
|
| -// started before this task runs, the task is simply cancelled.
|
| -class WorkerThread::ForceTerminationTask final {
|
| - public:
|
| - static std::unique_ptr<ForceTerminationTask> create(
|
| - WorkerThread* workerThread) {
|
| - return wrapUnique(new ForceTerminationTask(workerThread));
|
| - }
|
| -
|
| - void schedule() {
|
| - DCHECK(isMainThread());
|
| - Platform::current()->mainThread()->getWebTaskRunner()->postDelayedTask(
|
| - BLINK_FROM_HERE, m_cancellableTaskFactory->cancelAndCreate(),
|
| - m_workerThread->m_forceTerminationDelayInMs);
|
| - }
|
| -
|
| - private:
|
| - explicit ForceTerminationTask(WorkerThread* workerThread)
|
| - : m_workerThread(workerThread) {
|
| - DCHECK(isMainThread());
|
| - m_cancellableTaskFactory =
|
| - CancellableTaskFactory::create(this, &ForceTerminationTask::run);
|
| - }
|
| -
|
| - void run() {
|
| - DCHECK(isMainThread());
|
| - MutexLocker lock(m_workerThread->m_threadStateMutex);
|
| - if (m_workerThread->m_threadState == ThreadState::ReadyToShutdown) {
|
| - // Shutdown sequence is now running. Just return.
|
| - return;
|
| - }
|
| - if (m_workerThread->m_runningDebuggerTask) {
|
| - // Any debugger task is guaranteed to finish, so we can wait for the
|
| - // completion. Shutdown sequence will start after that.
|
| - return;
|
| - }
|
| -
|
| - m_workerThread->forciblyTerminateExecution(
|
| - lock, ExitCode::AsyncForciblyTerminated);
|
| - }
|
| -
|
| - WorkerThread* m_workerThread;
|
| - std::unique_ptr<CancellableTaskFactory> m_cancellableTaskFactory;
|
| -};
|
| +const long long kForcibleTerminationDelayInMs = 2000; // 2 secs
|
|
|
| static Mutex& threadSetMutex() {
|
| DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex);
|
| @@ -344,7 +295,7 @@ bool WorkerThread::isForciblyTerminated() {
|
| WorkerThread::WorkerThread(PassRefPtr<WorkerLoaderProxy> workerLoaderProxy,
|
| WorkerReportingProxy& workerReportingProxy)
|
| : m_workerThreadId(getNextWorkerThreadId()),
|
| - m_forceTerminationDelayInMs(kForceTerminationDelayInMs),
|
| + m_forcibleTerminationDelayInMs(kForcibleTerminationDelayInMs),
|
| m_inspectorTaskRunner(wrapUnique(new InspectorTaskRunner())),
|
| m_workerLoaderProxy(workerLoaderProxy),
|
| m_workerReportingProxy(workerReportingProxy),
|
| @@ -376,7 +327,7 @@ void WorkerThread::terminateInternal(TerminationMode mode) {
|
| // for the completion even if the synchronous forcible
|
| // termination is requested. Shutdown sequence will start
|
| // after the task.
|
| - DCHECK(!m_scheduledForceTerminationTask);
|
| + DCHECK(!m_forcibleTerminationTaskHandle.isActive());
|
| return;
|
| }
|
|
|
| @@ -385,7 +336,7 @@ void WorkerThread::terminateInternal(TerminationMode mode) {
|
| // main thread and the scheduled termination task never runs.
|
| if (mode == TerminationMode::Forcible &&
|
| m_exitCode == ExitCode::NotTerminated) {
|
| - DCHECK(m_scheduledForceTerminationTask);
|
| + DCHECK(m_forcibleTerminationTaskHandle.isActive());
|
| forciblyTerminateExecution(lock, ExitCode::SyncForciblyTerminated);
|
| }
|
| return;
|
| @@ -398,9 +349,16 @@ void WorkerThread::terminateInternal(TerminationMode mode) {
|
| forciblyTerminateExecution(lock, ExitCode::SyncForciblyTerminated);
|
| break;
|
| case TerminationMode::Graceful:
|
| - DCHECK(!m_scheduledForceTerminationTask);
|
| - m_scheduledForceTerminationTask = ForceTerminationTask::create(this);
|
| - m_scheduledForceTerminationTask->schedule();
|
| + DCHECK(!m_forcibleTerminationTaskHandle.isActive());
|
| + m_forcibleTerminationTaskHandle =
|
| + Platform::current()
|
| + ->mainThread()
|
| + ->getWebTaskRunner()
|
| + ->postDelayedCancellableTask(
|
| + BLINK_FROM_HERE,
|
| + WTF::bind(&WorkerThread::mayForciblyTerminateExecution,
|
| + WTF::unretained(this)),
|
| + m_forcibleTerminationDelayInMs);
|
| break;
|
| }
|
| }
|
| @@ -442,6 +400,22 @@ bool WorkerThread::shouldScheduleToTerminateExecution(const MutexLocker& lock) {
|
| return false;
|
| }
|
|
|
| +void WorkerThread::mayForciblyTerminateExecution() {
|
| + DCHECK(isMainThread());
|
| + MutexLocker lock(m_threadStateMutex);
|
| + if (m_threadState == ThreadState::ReadyToShutdown) {
|
| + // Shutdown sequence is now running. Just return.
|
| + return;
|
| + }
|
| + if (m_runningDebuggerTask) {
|
| + // Any debugger task is guaranteed to finish, so we can wait for the
|
| + // completion. Shutdown sequence will start after that.
|
| + return;
|
| + }
|
| +
|
| + forciblyTerminateExecution(lock, ExitCode::AsyncForciblyTerminated);
|
| +}
|
| +
|
| void WorkerThread::forciblyTerminateExecution(const MutexLocker& lock,
|
| ExitCode exitCode) {
|
| DCHECK(isMainThread());
|
| @@ -452,7 +426,7 @@ void WorkerThread::forciblyTerminateExecution(const MutexLocker& lock,
|
| setExitCode(lock, exitCode);
|
|
|
| isolate()->TerminateExecution();
|
| - m_scheduledForceTerminationTask.reset();
|
| + m_forcibleTerminationTaskHandle.cancel();
|
| }
|
|
|
| bool WorkerThread::isInShutdown() {
|
|
|