xref: /aosp_15_r20/external/cronet/base/threading/thread_checker.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/threading/thread_checker.h"
6 
7 #if DCHECK_IS_ON()
8 #include <memory>
9 #include <ostream>
10 #include <string_view>
11 
12 #include "base/check.h"
13 #include "base/debug/stack_trace.h"
14 #endif
15 
16 namespace base {
17 
18 #if DCHECK_IS_ON()
ScopedValidateThreadChecker(const ThreadChecker & checker)19 ScopedValidateThreadChecker::ScopedValidateThreadChecker(
20     const ThreadChecker& checker) {
21   std::unique_ptr<debug::StackTrace> bound_at;
22   DCHECK(checker.CalledOnValidThread(&bound_at))
23       << (bound_at ? "\nWas attached to thread at:\n" + bound_at->ToString()
24                    : "");
25 }
26 
ScopedValidateThreadChecker(const ThreadChecker & checker,std::string_view msg)27 ScopedValidateThreadChecker::ScopedValidateThreadChecker(
28     const ThreadChecker& checker,
29     std::string_view msg) {
30   std::unique_ptr<debug::StackTrace> bound_at;
31   DCHECK(checker.CalledOnValidThread(&bound_at))
32       << msg
33       << (bound_at ? "\nWas attached to thread at:\n" + bound_at->ToString()
34                    : "");
35 }
36 
37 ScopedValidateThreadChecker::~ScopedValidateThreadChecker() = default;
38 #endif  // DCHECK_IS_ON()
39 
40 }  // namespace base
41