1 // Copyright 2015 The Shaderc Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "libshaderc_util/mutex.h"
16 
17 #include <gmock/gmock.h>
18 #include <thread>
19 
20 namespace {
21 
TEST(MutexTest,CanCreateMutex)22 TEST(MutexTest, CanCreateMutex) {
23   shaderc_util::mutex mutex;
24   mutex.lock();
25   mutex.unlock();
26 }
27 
28 #ifndef SHADERC_DISABLE_THREADED_TESTS
29 
increment_by_1000(shaderc_util::mutex & mut,int & i)30 void increment_by_1000(shaderc_util::mutex& mut, int& i) {
31   for(size_t j = 0; j < 1000; ++j) {
32     mut.lock();
33     i = i + 1;
34     mut.unlock();
35   }
36 }
37 
TEST(MutexTest,MutexLocks)38 TEST(MutexTest, MutexLocks) {
39   shaderc_util::mutex mutex;
40   int i = 0;
41   std::thread t1([&mutex, &i]() { increment_by_1000(mutex, i); });
42   std::thread t2([&mutex, &i]() { increment_by_1000(mutex, i); });
43   std::thread t3([&mutex, &i]() { increment_by_1000(mutex, i); });
44   t1.join();
45   t2.join();
46   t3.join();
47   EXPECT_EQ(3000, i);
48 }
49 #endif // SHADERC_DISABLE_THREADED_TESTS
50 
51 }  // anonymous namespace
52