- add sources.
[platform/framework/web/crosswalk.git] / src / base / threading / non_thread_safe.h
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.
4
5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_
6 #define BASE_THREADING_NON_THREAD_SAFE_H_
7
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"
13
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
21 #if ENABLE_NON_THREAD_SAFE
22 #include "base/threading/non_thread_safe_impl.h"
23 #endif
24
25 namespace base {
26
27 // Do nothing implementation of NonThreadSafe, for release mode.
28 //
29 // Note: You should almost always use the NonThreadSafe class to get
30 // the right version of the class for your build configuration.
31 class NonThreadSafeDoNothing {
32  public:
33   bool CalledOnValidThread() const {
34     return true;
35   }
36
37  protected:
38   ~NonThreadSafeDoNothing() {}
39   void DetachFromThread() {}
40 };
41
42 // NonThreadSafe is a helper class used to help verify that methods of a
43 // class are called from the same thread.  One can inherit from this class
44 // and use CalledOnValidThread() to verify.
45 //
46 // This is intended to be used with classes that appear to be thread safe, but
47 // aren't.  For example, a service or a singleton like the preferences system.
48 //
49 // Example:
50 // class MyClass : public base::NonThreadSafe {
51 //  public:
52 //   void Foo() {
53 //     DCHECK(CalledOnValidThread());
54 //     ... (do stuff) ...
55 //   }
56 // }
57 //
58 // Note that base::ThreadChecker offers identical functionality to
59 // NonThreadSafe, but does not require inheritence. In general, it is preferable
60 // to have a base::ThreadChecker as a member, rather than inherit from
61 // NonThreadSafe. For more details about when to choose one over the other, see
62 // the documentation for base::ThreadChecker.
63 #if ENABLE_NON_THREAD_SAFE
64 typedef NonThreadSafeImpl NonThreadSafe;
65 #else
66 typedef NonThreadSafeDoNothing NonThreadSafe;
67 #endif  // ENABLE_NON_THREAD_SAFE
68
69 #undef ENABLE_NON_THREAD_SAFE
70
71 }  // namespace base
72
73 #endif  // BASE_NON_THREAD_SAFE_H_