1 // Copyright (c) 2012 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.
5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_
6 #define BASE_THREADING_NON_THREAD_SAFE_H_
8 // Classes deriving from NonThreadSafe may need to suppress MSVC warning 4275:
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"
14 // See comment at top of thread_checker.h
15 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
16 #define ENABLE_NON_THREAD_SAFE 1
18 #define ENABLE_NON_THREAD_SAFE 0
21 #include "base/threading/non_thread_safe_impl.h"
25 // Do nothing implementation of NonThreadSafe, for release mode.
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
{
31 bool CalledOnValidThread() const {
36 ~NonThreadSafeDoNothing() {}
37 void DetachFromThread() {}
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.
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.
48 // class MyClass : public base::NonThreadSafe {
51 // DCHECK(CalledOnValidThread());
56 // Note that base::ThreadChecker offers identical functionality to
57 // NonThreadSafe, but does not require inheritance. In general, it is preferable
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.
61 #if ENABLE_NON_THREAD_SAFE
62 typedef NonThreadSafeImpl NonThreadSafe
;
64 typedef NonThreadSafeDoNothing NonThreadSafe
;
65 #endif // ENABLE_NON_THREAD_SAFE
67 #undef ENABLE_NON_THREAD_SAFE
71 #endif // BASE_THREADING_NON_THREAD_SAFE_H_