• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
18 //
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.
30 
31 #ifndef CEF_INCLUDE_BASE_THREAD_CHECKER_H_
32 #define CEF_INCLUDE_BASE_THREAD_CHECKER_H_
33 #pragma once
34 
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(USING_CHROMIUM_INCLUDES)
41 // When building CEF include the Chromium header directly.
42 #include "base/threading/thread_checker.h"
43 #else  // !USING_CHROMIUM_INCLUDES
44 // The following is substantially similar to the Chromium implementation.
45 // If the Chromium implementation diverges the below implementation should be
46 // updated to match.
47 
48 #include "include/base/cef_logging.h"
49 #include "include/base/internal/cef_thread_checker_impl.h"
50 
51 // Apart from debug builds, we also enable the thread checker in
52 // builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots
53 // with this define will get the same level of thread checking as
54 // debug bots.
55 #if DCHECK_IS_ON()
56 #define ENABLE_THREAD_CHECKER 1
57 #else
58 #define ENABLE_THREAD_CHECKER 0
59 #endif
60 
61 namespace base {
62 
63 namespace cef_internal {
64 
65 // Do nothing implementation, for use in release mode.
66 //
67 // Note: You should almost always use the ThreadChecker class to get the
68 // right version for your build configuration.
69 class ThreadCheckerDoNothing {
70  public:
CalledOnValidThread()71   bool CalledOnValidThread() const { return true; }
72 
DetachFromThread()73   void DetachFromThread() {}
74 };
75 
76 }  // namespace cef_internal
77 
78 // ThreadChecker is a helper class used to help verify that some methods of a
79 // class are called from the same thread. It provides identical functionality to
80 // base::NonThreadSafe, but it is meant to be held as a member variable, rather
81 // than inherited from base::NonThreadSafe.
82 //
83 // While inheriting from base::NonThreadSafe may give a clear indication about
84 // the thread-safety of a class, it may also lead to violations of the style
85 // guide with regard to multiple inheritance. The choice between having a
86 // ThreadChecker member and inheriting from base::NonThreadSafe should be based
87 // on whether:
88 //  - Derived classes need to know the thread they belong to, as opposed to
89 //    having that functionality fully encapsulated in the base class.
90 //  - Derived classes should be able to reassign the base class to another
91 //    thread, via DetachFromThread.
92 //
93 // If neither of these are true, then having a ThreadChecker member and calling
94 // CalledOnValidThread is the preferable solution.
95 //
96 // Example:
97 // class MyClass {
98 //  public:
99 //   void Foo() {
100 //     DCHECK(thread_checker_.CalledOnValidThread());
101 //     ... (do stuff) ...
102 //   }
103 //
104 //  private:
105 //   ThreadChecker thread_checker_;
106 // }
107 //
108 // In Release mode, CalledOnValidThread will always return true.
109 #if ENABLE_THREAD_CHECKER
110 class ThreadChecker : public cef_internal::ThreadCheckerImpl {};
111 #else
112 class ThreadChecker : public cef_internal::ThreadCheckerDoNothing {};
113 #endif  // ENABLE_THREAD_CHECKER
114 
115 #undef ENABLE_THREAD_CHECKER
116 
117 }  // namespace base
118 
119 #endif  // !USING_CHROMIUM_INCLUDES
120 
121 #endif  // CEF_INCLUDE_BASE_THREAD_CHECKER_H_
122