1 // Copyright (c) 2011 Google Inc. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google Inc. nor the name Chromium Embedded 14 // Framework nor the names of its contributors may be used to endorse 15 // or promote products derived from this software without specific prior 16 // written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // Do not include this header file directly. Use base/cef_lock.h instead. 31 32 #ifndef CEF_INCLUDE_BASE_INTERNAL_CEF_LOCK_IMPL_H_ 33 #define CEF_INCLUDE_BASE_INTERNAL_CEF_LOCK_IMPL_H_ 34 35 #include "include/base/cef_build.h" 36 37 #if defined(OS_WIN) 38 #include <windows.h> 39 #elif defined(OS_POSIX) 40 #include <pthread.h> 41 #endif 42 43 namespace base { 44 namespace cef_internal { 45 46 // This class implements the underlying platform-specific spin-lock mechanism 47 // used for the Lock class. Most users should not use LockImpl directly, but 48 // should instead use Lock. 49 class LockImpl { 50 public: 51 #if defined(OS_WIN) 52 typedef CRITICAL_SECTION NativeHandle; 53 #elif defined(OS_POSIX) 54 typedef pthread_mutex_t NativeHandle; 55 #endif 56 57 LockImpl(); 58 59 LockImpl(const LockImpl&) = delete; 60 LockImpl& operator=(const LockImpl&) = delete; 61 62 ~LockImpl(); 63 64 // If the lock is not held, take it and return true. If the lock is already 65 // held by something else, immediately return false. 66 bool Try(); 67 68 // Take the lock, blocking until it is available if necessary. 69 void Lock(); 70 71 // Release the lock. This must only be called by the lock's holder: after 72 // a successful call to Try, or a call to Lock. 73 void Unlock(); 74 75 // Return the native underlying lock. 76 // TODO(awalker): refactor lock and condition variables so that this is 77 // unnecessary. native_handle()78 NativeHandle* native_handle() { return &native_handle_; } 79 80 private: 81 NativeHandle native_handle_; 82 }; 83 84 } // namespace cef_internal 85 } // namespace base 86 87 #endif // CEF_INCLUDE_BASE_INTERNAL_CEF_LOCK_IMPL_H_ 88