blob: d41c08608c96a12d17c7a4d0da40d52607f81854 [file] [log] [blame]
rsleevi@chromium.org6c34bd72012-06-26 05:43:17 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
brettw@chromium.org088ae932011-01-01 13:48:49 +09005#ifndef BASE_THREADING_NON_THREAD_SAFE_H_
6#define BASE_THREADING_NON_THREAD_SAFE_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
rsleevi@chromium.org4218d212012-06-27 12:13:35 +09008// Classes deriving from NonThreadSafe may need to suppress MSVC warning 4275:
rvargas@google.com1ed7f9a2011-05-19 03:29:36 +09009// 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
rsleevi@chromium.org4218d212012-06-27 12:13:35 +090014// 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
joi@chromium.orgb5d63f72011-03-04 08:38:51 +090021#include "base/threading/non_thread_safe_impl.h"
initial.commit3f4a7322008-07-27 06:49:38 +090022
brettw@chromium.org088ae932011-01-01 13:48:49 +090023namespace base {
24
joi@chromium.orgb5d63f72011-03-04 08:38:51 +090025// 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.
29class NonThreadSafeDoNothing {
30 public:
31 bool CalledOnValidThread() const {
32 return true;
33 }
34
35 protected:
rsleevi@chromium.orgd55e9a42012-06-26 15:23:00 +090036 ~NonThreadSafeDoNothing() {}
joi@chromium.orgb5d63f72011-03-04 08:38:51 +090037 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.commit3f4a7322008-07-27 06:49:38 +090043//
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:
brettw@chromium.org088ae932011-01-01 13:48:49 +090048// class MyClass : public base::NonThreadSafe {
initial.commit3f4a7322008-07-27 06:49:38 +090049// public:
50// void Foo() {
51// DCHECK(CalledOnValidThread());
52// ... (do stuff) ...
53// }
54// }
55//
rsleevi@chromium.org6c34bd72012-06-26 05:43:17 +090056// Note that base::ThreadChecker offers identical functionality to
thakis@chromium.org7bfcfc42013-11-05 13:24:58 +090057// NonThreadSafe, but does not require inheritance. In general, it is preferable
rsleevi@chromium.org6c34bd72012-06-26 05:43:17 +090058// 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.
rsleevi@chromium.org4218d212012-06-27 12:13:35 +090061#if ENABLE_NON_THREAD_SAFE
rsleevi@chromium.orgeae5e582012-07-11 09:07:54 +090062typedef NonThreadSafeImpl NonThreadSafe;
initial.commit3f4a7322008-07-27 06:49:38 +090063#else
rsleevi@chromium.orgeae5e582012-07-11 09:07:54 +090064typedef NonThreadSafeDoNothing NonThreadSafe;
rsleevi@chromium.org4218d212012-06-27 12:13:35 +090065#endif // ENABLE_NON_THREAD_SAFE
66
67#undef ENABLE_NON_THREAD_SAFE
initial.commit3f4a7322008-07-27 06:49:38 +090068
brettw@chromium.org088ae932011-01-01 13:48:49 +090069} // namespace base
70
thestig@chromium.orge2cdbbb2014-02-16 05:47:58 +090071#endif // BASE_THREADING_NON_THREAD_SAFE_H_