blob: 16069c25f7ab064672d96f3bb0426b14790d2b47 [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/sequenced_task_checker.h"
18#include "rtc_base/task_queue.h"
perkj9c16fe82016-07-12 15:04:07 -070019
20namespace rtc {
21
22SequencedTaskCheckerImpl::SequencedTaskCheckerImpl()
23 : attached_(true), valid_queue_(TaskQueue::Current()) {}
24
25SequencedTaskCheckerImpl::~SequencedTaskCheckerImpl() {}
26
27bool SequencedTaskCheckerImpl::CalledSequentially() const {
kthelgason44e0efe2016-11-02 10:28:20 -070028 QueueId current_queue = TaskQueue::Current();
29#if defined(WEBRTC_MAC)
30 // If we're not running on a TaskQueue, use the system dispatch queue
31 // label as an identifier.
32 if (current_queue == nullptr)
33 current_queue = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
34#endif
perkj9c16fe82016-07-12 15:04:07 -070035 CritScope scoped_lock(&lock_);
36 if (!attached_) { // true if previously detached.
37 valid_queue_ = current_queue;
38 attached_ = true;
39 }
40 if (!valid_queue_)
41 return thread_checker_.CalledOnValidThread();
42 return valid_queue_ == current_queue;
43}
44
45void SequencedTaskCheckerImpl::Detach() {
46 CritScope scoped_lock(&lock_);
47 attached_ = false;
48 valid_queue_ = nullptr;
49 thread_checker_.DetachFromThread();
50}
51
52namespace internal {
53
54SequencedTaskCheckerScope::SequencedTaskCheckerScope(
55 const SequencedTaskChecker* checker) {
56 RTC_DCHECK(checker->CalledSequentially());
57}
58
59SequencedTaskCheckerScope::~SequencedTaskCheckerScope() {}
60
61} // namespace internal
62} // namespace rtc