Skip to content
Permalink
Browse files
[Workers] Simplify early dedicated worker termination
[Workers] Simplify early dedicated worker termination
https://bugs.webkit.org/show_bug.cgi?id=248173

Reviewed by Darin Adler.

Merge - https://src.chromium.org/viewvc/blink?view=revision&revision=180532

When the worker was already asked to terminate by the time script content is loaded,
there is no need to create and start worker thread, we could exit early instead.

* Source/WebCore/workers/WorkerMessagingProxy.cpp:
(WorkerMessagingProxy::startWorkerGlobalScope): Add condition to terminate worker early
(WorkerMessagingProxy::setResourcCachingDisabledByWebInspector): Remove worker termination logic since moved above, also add ASSERT and update value of "queuedEarlyTasks"

Canonical link: https://commits.webkit.org/256949@main
  • Loading branch information
Ahmad-S792 authored and Ahmad Saleem committed Nov 22, 2022
1 parent 4b03741 commit 0987e578a6a8813156f74ebbf01490e4d31aa8f8
Showing 1 changed file with 14 additions and 13 deletions.
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2008-2017 Apple Inc. All Rights Reserved.
* Copyright (C) 2009 Google Inc. All Rights Reserved.
* Copyright (C) 2009-2022 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -123,6 +123,11 @@ void WorkerMessagingProxy::startWorkerGlobalScope(const URL& scriptURL, PAL::Ses
{
if (!m_scriptExecutionContext)
return;

if (m_askedToTerminate) {
// Worker.terminate() could be called from JS before the thread was created.
return;
}

auto* parentWorkerGlobalScope = dynamicDowncast<WorkerGlobalScope>(m_scriptExecutionContext.get());
WorkerThreadStartMode startMode = m_inspectorProxy->workerStartMode(*m_scriptExecutionContext.get());
@@ -325,21 +330,17 @@ void WorkerMessagingProxy::setResourceCachingDisabledByWebInspector(bool disable

void WorkerMessagingProxy::workerThreadCreated(DedicatedWorkerThread& workerThread)
{
ASSERT(!m_askedToTerminate);
m_workerThread = &workerThread;

if (m_askedToTerminate) {
// Worker.terminate() could be called from JS before the thread was created.
m_workerThread->stop(nullptr);
} else {
if (m_askedToSuspend) {
m_askedToSuspend = false;
m_workerThread->suspend();
}

auto queuedEarlyTasks = WTFMove(m_queuedEarlyTasks);
for (auto& task : queuedEarlyTasks)
m_workerThread->runLoop().postTask(WTFMove(*task));
if (m_askedToSuspend) {
m_askedToSuspend = false;
m_workerThread->suspend();
}

auto queuedEarlyTasks = std::exchange(m_queuedEarlyTasks, { });
for (auto& task : queuedEarlyTasks)
m_workerThread->runLoop().postTask(WTFMove(*task));
}

void WorkerMessagingProxy::workerObjectDestroyed()

0 comments on commit 0987e57

Please sign in to comment.