| 6 |
| 7 | #include "base/lazy_instance.h" |
| 8 | #include "base/threading/thread_local.h" |
| 9 | #include "base/threading/thread_restrictions.h" |
| 10 | #include "base/time/time.h" |
| 11 | #include "base/trace_event/trace_event.h" |
| 12 | #include "build/build_config.h" |
| 13 |
| 14 | namespace base { |
| 15 |
| 16 | namespace { |
| 17 |
| 18 | #if DCHECK_IS_ON() |
| 19 | // Used to verify that the trace events used in the constructor do not result in |
| 20 | // instantiating a ScopedBlockingCall themselves (which would cause an infinite |
| 21 | // reentrancy loop). |
| 22 | LazyInstance<ThreadLocalBoolean>::Leaky tls_construction_in_progress = |
| 23 | LAZY_INSTANCE_INITIALIZER; |
| 24 | #endif |
| 25 |
| 26 | } // namespace |
| 27 |
| 28 | ScopedBlockingCall::ScopedBlockingCall(const Location& from_here, |
| 29 | BlockingType blocking_type) |
| 30 | : UncheckedScopedBlockingCall(from_here, blocking_type) { |
| 31 | #if DCHECK_IS_ON() |
| 32 | DCHECK(!tls_construction_in_progress.Get().Get()); |
| 33 | tls_construction_in_progress.Get().Set(true); |
| 34 | #endif |
| 35 |
| 36 | internal::AssertBlockingAllowed(); |
| 37 | TRACE_EVENT_BEGIN2("base", "ScopedBlockingCall", "file_name", |
| 38 | from_here.file_name(), "function_name", |
| 39 | from_here.function_name()); |
| 40 | #if DCHECK_IS_ON() |
| 41 | tls_construction_in_progress.Get().Set(false); |
| 42 | #endif |
| 43 | } |
| 44 |
| 45 | ScopedBlockingCall::~ScopedBlockingCall() { |
| 46 | TRACE_EVENT_END0("base", "ScopedBlockingCall"); |
| 47 | } |
| 48 |
| 49 | namespace internal { |
| 50 |
| 51 | ScopedBlockingCallWithBaseSyncPrimitives:: |
| 52 | ScopedBlockingCallWithBaseSyncPrimitives(const Location& from_here, |
| 53 | BlockingType blocking_type) |
| 54 | : UncheckedScopedBlockingCall(from_here, blocking_type) { |
| 55 | #if DCHECK_IS_ON() |
| 56 | DCHECK(!tls_construction_in_progress.Get().Get()); |
| 57 | tls_construction_in_progress.Get().Set(true); |
| 58 | #endif |
| 59 |
| 60 | internal::AssertBaseSyncPrimitivesAllowed(); |
| 61 | TRACE_EVENT_BEGIN2("base", "ScopedBlockingCallWithBaseSyncPrimitives", |
| 62 | "file_name", from_here.file_name(), "function_name", |
| 63 | from_here.function_name()); |
| 64 |
| 65 | #if DCHECK_IS_ON() |
| 66 | tls_construction_in_progress.Get().Set(false); |
| 67 | #endif |
| 68 | } |
| 69 |
| 70 | ScopedBlockingCallWithBaseSyncPrimitives:: |
| 71 | ~ScopedBlockingCallWithBaseSyncPrimitives() { |
| 72 | TRACE_EVENT_END0("base", "ScopedBlockingCallWithBaseSyncPrimitives"); |
| 73 | } |
| 74 |
| 75 | } // namespace internal |
| 76 |
| 77 | } // namespace base |
| 78 |