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/platform_thread.h" | |
16 #include "webrtc/base/thread_annotations.h" | |
17 | |
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. | |
26 class LOCKABLE RaceChecker { | |
27 public: | |
28 friend class internal::RaceCheckerScope; | |
29 RaceChecker(); | |
30 private: | |
31 bool Acquire() const EXCLUSIVE_LOCK_FUNCTION(); | |
32 void Release() const UNLOCK_FUNCTION(); | |
33 | |
34 // Volatile to prevent code being optimized away in Acquire()/Release(). | |
35 mutable volatile int access_count_ = 0; | |
36 mutable volatile PlatformThreadRef accessing_thread_; | |
37 }; | |
38 | |
39 namespace internal { | |
40 class SCOPED_LOCKABLE RaceCheckerScope { | |
41 public: | |
42 explicit RaceCheckerScope(const RaceChecker* race_checker) | |
43 EXCLUSIVE_LOCK_FUNCTION(race_checker); | |
44 | |
45 bool RaceDetected() const; | |
46 ~RaceCheckerScope() UNLOCK_FUNCTION(); | |
47 | |
48 private: | |
49 const RaceChecker* const race_checker_; | |
50 const bool race_check_ok_; | |
51 }; | |
52 | |
53 class SCOPED_LOCKABLE RaceCheckerScopeDoNothing { | |
54 public: | |
55 explicit RaceCheckerScopeDoNothing(RaceChecker* race_checker) | |
danilchap
2016/06/27 17:58:57
const RaceChecker*
pbos-webrtc
2016/06/27 18:06:39
Done.
| |
56 EXCLUSIVE_LOCK_FUNCTION(race_checker) {} | |
57 | |
58 ~RaceCheckerScopeDoNothing() UNLOCK_FUNCTION() {} | |
59 }; | |
60 | |
61 } // namespace internal | |
62 } // namespace rtc | |
63 | |
64 #define RTC_CHECK_RUNS_SINGLE_THREADED(x) \ | |
65 rtc::internal::RaceCheckerScope race_checker(x); \ | |
66 RTC_CHECK(!race_checker.RaceDetected()) | |
67 | |
68 #if RTC_DCHECK_IS_ON | |
69 #define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \ | |
70 rtc::internal::RaceCheckerScope race_checker(x); \ | |
71 RTC_DCHECK(!race_checker.RaceDetected()) | |
72 #else | |
73 #define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \ | |
74 rtc::internal::RaceCheckerScopeDoNothing race_checker(x) | |
75 #endif | |
76 | |
77 #endif // WEBRTC_BASE_RACE_CHECKER_H_ | |
OLD | NEW |