license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_NON_THREAD_SAFE_H__ |
| 6 | #define BASE_NON_THREAD_SAFE_H__ |
| 7 | |
| 8 | #include "base/logging.h" |
| 9 | |
| 10 | // A helper class used to help verify that methods of a class are |
| 11 | // called from the same thread. One can inherit from this class and use |
| 12 | // CalledOnValidThread() to verify. |
| 13 | // |
| 14 | // This is intended to be used with classes that appear to be thread safe, but |
| 15 | // aren't. For example, a service or a singleton like the preferences system. |
| 16 | // |
| 17 | // Example: |
| 18 | // class MyClass : public NonThreadSafe { |
| 19 | // public: |
| 20 | // void Foo() { |
| 21 | // DCHECK(CalledOnValidThread()); |
| 22 | // ... (do stuff) ... |
| 23 | // } |
| 24 | // } |
| 25 | // |
| 26 | // In Release mode, CalledOnValidThread will always return true. |
| 27 | // |
| 28 | #ifndef NDEBUG |
| 29 | class NonThreadSafe { |
| 30 | public: |
| 31 | NonThreadSafe(); |
| 32 | ~NonThreadSafe(); |
| 33 | |
| 34 | protected: |
| 35 | bool CalledOnValidThread() const; |
| 36 | |
| 37 | private: |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 38 | int valid_thread_id_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 39 | }; |
| 40 | #else |
| 41 | // Do nothing in release mode. |
| 42 | class NonThreadSafe { |
| 43 | public: |
| 44 | NonThreadSafe() {} |
| 45 | ~NonThreadSafe() {} |
| 46 | |
| 47 | protected: |
| 48 | bool CalledOnValidThread() const { |
| 49 | return true; |
| 50 | } |
| 51 | }; |
| 52 | #endif // NDEBUG |
| 53 | |
| 54 | #endif // BASE_NON_THREAD_SAFE_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 55 | |