scoped_blocking_call.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/threading/scoped_blocking_call.h"
  5. #include "base/lazy_instance.h"
  6. #include "base/threading/thread_local.h"
  7. #include "base/threading/thread_restrictions.h"
  8. #include "base/time/time.h"
  9. #include "base/trace_event/base_tracing.h"
  10. #include "base/tracing_buildflags.h"
  11. #include "build/build_config.h"
  12. #if BUILDFLAG(ENABLE_BASE_TRACING)
  13. #include "third_party/perfetto/protos/perfetto/trace/track_event/source_location.pbzero.h" // nogncheck
  14. #endif // BUILDFLAG(ENABLE_BASE_TRACING)
  15. namespace base {
  16. namespace {
  17. #if DCHECK_IS_ON()
  18. // Used to verify that the trace events used in the constructor do not result in
  19. // instantiating a ScopedBlockingCall themselves (which would cause an infinite
  20. // reentrancy loop).
  21. LazyInstance<ThreadLocalBoolean>::Leaky tls_construction_in_progress =
  22. LAZY_INSTANCE_INITIALIZER;
  23. #endif
  24. } // namespace
  25. ScopedBlockingCall::ScopedBlockingCall(const Location& from_here,
  26. BlockingType blocking_type)
  27. : UncheckedScopedBlockingCall(
  28. from_here,
  29. blocking_type,
  30. UncheckedScopedBlockingCall::BlockingCallType::kRegular) {
  31. #if DCHECK_IS_ON()
  32. DCHECK(!tls_construction_in_progress.Get().Get());
  33. tls_construction_in_progress.Get().Set(true);
  34. #endif
  35. internal::AssertBlockingAllowed();
  36. TRACE_EVENT_BEGIN(
  37. "base", "ScopedBlockingCall", [&](perfetto::EventContext ctx) {
  38. ctx.event()->set_source_location_iid(
  39. base::trace_event::InternedSourceLocation::Get(&ctx, from_here));
  40. });
  41. #if DCHECK_IS_ON()
  42. tls_construction_in_progress.Get().Set(false);
  43. #endif
  44. }
  45. ScopedBlockingCall::~ScopedBlockingCall() {
  46. TRACE_EVENT_END("base");
  47. }
  48. namespace internal {
  49. ScopedBlockingCallWithBaseSyncPrimitives::
  50. ScopedBlockingCallWithBaseSyncPrimitives(const Location& from_here,
  51. BlockingType blocking_type)
  52. : UncheckedScopedBlockingCall(
  53. from_here,
  54. blocking_type,
  55. UncheckedScopedBlockingCall::BlockingCallType::kBaseSyncPrimitives) {
  56. #if DCHECK_IS_ON()
  57. DCHECK(!tls_construction_in_progress.Get().Get());
  58. tls_construction_in_progress.Get().Set(true);
  59. #endif
  60. internal::AssertBaseSyncPrimitivesAllowed();
  61. TRACE_EVENT_BEGIN(
  62. "base", "ScopedBlockingCallWithBaseSyncPrimitives",
  63. [&](perfetto::EventContext ctx) {
  64. perfetto::protos::pbzero::SourceLocation* source_location_data =
  65. ctx.event()->set_source_location();
  66. source_location_data->set_file_name(from_here.file_name());
  67. source_location_data->set_function_name(from_here.function_name());
  68. });
  69. #if DCHECK_IS_ON()
  70. tls_construction_in_progress.Get().Set(false);
  71. #endif
  72. }
  73. ScopedBlockingCallWithBaseSyncPrimitives::
  74. ~ScopedBlockingCallWithBaseSyncPrimitives() {
  75. TRACE_EVENT_END("base");
  76. }
  77. } // namespace internal
  78. } // namespace base