Path: blob/main/libshaderc_util/src/mutex_test.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/mutex.h"1516#include <gmock/gmock.h>17#include <thread>1819namespace {2021TEST(MutexTest, CanCreateMutex) {22shaderc_util::mutex mutex;23mutex.lock();24mutex.unlock();25}2627#ifndef SHADERC_DISABLE_THREADED_TESTS2829void increment_by_1000(shaderc_util::mutex& mut, int& i) {30for(size_t j = 0; j < 1000; ++j) {31mut.lock();32i = i + 1;33mut.unlock();34}35}3637TEST(MutexTest, MutexLocks) {38shaderc_util::mutex mutex;39int i = 0;40std::thread t1([&mutex, &i]() { increment_by_1000(mutex, i); });41std::thread t2([&mutex, &i]() { increment_by_1000(mutex, i); });42std::thread t3([&mutex, &i]() { increment_by_1000(mutex, i); });43t1.join();44t2.join();45t3.join();46EXPECT_EQ(3000, i);47}48#endif // SHADERC_DISABLE_THREADED_TESTS4950} // anonymous namespace515253