ipc/chromium/src/base/lock_impl_posix.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/lock_impl_posix.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,48 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/lock_impl.h"
     1.9 +
    1.10 +#include <errno.h>
    1.11 +
    1.12 +#include "base/logging.h"
    1.13 +
    1.14 +LockImpl::LockImpl() {
    1.15 +#ifndef NDEBUG
    1.16 +  // In debug, setup attributes for lock error checking.
    1.17 +  pthread_mutexattr_t mta;
    1.18 +  int rv = pthread_mutexattr_init(&mta);
    1.19 +  DCHECK_EQ(rv, 0);
    1.20 +  rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
    1.21 +  DCHECK_EQ(rv, 0);
    1.22 +  rv = pthread_mutex_init(&os_lock_, &mta);
    1.23 +  DCHECK_EQ(rv, 0);
    1.24 +  rv = pthread_mutexattr_destroy(&mta);
    1.25 +  DCHECK_EQ(rv, 0);
    1.26 +#else
    1.27 +  // In release, go with the default lock attributes.
    1.28 +  pthread_mutex_init(&os_lock_, NULL);
    1.29 +#endif
    1.30 +}
    1.31 +
    1.32 +LockImpl::~LockImpl() {
    1.33 +  int rv = pthread_mutex_destroy(&os_lock_);
    1.34 +  DCHECK_EQ(rv, 0);
    1.35 +}
    1.36 +
    1.37 +bool LockImpl::Try() {
    1.38 +  int rv = pthread_mutex_trylock(&os_lock_);
    1.39 +  DCHECK(rv == 0 || rv == EBUSY);
    1.40 +  return rv == 0;
    1.41 +}
    1.42 +
    1.43 +void LockImpl::Lock() {
    1.44 +  int rv = pthread_mutex_lock(&os_lock_);
    1.45 +  DCHECK_EQ(rv, 0);
    1.46 +}
    1.47 +
    1.48 +void LockImpl::Unlock() {
    1.49 +  int rv = pthread_mutex_unlock(&os_lock_);
    1.50 +  DCHECK_EQ(rv, 0);
    1.51 +}

mercurial