• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2019 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 #include "rtc_base/synchronization/sequence_checker.h"
11 
12 #if defined(WEBRTC_MAC)
13 #include <dispatch/dispatch.h>
14 #endif
15 
16 #include "rtc_base/strings/string_builder.h"
17 
18 namespace webrtc {
19 namespace {
20 // On Mac, returns the label of the current dispatch queue; elsewhere, return
21 // null.
GetSystemQueueRef()22 const void* GetSystemQueueRef() {
23 #if defined(WEBRTC_MAC)
24   return dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
25 #else
26   return nullptr;
27 #endif
28 }
29 
30 }  // namespace
31 
ExpectationToString(const webrtc::SequenceChecker * checker)32 std::string ExpectationToString(const webrtc::SequenceChecker* checker) {
33 #if RTC_DCHECK_IS_ON
34   return checker->ExpectationToString();
35 #endif
36   return std::string();
37 }
38 
SequenceCheckerImpl()39 SequenceCheckerImpl::SequenceCheckerImpl()
40     : attached_(true),
41       valid_thread_(rtc::CurrentThreadRef()),
42       valid_queue_(TaskQueueBase::Current()),
43       valid_system_queue_(GetSystemQueueRef()) {}
44 
45 SequenceCheckerImpl::~SequenceCheckerImpl() = default;
46 
IsCurrent() const47 bool SequenceCheckerImpl::IsCurrent() const {
48   const TaskQueueBase* const current_queue = TaskQueueBase::Current();
49   const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
50   const void* const current_system_queue = GetSystemQueueRef();
51   MutexLock scoped_lock(&lock_);
52   if (!attached_) {  // Previously detached.
53     attached_ = true;
54     valid_thread_ = current_thread;
55     valid_queue_ = current_queue;
56     valid_system_queue_ = current_system_queue;
57     return true;
58   }
59   if (valid_queue_ || current_queue) {
60     return valid_queue_ == current_queue;
61   }
62   if (valid_system_queue_ && valid_system_queue_ == current_system_queue) {
63     return true;
64   }
65   return rtc::IsThreadRefEqual(valid_thread_, current_thread);
66 }
67 
Detach()68 void SequenceCheckerImpl::Detach() {
69   MutexLock scoped_lock(&lock_);
70   attached_ = false;
71   // We don't need to touch the other members here, they will be
72   // reset on the next call to IsCurrent().
73 }
74 
75 #if RTC_DCHECK_IS_ON
ExpectationToString() const76 std::string SequenceCheckerImpl::ExpectationToString() const {
77   const TaskQueueBase* const current_queue = TaskQueueBase::Current();
78   const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
79   const void* const current_system_queue = GetSystemQueueRef();
80   MutexLock scoped_lock(&lock_);
81   if (!attached_)
82     return "Checker currently not attached.";
83 
84   // The format of the string is meant to compliment the one we have inside of
85   // FatalLog() (checks.cc).  Example:
86   //
87   // # Expected: TQ: 0x0 SysQ: 0x7fff69541330 Thread: 0x11dcf6dc0
88   // # Actual:   TQ: 0x7fa8f0604190 SysQ: 0x7fa8f0604a30 Thread: 0x700006f1a000
89   // TaskQueue doesn't match
90 
91   rtc::StringBuilder message;
92   message.AppendFormat(
93       "# Expected: TQ: %p SysQ: %p Thread: %p\n"
94       "# Actual:   TQ: %p SysQ: %p Thread: %p\n",
95       valid_queue_, valid_system_queue_,
96       reinterpret_cast<const void*>(valid_thread_), current_queue,
97       current_system_queue, reinterpret_cast<const void*>(current_thread));
98 
99   if ((valid_queue_ || current_queue) && valid_queue_ != current_queue) {
100     message << "TaskQueue doesn't match\n";
101   } else if (valid_system_queue_ &&
102              valid_system_queue_ != current_system_queue) {
103     message << "System queue doesn't match\n";
104   } else if (!rtc::IsThreadRefEqual(valid_thread_, current_thread)) {
105     message << "Threads don't match\n";
106   }
107 
108   return message.Release();
109 }
110 #endif  // RTC_DCHECK_IS_ON
111 
112 }  // namespace webrtc
113