mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-05-16 20:31:20 +00:00
An async callback which calls Queue::Submit will cause reentrance in QueueBase::Tick and CreatePipelineAsyncTracker::Tick, which invalidates the task queue being used by the original call, and leads to a crash from an invalid pointer. The Tick functions should remove the tasks from the queues before the callbacks are called, so invalidation doesn't cause a crash. Bug: dawn:729 Change-Id: I0d952d51040a3d1a475767400de3333a8b9b0821 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/45900 Commit-Queue: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org>
125 lines
5.3 KiB
C++
125 lines
5.3 KiB
C++
// Copyright 2020 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_native/CreatePipelineAsyncTracker.h"
|
|
|
|
#include "dawn_native/ComputePipeline.h"
|
|
#include "dawn_native/Device.h"
|
|
#include "dawn_native/RenderPipeline.h"
|
|
|
|
namespace dawn_native {
|
|
|
|
CreatePipelineAsyncTaskBase::CreatePipelineAsyncTaskBase(void* userdata) : mUserData(userdata) {
|
|
}
|
|
|
|
CreatePipelineAsyncTaskBase::~CreatePipelineAsyncTaskBase() {
|
|
}
|
|
|
|
CreateComputePipelineAsyncTask::CreateComputePipelineAsyncTask(
|
|
ComputePipelineBase* pipeline,
|
|
WGPUCreateComputePipelineAsyncCallback callback,
|
|
void* userdata)
|
|
: CreatePipelineAsyncTaskBase(userdata),
|
|
mPipeline(pipeline),
|
|
mCreateComputePipelineAsyncCallback(callback) {
|
|
}
|
|
|
|
void CreateComputePipelineAsyncTask::Finish(WGPUCreatePipelineAsyncStatus status) {
|
|
ASSERT(mPipeline != nullptr);
|
|
ASSERT(mCreateComputePipelineAsyncCallback != nullptr);
|
|
|
|
if (status != WGPUCreatePipelineAsyncStatus_Success) {
|
|
// TODO(jiawei.shao@intel.com): support handling device lost
|
|
ASSERT(status == WGPUCreatePipelineAsyncStatus_DeviceDestroyed);
|
|
mCreateComputePipelineAsyncCallback(WGPUCreatePipelineAsyncStatus_DeviceDestroyed,
|
|
nullptr, "Device destroyed before callback",
|
|
mUserData);
|
|
mPipeline->Release();
|
|
} else {
|
|
mCreateComputePipelineAsyncCallback(
|
|
status, reinterpret_cast<WGPUComputePipeline>(mPipeline), "", mUserData);
|
|
}
|
|
|
|
// Set mCreateComputePipelineAsyncCallback to nullptr in case it is called more than once.
|
|
mCreateComputePipelineAsyncCallback = nullptr;
|
|
}
|
|
|
|
CreateRenderPipelineAsyncTask::CreateRenderPipelineAsyncTask(
|
|
RenderPipelineBase* pipeline,
|
|
WGPUCreateRenderPipelineAsyncCallback callback,
|
|
void* userdata)
|
|
: CreatePipelineAsyncTaskBase(userdata),
|
|
mPipeline(pipeline),
|
|
mCreateRenderPipelineAsyncCallback(callback) {
|
|
}
|
|
|
|
void CreateRenderPipelineAsyncTask::Finish(WGPUCreatePipelineAsyncStatus status) {
|
|
ASSERT(mPipeline != nullptr);
|
|
ASSERT(mCreateRenderPipelineAsyncCallback != nullptr);
|
|
|
|
if (status != WGPUCreatePipelineAsyncStatus_Success) {
|
|
// TODO(jiawei.shao@intel.com): support handling device lost
|
|
ASSERT(status == WGPUCreatePipelineAsyncStatus_DeviceDestroyed);
|
|
mCreateRenderPipelineAsyncCallback(WGPUCreatePipelineAsyncStatus_DeviceDestroyed,
|
|
nullptr, "Device destroyed before callback",
|
|
mUserData);
|
|
mPipeline->Release();
|
|
} else {
|
|
mCreateRenderPipelineAsyncCallback(
|
|
status, reinterpret_cast<WGPURenderPipeline>(mPipeline), "", mUserData);
|
|
}
|
|
|
|
// Set mCreatePipelineAsyncCallback to nullptr in case it is called more than once.
|
|
mCreateRenderPipelineAsyncCallback = nullptr;
|
|
}
|
|
|
|
CreatePipelineAsyncTracker::CreatePipelineAsyncTracker(DeviceBase* device) : mDevice(device) {
|
|
}
|
|
|
|
CreatePipelineAsyncTracker::~CreatePipelineAsyncTracker() {
|
|
ASSERT(mCreatePipelineAsyncTasksInFlight.Empty());
|
|
}
|
|
|
|
void CreatePipelineAsyncTracker::TrackTask(std::unique_ptr<CreatePipelineAsyncTaskBase> task,
|
|
ExecutionSerial serial) {
|
|
mCreatePipelineAsyncTasksInFlight.Enqueue(std::move(task), serial);
|
|
mDevice->AddFutureSerial(serial);
|
|
}
|
|
|
|
void CreatePipelineAsyncTracker::Tick(ExecutionSerial finishedSerial) {
|
|
// If a user calls Queue::Submit inside Create*PipelineAsync, then the device will be
|
|
// ticked, which in turns ticks the tracker, causing reentrance here. To prevent the
|
|
// reentrant call from invalidating mCreatePipelineAsyncTasksInFlight while in use by the
|
|
// first call, we remove the tasks to finish from the queue, update
|
|
// mCreatePipelineAsyncTasksInFlight, then run the callbacks.
|
|
std::vector<std::unique_ptr<CreatePipelineAsyncTaskBase>> tasks;
|
|
for (auto& task : mCreatePipelineAsyncTasksInFlight.IterateUpTo(finishedSerial)) {
|
|
tasks.push_back(std::move(task));
|
|
}
|
|
mCreatePipelineAsyncTasksInFlight.ClearUpTo(finishedSerial);
|
|
|
|
for (auto& task : tasks) {
|
|
task->Finish(WGPUCreatePipelineAsyncStatus_Success);
|
|
}
|
|
}
|
|
|
|
void CreatePipelineAsyncTracker::ClearForShutDown() {
|
|
for (auto& task : mCreatePipelineAsyncTasksInFlight.IterateAll()) {
|
|
task->Finish(WGPUCreatePipelineAsyncStatus_DeviceDestroyed);
|
|
}
|
|
mCreatePipelineAsyncTasksInFlight.Clear();
|
|
}
|
|
|
|
} // namespace dawn_native
|