1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
2 // Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CEF_INCLUDE_BASE_THREAD_CHECKER_H_
32 #define CEF_INCLUDE_BASE_THREAD_CHECKER_H_
35 #if defined(BASE_THREADING_THREAD_CHECKER_H_)
36 // Do nothing if the Chromium header has already been included.
37 // This can happen in cases where Chromium code is used directly by the
38 // client application. When using Chromium code directly always include
39 // the Chromium header first to avoid type conflicts.
40 #elif defined(BUILDING_CEF_SHARED)
41 // When building CEF include the Chromium header directly.
42 #include "base/threading/thread_checker.h"
43 #else // !BUILDING_CEF_SHARED
44 // The following is substantially similar to the Chromium implementation.
45 // If the Chromium implementation diverges the below implementation should be
48 // Apart from debug builds, we also enable the thread checker in
49 // builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots
50 // with this define will get the same level of thread checking as
53 // Note that this does not perfectly match situations where DCHECK is
54 // enabled. For example a non-official release build may have
55 // DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be
56 // disabled) but have DCHECKs enabled at runtime.
57 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
58 #define ENABLE_THREAD_CHECKER 1
60 #define ENABLE_THREAD_CHECKER 0
63 #include "include/base/internal/cef_thread_checker_impl.h"
67 // Do nothing implementation, for use in release mode.
69 // Note: You should almost always use the ThreadChecker class to get the
70 // right version for your build configuration.
71 class ThreadCheckerDoNothing {
73 bool CalledOnValidThread() const {
77 void DetachFromThread() {}
80 // ThreadChecker is a helper class used to help verify that some methods of a
81 // class are called from the same thread. It provides identical functionality to
82 // base::NonThreadSafe, but it is meant to be held as a member variable, rather
83 // than inherited from base::NonThreadSafe.
85 // While inheriting from base::NonThreadSafe may give a clear indication about
86 // the thread-safety of a class, it may also lead to violations of the style
87 // guide with regard to multiple inheritance. The choice between having a
88 // ThreadChecker member and inheriting from base::NonThreadSafe should be based
90 // - Derived classes need to know the thread they belong to, as opposed to
91 // having that functionality fully encapsulated in the base class.
92 // - Derived classes should be able to reassign the base class to another
93 // thread, via DetachFromThread.
95 // If neither of these are true, then having a ThreadChecker member and calling
96 // CalledOnValidThread is the preferable solution.
102 // DCHECK(thread_checker_.CalledOnValidThread());
103 // ... (do stuff) ...
107 // ThreadChecker thread_checker_;
110 // In Release mode, CalledOnValidThread will always return true.
111 #if ENABLE_THREAD_CHECKER
112 class ThreadChecker : public ThreadCheckerImpl {
115 class ThreadChecker : public ThreadCheckerDoNothing {
117 #endif // ENABLE_THREAD_CHECKER
119 #undef ENABLE_THREAD_CHECKER
123 #endif // !BUILDING_CEF_SHARED
125 #endif // CEF_INCLUDE_BASE_THREAD_CHECKER_H_