mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-08-29 06:55:56 +00:00
Bug: dawn:1275 Change-Id: Ic60a00107a015bc677ff929c492f1085ffc38482 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/79083 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Ben Clayton <bclayton@chromium.org>
66 lines
2.4 KiB
C++
66 lines
2.4 KiB
C++
#include "dawn/native/AsyncTask.h"
|
|
|
|
#include "dawn/platform/DawnPlatform.h"
|
|
|
|
namespace dawn::native {
|
|
|
|
AsyncTaskManager::AsyncTaskManager(dawn::platform::WorkerTaskPool* workerTaskPool)
|
|
: mWorkerTaskPool(workerTaskPool) {
|
|
}
|
|
|
|
void AsyncTaskManager::PostTask(AsyncTask asyncTask) {
|
|
// If these allocations becomes expensive, we can slab-allocate tasks.
|
|
Ref<WaitableTask> waitableTask = AcquireRef(new WaitableTask());
|
|
waitableTask->taskManager = this;
|
|
waitableTask->asyncTask = std::move(asyncTask);
|
|
|
|
{
|
|
// We insert new waitableTask objects into mPendingTasks in main thread (PostTask()),
|
|
// and we may remove waitableTask objects from mPendingTasks in either main thread
|
|
// (WaitAllPendingTasks()) or sub-thread (TaskCompleted), so mPendingTasks should be
|
|
// protected by a mutex.
|
|
std::lock_guard<std::mutex> lock(mPendingTasksMutex);
|
|
mPendingTasks.emplace(waitableTask.Get(), waitableTask);
|
|
}
|
|
|
|
// Ref the task since it is accessed inside the worker function.
|
|
// The worker function will acquire and release the task upon completion.
|
|
waitableTask->Reference();
|
|
waitableTask->waitableEvent =
|
|
mWorkerTaskPool->PostWorkerTask(DoWaitableTask, waitableTask.Get());
|
|
}
|
|
|
|
void AsyncTaskManager::HandleTaskCompletion(WaitableTask* task) {
|
|
std::lock_guard<std::mutex> lock(mPendingTasksMutex);
|
|
auto iter = mPendingTasks.find(task);
|
|
if (iter != mPendingTasks.end()) {
|
|
mPendingTasks.erase(iter);
|
|
}
|
|
}
|
|
|
|
void AsyncTaskManager::WaitAllPendingTasks() {
|
|
std::unordered_map<WaitableTask*, Ref<WaitableTask>> allPendingTasks;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(mPendingTasksMutex);
|
|
allPendingTasks.swap(mPendingTasks);
|
|
}
|
|
|
|
for (auto& [_, task] : allPendingTasks) {
|
|
task->waitableEvent->Wait();
|
|
}
|
|
}
|
|
|
|
bool AsyncTaskManager::HasPendingTasks() {
|
|
std::lock_guard<std::mutex> lock(mPendingTasksMutex);
|
|
return !mPendingTasks.empty();
|
|
}
|
|
|
|
void AsyncTaskManager::DoWaitableTask(void* task) {
|
|
Ref<WaitableTask> waitableTask = AcquireRef(static_cast<WaitableTask*>(task));
|
|
waitableTask->asyncTask();
|
|
waitableTask->taskManager->HandleTaskCompletion(waitableTask.Get());
|
|
}
|
|
|
|
} // namespace dawn::native
|