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 #include "webrtc/base/race_checker.h" |
| 12 |
| 13 namespace rtc { |
| 14 RaceChecker::RaceChecker() {} |
| 15 bool RaceChecker::Acquire() const { |
| 16 const PlatformThreadRef current_thread = CurrentThreadRef(); |
| 17 // Set new accessing thread if this is a new use. |
| 18 if (access_count_++ == 0) |
| 19 accessing_thread_ = current_thread; |
| 20 // If this is being used concurrently this check will fail for the second |
| 21 // thread entering since it won't set the thread. Recursive use of checked |
| 22 // methods are OK since the accessing thread remains the same. |
| 23 return IsThreadRefEqual(accessing_thread_, current_thread); |
| 24 } |
| 25 |
| 26 void RaceChecker::Release() const { |
| 27 --access_count_; |
| 28 } |
| 29 |
| 30 namespace internal { |
| 31 RaceCheckerScope::RaceCheckerScope(const RaceChecker* race_checker) |
| 32 : race_checker_(race_checker), race_check_ok_(race_checker->Acquire()) {} |
| 33 |
| 34 bool RaceCheckerScope::RaceDetected() const { |
| 35 return !race_check_ok_; |
| 36 } |
| 37 |
| 38 RaceCheckerScope::~RaceCheckerScope() { |
| 39 race_checker_->Release(); |
| 40 } |
| 41 |
| 42 } // namespace internal |
| 43 } // namespace rtc |
OLD | NEW |