Implement WaitableEvent and WorkerTaskPool for multi-threaded tasks

This patch adds the basic implementation of WaitableEvent and
WorkerTaskPool for multi-threaded tasks in Dawn (for example, the
multi-threaded implementation of CreateReady*Pipeline()).

BUG=dawn:529
TEST=dawn_unittests

Change-Id: Ibf84348f4c0f0d26badc19ae94cd536cef89d084
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/36360
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
This commit is contained in:
Jiawei Shao
2021-01-20 08:56:07 +00:00
committed by Commit Bot service account
parent 311a17a8fe
commit 064f33e441
11 changed files with 312 additions and 0 deletions

View File

@@ -25,6 +25,8 @@ dawn_component("dawn_platform") {
"${dawn_root}/src/include/dawn_platform/DawnPlatform.h",
"${dawn_root}/src/include/dawn_platform/dawn_platform_export.h",
"DawnPlatform.cpp",
"WorkerThread.cpp",
"WorkerThread.h",
"tracing/EventTracer.cpp",
"tracing/EventTracer.h",
"tracing/TraceEvent.h",

View File

@@ -23,6 +23,8 @@ target_sources(dawn_platform PRIVATE
"${DAWN_INCLUDE_DIR}/dawn_platform/DawnPlatform.h"
"${DAWN_INCLUDE_DIR}/dawn_platform/dawn_platform_export.h"
"DawnPlatform.cpp"
"WorkerThread.cpp"
"WorkerThread.h"
"tracing/EventTracer.cpp"
"tracing/EventTracer.h"
"tracing/TraceEvent.h"

View File

@@ -13,6 +13,7 @@
// limitations under the License.
#include "dawn_platform/DawnPlatform.h"
#include "dawn_platform/WorkerThread.h"
#include "common/Assert.h"
@@ -55,4 +56,8 @@ namespace dawn_platform {
return nullptr;
}
std::unique_ptr<dawn_platform::WorkerTaskPool> Platform::CreateWorkerTaskPool() {
return std::make_unique<AsyncWorkerThreadPool>();
}
} // namespace dawn_platform

View File

@@ -0,0 +1,51 @@
// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn_platform/WorkerThread.h"
#include <future>
#include "common/Assert.h"
namespace {
class AsyncWaitableEvent final : public dawn_platform::WaitableEvent {
public:
explicit AsyncWaitableEvent(std::function<void()> func) {
mFuture = std::async(std::launch::async, func);
}
virtual ~AsyncWaitableEvent() override {
ASSERT(IsComplete());
}
void Wait() override {
ASSERT(mFuture.valid());
mFuture.wait();
}
bool IsComplete() override {
ASSERT(mFuture.valid());
return mFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
}
private:
std::future<void> mFuture;
};
} // anonymous namespace
std::unique_ptr<dawn_platform::WaitableEvent> AsyncWorkerThreadPool::PostWorkerTask(
dawn_platform::PostWorkerTaskCallback callback,
void* userdata) {
std::function<void()> doTask = [callback, userdata]() { callback(userdata); };
return std::make_unique<AsyncWaitableEvent>(doTask);
}

View File

@@ -0,0 +1,28 @@
// Copyright 2021 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COMMON_WORKERTHREAD_H_
#define COMMON_WORKERTHREAD_H_
#include "common/NonCopyable.h"
#include "dawn_platform/DawnPlatform.h"
class AsyncWorkerThreadPool : public dawn_platform::WorkerTaskPool, public NonCopyable {
public:
std::unique_ptr<dawn_platform::WaitableEvent> PostWorkerTask(
dawn_platform::PostWorkerTaskCallback callback,
void* userdata) override;
};
#endif