| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_THREADING_THREAD_CHECKER_H_ | 5 #ifndef BASE_THREADING_THREAD_CHECKER_H_ |
| 6 #define BASE_THREADING_THREAD_CHECKER_H_ | 6 #define BASE_THREADING_THREAD_CHECKER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #ifndef NDEBUG | 9 // Apart from debug builds, we also enable the thread checker in |
| 10 // builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots |
| 11 // with this define will get the same level of thread checking as |
| 12 // debug bots. |
| 13 // |
| 14 // Note that this does not perfectly match situations where DCHECK is |
| 15 // enabled. For example a non-official release build may have |
| 16 // DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be |
| 17 // disabled) but have DCHECKs enabled at runtime. |
| 18 #define ENABLE_THREAD_CHECKER (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 19 |
| 20 #if ENABLE_THREAD_CHECKER |
| 10 #include "base/threading/thread_checker_impl.h" | 21 #include "base/threading/thread_checker_impl.h" |
| 11 #endif | 22 #endif |
| 12 | 23 |
| 13 namespace base { | 24 namespace base { |
| 14 | 25 |
| 15 // Do nothing implementation, for use in release mode. | 26 // Do nothing implementation, for use in release mode. |
| 16 // | 27 // |
| 17 // Note: You should almost always use the ThreadChecker class to get the | 28 // Note: You should almost always use the ThreadChecker class to get the |
| 18 // right version for your build configuration. | 29 // right version for your build configuration. |
| 19 class ThreadCheckerDoNothing { | 30 class ThreadCheckerDoNothing { |
| 20 public: | 31 public: |
| 21 bool CalledOnValidThread() const { | 32 bool CalledOnValidThread() const { |
| 22 return true; | 33 return true; |
| 23 } | 34 } |
| 24 | 35 |
| 25 void DetachFromThread() {} | 36 void DetachFromThread() {} |
| 37 |
| 38 static bool IsEnabled() { |
| 39 return false; |
| 40 } |
| 26 }; | 41 }; |
| 27 | 42 |
| 28 // Before using this class, please consider using NonThreadSafe as it | 43 // Before using this class, please consider using NonThreadSafe as it |
| 29 // makes it much easier to determine the nature of your class. | 44 // makes it much easier to determine the nature of your class. |
| 30 // | 45 // |
| 31 // ThreadChecker is a helper class used to help verify that some methods of a | 46 // 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 | 47 // class are called from the same thread. One can inherit from this class and |
| 33 // use CalledOnValidThread() to verify. | 48 // use CalledOnValidThread() to verify. |
| 34 // | 49 // |
| 35 // Inheriting from class indicates that one must be careful when using the | 50 // 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 | 51 // class with multiple threads. However, it is up to the class document to |
| 37 // indicate how it can be used with threads. | 52 // indicate how it can be used with threads. |
| 38 // | 53 // |
| 39 // Example: | 54 // Example: |
| 40 // class MyClass : public ThreadChecker { | 55 // class MyClass : public ThreadChecker { |
| 41 // public: | 56 // public: |
| 42 // void Foo() { | 57 // void Foo() { |
| 43 // DCHECK(CalledOnValidThread()); | 58 // DCHECK(CalledOnValidThread()); |
| 44 // ... (do stuff) ... | 59 // ... (do stuff) ... |
| 45 // } | 60 // } |
| 46 // } | 61 // } |
| 47 // | 62 // |
| 48 // In Release mode, CalledOnValidThread will always return true. | 63 // In Release mode, CalledOnValidThread will always return true. |
| 49 #ifndef NDEBUG | 64 #if ENABLE_THREAD_CHECKER |
| 50 class ThreadChecker : public ThreadCheckerImpl { | 65 class ThreadChecker : public ThreadCheckerImpl { |
| 51 }; | 66 }; |
| 52 #else | 67 #else |
| 53 class ThreadChecker : public ThreadCheckerDoNothing { | 68 class ThreadChecker : public ThreadCheckerDoNothing { |
| 54 }; | 69 }; |
| 55 #endif // NDEBUG | 70 #endif // ENABLE_THREAD_CHECKER |
| 71 |
| 72 #undef ENABLE_THREAD_CHECKER |
| 56 | 73 |
| 57 } // namespace base | 74 } // namespace base |
| 58 | 75 |
| 59 #endif // BASE_THREADING_THREAD_CHECKER_H_ | 76 #endif // BASE_THREADING_THREAD_CHECKER_H_ |
| OLD | NEW |