Commit 1bfac224 authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

WorkerTaskQueue, a highly-speculative experimental worker task scheduling API


Bug: 879306
Change-Id: Ia1458ab7275bbcfeeb24e358b5702cb5d5e57c2e
Reviewed-on: https://chromium-review.googlesource.com/1196025
Commit-Queue: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587820}
parent ccc9362f
Posts one hundred tasks that return 1...100, and sums them.
5050
<body>
Posts one hundred tasks that return 1...100, and sums them.<br>
<script>
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
}
var queue = new WorkerTaskQueue("user-interaction");
var sum = 0;
var done = 0;
function maybeFinish() {
done++;
if (done == 100) {
document.body.appendChild(document.createTextNode(sum));
document.body.appendChild(document.createElement("br"));
if (window.testRunner)
testRunner.notifyDone();
}
};
for (var i = 1; i < 101; i++) {
queue.postTask(j => j, undefined, i)
.then(result => sum += result, maybeFinish)
.then(maybeFinish);
}
</script>
</body>
Schedules several dozen tasks, then cancels all but the last.
Fibonnaci #10 is 55
<body>
Schedules several dozen tasks, then cancels all but the last.<br>
<script>
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
}
var queue = new WorkerTaskQueue("background");
var stop = 11;
var next = 0;
function print(result) {
document.body.appendChild(document.createTextNode("Fibonnaci #" + next++ + " is " +result));
document.body.appendChild(document.createElement("br"));
if (next == stop) {
if (window.testRunner)
testRunner.notifyDone();
}
};
function fail() {
next++;
}
function fib(n) {
if (n == 0 || n == 1)
return n;
return fib(n - 1) + fib(n - 2);
}
const controller = new AbortController();
const signal = controller.signal;
for (var i = 0; i < stop; i++) {
queue.postTask(fib, i < stop - 1 ? signal : undefined, i)
.then(print, fail);
}
controller.abort();
</script>
</body>
......@@ -9972,6 +9972,10 @@ interface Worker : EventTarget
method terminate
setter onerror
setter onmessage
interface WorkerTaskQueue
attribute @@toStringTag
method constructor
method postTask
interface Worklet
attribute @@toStringTag
method addModule
......
......@@ -447,6 +447,7 @@ core_idl_files =
"typed_arrays/uint8_array.idl",
"typed_arrays/uint8_clamped_array.idl",
"url/url_search_params.idl",
"workers/experimental/worker_task_queue.idl",
"workers/shared_worker.idl",
"workers/worker.idl",
"workers/worker_location.idl",
......
......@@ -20,6 +20,10 @@ blink_core_sources("workers") {
"dedicated_worker_thread.h",
"execution_context_worker_registry.cc",
"execution_context_worker_registry.h",
"experimental/thread_pool.cc",
"experimental/thread_pool.h",
"experimental/worker_task_queue.cc",
"experimental/worker_task_queue.h",
"global_scope_creation_params.cc",
"global_scope_creation_params.h",
"installed_scripts_manager.cc",
......
japhet@chromium.org
# TEAM: worker-dev@chromium.org
# COMPONENT: Blink>Workers
This directory contains an experimental API for farming tasks out to a pool of worker threads. API is still highly in flux, and is not anywhere near ready to ship.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_THREAD_POOL_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_THREAD_POOL_H_
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class AbortSignal;
class Document;
class SerializedScriptValue;
class ThreadPoolMessagingProxy;
class ThreadPool final : public GarbageCollected<ThreadPool>,
public Supplement<Document> {
USING_GARBAGE_COLLECTED_MIXIN(ThreadPool);
public:
static const char kSupplementName[];
static ThreadPool* From(Document&);
void PostTask(scoped_refptr<SerializedScriptValue> task,
ScriptPromiseResolver*,
AbortSignal*,
const Vector<scoped_refptr<SerializedScriptValue>>& arguments,
TaskType);
void Trace(blink::Visitor*) final;
private:
ThreadPool(Document&);
~ThreadPool() = default;
friend ThreadPoolMessagingProxy;
ThreadPoolMessagingProxy* GetProxyForTaskType(TaskType);
void TaskCompleted(size_t task_id, scoped_refptr<SerializedScriptValue>);
void AbortTask(size_t task_id, TaskType task_type);
Member<Document> document_;
HeapVector<Member<ThreadPoolMessagingProxy>> context_proxies_;
size_t next_task_id_ = 1;
HeapHashMap<int, Member<ScriptPromiseResolver>> resolvers_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_THREAD_POOL_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/workers/experimental/worker_task_queue.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/workers/experimental/thread_pool.h"
namespace blink {
WorkerTaskQueue* WorkerTaskQueue::Create(ExecutionContext* context,
const String& type,
ExceptionState& exception_state) {
if (context->IsContextDestroyed()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidAccessError,
"The context provided is invalid.");
return nullptr;
}
if (!context->IsDocument()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidAccessError,
"WorkerTaskQueue can only be constructed from a document.");
return nullptr;
}
DCHECK(type == "user-interaction" || type == "background");
TaskType task_type = type == "user-interaction" ? TaskType::kUserInteraction
: TaskType::kIdleTask;
return new WorkerTaskQueue(ToDocument(context), task_type);
}
WorkerTaskQueue::WorkerTaskQueue(Document* document, TaskType task_type)
: document_(document), task_type_(task_type) {}
ScriptPromise WorkerTaskQueue::postTask(ScriptState* script_state,
const ScriptValue& task,
AbortSignal* signal,
const Vector<ScriptValue>& arguments) {
DCHECK(document_->IsContextThread());
DCHECK(task.IsFunction());
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
scoped_refptr<SerializedScriptValue> serialized_task =
SerializedScriptValue::SerializeAndSwallowExceptions(
script_state->GetIsolate(),
task.V8Value()->ToString(script_state->GetIsolate()));
if (!serialized_task) {
resolver->Reject();
return resolver->Promise();
}
Vector<scoped_refptr<SerializedScriptValue>> serialized_arguments;
serialized_arguments.ReserveInitialCapacity(arguments.size());
for (auto& argument : arguments) {
scoped_refptr<SerializedScriptValue> serialized_argument =
SerializedScriptValue::SerializeAndSwallowExceptions(
script_state->GetIsolate(), argument.V8Value());
if (!serialized_argument) {
resolver->Reject();
return resolver->Promise();
}
serialized_arguments.push_back(serialized_argument);
}
ThreadPool::From(*document_)
->PostTask(std::move(serialized_task), resolver, signal,
std::move(serialized_arguments), task_type_);
return resolver->Promise();
}
void WorkerTaskQueue::Trace(blink::Visitor* visitor) {
ScriptWrappable::Trace(visitor);
visitor->Trace(document_);
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_WORKER_TASK_QUEUE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_WORKER_TASK_QUEUE_H_
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
namespace blink {
class AbortSignal;
class Document;
class ExceptionState;
class ExecutionContext;
class CORE_EXPORT WorkerTaskQueue : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
static WorkerTaskQueue* Create(ExecutionContext*,
const String&,
ExceptionState&);
~WorkerTaskQueue() override = default;
ScriptPromise postTask(ScriptState*,
const ScriptValue& task,
AbortSignal*,
const Vector<ScriptValue>& arguments);
void Trace(blink::Visitor*) override;
private:
WorkerTaskQueue(Document*, TaskType);
Member<Document> document_;
const TaskType task_type_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_EXPERIMENTAL_WORKER_TASK_QUEUE_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
enum TaskQueueType { "user-interaction", "background" };
[
Constructor(TaskQueueType queue_type),
ConstructorCallWith=ExecutionContext,
RaisesException=Constructor,
RuntimeEnabled=WorkerTaskQueue
] interface WorkerTaskQueue {
[CallWith=ScriptState] Promise<any> postTask(CallbackFunctionTreatedAsScriptValue task, optional AbortSignal signal = null, any... arguments);
};
......@@ -339,6 +339,11 @@ scheduler::WorkerScheduler* WorkerThread::GetScheduler() {
return worker_scheduler_.get();
}
scoped_refptr<base::SingleThreadTaskRunner>
WorkerThread::GetControlTaskRunner() {
return worker_scheduler_->GetWorkerThreadScheduler()->ControlTaskQueue();
}
void WorkerThread::ChildThreadStartedOnWorkerThread(WorkerThread* child) {
DCHECK(IsCurrentThread());
#if DCHECK_IS_ON()
......
......@@ -206,6 +206,9 @@ class CORE_EXPORT WorkerThread : public WebThread::TaskObserver {
return worker_scheduler_->GetTaskRunner(type);
}
// TODO(japhet): Hack to support an experimental worker scheduling API.
scoped_refptr<base::SingleThreadTaskRunner> GetControlTaskRunner();
void ChildThreadStartedOnWorkerThread(WorkerThread*);
void ChildThreadTerminatedOnWorkerThread(WorkerThread*);
......
......@@ -1378,6 +1378,10 @@
status: "test",
implied_by: ["WorkerNosniffBlock"],
},
{
name: "WorkerTaskQueue",
status: "experimental"
},
{
name: "WorkStealingInScriptRunner",
status: "experimental",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment