OLD | NEW |
(Empty) | |
| 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 |
| 11 #ifndef WEBRTC_BASE_RACE_CHECKER_H_ |
| 12 #define WEBRTC_BASE_RACE_CHECKER_H_ |
| 13 |
| 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/criticalsection.h" |
| 16 #include "webrtc/base/platform_thread.h" |
| 17 #include "webrtc/base/thread_annotations.h" |
| 18 |
| 19 namespace rtc { |
| 20 |
| 21 namespace internal { |
| 22 class RaceCheckerScope; |
| 23 } // namespace internal |
| 24 |
| 25 class LOCKABLE RaceChecker { |
| 26 public: |
| 27 friend class internal::RaceCheckerScope; |
| 28 RaceChecker(); |
| 29 private: |
| 30 bool Acquire() const EXCLUSIVE_LOCK_FUNCTION(); |
| 31 void Release() const UNLOCK_FUNCTION(); |
| 32 CriticalSection crit_; |
| 33 mutable int access_count_ GUARDED_BY(crit_) = 0; |
| 34 mutable PlatformThreadRef accessing_thread_ GUARDED_BY(crit_); |
| 35 }; |
| 36 |
| 37 namespace internal { |
| 38 class SCOPED_LOCKABLE RaceCheckerScope { |
| 39 public: |
| 40 explicit RaceCheckerScope(const RaceChecker* race_checker) |
| 41 EXCLUSIVE_LOCK_FUNCTION(race_checker); |
| 42 |
| 43 bool RaceDetected() const; |
| 44 ~RaceCheckerScope() UNLOCK_FUNCTION(); |
| 45 |
| 46 private: |
| 47 const RaceChecker* const race_checker_; |
| 48 const bool race_check_ok_; |
| 49 }; |
| 50 |
| 51 class SCOPED_LOCKABLE RaceCheckerScopeDoNothing { |
| 52 public: |
| 53 explicit RaceCheckerScopeDoNothing(RaceChecker* race_checker) |
| 54 EXCLUSIVE_LOCK_FUNCTION(race_checker) {} |
| 55 |
| 56 ~RaceCheckerScopeDoNothing() UNLOCK_FUNCTION() {} |
| 57 }; |
| 58 |
| 59 } // namespace internal |
| 60 } // namespace rtc |
| 61 |
| 62 #define RTC_CHECK_RUNS_SINGLE_THREADED(x) \ |
| 63 rtc::internal::RaceCheckerScope race_checker(x); \ |
| 64 RTC_CHECK(!race_checker.RaceDetected()) |
| 65 |
| 66 #if RTC_DCHECK_IS_ON |
| 67 #define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \ |
| 68 rtc::internal::RaceCheckerScope race_checker(x); \ |
| 69 RTC_DCHECK(!race_checker.RaceDetected()) |
| 70 #else |
| 71 #define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \ |
| 72 rtc::internal::RaceCheckerScopeDoNothing race_checker(x) |
| 73 #endif |
| 74 |
| 75 #endif // WEBRTC_BASE_RACE_CHECKER_H_ |
OLD | NEW |