dawn-cmake/src/dawn_native/AsyncTask.cpp
Corentin Wallez ec9cf2a85c Rename namespace dawn_native to dawn::native.
But keep a namespace alias to avoid breaking project that depend on the
previous namespace name while they get updated.

Done with through the following steps:

 - git grep -l dawn_native:: | xargs sed -i "" "s/dawn_native::/dawn::native::/g"
 - git grep -l "namespace dawn_native" | xargs sed -i "" "s/namespace dawn_native/namespace dawn::native/g"
 - git cl format
 - Manual fixups in generator/templates (and the addition of
   namespace_case in dawn_json_generator.py).
 - The addition of the namespace alias in DawnNative.h

Bug: dawn:824
Change-Id: I676cc4e3ced2e0e4bab32a0d66d7eaf9537e3f09
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/75982
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Auto-Submit: Corentin Wallez <cwallez@chromium.org>
2022-01-12 09:17:35 +00:00

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