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 #include "base/lock_impl.h" | 5 #include "base/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 LockImpl::LockImpl() { | 11 LockImpl::LockImpl() { |
12 #ifndef NDEBUG | |
13 // In debug, setup attributes for lock error checking. | |
12 pthread_mutexattr_t mta; | 14 pthread_mutexattr_t mta; |
13 int rv = pthread_mutexattr_init(&mta); | 15 int rv = pthread_mutexattr_init(&mta); |
14 DCHECK(rv == 0); | 16 DCHECK(rv == 0); |
15 //rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE); | 17 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
16 DCHECK(rv == 0); | 18 DCHECK(rv == 0); |
17 rv = pthread_mutex_init(&os_lock_, &mta); | 19 rv = pthread_mutex_init(&os_lock_, &mta); |
18 DCHECK(rv == 0); | 20 DCHECK(rv == 0); |
19 rv = pthread_mutexattr_destroy(&mta); | 21 rv = pthread_mutexattr_destroy(&mta); |
20 DCHECK(rv == 0); | 22 DCHECK(rv == 0); |
23 #else | |
24 // In release, go with the default lock attributes. | |
25 pthread_mutex_init(&os_lock_, NULL); | |
26 #endif | |
mendola
2008/10/01 14:25:21
this is fine if we want an undefined behavior in c
| |
21 } | 27 } |
22 | 28 |
23 LockImpl::~LockImpl() { | 29 LockImpl::~LockImpl() { |
24 int rv = pthread_mutex_destroy(&os_lock_); | 30 int rv = pthread_mutex_destroy(&os_lock_); |
25 DCHECK(rv == 0); | 31 DCHECK(rv == 0); |
26 } | 32 } |
27 | 33 |
28 bool LockImpl::Try() { | 34 bool LockImpl::Try() { |
29 int rv = pthread_mutex_trylock(&os_lock_); | 35 int rv = pthread_mutex_trylock(&os_lock_); |
30 DCHECK(rv == 0 || rv == EBUSY); | 36 DCHECK(rv == 0 || rv == EBUSY); |
31 return rv == 0; | 37 return rv == 0; |
32 } | 38 } |
33 | 39 |
34 void LockImpl::Lock() { | 40 void LockImpl::Lock() { |
35 int rv = pthread_mutex_lock(&os_lock_); | 41 int rv = pthread_mutex_lock(&os_lock_); |
36 DCHECK(rv == 0); | 42 DCHECK(rv == 0); |
37 } | 43 } |
38 | 44 |
39 void LockImpl::Unlock() { | 45 void LockImpl::Unlock() { |
40 int rv = pthread_mutex_unlock(&os_lock_); | 46 int rv = pthread_mutex_unlock(&os_lock_); |
41 DCHECK(rv == 0); | 47 DCHECK(rv == 0); |
42 } | 48 } |
43 | 49 |
OLD | NEW |