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