1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011 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_CEF_LOCK_H_ 32 #define CEF_INCLUDE_BASE_CEF_LOCK_H_ 33 #pragma once 34 35 #if defined(USING_CHROMIUM_INCLUDES) 36 // When building CEF include the Chromium header directly. 37 #include "base/synchronization/lock.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/cef_platform_thread.h" 45 #include "include/base/internal/cef_lock_impl.h" 46 47 namespace base { 48 namespace cef_internal { 49 50 // A convenient wrapper for an OS specific critical section. The only real 51 // intelligence in this class is in debug mode for the support for the 52 // AssertAcquired() method. 53 class Lock { 54 public: 55 #if !DCHECK_IS_ON() // Optimized wrapper implementation Lock()56 Lock() : lock_() {} 57 58 Lock(const Lock&) = delete; 59 Lock& operator=(const Lock&) = delete; 60 ~Lock()61 ~Lock() {} Acquire()62 void Acquire() { lock_.Lock(); } Release()63 void Release() { lock_.Unlock(); } 64 65 // If the lock is not held, take it and return true. If the lock is already 66 // held by another thread, immediately return false. This must not be called 67 // by a thread already holding the lock (what happens is undefined and an 68 // assertion may fail). Try()69 bool Try() { return lock_.Try(); } 70 71 // Null implementation if not debug. AssertAcquired()72 void AssertAcquired() const {} 73 #else 74 Lock(); 75 ~Lock(); 76 77 // NOTE: Although windows critical sections support recursive locks, we do not 78 // allow this, and we will commonly fire a DCHECK() if a thread attempts to 79 // acquire the lock a second time (while already holding it). 80 void Acquire() { 81 lock_.Lock(); 82 CheckUnheldAndMark(); 83 } 84 void Release() { 85 CheckHeldAndUnmark(); 86 lock_.Unlock(); 87 } 88 89 bool Try() { 90 bool rv = lock_.Try(); 91 if (rv) { 92 CheckUnheldAndMark(); 93 } 94 return rv; 95 } 96 97 void AssertAcquired() const; 98 #endif // !DCHECK_IS_ON() 99 100 private: 101 #if DCHECK_IS_ON() 102 // Members and routines taking care of locks assertions. 103 // Note that this checks for recursive locks and allows them 104 // if the variable is set. This is allowed by the underlying implementation 105 // on windows but not on Posix, so we're doing unneeded checks on Posix. 106 // It's worth it to share the code. 107 void CheckHeldAndUnmark(); 108 void CheckUnheldAndMark(); 109 110 // All private data is implicitly protected by lock_. 111 // Be VERY careful to only access members under that lock. 112 base::PlatformThreadRef owning_thread_ref_; 113 #endif // DCHECK_IS_ON() 114 115 // Platform specific underlying lock implementation. 116 LockImpl lock_; 117 }; 118 119 // A helper class that acquires the given Lock while the AutoLock is in scope. 120 class AutoLock { 121 public: 122 struct AlreadyAcquired {}; 123 AutoLock(Lock & lock)124 explicit AutoLock(Lock& lock) : lock_(lock) { lock_.Acquire(); } 125 AutoLock(Lock & lock,const AlreadyAcquired &)126 AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { 127 lock_.AssertAcquired(); 128 } 129 130 AutoLock(const AutoLock&) = delete; 131 AutoLock& operator=(const AutoLock&) = delete; 132 ~AutoLock()133 ~AutoLock() { 134 lock_.AssertAcquired(); 135 lock_.Release(); 136 } 137 138 private: 139 Lock& lock_; 140 }; 141 142 // AutoUnlock is a helper that will Release() the |lock| argument in the 143 // constructor, and re-Acquire() it in the destructor. 144 class AutoUnlock { 145 public: AutoUnlock(Lock & lock)146 explicit AutoUnlock(Lock& lock) : lock_(lock) { 147 // We require our caller to have the lock. 148 lock_.AssertAcquired(); 149 lock_.Release(); 150 } 151 152 AutoUnlock(const AutoUnlock&) = delete; 153 AutoUnlock& operator=(const AutoUnlock&) = delete; 154 ~AutoUnlock()155 ~AutoUnlock() { lock_.Acquire(); } 156 157 private: 158 Lock& lock_; 159 }; 160 161 } // namespace cef_internal 162 163 // Implement classes in the cef_internal namespace and then expose them to the 164 // base namespace. This avoids conflicts with the base.lib implementation when 165 // linking sandbox support on Windows. 166 using cef_internal::AutoLock; 167 using cef_internal::AutoUnlock; 168 using cef_internal::Lock; 169 170 } // namespace base 171 172 #endif // !USING_CHROMIUM_INCLUDES 173 174 #endif // CEF_INCLUDE_BASE_CEF_LOCK_H_ 175