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