michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/lock_impl.h" michael@0: michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: LockImpl::LockImpl() { michael@0: #ifndef NDEBUG michael@0: // In debug, setup attributes for lock error checking. michael@0: pthread_mutexattr_t mta; michael@0: int rv = pthread_mutexattr_init(&mta); michael@0: DCHECK_EQ(rv, 0); michael@0: rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); michael@0: DCHECK_EQ(rv, 0); michael@0: rv = pthread_mutex_init(&os_lock_, &mta); michael@0: DCHECK_EQ(rv, 0); michael@0: rv = pthread_mutexattr_destroy(&mta); michael@0: DCHECK_EQ(rv, 0); michael@0: #else michael@0: // In release, go with the default lock attributes. michael@0: pthread_mutex_init(&os_lock_, NULL); michael@0: #endif michael@0: } michael@0: michael@0: LockImpl::~LockImpl() { michael@0: int rv = pthread_mutex_destroy(&os_lock_); michael@0: DCHECK_EQ(rv, 0); michael@0: } michael@0: michael@0: bool LockImpl::Try() { michael@0: int rv = pthread_mutex_trylock(&os_lock_); michael@0: DCHECK(rv == 0 || rv == EBUSY); michael@0: return rv == 0; michael@0: } michael@0: michael@0: void LockImpl::Lock() { michael@0: int rv = pthread_mutex_lock(&os_lock_); michael@0: DCHECK_EQ(rv, 0); michael@0: } michael@0: michael@0: void LockImpl::Unlock() { michael@0: int rv = pthread_mutex_unlock(&os_lock_); michael@0: DCHECK_EQ(rv, 0); michael@0: }