130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun/* 230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * Copyright 2011 Google Inc. All Rights Reserved. 330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * 430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * Licensed under the Apache License, Version 2.0 (the "License"); 530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * you may not use this file except in compliance with the License. 630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * You may obtain a copy of the License at 730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * 830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * http://www.apache.org/licenses/LICENSE-2.0 930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * 1030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * Unless required by applicable law or agreed to in writing, software 1130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * distributed under the License is distributed on an "AS IS" BASIS, 1230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * See the License for the specific language governing permissions and 1430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun * limitations under the License. 1530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun */ 1630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 1730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun#include <stdlib.h> 1830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 1930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun#include "gtest/gtest.h" 2030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun#include "sfntly/port/lock.h" 2130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun#include "test/platform_thread.h" 2230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 2330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunnamespace sfntly { 2430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 2530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun// Basic test to make sure that Acquire()/Unlock()/Try() don't crash 2630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 2730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunclass BasicLockTestThread : public PlatformThread::Delegate { 2830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun public: 2930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun BasicLockTestThread(Lock* lock) : lock_(lock), acquired_(0) {} 3030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 3130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun virtual void ThreadMain() { 3230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 10; i++) { 3330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Acquire(); 3430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired_++; 3530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Unlock(); 3630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 3730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 10; i++) { 3830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Acquire(); 3930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired_++; 4030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 20); 4130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Unlock(); 4230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 4330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 10; i++) { 4430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun if (lock_->Try()) { 4530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired_++; 4630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 20); 4730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Unlock(); 4830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 4930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 5030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 5130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 5230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int acquired() const { return acquired_; } 5330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 5430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun private: 5530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock* lock_; 5630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int acquired_; 5730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 5830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun NO_COPY_AND_ASSIGN(BasicLockTestThread); 5930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun}; 6030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 6130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunbool BasicLockTest() { 6230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock lock; 6330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun BasicLockTestThread thread(&lock); 6430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle = kNullThreadHandle; 6530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 6630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread, &handle)); 6730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 6830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int acquired = 0; 6930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 5; i++) { 7030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Acquire(); 7130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired++; 7230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 7330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 7430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 10; i++) { 7530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Acquire(); 7630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired++; 7730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 20); 7830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 7930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 8030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 10; i++) { 8130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun if (lock.Try()) { 8230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired++; 8330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 20); 8430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 8530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 8630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 8730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 5; i++) { 8830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Acquire(); 8930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun acquired++; 9030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 20); 9130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 9230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 9330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 9430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle); 9530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 9630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_GE(acquired, 20); 9730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_GE(thread.acquired(), 20); 9830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 9930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun return true; 10030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 10130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 10230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun// Test that Try() works as expected ------------------------------------------- 10330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 10430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunclass TryLockTestThread : public PlatformThread::Delegate { 10530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun public: 10630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun TryLockTestThread(Lock* lock) : lock_(lock), got_lock_(false) {} 10730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 10830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun virtual void ThreadMain() { 10930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun got_lock_ = lock_->Try(); 11030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun if (got_lock_) 11130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock_->Unlock(); 11230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 11330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 11430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun bool got_lock() const { return got_lock_; } 11530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 11630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun private: 11730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock* lock_; 11830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun bool got_lock_; 11930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 12030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun NO_COPY_AND_ASSIGN(TryLockTestThread); 12130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun}; 12230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 12330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunbool TryLockTest() { 12430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock lock; 12530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 12630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(lock.Try()); 12730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun // We now have the lock.... 12830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 12930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun // This thread will not be able to get the lock. 13030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun { 13130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun TryLockTestThread thread(&lock); 13230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle = kNullThreadHandle; 13330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 13430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread, &handle)); 13530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 13630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle); 13730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 13830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_FALSE(thread.got_lock()); 13930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 14030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 14130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 14230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 14330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun // This thread will.... 14430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun { 14530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun TryLockTestThread thread(&lock); 14630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle = kNullThreadHandle; 14730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 14830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread, &handle)); 14930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 15030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle); 15130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 15230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(thread.got_lock()); 15330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun // But it released it.... 15430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(lock.Try()); 15530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 15630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 15730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock.Unlock(); 15830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun return true; 15930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 16030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 16130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun// Tests that locks actually exclude ------------------------------------------- 16230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 16330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunclass MutexLockTestThread : public PlatformThread::Delegate { 16430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun public: 16530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread(Lock* lock, int* value) : lock_(lock), value_(value) {} 16630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 16730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun // Static helper which can also be called from the main thread. 16830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun static void DoStuff(Lock* lock, int* value) { 16930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun for (int i = 0; i < 40; i++) { 17030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock->Acquire(); 17130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int v = *value; 17230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Sleep(rand() % 10); 17330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun *value = v + 1; 17430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun lock->Unlock(); 17530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 17630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 17730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 17830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun virtual void ThreadMain() { 17930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun DoStuff(lock_, value_); 18030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun } 18130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 18230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun private: 18330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock* lock_; 18430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int* value_; 18530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 18630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun NO_COPY_AND_ASSIGN(MutexLockTestThread); 18730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun}; 18830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 18930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunbool MutexTwoThreads() { 19030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock lock; 19130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int value = 0; 19230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 19330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread thread(&lock, &value); 19430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle = kNullThreadHandle; 19530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 19630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread, &handle)); 19730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 19830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread::DoStuff(&lock, &value); 19930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 20030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle); 20130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 20230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_EQ(2 * 40, value); 20330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun return true; 20430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 20530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 20630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurunbool MutexFourThreads() { 20730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun Lock lock; 20830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun int value = 0; 20930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 21030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread thread1(&lock, &value); 21130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread thread2(&lock, &value); 21230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread thread3(&lock, &value); 21330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle1 = kNullThreadHandle; 21430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle2 = kNullThreadHandle; 21530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThreadHandle handle3 = kNullThreadHandle; 21630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 21730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread1, &handle1)); 21830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread2, &handle2)); 21930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_TRUE(PlatformThread::Create(&thread3, &handle3)); 22030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 22130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun MutexLockTestThread::DoStuff(&lock, &value); 22230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 22330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle1); 22430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle2); 22530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun PlatformThread::Join(handle3); 22630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 22730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun EXPECT_EQ(4 * 40, value); 22830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun return true; 22930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 23030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 23130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} // namespace sfntly 23230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 23330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim GurunTEST(LockTest, Basic) { 23430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun ASSERT_TRUE(sfntly::BasicLockTest()); 23530d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 23630d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 23730d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim GurunTEST(LockTest, TryLock) { 23830d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun ASSERT_TRUE(sfntly::TryLockTest()); 23930d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 24030d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun 24130d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim GurunTEST(LockTest, Mutex) { 24230d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun ASSERT_TRUE(sfntly::MutexTwoThreads()); 24330d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun ASSERT_TRUE(sfntly::MutexFourThreads()); 24430d4e1f3d81ad9f7a1aa14ce6d2ceb5df56c15cdSelim Gurun} 245