blob: d7f46ead7df4ca845926490c2dc3b7159c18ba45 [file] [log] [blame]
perkj9c16fe82016-07-12 15:04:07 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/sequenced_task_checker_impl.h"
perkj9c16fe82016-07-12 15:04:07 -070012
kthelgason44e0efe2016-11-02 10:28:20 -070013#if defined(WEBRTC_MAC)
14#include <dispatch/dispatch.h>
15#endif
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/platform_thread.h"
18#include "rtc_base/sequenced_task_checker.h"
19#include "rtc_base/task_queue.h"
perkj9c16fe82016-07-12 15:04:07 -070020
21namespace rtc {
22
23SequencedTaskCheckerImpl::SequencedTaskCheckerImpl()
24 : attached_(true), valid_queue_(TaskQueue::Current()) {}
25
26SequencedTaskCheckerImpl::~SequencedTaskCheckerImpl() {}
27
28bool SequencedTaskCheckerImpl::CalledSequentially() const {
kthelgason44e0efe2016-11-02 10:28:20 -070029 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
perkj9c16fe82016-07-12 15:04:07 -070036 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
46void SequencedTaskCheckerImpl::Detach() {
47 CritScope scoped_lock(&lock_);
48 attached_ = false;
49 valid_queue_ = nullptr;
50 thread_checker_.DetachFromThread();
51}
52
53namespace internal {
54
55SequencedTaskCheckerScope::SequencedTaskCheckerScope(
56 const SequencedTaskChecker* checker) {
57 RTC_DCHECK(checker->CalledSequentially());
58}
59
60SequencedTaskCheckerScope::~SequencedTaskCheckerScope() {}
61
62} // namespace internal
63} // namespace rtc