• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 
5 #ifndef BASE_THREADING_THREAD_CHECKER_H_
6 #define BASE_THREADING_THREAD_CHECKER_H_
7 #pragma once
8 
9 #ifndef NDEBUG
10 #include "base/threading/thread_checker_impl.h"
11 #endif
12 
13 namespace base {
14 
15 // Do nothing implementation, for use in release mode.
16 //
17 // Note: You should almost always use the ThreadChecker class to get the
18 // right version for your build configuration.
19 class ThreadCheckerDoNothing {
20  public:
CalledOnValidThread()21   bool CalledOnValidThread() const {
22     return true;
23   }
24 
DetachFromThread()25   void DetachFromThread() {}
26 };
27 
28 // Before using this class, please consider using NonThreadSafe as it
29 // makes it much easier to determine the nature of your class.
30 //
31 // ThreadChecker is a helper class used to help verify that some methods of a
32 // class are called from the same thread.  One can inherit from this class and
33 // use CalledOnValidThread() to verify.
34 //
35 // Inheriting from class indicates that one must be careful when using the
36 // class with multiple threads. However, it is up to the class document to
37 // indicate how it can be used with threads.
38 //
39 // Example:
40 // class MyClass : public ThreadChecker {
41 //  public:
42 //   void Foo() {
43 //     DCHECK(CalledOnValidThread());
44 //     ... (do stuff) ...
45 //   }
46 // }
47 //
48 // In Release mode, CalledOnValidThread will always return true.
49 #ifndef NDEBUG
50 class ThreadChecker : public ThreadCheckerImpl {
51 };
52 #else
53 class ThreadChecker : public ThreadCheckerDoNothing {
54 };
55 #endif  // NDEBUG
56 
57 }  // namespace base
58 
59 #endif  // BASE_THREADING_THREAD_CHECKER_H_
60