lock_impl.h (1707B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef BASE_LOCK_IMPL_H_ 8 #define BASE_LOCK_IMPL_H_ 9 10 #include "base/basictypes.h" 11 12 #if defined(XP_WIN) 13 # include <windows.h> 14 #else 15 # include <pthread.h> 16 #endif 17 18 namespace base { 19 namespace internal { 20 21 // This class implements the underlying platform-specific spin-lock mechanism 22 // used for the Lock class. Most users should not use LockImpl directly, but 23 // should instead use Lock. 24 class LockImpl { 25 public: 26 #if defined(XP_WIN) 27 using NativeHandle = SRWLOCK; 28 #else 29 using NativeHandle = pthread_mutex_t; 30 #endif 31 32 LockImpl(); 33 ~LockImpl(); 34 35 // If the lock is not held, take it and return true. If the lock is already 36 // held by something else, immediately return false. 37 bool Try(); 38 39 // Take the lock, blocking until it is available if necessary. 40 void Lock(); 41 42 // Release the lock. This must only be called by the lock's holder: after 43 // a successful call to Try, or a call to Lock. 44 void Unlock(); 45 46 // Return the native underlying lock. 47 // TODO(awalker): refactor lock and condition variables so that this is 48 // unnecessary. 49 NativeHandle* native_handle() { return &native_handle_; } 50 51 #if defined(XP_UNIX) 52 // Whether this lock will attempt to use priority inheritance. 53 static bool PriorityInheritanceAvailable(); 54 #endif 55 56 private: 57 NativeHandle native_handle_; 58 59 DISALLOW_COPY_AND_ASSIGN(LockImpl); 60 }; 61 62 } // namespace internal 63 } // namespace base 64 65 #endif // BASE_LOCK_IMPL_H_