Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 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 | |
| 15 | RaceChecker::RaceChecker() {} |
| 16 | |
| 17 | bool RaceChecker::Acquire() const { |
| 18 | const PlatformThreadRef current_thread = CurrentThreadRef(); |
| 19 | // Set new accessing thread if this is a new use. |
| 20 | if (access_count_++ == 0) |
| 21 | accessing_thread_ = current_thread; |
| 22 | // If this is being used concurrently this check will fail for the second |
| 23 | // thread entering since it won't set the thread. Recursive use of checked |
| 24 | // methods are OK since the accessing thread remains the same. |
| 25 | const PlatformThreadRef accessing_thread = accessing_thread_; |
| 26 | return IsThreadRefEqual(accessing_thread, current_thread); |
| 27 | } |
| 28 | |
| 29 | void RaceChecker::Release() const { |
| 30 | --access_count_; |
| 31 | } |
| 32 | |
| 33 | namespace internal { |
| 34 | RaceCheckerScope::RaceCheckerScope(const RaceChecker* race_checker) |
| 35 | : race_checker_(race_checker), race_check_ok_(race_checker->Acquire()) {} |
| 36 | |
| 37 | bool RaceCheckerScope::RaceDetected() const { |
| 38 | return !race_check_ok_; |
| 39 | } |
| 40 | |
| 41 | RaceCheckerScope::~RaceCheckerScope() { |
| 42 | race_checker_->Release(); |
| 43 | } |
| 44 | |
| 45 | } // namespace internal |
| 46 | } // namespace rtc |