webrtc/rtc_base/race_checker.cc
Peter Kasting 662d7f11d5 Fixes to support building in -std=c++20 mode.
* Structs with user-declared constructors are no longer considered
  aggregates, so remove the declarations when possible
* Types of both arguments to "==" must match to avoid "ambiguous
  function call" warning
* Various types of math involving enums are deprecated, so replace with
  constexprs where necessary
* ABSL_CONST_INIT must be used on definition as well as declaration
* volatile memory may no longer be read from and written to by the same
  operator, so replace e.g. "n++" with "n = n + 1"
* Replace an outdated check for no_unique_address support with
  __has_cpp_attribute
* std::result_of(f(x)) has been removed, replace with
  std::invoke_result(f, x)

Bug: chromium:1284275
Change-Id: I77b366ab1da7eb2c1e4c825b2714417c31ee5903
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/261221
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Tomas Gunnarsson <tommi@google.com>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36786}
2022-05-05 17:15:58 +00:00

56 lines
2.2 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.
*/
#include "rtc_base/race_checker.h"
namespace rtc {
RaceChecker::RaceChecker() {}
// Note that the implementation here is in itself racy, but we pretend it does
// not matter because we want this useful in release builds without having to
// pay the cost of using atomics. A race hitting the race checker is likely to
// cause access_count_ to diverge from zero and therefore cause the ThreadRef
// comparison to fail, signaling a race, although it may not be in the exact
// spot where a race *first* appeared in the code we're trying to protect. There
// is also a chance that an actual race is missed, however the probability of
// that has been considered small enough to be an acceptable trade off.
bool RaceChecker::Acquire() const {
const PlatformThreadRef current_thread = CurrentThreadRef();
// Set new accessing thread if this is a new use.
const int current_access_count = access_count_;
access_count_ = access_count_ + 1;
if (current_access_count == 0)
accessing_thread_ = current_thread;
// If this is being used concurrently this check will fail for the second
// thread entering since it won't set the thread. Recursive use of checked
// methods are OK since the accessing thread remains the same.
const PlatformThreadRef accessing_thread = accessing_thread_;
return IsThreadRefEqual(accessing_thread, current_thread);
}
void RaceChecker::Release() const {
access_count_ = access_count_ - 1;
}
namespace internal {
RaceCheckerScope::RaceCheckerScope(const RaceChecker* race_checker)
: race_checker_(race_checker), race_check_ok_(race_checker->Acquire()) {}
bool RaceCheckerScope::RaceDetected() const {
return !race_check_ok_;
}
RaceCheckerScope::~RaceCheckerScope() {
race_checker_->Release();
}
} // namespace internal
} // namespace rtc