danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/thread_annotations.h" |
| 12 | #include "test/gtest.h" |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 16 | class RTC_LOCKABLE Lock { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 17 | public: |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 18 | void EnterWrite() const RTC_EXCLUSIVE_LOCK_FUNCTION() {} |
| 19 | void EnterRead() const RTC_SHARED_LOCK_FUNCTION() {} |
| 20 | bool TryEnterWrite() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 21 | return true; |
| 22 | } |
| 23 | bool TryEnterRead() const RTC_SHARED_TRYLOCK_FUNCTION(true) { return true; } |
| 24 | void Leave() const RTC_UNLOCK_FUNCTION() {} |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 27 | class RTC_SCOPED_LOCKABLE ScopeLock { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 28 | public: |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 29 | explicit ScopeLock(const Lock& lock) RTC_EXCLUSIVE_LOCK_FUNCTION(lock) {} |
| 30 | ~ScopeLock() RTC_UNLOCK_FUNCTION() {} |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class ThreadSafe { |
| 34 | public: |
| 35 | ThreadSafe() { |
| 36 | pt_protected_by_lock_ = new int; |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ~ThreadSafe() { |
| 40 | delete pt_protected_by_lock_; |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void LockInOrder() { |
Danil Chapovalov | 3765199 | 2017-11-01 10:52:09 +0100 | [diff] [blame] | 44 | beforelock_.EnterWrite(); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 45 | lock_.EnterWrite(); |
| 46 | pt_lock_.EnterWrite(); |
| 47 | |
| 48 | pt_lock_.Leave(); |
| 49 | lock_.Leave(); |
Danil Chapovalov | 3765199 | 2017-11-01 10:52:09 +0100 | [diff] [blame] | 50 | beforelock_.Leave(); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Danil Chapovalov | 3765199 | 2017-11-01 10:52:09 +0100 | [diff] [blame] | 53 | void UnprotectedFunction() RTC_LOCKS_EXCLUDED(lock_, pt_lock_) { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 54 | // Can access unprotected Value. |
| 55 | unprotected_ = 15; |
| 56 | // Can access pointers themself, but not data they point to. |
| 57 | int* tmp = pt_protected_by_lock_; |
Danil Chapovalov | 3765199 | 2017-11-01 10:52:09 +0100 | [diff] [blame] | 58 | pt_protected_by_lock_ = tmp; |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void ReadProtected() { |
| 62 | lock_.EnterRead(); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 63 | unprotected_ = protected_by_lock_; |
| 64 | lock_.Leave(); |
| 65 | |
| 66 | if (pt_lock_.TryEnterRead()) { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 67 | unprotected_ = *pt_protected_by_lock_; |
| 68 | pt_lock_.Leave(); |
| 69 | } |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void WriteProtected() { |
| 73 | lock_.EnterWrite(); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 74 | protected_by_lock_ = unprotected_; |
| 75 | lock_.Leave(); |
| 76 | |
| 77 | if (pt_lock_.TryEnterWrite()) { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 78 | *pt_protected_by_lock_ = unprotected_; |
| 79 | pt_lock_.Leave(); |
| 80 | } |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void CallReadProtectedFunction() { |
| 84 | lock_.EnterRead(); |
| 85 | pt_lock_.EnterRead(); |
| 86 | ReadProtectedFunction(); |
| 87 | pt_lock_.Leave(); |
| 88 | lock_.Leave(); |
| 89 | } |
| 90 | |
| 91 | void CallWriteProtectedFunction() { |
| 92 | ScopeLock scope_lock(GetLock()); |
| 93 | ScopeLock pt_scope_lock(pt_lock_); |
| 94 | WriteProtectedFunction(); |
| 95 | } |
| 96 | |
| 97 | private: |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 98 | void ReadProtectedFunction() RTC_SHARED_LOCKS_REQUIRED(lock_, pt_lock_) { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 99 | unprotected_ = protected_by_lock_; |
| 100 | unprotected_ = *pt_protected_by_lock_; |
| 101 | } |
| 102 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 103 | void WriteProtectedFunction() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_, pt_lock_) { |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 104 | int x = protected_by_lock_; |
| 105 | *pt_protected_by_lock_ = x; |
| 106 | protected_by_lock_ = unprotected_; |
| 107 | } |
| 108 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 109 | const Lock& GetLock() RTC_LOCK_RETURNED(lock_) { return lock_; } |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 110 | |
Danil Chapovalov | 3765199 | 2017-11-01 10:52:09 +0100 | [diff] [blame] | 111 | Lock beforelock_ RTC_ACQUIRED_BEFORE(lock_); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 112 | Lock lock_; |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 113 | Lock pt_lock_ RTC_ACQUIRED_AFTER(lock_); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 114 | |
| 115 | int unprotected_ = 0; |
| 116 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 117 | int protected_by_lock_ RTC_GUARDED_BY(lock_) = 0; |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 118 | |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 119 | int* pt_protected_by_lock_ RTC_PT_GUARDED_BY(pt_lock_); |
danilchap | 0140408 | 2016-10-24 06:07:25 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace |
| 123 | |
| 124 | TEST(ThreadAnnotationsTest, Test) { |
| 125 | // This test ensure thread annotations doesn't break compilation. |
| 126 | // Thus no run-time expectations. |
| 127 | ThreadSafe t; |
| 128 | t.LockInOrder(); |
| 129 | t.UnprotectedFunction(); |
| 130 | t.ReadProtected(); |
| 131 | t.WriteProtected(); |
| 132 | t.CallReadProtectedFunction(); |
| 133 | t.CallWriteProtectedFunction(); |
| 134 | } |