blob: 6b5f84ff20bd15ad10763924a12cf1c8c6a405c9 [file] [log] [blame]
// 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