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