blob: 86158767ea94bdf575fea6c3f48eea500725828f [file] [log] [blame]
brettw@chromium.orge439a962011-01-02 08:16:20 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
mmentovai@google.comf5a40002008-08-09 01:19:43 +09004
brettw@chromium.orge439a962011-01-02 08:16:20 +09005#include "base/synchronization/lock_impl.h"
mmentovai@google.comf5a40002008-08-09 01:19:43 +09006
7#include <errno.h>
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +09008#include <string.h>
mmentovai@google.comf5a40002008-08-09 01:19:43 +09009
10#include "base/logging.h"
11
brettw@chromium.orge439a962011-01-02 08:16:20 +090012namespace base {
13namespace internal {
14
mmentovai@google.comf5a40002008-08-09 01:19:43 +090015LockImpl::LockImpl() {
deanm@chromium.orgd4eb87f2008-10-01 23:45:24 +090016#ifndef NDEBUG
17 // In debug, setup attributes for lock error checking.
erikkay@google.com3620e4c2008-08-12 00:38:27 +090018 pthread_mutexattr_t mta;
19 int rv = pthread_mutexattr_init(&mta);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090020 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
deanm@chromium.orgd4eb87f2008-10-01 23:45:24 +090021 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090022 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
erikkay@google.com3620e4c2008-08-12 00:38:27 +090023 rv = pthread_mutex_init(&os_lock_, &mta);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090024 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
erikkay@google.com3620e4c2008-08-12 00:38:27 +090025 rv = pthread_mutexattr_destroy(&mta);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090026 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
deanm@chromium.orgd4eb87f2008-10-01 23:45:24 +090027#else
28 // In release, go with the default lock attributes.
29 pthread_mutex_init(&os_lock_, NULL);
30#endif
mmentovai@google.comf5a40002008-08-09 01:19:43 +090031}
32
33LockImpl::~LockImpl() {
34 int rv = pthread_mutex_destroy(&os_lock_);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090035 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
mmentovai@google.comf5a40002008-08-09 01:19:43 +090036}
37
38bool LockImpl::Try() {
39 int rv = pthread_mutex_trylock(&os_lock_);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090040 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv);
mmentovai@google.comf5a40002008-08-09 01:19:43 +090041 return rv == 0;
42}
43
44void LockImpl::Lock() {
45 int rv = pthread_mutex_lock(&os_lock_);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090046 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
mmentovai@google.comf5a40002008-08-09 01:19:43 +090047}
48
49void LockImpl::Unlock() {
50 int rv = pthread_mutex_unlock(&os_lock_);
ajwong@chromium.org4d1bc942013-08-13 21:04:02 +090051 DCHECK_EQ(rv, 0) << ". " << strerror(rv);
mmentovai@google.comf5a40002008-08-09 01:19:43 +090052}
brettw@chromium.orge439a962011-01-02 08:16:20 +090053
54} // namespace internal
55} // namespace base