OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SYNCHRONIZATION_LOCK_IMPL_H_ | 5 #ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
6 #define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ | 6 #define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/logging.h" |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
11 | 12 |
12 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
13 #include <windows.h> | 14 #include <windows.h> |
14 #elif defined(OS_POSIX) | 15 #elif defined(OS_POSIX) |
| 16 #include <errno.h> |
15 #include <pthread.h> | 17 #include <pthread.h> |
16 #endif | 18 #endif |
17 | 19 |
18 namespace base { | 20 namespace base { |
19 namespace internal { | 21 namespace internal { |
20 | 22 |
21 // This class implements the underlying platform-specific spin-lock mechanism | 23 // This class implements the underlying platform-specific spin-lock mechanism |
22 // used for the Lock class. Most users should not use LockImpl directly, but | 24 // used for the Lock class. Most users should not use LockImpl directly, but |
23 // should instead use Lock. | 25 // should instead use Lock. |
24 class BASE_EXPORT LockImpl { | 26 class BASE_EXPORT LockImpl { |
25 public: | 27 public: |
26 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
27 using NativeHandle = SRWLOCK; | 29 using NativeHandle = SRWLOCK; |
28 #elif defined(OS_POSIX) | 30 #elif defined(OS_POSIX) |
29 using NativeHandle = pthread_mutex_t; | 31 using NativeHandle = pthread_mutex_t; |
30 #endif | 32 #endif |
31 | 33 |
32 LockImpl(); | 34 LockImpl(); |
33 ~LockImpl(); | 35 ~LockImpl(); |
34 | 36 |
35 // If the lock is not held, take it and return true. If the lock is already | 37 // If the lock is not held, take it and return true. If the lock is already |
36 // held by something else, immediately return false. | 38 // held by something else, immediately return false. |
37 bool Try(); | 39 bool Try(); |
38 | 40 |
39 // Take the lock, blocking until it is available if necessary. | 41 // Take the lock, blocking until it is available if necessary. |
40 void Lock(); | 42 void Lock(); |
41 | 43 |
42 // Release the lock. This must only be called by the lock's holder: after | 44 // 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. | 45 // a successful call to Try, or a call to Lock. |
44 void Unlock(); | 46 inline void Unlock(); |
45 | 47 |
46 // Return the native underlying lock. | 48 // Return the native underlying lock. |
47 // TODO(awalker): refactor lock and condition variables so that this is | 49 // TODO(awalker): refactor lock and condition variables so that this is |
48 // unnecessary. | 50 // unnecessary. |
49 NativeHandle* native_handle() { return &native_handle_; } | 51 NativeHandle* native_handle() { return &native_handle_; } |
50 | 52 |
51 #if defined(OS_POSIX) | 53 #if defined(OS_POSIX) |
52 // Whether this lock will attempt to use priority inheritance. | 54 // Whether this lock will attempt to use priority inheritance. |
53 static bool PriorityInheritanceAvailable(); | 55 static bool PriorityInheritanceAvailable(); |
54 #endif | 56 #endif |
55 | 57 |
56 private: | 58 private: |
57 NativeHandle native_handle_; | 59 NativeHandle native_handle_; |
58 | 60 |
59 DISALLOW_COPY_AND_ASSIGN(LockImpl); | 61 DISALLOW_COPY_AND_ASSIGN(LockImpl); |
60 }; | 62 }; |
61 | 63 |
| 64 #if defined(OS_WIN) |
| 65 void LockImpl::Unlock() { |
| 66 ::ReleaseSRWLockExclusive(&native_handle_); |
| 67 } |
| 68 #elif defined(OS_POSIX) |
| 69 void LockImpl::Unlock() { |
| 70 int rv = pthread_mutex_unlock(&native_handle_); |
| 71 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 72 } |
| 73 #endif |
| 74 |
62 } // namespace internal | 75 } // namespace internal |
63 } // namespace base | 76 } // namespace base |
64 | 77 |
65 #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ | 78 #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
OLD | NEW |