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 #include "include/base/cef_macros.h" 44 45 namespace base { 46 namespace cef_internal { 47 48 // This class implements the underlying platform-specific spin-lock mechanism 49 // used for the Lock class. Most users should not use LockImpl directly, but 50 // should instead use Lock. 51 class LockImpl { 52 public: 53 #if defined(OS_WIN) 54 typedef CRITICAL_SECTION NativeHandle; 55 #elif defined(OS_POSIX) 56 typedef pthread_mutex_t NativeHandle; 57 #endif 58 59 LockImpl(); 60 ~LockImpl(); 61 62 // If the lock is not held, take it and return true. If the lock is already 63 // held by something else, immediately return false. 64 bool Try(); 65 66 // Take the lock, blocking until it is available if necessary. 67 void Lock(); 68 69 // Release the lock. This must only be called by the lock's holder: after 70 // a successful call to Try, or a call to Lock. 71 void Unlock(); 72 73 // Return the native underlying lock. 74 // TODO(awalker): refactor lock and condition variables so that this is 75 // unnecessary. native_handle()76 NativeHandle* native_handle() { return &native_handle_; } 77 78 private: 79 NativeHandle native_handle_; 80 81 DISALLOW_COPY_AND_ASSIGN(LockImpl); 82 }; 83 84 } // namespace cef_internal 85 } // namespace base 86 87 #endif // CEF_INCLUDE_BASE_INTERNAL_CEF_LOCK_IMPL_H_ 88