mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-05 22:23:29 +00:00
Only Constants.h remains in no namespace; but it may be addressed in the future as well. Bug: dawn:302 Change-Id: Ib9b9ab4b974ad1de1bb9f2302f4d24d8216f55e4 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132841 Kokoro: Austin Eng <enga@chromium.org> Auto-Submit: Austin Eng <enga@chromium.org> Reviewed-by: Loko Kung <lokokung@google.com> Commit-Queue: Austin Eng <enga@chromium.org>
102 lines
3.0 KiB
C++
102 lines
3.0 KiB
C++
// Copyright 2023 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/common/Mutex.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace dawn {
|
|
namespace {
|
|
|
|
#if defined(DAWN_ENABLE_ASSERTS)
|
|
constexpr bool kAssertEnabled = true;
|
|
#else
|
|
constexpr bool kAssertEnabled = false;
|
|
#endif
|
|
|
|
class MutexTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
// IsLockedByCurrentThread() requires DAWN_ENABLE_ASSERTS flag enabled.
|
|
if (!kAssertEnabled) {
|
|
GTEST_SKIP() << "DAWN_ENABLE_ASSERTS is not enabled";
|
|
}
|
|
}
|
|
|
|
Mutex mMutex;
|
|
};
|
|
|
|
// Simple Lock() then Unlock() test.
|
|
TEST_F(MutexTest, SimpleLockUnlock) {
|
|
mMutex.Lock();
|
|
EXPECT_TRUE(mMutex.IsLockedByCurrentThread());
|
|
mMutex.Unlock();
|
|
EXPECT_FALSE(mMutex.IsLockedByCurrentThread());
|
|
}
|
|
|
|
// Test AutoLock automatically locks the mutex and unlocks it when out of scope.
|
|
TEST_F(MutexTest, AutoLock) {
|
|
{
|
|
Mutex::AutoLock autoLock(&mMutex);
|
|
EXPECT_TRUE(mMutex.IsLockedByCurrentThread());
|
|
}
|
|
EXPECT_FALSE(mMutex.IsLockedByCurrentThread());
|
|
}
|
|
|
|
// Test AutoLockAndHoldRef will keep the mutex alive
|
|
TEST_F(MutexTest, AutoLockAndHoldRef) {
|
|
auto* mutex = new Mutex();
|
|
EXPECT_EQ(mutex->GetRefCountForTesting(), 1u);
|
|
{
|
|
Mutex::AutoLockAndHoldRef autoLock(mutex);
|
|
EXPECT_TRUE(mutex->IsLockedByCurrentThread());
|
|
EXPECT_EQ(mutex->GetRefCountForTesting(), 2u);
|
|
|
|
mutex->Release();
|
|
EXPECT_EQ(mutex->GetRefCountForTesting(), 1u);
|
|
}
|
|
}
|
|
|
|
using MutexDeathTest = MutexTest;
|
|
|
|
// Test that Unlock() call on unlocked mutex will cause assertion failure.
|
|
TEST_F(MutexDeathTest, UnlockWhenNotLocked) {
|
|
ASSERT_DEATH({ mMutex.Unlock(); }, "");
|
|
}
|
|
|
|
// Double Lock() calls should be cause assertion failure
|
|
TEST_F(MutexDeathTest, DoubleLockCalls) {
|
|
mMutex.Lock();
|
|
EXPECT_TRUE(mMutex.IsLockedByCurrentThread());
|
|
ASSERT_DEATH({ mMutex.Lock(); }, "");
|
|
mMutex.Unlock();
|
|
}
|
|
|
|
// Lock() then use AutoLock should cause assertion failure.
|
|
TEST_F(MutexDeathTest, LockThenAutoLock) {
|
|
mMutex.Lock();
|
|
EXPECT_TRUE(mMutex.IsLockedByCurrentThread());
|
|
ASSERT_DEATH({ Mutex::AutoLock autoLock(&mMutex); }, "");
|
|
mMutex.Unlock();
|
|
}
|
|
|
|
// Use AutoLock then call Lock() should cause assertion failure.
|
|
TEST_F(MutexDeathTest, AutoLockThenLock) {
|
|
Mutex::AutoLock autoLock(&mMutex);
|
|
EXPECT_TRUE(mMutex.IsLockedByCurrentThread());
|
|
ASSERT_DEATH({ mMutex.Lock(); }, "");
|
|
}
|
|
|
|
} // anonymous namespace
|
|
} // namespace dawn
|