You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/thread_annotations.h"
  11. #include "test/gtest.h"
  12. namespace {
  13. class RTC_LOCKABLE Lock {
  14. public:
  15. void EnterWrite() const RTC_EXCLUSIVE_LOCK_FUNCTION() {}
  16. void EnterRead() const RTC_SHARED_LOCK_FUNCTION() {}
  17. bool TryEnterWrite() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
  18. return true;
  19. }
  20. bool TryEnterRead() const RTC_SHARED_TRYLOCK_FUNCTION(true) { return true; }
  21. void Leave() const RTC_UNLOCK_FUNCTION() {}
  22. };
  23. class RTC_SCOPED_LOCKABLE ScopeLock {
  24. public:
  25. explicit ScopeLock(const Lock& lock) RTC_EXCLUSIVE_LOCK_FUNCTION(lock) {}
  26. ~ScopeLock() RTC_UNLOCK_FUNCTION() {}
  27. };
  28. class ThreadSafe {
  29. public:
  30. ThreadSafe() { pt_protected_by_lock_ = new int; }
  31. ~ThreadSafe() { delete pt_protected_by_lock_; }
  32. void LockInOrder() {
  33. beforelock_.EnterWrite();
  34. lock_.EnterWrite();
  35. pt_lock_.EnterWrite();
  36. pt_lock_.Leave();
  37. lock_.Leave();
  38. beforelock_.Leave();
  39. }
  40. void UnprotectedFunction() RTC_LOCKS_EXCLUDED(lock_, pt_lock_) {
  41. // Can access unprotected Value.
  42. unprotected_ = 15;
  43. // Can access pointers themself, but not data they point to.
  44. int* tmp = pt_protected_by_lock_;
  45. pt_protected_by_lock_ = tmp;
  46. }
  47. void ReadProtected() {
  48. lock_.EnterRead();
  49. unprotected_ = protected_by_lock_;
  50. lock_.Leave();
  51. if (pt_lock_.TryEnterRead()) {
  52. unprotected_ = *pt_protected_by_lock_;
  53. pt_lock_.Leave();
  54. }
  55. }
  56. void WriteProtected() {
  57. lock_.EnterWrite();
  58. protected_by_lock_ = unprotected_;
  59. lock_.Leave();
  60. if (pt_lock_.TryEnterWrite()) {
  61. *pt_protected_by_lock_ = unprotected_;
  62. pt_lock_.Leave();
  63. }
  64. }
  65. void CallReadProtectedFunction() {
  66. lock_.EnterRead();
  67. pt_lock_.EnterRead();
  68. ReadProtectedFunction();
  69. pt_lock_.Leave();
  70. lock_.Leave();
  71. }
  72. void CallWriteProtectedFunction() {
  73. ScopeLock scope_lock(GetLock());
  74. ScopeLock pt_scope_lock(pt_lock_);
  75. WriteProtectedFunction();
  76. }
  77. private:
  78. void ReadProtectedFunction() RTC_SHARED_LOCKS_REQUIRED(lock_, pt_lock_) {
  79. unprotected_ = protected_by_lock_;
  80. unprotected_ = *pt_protected_by_lock_;
  81. }
  82. void WriteProtectedFunction() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_, pt_lock_) {
  83. int x = protected_by_lock_;
  84. *pt_protected_by_lock_ = x;
  85. protected_by_lock_ = unprotected_;
  86. }
  87. const Lock& GetLock() RTC_LOCK_RETURNED(lock_) { return lock_; }
  88. Lock beforelock_ RTC_ACQUIRED_BEFORE(lock_);
  89. Lock lock_;
  90. Lock pt_lock_ RTC_ACQUIRED_AFTER(lock_);
  91. int unprotected_ = 0;
  92. int protected_by_lock_ RTC_GUARDED_BY(lock_) = 0;
  93. int* pt_protected_by_lock_ RTC_PT_GUARDED_BY(pt_lock_);
  94. };
  95. } // namespace
  96. TEST(ThreadAnnotationsTest, Test) {
  97. // This test ensure thread annotations doesn't break compilation.
  98. // Thus no run-time expectations.
  99. ThreadSafe t;
  100. t.LockInOrder();
  101. t.UnprotectedFunction();
  102. t.ReadProtected();
  103. t.WriteProtected();
  104. t.CallReadProtectedFunction();
  105. t.CallWriteProtectedFunction();
  106. }