Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [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 | #ifndef RTC_BASE_RACE_CHECKER_H_ |
| 12 | #define RTC_BASE_RACE_CHECKER_H_ |
Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/platform_thread.h" |
| 16 | #include "rtc_base/thread_annotations.h" |
Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | namespace rtc { |
| 19 | |
| 20 | namespace internal { |
| 21 | class RaceCheckerScope; |
| 22 | } // namespace internal |
| 23 | |
| 24 | // Best-effort race-checking implementation. This primitive uses no |
| 25 | // synchronization at all to be as-fast-as-possible in the non-racy case. |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 26 | class RTC_LOCKABLE RaceChecker { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | public: |
| 28 | friend class internal::RaceCheckerScope; |
| 29 | RaceChecker(); |
| 30 | |
| 31 | private: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 32 | bool Acquire() const RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 33 | void Release() const RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | |
| 35 | // Volatile to prevent code being optimized away in Acquire()/Release(). |
| 36 | mutable volatile int access_count_ = 0; |
| 37 | mutable volatile PlatformThreadRef accessing_thread_; |
| 38 | }; |
| 39 | |
| 40 | namespace internal { |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 41 | class RTC_SCOPED_LOCKABLE RaceCheckerScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | public: |
| 43 | explicit RaceCheckerScope(const RaceChecker* race_checker) |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 44 | RTC_EXCLUSIVE_LOCK_FUNCTION(race_checker); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 45 | |
| 46 | bool RaceDetected() const; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 47 | ~RaceCheckerScope() RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | const RaceChecker* const race_checker_; |
| 51 | const bool race_check_ok_; |
| 52 | }; |
| 53 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 54 | class RTC_SCOPED_LOCKABLE RaceCheckerScopeDoNothing { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | public: |
| 56 | explicit RaceCheckerScopeDoNothing(const RaceChecker* race_checker) |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 57 | RTC_EXCLUSIVE_LOCK_FUNCTION(race_checker) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 59 | ~RaceCheckerScopeDoNothing() RTC_UNLOCK_FUNCTION() {} |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace internal |
| 63 | } // namespace rtc |
| 64 | |
| 65 | #define RTC_CHECK_RUNS_SERIALIZED(x) \ |
| 66 | rtc::internal::RaceCheckerScope race_checker(x); \ |
| 67 | RTC_CHECK(!race_checker.RaceDetected()) |
| 68 | |
| 69 | #if RTC_DCHECK_IS_ON |
| 70 | #define RTC_DCHECK_RUNS_SERIALIZED(x) \ |
| 71 | rtc::internal::RaceCheckerScope race_checker(x); \ |
| 72 | RTC_DCHECK(!race_checker.RaceDetected()) |
| 73 | #else |
| 74 | #define RTC_DCHECK_RUNS_SERIALIZED(x) \ |
| 75 | rtc::internal::RaceCheckerScopeDoNothing race_checker(x) |
| 76 | #endif |
Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 77 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 78 | #endif // RTC_BASE_RACE_CHECKER_H_ |