Add a per-thread proc table using thread local storage

In situations where both dawn_wire and dawn_native are used on separate
threads (Chrome with --single-process or --in-process-gpu), it's
desirable to have a per-thread proc table so that the WebGPU C++ API can
still be used. This eliminates classes of bugs with manual
reference/release errors.

This also changes many of the GetProcs functions to return const
references to the static proc tables known at compile time, instead of a
copy.

Bug: none
Change-Id: I8775bb715b312dd9476a1903fbd797d4b1302614
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/29240
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng
2020-10-05 19:53:58 +00:00
committed by Commit Bot service account
parent df90930683
commit b04a92f01b
20 changed files with 248 additions and 41 deletions

View File

@@ -168,6 +168,7 @@ test("dawn_unittests") {
"unittests/MathTests.cpp",
"unittests/ObjectBaseTests.cpp",
"unittests/PerStageTests.cpp",
"unittests/PerThreadProcTests.cpp",
"unittests/PlacementAllocatedTests.cpp",
"unittests/RefCountedTests.cpp",
"unittests/ResultTests.cpp",

View File

@@ -744,12 +744,9 @@ void DawnTestBase::SetUp() {
clientDesc.serializer = mC2sBuf.get();
mWireClient.reset(new dawn_wire::WireClient(clientDesc));
WGPUDevice clientDevice = mWireClient->GetDevice();
DawnProcTable clientProcs = dawn_wire::WireClient::GetProcs();
cDevice = mWireClient->GetDevice();
procs = dawn_wire::client::GetProcs();
mS2cBuf->SetHandler(mWireClient.get());
procs = clientProcs;
cDevice = clientDevice;
} else {
procs = backendProcs;
cDevice = backendDevice;

View File

@@ -50,8 +50,7 @@ class WindowSurfaceInstanceTests : public testing::Test {
});
DAWN_SKIP_TEST_IF(!glfwInit());
DawnProcTable procs = dawn_native::GetProcs();
dawnProcSetProcs(&procs);
dawnProcSetProcs(&dawn_native::GetProcs());
mInstance = wgpu::CreateInstance();
}

View File

@@ -72,7 +72,7 @@ namespace {
mWireClient = std::make_unique<dawn_wire::WireClient>(clientDesc);
mDevice = wgpu::Device::Acquire(mWireClient->GetDevice());
mProcs = dawn_wire::WireClient::GetProcs();
mProcs = dawn_wire::client::GetProcs();
break;
}

View File

@@ -0,0 +1,118 @@
// 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/dawn_thread_dispatch_proc.h"
#include "dawn/webgpu_cpp.h"
#include "dawn_native/DawnNative.h"
#include "dawn_native/Instance.h"
#include "dawn_native/null/DeviceNull.h"
#include <gtest/gtest.h>
#include <atomic>
#include <thread>
class PerThreadProcTests : public testing::Test {
public:
PerThreadProcTests()
: mNativeInstance(dawn_native::InstanceBase::Create()),
mNativeAdapter(mNativeInstance.Get()) {
}
~PerThreadProcTests() override = default;
protected:
Ref<dawn_native::InstanceBase> mNativeInstance;
dawn_native::null::Adapter mNativeAdapter;
};
// Test that procs can be set per thread. This test overrides deviceCreateBuffer with a dummy proc
// for each thread that increments a counter. Because each thread has their own proc and counter,
// there should be no data races. The per-thread procs also check that the current thread id is
// exactly equal to the expected thread id.
TEST_F(PerThreadProcTests, DispatchesPerThread) {
dawnProcSetProcs(&dawnThreadDispatchProcTable);
// Threads will block on this atomic to be sure we set procs on both threads before
// either thread calls the procs.
std::atomic<bool> ready(false);
static int threadACounter = 0;
static int threadBCounter = 0;
static std::atomic<std::thread::id> threadIdA;
static std::atomic<std::thread::id> threadIdB;
constexpr int kThreadATargetCount = 28347;
constexpr int kThreadBTargetCount = 40420;
// Note: Acquire doesn't call reference or release.
wgpu::Device deviceA =
wgpu::Device::Acquire(reinterpret_cast<WGPUDevice>(mNativeAdapter.CreateDevice(nullptr)));
wgpu::Device deviceB =
wgpu::Device::Acquire(reinterpret_cast<WGPUDevice>(mNativeAdapter.CreateDevice(nullptr)));
std::thread threadA([&]() {
DawnProcTable procs = dawn_native::GetProcs();
procs.deviceCreateBuffer = [](WGPUDevice device,
WGPUBufferDescriptor const* descriptor) -> WGPUBuffer {
EXPECT_EQ(std::this_thread::get_id(), threadIdA);
threadACounter++;
return nullptr;
};
dawnProcSetPerThreadProcs(&procs);
while (!ready) {
} // Should be fast, so just spin.
for (int i = 0; i < kThreadATargetCount; ++i) {
deviceA.CreateBuffer(nullptr);
}
deviceA = nullptr;
dawnProcSetPerThreadProcs(nullptr);
});
std::thread threadB([&]() {
DawnProcTable procs = dawn_native::GetProcs();
procs.deviceCreateBuffer = [](WGPUDevice device,
WGPUBufferDescriptor const* bufferDesc) -> WGPUBuffer {
EXPECT_EQ(std::this_thread::get_id(), threadIdB);
threadBCounter++;
return nullptr;
};
dawnProcSetPerThreadProcs(&procs);
while (!ready) {
} // Should be fast, so just spin.
for (int i = 0; i < kThreadBTargetCount; ++i) {
deviceB.CreateBuffer(nullptr);
}
deviceB = nullptr;
dawnProcSetPerThreadProcs(nullptr);
});
threadIdA = threadA.get_id();
threadIdB = threadB.get_id();
ready = true;
threadA.join();
threadB.join();
EXPECT_EQ(threadACounter, kThreadATargetCount);
EXPECT_EQ(threadBCounter, kThreadBTargetCount);
dawnProcSetProcs(nullptr);
}

View File

@@ -40,8 +40,7 @@ ValidationTest::ValidationTest() {
ASSERT(foundNullAdapter);
DawnProcTable procs = dawn_native::GetProcs();
dawnProcSetProcs(&procs);
dawnProcSetProcs(&dawn_native::GetProcs());
device = CreateDeviceFromAdapter(adapter, std::vector<const char*>());
}

View File

@@ -29,8 +29,7 @@ using namespace dawn_wire;
class WireMultipleDeviceTests : public testing::Test {
protected:
void SetUp() override {
DawnProcTable procs = dawn_wire::WireClient::GetProcs();
dawnProcSetProcs(&procs);
dawnProcSetProcs(&dawn_wire::client::GetProcs());
}
void TearDown() override {

View File

@@ -66,8 +66,7 @@ void WireTest::SetUp() {
mS2cBuf->SetHandler(mWireClient.get());
device = mWireClient->GetDevice();
DawnProcTable clientProcs = dawn_wire::WireClient::GetProcs();
dawnProcSetProcs(&clientProcs);
dawnProcSetProcs(&dawn_wire::client::GetProcs());
apiDevice = mockDevice;