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
3 : // found in the LICENSE file.
4 :
5 : #include "base/lock_impl.h"
6 :
7 : #include <errno.h>
8 :
9 : #include "base/logging.h"
10 :
11 14376 : LockImpl::LockImpl() {
12 : #ifndef NDEBUG
13 : // In debug, setup attributes for lock error checking.
14 : pthread_mutexattr_t mta;
15 14376 : int rv = pthread_mutexattr_init(&mta);
16 14376 : DCHECK_EQ(rv, 0);
17 14376 : rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
18 14376 : DCHECK_EQ(rv, 0);
19 14376 : rv = pthread_mutex_init(&os_lock_, &mta);
20 14376 : DCHECK_EQ(rv, 0);
21 14376 : rv = pthread_mutexattr_destroy(&mta);
22 14376 : DCHECK_EQ(rv, 0);
23 : #else
24 : // In release, go with the default lock attributes.
25 : pthread_mutex_init(&os_lock_, NULL);
26 : #endif
27 14376 : }
28 :
29 11488 : LockImpl::~LockImpl() {
30 11488 : int rv = pthread_mutex_destroy(&os_lock_);
31 11488 : DCHECK_EQ(rv, 0);
32 11488 : }
33 :
34 0 : bool LockImpl::Try() {
35 0 : int rv = pthread_mutex_trylock(&os_lock_);
36 0 : DCHECK(rv == 0 || rv == EBUSY);
37 0 : return rv == 0;
38 : }
39 :
40 93226 : void LockImpl::Lock() {
41 93226 : int rv = pthread_mutex_lock(&os_lock_);
42 93226 : DCHECK_EQ(rv, 0);
43 93226 : }
44 :
45 93226 : void LockImpl::Unlock() {
46 93226 : int rv = pthread_mutex_unlock(&os_lock_);
47 93226 : DCHECK_EQ(rv, 0);
48 93226 : }
|