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.

71 lines
2.3 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/system/thread_registry.h"
  11. #include <map>
  12. #include <utility>
  13. #include "absl/base/attributes.h"
  14. #include "rtc_base/critical_section.h"
  15. #include "rtc_base/logging.h"
  16. #include "rtc_base/platform_thread_types.h"
  17. #include "sdk/android/native_api/stacktrace/stacktrace.h"
  18. namespace webrtc {
  19. namespace {
  20. struct ThreadData {
  21. const rtc::PlatformThreadId thread_id;
  22. const rtc::Location location;
  23. };
  24. // The map of registered threads, and the lock that protects it. We create the
  25. // map on first use, and never destroy it.
  26. ABSL_CONST_INIT rtc::GlobalLock g_thread_registry_lock;
  27. ABSL_CONST_INIT std::map<const ScopedRegisterThreadForDebugging*, ThreadData>*
  28. g_registered_threads = nullptr;
  29. } // namespace
  30. ScopedRegisterThreadForDebugging::ScopedRegisterThreadForDebugging(
  31. rtc::Location location) {
  32. rtc::GlobalLockScope gls(&g_thread_registry_lock);
  33. if (g_registered_threads == nullptr) {
  34. g_registered_threads =
  35. new std::map<const ScopedRegisterThreadForDebugging*, ThreadData>();
  36. }
  37. const auto result = g_registered_threads->insert(
  38. std::make_pair(this, ThreadData{rtc::CurrentThreadId(), location}));
  39. RTC_DCHECK(result.second); // Insertion succeeded without collisions.
  40. }
  41. ScopedRegisterThreadForDebugging::~ScopedRegisterThreadForDebugging() {
  42. rtc::GlobalLockScope gls(&g_thread_registry_lock);
  43. RTC_DCHECK(g_registered_threads != nullptr);
  44. const int num_erased = g_registered_threads->erase(this);
  45. RTC_DCHECK_EQ(num_erased, 1);
  46. }
  47. void PrintStackTracesOfRegisteredThreads() {
  48. rtc::GlobalLockScope gls(&g_thread_registry_lock);
  49. if (g_registered_threads == nullptr) {
  50. return;
  51. }
  52. for (const auto& e : *g_registered_threads) {
  53. const ThreadData& td = e.second;
  54. RTC_LOG(LS_WARNING) << "Thread " << td.thread_id << " registered at "
  55. << td.location.ToString() << ":";
  56. RTC_LOG(LS_WARNING) << StackTraceToString(GetStackTrace(td.thread_id));
  57. }
  58. }
  59. } // namespace webrtc