| 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 #ifndef NDEBUG |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/threading/thread_checker_impl.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #endif |
| 12 #endif // NDEBUG | |
| 13 | 12 |
| 14 namespace base { | 13 namespace base { |
| 15 | 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: |
| 21 bool CalledOnValidThread() const { |
| 22 return true; |
| 23 } |
| 24 |
| 25 void DetachFromThread() {} |
| 26 }; |
| 27 |
| 16 // Before using this class, please consider using NonThreadSafe as it | 28 // Before using this class, please consider using NonThreadSafe as it |
| 17 // makes it much easier to determine the nature of your class. | 29 // makes it much easier to determine the nature of your class. |
| 18 // | 30 // |
| 19 // A helper class used to help verify that some methods of a class are | 31 // ThreadChecker is a helper class used to help verify that some methods of a |
| 20 // called from the same thread. One can inherit from this class and use | 32 // class are called from the same thread. One can inherit from this class and |
| 21 // CalledOnValidThread() to verify. | 33 // use CalledOnValidThread() to verify. |
| 22 // | 34 // |
| 23 // Inheriting from class indicates that one must be careful when using the class | 35 // Inheriting from class indicates that one must be careful when using the |
| 24 // with multiple threads. However, it is up to the class document to indicate | 36 // class with multiple threads. However, it is up to the class document to |
| 25 // how it can be used with threads. | 37 // indicate how it can be used with threads. |
| 26 // | 38 // |
| 27 // Example: | 39 // Example: |
| 28 // class MyClass : public ThreadChecker { | 40 // class MyClass : public ThreadChecker { |
| 29 // public: | 41 // public: |
| 30 // void Foo() { | 42 // void Foo() { |
| 31 // DCHECK(CalledOnValidThread()); | 43 // DCHECK(CalledOnValidThread()); |
| 32 // ... (do stuff) ... | 44 // ... (do stuff) ... |
| 33 // } | 45 // } |
| 34 // } | 46 // } |
| 35 // | 47 // |
| 36 // In Release mode, CalledOnValidThread will always return true. | 48 // In Release mode, CalledOnValidThread will always return true. |
| 37 // | |
| 38 #ifndef NDEBUG | 49 #ifndef NDEBUG |
| 39 class ThreadChecker { | 50 class ThreadChecker : public ThreadCheckerImpl { |
| 40 public: | |
| 41 ThreadChecker(); | |
| 42 ~ThreadChecker(); | |
| 43 | |
| 44 bool CalledOnValidThread() const; | |
| 45 | |
| 46 // Changes the thread that is checked for in CalledOnValidThread. This may | |
| 47 // be useful when an object may be created on one thread and then used | |
| 48 // exclusively on another thread. | |
| 49 void DetachFromThread(); | |
| 50 | |
| 51 private: | |
| 52 void EnsureThreadIdAssigned() const; | |
| 53 | |
| 54 mutable base::Lock lock_; | |
| 55 // This is mutable so that CalledOnValidThread can set it. | |
| 56 // It's guarded by |lock_|. | |
| 57 mutable PlatformThreadId valid_thread_id_; | |
| 58 }; | 51 }; |
| 59 #else | 52 #else |
| 60 // Do nothing in release mode. | 53 class ThreadChecker : public ThreadCheckerDoNothing { |
| 61 class ThreadChecker { | |
| 62 public: | |
| 63 bool CalledOnValidThread() const { | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 void DetachFromThread() {} | |
| 68 }; | 54 }; |
| 69 #endif // NDEBUG | 55 #endif // NDEBUG |
| 70 | 56 |
| 71 } // namespace base | 57 } // namespace base |
| 72 | 58 |
| 73 #endif // BASE_THREADING_THREAD_CHECKER_H_ | 59 #endif // BASE_THREADING_THREAD_CHECKER_H_ |
| OLD | NEW |