[email protected] | 81c309c | 2012-06-25 20:43:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 5 | #ifndef BASE_THREADING_NON_THREAD_SAFE_H_ |
6 | #define BASE_THREADING_NON_THREAD_SAFE_H_ | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
[email protected] | a571ce85 | 2012-06-27 03:13:35 | [diff] [blame] | 8 | // Classes deriving from NonThreadSafe may need to suppress MSVC warning 4275: |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 9 | // non dll-interface class 'Bar' used as base for dll-interface class 'Foo'. |
10 | // There is a specific macro to do it: NON_EXPORTED_BASE(), defined in | ||||
11 | // compiler_specific.h | ||||
12 | #include "base/compiler_specific.h" | ||||
13 | |||||
[email protected] | a571ce85 | 2012-06-27 03:13:35 | [diff] [blame] | 14 | // See comment at top of thread_checker.h |
15 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | ||||
16 | #define ENABLE_NON_THREAD_SAFE 1 | ||||
17 | #else | ||||
18 | #define ENABLE_NON_THREAD_SAFE 0 | ||||
19 | #endif | ||||
20 | |||||
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 21 | #include "base/threading/non_thread_safe_impl.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 23 | namespace base { |
24 | |||||
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 25 | // Do nothing implementation of NonThreadSafe, for release mode. |
26 | // | ||||
27 | // Note: You should almost always use the NonThreadSafe class to get | ||||
28 | // the right version of the class for your build configuration. | ||||
29 | class NonThreadSafeDoNothing { | ||||
30 | public: | ||||
31 | bool CalledOnValidThread() const { | ||||
32 | return true; | ||||
33 | } | ||||
34 | |||||
35 | protected: | ||||
[email protected] | cb93248 | 2012-06-26 06:23:00 | [diff] [blame] | 36 | ~NonThreadSafeDoNothing() {} |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 37 | void DetachFromThread() {} |
38 | }; | ||||
39 | |||||
40 | // NonThreadSafe is a helper class used to help verify that methods of a | ||||
41 | // class are called from the same thread. One can inherit from this class | ||||
42 | // and use CalledOnValidThread() to verify. | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | // |
44 | // This is intended to be used with classes that appear to be thread safe, but | ||||
45 | // aren't. For example, a service or a singleton like the preferences system. | ||||
46 | // | ||||
47 | // Example: | ||||
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 48 | // class MyClass : public base::NonThreadSafe { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 49 | // public: |
50 | // void Foo() { | ||||
51 | // DCHECK(CalledOnValidThread()); | ||||
52 | // ... (do stuff) ... | ||||
53 | // } | ||||
54 | // } | ||||
55 | // | ||||
[email protected] | 81c309c | 2012-06-25 20:43:17 | [diff] [blame] | 56 | // Note that base::ThreadChecker offers identical functionality to |
[email protected] | 600cdb7c | 2013-11-05 04:24:58 | [diff] [blame] | 57 | // NonThreadSafe, but does not require inheritance. In general, it is preferable |
[email protected] | 81c309c | 2012-06-25 20:43:17 | [diff] [blame] | 58 | // to have a base::ThreadChecker as a member, rather than inherit from |
59 | // NonThreadSafe. For more details about when to choose one over the other, see | ||||
60 | // the documentation for base::ThreadChecker. | ||||
[email protected] | a571ce85 | 2012-06-27 03:13:35 | [diff] [blame] | 61 | #if ENABLE_NON_THREAD_SAFE |
[email protected] | 95bbcc7 | 2012-07-11 00:07:54 | [diff] [blame] | 62 | typedef NonThreadSafeImpl NonThreadSafe; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 63 | #else |
[email protected] | 95bbcc7 | 2012-07-11 00:07:54 | [diff] [blame] | 64 | typedef NonThreadSafeDoNothing NonThreadSafe; |
[email protected] | a571ce85 | 2012-06-27 03:13:35 | [diff] [blame] | 65 | #endif // ENABLE_NON_THREAD_SAFE |
66 | |||||
67 | #undef ENABLE_NON_THREAD_SAFE | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 69 | } // namespace base |
70 | |||||
[email protected] | 587b80e | 2014-02-15 20:47:58 | [diff] [blame] | 71 | #endif // BASE_THREADING_NON_THREAD_SAFE_H_ |