You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #if defined(WEBRTC_MAC)
  12. #include <dispatch/dispatch.h>
  13. #endif
  14. namespace webrtc {
  15. namespace {
  16. // On Mac, returns the label of the current dispatch queue; elsewhere, return
  17. // null.
  18. const void* GetSystemQueueRef() {
  19. #if defined(WEBRTC_MAC)
  20. return dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
  21. #else
  22. return nullptr;
  23. #endif
  24. }
  25. } // namespace
  26. SequenceCheckerImpl::SequenceCheckerImpl()
  27. : attached_(true),
  28. valid_thread_(rtc::CurrentThreadRef()),
  29. valid_queue_(TaskQueueBase::Current()),
  30. valid_system_queue_(GetSystemQueueRef()) {}
  31. SequenceCheckerImpl::~SequenceCheckerImpl() = default;
  32. bool SequenceCheckerImpl::IsCurrent() const {
  33. const TaskQueueBase* const current_queue = TaskQueueBase::Current();
  34. const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
  35. const void* const current_system_queue = GetSystemQueueRef();
  36. rtc::CritScope scoped_lock(&lock_);
  37. if (!attached_) { // Previously detached.
  38. attached_ = true;
  39. valid_thread_ = current_thread;
  40. valid_queue_ = current_queue;
  41. valid_system_queue_ = current_system_queue;
  42. return true;
  43. }
  44. if (valid_queue_ || current_queue) {
  45. return valid_queue_ == current_queue;
  46. }
  47. if (valid_system_queue_ && valid_system_queue_ == current_system_queue) {
  48. return true;
  49. }
  50. return rtc::IsThreadRefEqual(valid_thread_, current_thread);
  51. }
  52. void SequenceCheckerImpl::Detach() {
  53. rtc::CritScope scoped_lock(&lock_);
  54. attached_ = false;
  55. // We don't need to touch the other members here, they will be
  56. // reset on the next call to IsCurrent().
  57. }
  58. } // namespace webrtc