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