Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 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: |
| 13 | void Acquire() EXCLUSIVE_LOCK_FUNCTION() {} |
| 14 | void Release() UNLOCK_FUNCTION() {} |
| 15 | }; |
| 16 | |
| 17 | class SCOPED_LOCKABLE AutoLock { |
| 18 | public: |
| 19 | AutoLock(Lock& lock) EXCLUSIVE_LOCK_FUNCTION(lock) : lock_(lock) { |
| 20 | lock.Acquire(); |
| 21 | } |
| 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 | |
| 38 | void ThreadSafe::ExplicitIncrement() { |
| 39 | lock_.Acquire(); |
| 40 | ++counter_; |
| 41 | lock_.Release(); |
| 42 | } |
| 43 | |
| 44 | void ThreadSafe::ImplicitIncrement() { |
| 45 | AutoLock auto_lock(lock_); |
| 46 | counter_++; |
| 47 | } |
| 48 | |
| 49 | TEST(ThreadAnnotationsTest, ExplicitIncrement) { |
| 50 | ThreadSafe thread_safe; |
| 51 | thread_safe.ExplicitIncrement(); |
| 52 | } |
| 53 | TEST(ThreadAnnotationsTest, ImplicitIncrement) { |
| 54 | ThreadSafe thread_safe; |
| 55 | thread_safe.ImplicitIncrement(); |
| 56 | } |
| 57 | |
| 58 | } // anonymous namespace |