Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Unified Diff: webrtc/base/race_checker.h

Issue 2097403002: Add a race-checking mechanism. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/base/race_checker.h
diff --git a/webrtc/base/race_checker.h b/webrtc/base/race_checker.h
new file mode 100644
index 0000000000000000000000000000000000000000..02bc7d078ddb0cc53c26c8688656fe7c1b466c68
--- /dev/null
+++ b/webrtc/base/race_checker.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef WEBRTC_BASE_RACE_CHECKER_H_
+#define WEBRTC_BASE_RACE_CHECKER_H_
+
+#include "webrtc/base/checks.h"
+#include "webrtc/base/criticalsection.h"
+#include "webrtc/base/platform_thread.h"
+#include "webrtc/base/thread_annotations.h"
+
+namespace rtc {
+
+namespace internal {
+class RaceCheckerScope;
+} // namespace internal
+
+class LOCKABLE RaceChecker {
+ public:
+ friend class internal::RaceCheckerScope;
+ RaceChecker();
+ private:
+ bool Acquire() const EXCLUSIVE_LOCK_FUNCTION();
+ void Release() const UNLOCK_FUNCTION();
+ CriticalSection crit_;
+ mutable int access_count_ GUARDED_BY(crit_) = 0;
+ mutable PlatformThreadRef accessing_thread_ GUARDED_BY(crit_);
+};
+
+namespace internal {
+class SCOPED_LOCKABLE RaceCheckerScope {
+ public:
+ explicit RaceCheckerScope(const RaceChecker* race_checker)
+ EXCLUSIVE_LOCK_FUNCTION(race_checker);
+
+ bool RaceDetected() const;
+ ~RaceCheckerScope() UNLOCK_FUNCTION();
+
+ private:
+ const RaceChecker* const race_checker_;
+ const bool race_check_ok_;
+};
+
+class SCOPED_LOCKABLE RaceCheckerScopeDoNothing {
+ public:
+ explicit RaceCheckerScopeDoNothing(RaceChecker* race_checker)
+ EXCLUSIVE_LOCK_FUNCTION(race_checker) {}
+
+ ~RaceCheckerScopeDoNothing() UNLOCK_FUNCTION() {}
+};
+
+} // namespace internal
+} // namespace rtc
+
+#define RTC_CHECK_RUNS_SINGLE_THREADED(x) \
+ rtc::internal::RaceCheckerScope race_checker(x); \
+ RTC_CHECK(!race_checker.RaceDetected())
+
+#if RTC_DCHECK_IS_ON
+#define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \
+ rtc::internal::RaceCheckerScope race_checker(x); \
+ RTC_DCHECK(!race_checker.RaceDetected())
+#else
+#define RTC_DCHECK_RUNS_SINGLE_THREADED(x) \
+ rtc::internal::RaceCheckerScopeDoNothing race_checker(x)
+#endif
+
+#endif // WEBRTC_BASE_RACE_CHECKER_H_

Powered by Google App Engine
This is Rietveld 408576698