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