webrtc_m130/rtc_base/race_checker.h
Oleh Prypin 96a0f61917 Revert "[cleanup] Remove useless includes."
This reverts commit be8b5348c76105f8fe869b0cae4065ddca106419.

Reason for revert: Breaks downstream project

Original change's description:
> [cleanup] Remove useless includes.
> 
> Manual cleanup guided by include-what-you-use diagnostic.
> 
> Bug: webrtc:8311
> Change-Id: I00be03392cc7ee005101427ea7dc701621ccea68
> Reviewed-on: https://webrtc-review.googlesource.com/c/103320
> Commit-Queue: Yves Gerey <yvesg@webrtc.org>
> Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#25013}

TBR=phoglund@google.com,phoglund@webrtc.org,yvesg@webrtc.org

Change-Id: I7a6e1cdfef685173b76f234ad598083043dcd9a0
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:8311
Reviewed-on: https://webrtc-review.googlesource.com/c/104022
Reviewed-by: Oleh Prypin <oprypin@webrtc.org>
Commit-Queue: Oleh Prypin <oprypin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25015}
2018-10-05 13:13:45 +00:00

79 lines
2.3 KiB
C++

/*
* 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 RTC_BASE_RACE_CHECKER_H_
#define RTC_BASE_RACE_CHECKER_H_
#include "rtc_base/checks.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/thread_annotations.h"
namespace rtc {
namespace internal {
class RaceCheckerScope;
} // namespace internal
// Best-effort race-checking implementation. This primitive uses no
// synchronization at all to be as-fast-as-possible in the non-racy case.
class RTC_LOCKABLE RaceChecker {
public:
friend class internal::RaceCheckerScope;
RaceChecker();
private:
bool Acquire() const RTC_EXCLUSIVE_LOCK_FUNCTION();
void Release() const RTC_UNLOCK_FUNCTION();
// Volatile to prevent code being optimized away in Acquire()/Release().
mutable volatile int access_count_ = 0;
mutable volatile PlatformThreadRef accessing_thread_;
};
namespace internal {
class RTC_SCOPED_LOCKABLE RaceCheckerScope {
public:
explicit RaceCheckerScope(const RaceChecker* race_checker)
RTC_EXCLUSIVE_LOCK_FUNCTION(race_checker);
bool RaceDetected() const;
~RaceCheckerScope() RTC_UNLOCK_FUNCTION();
private:
const RaceChecker* const race_checker_;
const bool race_check_ok_;
};
class RTC_SCOPED_LOCKABLE RaceCheckerScopeDoNothing {
public:
explicit RaceCheckerScopeDoNothing(const RaceChecker* race_checker)
RTC_EXCLUSIVE_LOCK_FUNCTION(race_checker) {}
~RaceCheckerScopeDoNothing() RTC_UNLOCK_FUNCTION() {}
};
} // namespace internal
} // namespace rtc
#define RTC_CHECK_RUNS_SERIALIZED(x) \
rtc::internal::RaceCheckerScope race_checker(x); \
RTC_CHECK(!race_checker.RaceDetected())
#if RTC_DCHECK_IS_ON
#define RTC_DCHECK_RUNS_SERIALIZED(x) \
rtc::internal::RaceCheckerScope race_checker(x); \
RTC_DCHECK(!race_checker.RaceDetected())
#else
#define RTC_DCHECK_RUNS_SERIALIZED(x) \
rtc::internal::RaceCheckerScopeDoNothing race_checker(x)
#endif
#endif // RTC_BASE_RACE_CHECKER_H_