perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [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/sequenced_task_checker_impl.h" |
| 12 | |
| 13 | #include "webrtc/base/platform_thread.h" |
| 14 | #include "webrtc/base/sequenced_task_checker.h" |
perkj | 32012f8 | 2016-07-14 23:32:08 -0700 | [diff] [blame] | 15 | #include "webrtc/base/task_queue.h" |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | SequencedTaskCheckerImpl::SequencedTaskCheckerImpl() |
| 20 | : attached_(true), valid_queue_(TaskQueue::Current()) {} |
| 21 | |
| 22 | SequencedTaskCheckerImpl::~SequencedTaskCheckerImpl() {} |
| 23 | |
| 24 | bool SequencedTaskCheckerImpl::CalledSequentially() const { |
| 25 | TaskQueue* current_queue = TaskQueue::Current(); |
| 26 | CritScope scoped_lock(&lock_); |
| 27 | if (!attached_) { // true if previously detached. |
| 28 | valid_queue_ = current_queue; |
| 29 | attached_ = true; |
| 30 | } |
| 31 | if (!valid_queue_) |
| 32 | return thread_checker_.CalledOnValidThread(); |
| 33 | return valid_queue_ == current_queue; |
| 34 | } |
| 35 | |
| 36 | void SequencedTaskCheckerImpl::Detach() { |
| 37 | CritScope scoped_lock(&lock_); |
| 38 | attached_ = false; |
| 39 | valid_queue_ = nullptr; |
| 40 | thread_checker_.DetachFromThread(); |
| 41 | } |
| 42 | |
| 43 | namespace internal { |
| 44 | |
| 45 | SequencedTaskCheckerScope::SequencedTaskCheckerScope( |
| 46 | const SequencedTaskChecker* checker) { |
| 47 | RTC_DCHECK(checker->CalledSequentially()); |
| 48 | } |
| 49 | |
| 50 | SequencedTaskCheckerScope::~SequencedTaskCheckerScope() {} |
| 51 | |
| 52 | } // namespace internal |
| 53 | } // namespace rtc |