tracing_tls.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2021 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. #ifndef BASE_TRACING_TRACING_TLS_H_
  5. #define BASE_TRACING_TRACING_TLS_H_
  6. #include "base/base_export.h"
  7. #include "base/check.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/threading/thread_local.h"
  10. namespace base {
  11. namespace tracing {
  12. // Returns a thread-local flag that records whether the calling thread is
  13. // running trace event related code. This is used to avoid writing trace events
  14. // re-entrantly.
  15. BASE_EXPORT ThreadLocalBoolean* GetThreadIsInTraceEventTLS();
  16. // A scoped class for automatically setting and clearing a thread-local boolean
  17. // flag.
  18. class BASE_EXPORT AutoThreadLocalBoolean {
  19. public:
  20. explicit AutoThreadLocalBoolean(ThreadLocalBoolean* thread_local_boolean)
  21. : thread_local_boolean_(thread_local_boolean) {
  22. DCHECK(!thread_local_boolean_->Get());
  23. thread_local_boolean_->Set(true);
  24. }
  25. ~AutoThreadLocalBoolean() { thread_local_boolean_->Set(false); }
  26. AutoThreadLocalBoolean(const AutoThreadLocalBoolean&) = delete;
  27. AutoThreadLocalBoolean& operator=(const AutoThreadLocalBoolean&) = delete;
  28. private:
  29. raw_ptr<base::ThreadLocalBoolean> thread_local_boolean_;
  30. };
  31. } // namespace tracing
  32. } // namespace base
  33. #endif // BASE_TRACING_TRACING_TLS_H_