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)19ScopedValidateThreadChecker::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)27ScopedValidateThreadChecker::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