| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_LOCK_IMPL_H_ | 5 #ifndef BASE_LOCK_IMPL_H_ |
| 6 #define BASE_LOCK_IMPL_H_ | 6 #define BASE_LOCK_IMPL_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #elif defined(OS_POSIX) | 12 #elif defined(OS_POSIX) |
| 13 #include <pthread.h> | 13 #include <pthread.h> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/platform_thread.h" |
| 17 | 18 |
| 18 // This class implements the underlying platform-specific spin-lock mechanism | 19 // This class implements the underlying platform-specific spin-lock mechanism |
| 19 // used for the Lock class. Most users should not use LockImpl directly, but | 20 // used for the Lock class. Most users should not use LockImpl directly, but |
| 20 // should instead use Lock. | 21 // should instead use Lock. |
| 21 class LockImpl { | 22 class LockImpl { |
| 22 public: | 23 public: |
| 23 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 24 typedef CRITICAL_SECTION OSLockType; | 25 typedef CRITICAL_SECTION OSLockType; |
| 25 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
| 26 typedef pthread_mutex_t OSLockType; | 27 typedef pthread_mutex_t OSLockType; |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 29 LockImpl(); | 30 LockImpl(); |
| 30 ~LockImpl(); | 31 ~LockImpl(); |
| 31 | 32 |
| 32 // If the lock is not held, take it and return true. If the lock is already | 33 // If the lock is not held, take it and return true. If the lock is already |
| 33 // held by something else, immediately return false. | 34 // held by something else, immediately return false. |
| 34 bool Try(); | 35 bool Try(); |
| 35 | 36 |
| 36 // Take the lock, blocking until it is available if necessary. | 37 // Take the lock, blocking until it is available if necessary. |
| 37 void Lock(); | 38 void Lock(); |
| 38 | 39 |
| 39 // Release the lock. This must only be called by the lock's holder: after | 40 // Release the lock. This must only be called by the lock's holder: after |
| 40 // a successful call to Try, or a call to Lock. | 41 // a successful call to Try, or a call to Lock. |
| 41 void Unlock(); | 42 void Unlock(); |
| 42 | 43 |
| 43 // Return the native underlying lock. | 44 // Debug-only method that will DCHECK() if the lock is not acquired by the |
| 45 // current thread. In non-debug builds, no check is performed. |
| 46 // Because linux and mac condition variables modify the underlyning lock |
| 47 // through the os_lock() method, runtime assertions can not be done on those |
| 48 // builds. |
| 49 #if defined(NDEBUG) || !defined(OS_WIN) |
| 50 void AssertAcquired() {} |
| 51 #else |
| 52 void AssertAcquired(); |
| 53 #endif |
| 54 |
| 55 // Return the native underlying lock. Not supported for Windows builds. |
| 44 // TODO(awalker): refactor lock and condition variables so that this is | 56 // TODO(awalker): refactor lock and condition variables so that this is |
| 45 // unnecessary. | 57 // unnecessary. |
| 58 #if !defined(OS_WIN) |
| 46 OSLockType* os_lock() { return &os_lock_; } | 59 OSLockType* os_lock() { return &os_lock_; } |
| 60 #endif |
| 47 | 61 |
| 48 private: | 62 private: |
| 49 OSLockType os_lock_; | 63 OSLockType os_lock_; |
| 50 | 64 |
| 51 #if !defined(NDEBUG) && defined(OS_WIN) | 65 #if !defined(NDEBUG) && defined(OS_WIN) |
| 52 // All private data is implicitly protected by lock_. | 66 // All private data is implicitly protected by lock_. |
| 53 // Be VERY careful to only access members under that lock. | 67 // Be VERY careful to only access members under that lock. |
| 68 PlatformThreadId owning_thread_id_; |
| 54 int32 recursion_count_shadow_; | 69 int32 recursion_count_shadow_; |
| 55 bool recursion_used_; // Allow debugging to continued after a DCHECK(). | 70 bool recursion_used_; // Allow debugging to continued after a DCHECK(). |
| 56 #endif // NDEBUG | 71 #endif // NDEBUG |
| 57 | 72 |
| 58 DISALLOW_COPY_AND_ASSIGN(LockImpl); | 73 DISALLOW_COPY_AND_ASSIGN(LockImpl); |
| 59 }; | 74 }; |
| 60 | 75 |
| 61 class AutoLockImpl { | |
| 62 public: | |
| 63 AutoLockImpl(LockImpl* lock_impl) | |
| 64 : lock_impl_(lock_impl) { | |
| 65 lock_impl_->Lock(); | |
| 66 } | |
| 67 | |
| 68 ~AutoLockImpl() { | |
| 69 lock_impl_->Unlock(); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 DISALLOW_EVIL_CONSTRUCTORS(AutoLockImpl); | |
| 74 | |
| 75 LockImpl* lock_impl_; | |
| 76 }; | |
| 77 | 76 |
| 78 #endif // BASE_LOCK_IMPL_H_ | 77 #endif // BASE_LOCK_IMPL_H_ |
| OLD | NEW |