| 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_NON_THREAD_SAFE_H_ | 5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_ |
| 6 #define BASE_THREADING_NON_THREAD_SAFE_H_ | 6 #define BASE_THREADING_NON_THREAD_SAFE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/threading/thread_checker.h" | 9 #ifndef NDEBUG |
| 10 #include "base/threading/non_thread_safe_impl.h" |
| 11 #endif |
| 10 | 12 |
| 11 namespace base { | 13 namespace base { |
| 12 | 14 |
| 13 // A helper class used to help verify that methods of a class are | 15 // Do nothing implementation of NonThreadSafe, for release mode. |
| 14 // called from the same thread. One can inherit from this class and use | 16 // |
| 15 // CalledOnValidThread() to verify. | 17 // Note: You should almost always use the NonThreadSafe class to get |
| 18 // the right version of the class for your build configuration. |
| 19 class NonThreadSafeDoNothing { |
| 20 public: |
| 21 bool CalledOnValidThread() const { |
| 22 return true; |
| 23 } |
| 24 |
| 25 protected: |
| 26 void DetachFromThread() {} |
| 27 }; |
| 28 |
| 29 // NonThreadSafe is a helper class used to help verify that methods of a |
| 30 // class are called from the same thread. One can inherit from this class |
| 31 // and use CalledOnValidThread() to verify. |
| 16 // | 32 // |
| 17 // This is intended to be used with classes that appear to be thread safe, but | 33 // This is intended to be used with classes that appear to be thread safe, but |
| 18 // aren't. For example, a service or a singleton like the preferences system. | 34 // aren't. For example, a service or a singleton like the preferences system. |
| 19 // | 35 // |
| 20 // Example: | 36 // Example: |
| 21 // class MyClass : public base::NonThreadSafe { | 37 // class MyClass : public base::NonThreadSafe { |
| 22 // public: | 38 // public: |
| 23 // void Foo() { | 39 // void Foo() { |
| 24 // DCHECK(CalledOnValidThread()); | 40 // DCHECK(CalledOnValidThread()); |
| 25 // ... (do stuff) ... | 41 // ... (do stuff) ... |
| 26 // } | 42 // } |
| 27 // } | 43 // } |
| 28 // | 44 // |
| 29 // In Release mode, CalledOnValidThread will always return true. | 45 // In Release mode, CalledOnValidThread will always return true. |
| 30 // | 46 // |
| 31 #ifndef NDEBUG | 47 #ifndef NDEBUG |
| 32 class NonThreadSafe { | 48 class NonThreadSafe : public NonThreadSafeImpl { |
| 33 public: | |
| 34 ~NonThreadSafe(); | |
| 35 | |
| 36 bool CalledOnValidThread() const; | |
| 37 | |
| 38 protected: | |
| 39 // Changes the thread that is checked for in CalledOnValidThread. The next | |
| 40 // call to CalledOnValidThread will attach this class to a new thread. It is | |
| 41 // up to the NonThreadSafe derived class to decide to expose this or not. | |
| 42 // This may be useful when an object may be created on one thread and then | |
| 43 // used exclusively on another thread. | |
| 44 void DetachFromThread(); | |
| 45 | |
| 46 private: | |
| 47 ThreadChecker thread_checker_; | |
| 48 }; | 49 }; |
| 49 #else | 50 #else |
| 50 // Do nothing in release mode. | 51 class NonThreadSafe : public NonThreadSafeDoNothing { |
| 51 class NonThreadSafe { | |
| 52 public: | |
| 53 bool CalledOnValidThread() const { | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 void DetachFromThread() {} | |
| 59 }; | 52 }; |
| 60 #endif // NDEBUG | 53 #endif // NDEBUG |
| 61 | 54 |
| 62 } // namespace base | 55 } // namespace base |
| 63 | 56 |
| 64 #endif // BASE_NON_THREAD_SAFE_H_ | 57 #endif // BASE_NON_THREAD_SAFE_H_ |
| OLD | NEW |