1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2018 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #include "thread_annotations.h" 6*635a8641SAndroid Build Coastguard Worker 7*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h" 8*635a8641SAndroid Build Coastguard Worker 9*635a8641SAndroid Build Coastguard Worker namespace { 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker class LOCKABLE Lock { 12*635a8641SAndroid Build Coastguard Worker public: Acquire()13*635a8641SAndroid Build Coastguard Worker void Acquire() EXCLUSIVE_LOCK_FUNCTION() {} Release()14*635a8641SAndroid Build Coastguard Worker void Release() UNLOCK_FUNCTION() {} 15*635a8641SAndroid Build Coastguard Worker }; 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker class SCOPED_LOCKABLE AutoLock { 18*635a8641SAndroid Build Coastguard Worker public: EXCLUSIVE_LOCK_FUNCTION(lock)19*635a8641SAndroid Build Coastguard Worker AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) { 20*635a8641SAndroid Build Coastguard Worker lock.Acquire(); 21*635a8641SAndroid Build Coastguard Worker } UNLOCK_FUNCTION()22*635a8641SAndroid Build Coastguard Worker ~AutoLock() UNLOCK_FUNCTION() { lock_.Release(); } 23*635a8641SAndroid Build Coastguard Worker 24*635a8641SAndroid Build Coastguard Worker private: 25*635a8641SAndroid Build Coastguard Worker Lock& lock_; 26*635a8641SAndroid Build Coastguard Worker }; 27*635a8641SAndroid Build Coastguard Worker 28*635a8641SAndroid Build Coastguard Worker class ThreadSafe { 29*635a8641SAndroid Build Coastguard Worker public: 30*635a8641SAndroid Build Coastguard Worker void ExplicitIncrement(); 31*635a8641SAndroid Build Coastguard Worker void ImplicitIncrement(); 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker private: 34*635a8641SAndroid Build Coastguard Worker Lock lock_; 35*635a8641SAndroid Build Coastguard Worker int counter_ GUARDED_BY(lock_); 36*635a8641SAndroid Build Coastguard Worker }; 37*635a8641SAndroid Build Coastguard Worker ExplicitIncrement()38*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::ExplicitIncrement() { 39*635a8641SAndroid Build Coastguard Worker lock_.Acquire(); 40*635a8641SAndroid Build Coastguard Worker ++counter_; 41*635a8641SAndroid Build Coastguard Worker lock_.Release(); 42*635a8641SAndroid Build Coastguard Worker } 43*635a8641SAndroid Build Coastguard Worker ImplicitIncrement()44*635a8641SAndroid Build Coastguard Workervoid ThreadSafe::ImplicitIncrement() { 45*635a8641SAndroid Build Coastguard Worker AutoLock auto_lock(lock_); 46*635a8641SAndroid Build Coastguard Worker counter_++; 47*635a8641SAndroid Build Coastguard Worker } 48*635a8641SAndroid Build Coastguard Worker TEST(ThreadAnnotationsTest,ExplicitIncrement)49*635a8641SAndroid Build Coastguard WorkerTEST(ThreadAnnotationsTest, ExplicitIncrement) { 50*635a8641SAndroid Build Coastguard Worker ThreadSafe thread_safe; 51*635a8641SAndroid Build Coastguard Worker thread_safe.ExplicitIncrement(); 52*635a8641SAndroid Build Coastguard Worker } TEST(ThreadAnnotationsTest,ImplicitIncrement)53*635a8641SAndroid Build Coastguard WorkerTEST(ThreadAnnotationsTest, ImplicitIncrement) { 54*635a8641SAndroid Build Coastguard Worker ThreadSafe thread_safe; 55*635a8641SAndroid Build Coastguard Worker thread_safe.ImplicitIncrement(); 56*635a8641SAndroid Build Coastguard Worker } 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker } // anonymous namespace 59