Home | History | Annotate | Download | only in synchronization
      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.
      4 
      5 #include "base/synchronization/lock_impl.h"
      6 
      7 #include <errno.h>
      8 
      9 #include "base/logging.h"
     10 
     11 namespace base {
     12 namespace internal {
     13 
     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
     30 }
     31 
     32 LockImpl::~LockImpl() {
     33   int rv = pthread_mutex_destroy(&os_lock_);
     34   DCHECK_EQ(rv, 0);
     35 }
     36 
     37 bool LockImpl::Try() {
     38   int rv = pthread_mutex_trylock(&os_lock_);
     39   DCHECK(rv == 0 || rv == EBUSY);
     40   return rv == 0;
     41 }
     42 
     43 void LockImpl::Lock() {
     44   int rv = pthread_mutex_lock(&os_lock_);
     45   DCHECK_EQ(rv, 0);
     46 }
     47 
     48 void LockImpl::Unlock() {
     49   int rv = pthread_mutex_unlock(&os_lock_);
     50   DCHECK_EQ(rv, 0);
     51 }
     52 
     53 }  // namespace internal
     54 }  // namespace base
     55