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 #include "base/synchronization/lock_impl.h" | 5 #include "base/synchronization/lock_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 LockImpl::LockImpl() { | 14 LockImpl::LockImpl() { |
| 15 #if defined(OS_MAC) && !defined(NDEBUG) |
| 16 liveness_token_ = LT_ALIVE; |
| 17 #endif |
| 18 |
15 #ifndef NDEBUG | 19 #ifndef NDEBUG |
16 // In debug, setup attributes for lock error checking. | 20 // In debug, setup attributes for lock error checking. |
17 pthread_mutexattr_t mta; | 21 pthread_mutexattr_t mta; |
18 int rv = pthread_mutexattr_init(&mta); | 22 int rv = pthread_mutexattr_init(&mta); |
19 DCHECK_EQ(rv, 0); | 23 DCHECK_EQ(rv, 0); |
20 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); | 24 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
21 DCHECK_EQ(rv, 0); | 25 DCHECK_EQ(rv, 0); |
22 rv = pthread_mutex_init(&os_lock_, &mta); | 26 rv = pthread_mutex_init(&os_lock_, &mta); |
23 DCHECK_EQ(rv, 0); | 27 DCHECK_EQ(rv, 0); |
24 rv = pthread_mutexattr_destroy(&mta); | 28 rv = pthread_mutexattr_destroy(&mta); |
25 DCHECK_EQ(rv, 0); | 29 DCHECK_EQ(rv, 0); |
26 #else | 30 #else |
27 // In release, go with the default lock attributes. | 31 // In release, go with the default lock attributes. |
28 pthread_mutex_init(&os_lock_, NULL); | 32 pthread_mutex_init(&os_lock_, NULL); |
29 #endif | 33 #endif |
30 } | 34 } |
31 | 35 |
32 LockImpl::~LockImpl() { | 36 LockImpl::~LockImpl() { |
| 37 #if defined(OS_MAC) && !defined(NDEBUG) |
| 38 CheckIsAlive(); |
| 39 liveness_token_ = LT_DELETED; |
| 40 #endif |
33 int rv = pthread_mutex_destroy(&os_lock_); | 41 int rv = pthread_mutex_destroy(&os_lock_); |
34 DCHECK_EQ(rv, 0); | 42 DCHECK_EQ(rv, 0); |
35 } | 43 } |
36 | 44 |
37 bool LockImpl::Try() { | 45 bool LockImpl::Try() { |
| 46 #if defined(OS_MAC) && !defined(NDEBUG) |
| 47 CheckIsAlive(); |
| 48 #endif |
38 int rv = pthread_mutex_trylock(&os_lock_); | 49 int rv = pthread_mutex_trylock(&os_lock_); |
39 DCHECK(rv == 0 || rv == EBUSY); | 50 DCHECK(rv == 0 || rv == EBUSY); |
40 return rv == 0; | 51 return rv == 0; |
41 } | 52 } |
42 | 53 |
43 void LockImpl::Lock() { | 54 void LockImpl::Lock() { |
| 55 #if defined(OS_MAC) && !defined(NDEBUG) |
| 56 CheckIsAlive(); |
| 57 #endif |
44 int rv = pthread_mutex_lock(&os_lock_); | 58 int rv = pthread_mutex_lock(&os_lock_); |
45 DCHECK_EQ(rv, 0); | 59 DCHECK_EQ(rv, 0); |
46 } | 60 } |
47 | 61 |
48 void LockImpl::Unlock() { | 62 void LockImpl::Unlock() { |
| 63 #if defined(OS_MAC) && !defined(NDEBUG) |
| 64 CheckIsAlive(); |
| 65 #endif |
49 int rv = pthread_mutex_unlock(&os_lock_); | 66 int rv = pthread_mutex_unlock(&os_lock_); |
50 DCHECK_EQ(rv, 0); | 67 DCHECK_EQ(rv, 0); |
51 } | 68 } |
52 | 69 |
| 70 #if defined(OS_MAC) && !defined(NDEBUG) |
| 71 bool LockImpl::CheckIsAlive() { |
| 72 CHECK_EQ(static_cast<uint32>(LT_ALIVE), liveness_token_); |
| 73 } |
| 74 #endif |
| 75 |
53 } // namespace internal | 76 } // namespace internal |
54 } // namespace base | 77 } // namespace base |
OLD | NEW |