summaryrefslogtreecommitdiff
path: root/chromium/base/tracing/tracing_tls.h
blob: 16aa159108c7ed5ee94ecf7f1791f5aeacf150e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_TRACING_TRACING_TLS_H_
#define BASE_TRACING_TRACING_TLS_H_

#include "base/base_export.h"
#include "base/threading/thread_local.h"

namespace base {
namespace tracing {

// Returns a thread-local flag that records whether the calling thread is
// running trace event related code. This is used to avoid writing trace events
// re-entrantly.
BASE_EXPORT ThreadLocalBoolean* GetThreadIsInTraceEventTLS();

// A scoped class for automatically setting and clearing a thread-local boolean
// flag.
class BASE_EXPORT AutoThreadLocalBoolean {
 public:
  explicit AutoThreadLocalBoolean(ThreadLocalBoolean* thread_local_boolean)
      : thread_local_boolean_(thread_local_boolean) {
    DCHECK(!thread_local_boolean_->Get());
    thread_local_boolean_->Set(true);
  }
  ~AutoThreadLocalBoolean() { thread_local_boolean_->Set(false); }
  AutoThreadLocalBoolean(const AutoThreadLocalBoolean&) = delete;
  AutoThreadLocalBoolean& operator=(const AutoThreadLocalBoolean&) = delete;

 private:
  base::ThreadLocalBoolean* thread_local_boolean_;
};

}  // namespace tracing
}  // namespace base

#endif  // BASE_TRACING_TRACING_TLS_H_