Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / base / synchronization / lock_impl_posix.cc
blobf638fcd321ce2a1d6bc2db9cc233062779b2f5f0
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
3 // found in the LICENSE file.
5 #include "base/synchronization/lock_impl.h"
7 #include <errno.h>
9 #include "base/logging.h"
11 namespace base {
12 namespace internal {
14 LockImpl::LockImpl() {
15 #ifndef NDEBUG
16 // In debug, setup attributes for lock error checking.
17 pthread_mutexattr_t mta;
18 int rv = pthread_mutexattr_init(&mta);
19 DCHECK_EQ(rv, 0);
20 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
21 DCHECK_EQ(rv, 0);
22 rv = pthread_mutex_init(&os_lock_, &mta);
23 DCHECK_EQ(rv, 0);
24 rv = pthread_mutexattr_destroy(&mta);
25 DCHECK_EQ(rv, 0);
26 #else
27 // In release, go with the default lock attributes.
28 pthread_mutex_init(&os_lock_, NULL);
29 #endif
32 LockImpl::~LockImpl() {
33 int rv = pthread_mutex_destroy(&os_lock_);
34 DCHECK_EQ(rv, 0);
37 bool LockImpl::Try() {
38 int rv = pthread_mutex_trylock(&os_lock_);
39 DCHECK(rv == 0 || rv == EBUSY);
40 return rv == 0;
43 void LockImpl::Lock() {
44 int rv = pthread_mutex_lock(&os_lock_);
45 DCHECK_EQ(rv, 0);
48 void LockImpl::Unlock() {
49 int rv = pthread_mutex_unlock(&os_lock_);
50 DCHECK_EQ(rv, 0);
53 } // namespace internal
54 } // namespace base