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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/sequenced_task_checker_impl.h" |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 12 | |
kthelgason | 44e0efe | 2016-11-02 10:28:20 -0700 | [diff] [blame] | 13 | #if defined(WEBRTC_MAC) |
| 14 | #include <dispatch/dispatch.h> |
| 15 | #endif |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/platform_thread.h" |
| 18 | #include "rtc_base/sequenced_task_checker.h" |
| 19 | #include "rtc_base/task_queue.h" |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | SequencedTaskCheckerImpl::SequencedTaskCheckerImpl() |
| 24 | : attached_(true), valid_queue_(TaskQueue::Current()) {} |
| 25 | |
| 26 | SequencedTaskCheckerImpl::~SequencedTaskCheckerImpl() {} |
| 27 | |
| 28 | bool SequencedTaskCheckerImpl::CalledSequentially() const { |
kthelgason | 44e0efe | 2016-11-02 10:28:20 -0700 | [diff] [blame] | 29 | QueueId current_queue = TaskQueue::Current(); |
| 30 | #if defined(WEBRTC_MAC) |
| 31 | // If we're not running on a TaskQueue, use the system dispatch queue |
| 32 | // label as an identifier. |
| 33 | if (current_queue == nullptr) |
| 34 | current_queue = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL); |
| 35 | #endif |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 36 | CritScope scoped_lock(&lock_); |
| 37 | if (!attached_) { // true if previously detached. |
| 38 | valid_queue_ = current_queue; |
| 39 | attached_ = true; |
| 40 | } |
| 41 | if (!valid_queue_) |
| 42 | return thread_checker_.CalledOnValidThread(); |
| 43 | return valid_queue_ == current_queue; |
| 44 | } |
| 45 | |
| 46 | void SequencedTaskCheckerImpl::Detach() { |
| 47 | CritScope scoped_lock(&lock_); |
| 48 | attached_ = false; |
| 49 | valid_queue_ = nullptr; |
| 50 | thread_checker_.DetachFromThread(); |
| 51 | } |
| 52 | |
| 53 | namespace internal { |
| 54 | |
| 55 | SequencedTaskCheckerScope::SequencedTaskCheckerScope( |
| 56 | const SequencedTaskChecker* checker) { |
| 57 | RTC_DCHECK(checker->CalledSequentially()); |
| 58 | } |
| 59 | |
| 60 | SequencedTaskCheckerScope::~SequencedTaskCheckerScope() {} |
| 61 | |
| 62 | } // namespace internal |
| 63 | } // namespace rtc |