brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 4 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 5 | #include "base/synchronization/lock_impl.h" |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 6 | |
| 7 | #include <errno.h> |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 8 | #include <string.h> |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 9 | |
| 10 | #include "base/logging.h" |
| 11 | |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 12 | namespace base { |
| 13 | namespace internal { |
| 14 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 15 | LockImpl::LockImpl() { |
deanm@chromium.org | d4eb87f | 2008-10-01 23:45:24 +0900 | [diff] [blame] | 16 | #ifndef NDEBUG |
| 17 | // In debug, setup attributes for lock error checking. |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 18 | pthread_mutexattr_t mta; |
| 19 | int rv = pthread_mutexattr_init(&mta); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 20 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
deanm@chromium.org | d4eb87f | 2008-10-01 23:45:24 +0900 | [diff] [blame] | 21 | rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 22 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 23 | rv = pthread_mutex_init(&os_lock_, &mta); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 24 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 25 | rv = pthread_mutexattr_destroy(&mta); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 26 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
deanm@chromium.org | d4eb87f | 2008-10-01 23:45:24 +0900 | [diff] [blame] | 27 | #else |
| 28 | // In release, go with the default lock attributes. |
| 29 | pthread_mutex_init(&os_lock_, NULL); |
| 30 | #endif |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | LockImpl::~LockImpl() { |
| 34 | int rv = pthread_mutex_destroy(&os_lock_); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 35 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool LockImpl::Try() { |
| 39 | int rv = pthread_mutex_trylock(&os_lock_); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 40 | DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 41 | return rv == 0; |
| 42 | } |
| 43 | |
| 44 | void LockImpl::Lock() { |
| 45 | int rv = pthread_mutex_lock(&os_lock_); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 46 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void LockImpl::Unlock() { |
| 50 | int rv = pthread_mutex_unlock(&os_lock_); |
ajwong@chromium.org | 4d1bc94 | 2013-08-13 21:04:02 +0900 | [diff] [blame^] | 51 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 52 | } |
brettw@chromium.org | e439a96 | 2011-01-02 08:16:20 +0900 | [diff] [blame] | 53 | |
| 54 | } // namespace internal |
| 55 | } // namespace base |