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 #ifndef BASE_SYNCHRONIZATION_LOCK_H_
      6 #define BASE_SYNCHRONIZATION_LOCK_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/logging.h"
     10 #include "base/macros.h"
     11 #include "base/synchronization/lock_impl.h"
     12 #include "base/threading/platform_thread.h"
     13 #include "build/build_config.h"
     14 
     15 namespace base {
     16 
     17 // A convenient wrapper for an OS specific critical section.  The only real
     18 // intelligence in this class is in debug mode for the support for the
     19 // AssertAcquired() method.
     20 class BASE_EXPORT Lock {
     21  public:
     22 #if !DCHECK_IS_ON()
     23    // Optimized wrapper implementation
     24   Lock() : lock_() {}
     25   ~Lock() {}
     26   void Acquire() { lock_.Lock(); }
     27   void Release() { lock_.Unlock(); }
     28 
     29   // If the lock is not held, take it and return true. If the lock is already
     30   // held by another thread, immediately return false. This must not be called
     31   // by a thread already holding the lock (what happens is undefined and an
     32   // assertion may fail).
     33   bool Try() { return lock_.Try(); }
     34 
     35   // Null implementation if not debug.
     36   void AssertAcquired() const {}
     37 #else
     38   Lock();
     39   ~Lock();
     40 
     41   // NOTE: We do not permit recursive locks and will commonly fire a DCHECK() if
     42   // a thread attempts to acquire the lock a second time (while already holding
     43   // it).
     44   void Acquire() {
     45     lock_.Lock();
     46     CheckUnheldAndMark();
     47   }
     48   void Release() {
     49     CheckHeldAndUnmark();
     50     lock_.Unlock();
     51   }
     52 
     53   bool Try() {
     54     bool rv = lock_.Try();
     55     if (rv) {
     56       CheckUnheldAndMark();
     57     }
     58     return rv;
     59   }
     60 
     61   void AssertAcquired() const;
     62 #endif  // DCHECK_IS_ON()
     63 
     64 #if defined(OS_POSIX) || defined(OS_WIN)
     65   // Both Windows and POSIX implementations of ConditionVariable need to be
     66   // able to see our lock and tweak our debugging counters, as they release and
     67   // acquire locks inside of their condition variable APIs.
     68   friend class ConditionVariable;
     69 #endif
     70 
     71  private:
     72 #if DCHECK_IS_ON()
     73   // Members and routines taking care of locks assertions.
     74   // Note that this checks for recursive locks and allows them
     75   // if the variable is set.  This is allowed by the underlying implementation
     76   // on windows but not on Posix, so we're doing unneeded checks on Posix.
     77   // It's worth it to share the code.
     78   void CheckHeldAndUnmark();
     79   void CheckUnheldAndMark();
     80 
     81   // All private data is implicitly protected by lock_.
     82   // Be VERY careful to only access members under that lock.
     83   base::PlatformThreadRef owning_thread_ref_;
     84 #endif  // DCHECK_IS_ON()
     85 
     86   // Platform specific underlying lock implementation.
     87   internal::LockImpl lock_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(Lock);
     90 };
     91 
     92 // A helper class that acquires the given Lock while the AutoLock is in scope.
     93 class AutoLock {
     94  public:
     95   struct AlreadyAcquired {};
     96 
     97   explicit AutoLock(Lock& lock) : lock_(lock) {
     98     lock_.Acquire();
     99   }
    100 
    101   AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) {
    102     lock_.AssertAcquired();
    103   }
    104 
    105   ~AutoLock() {
    106     lock_.AssertAcquired();
    107     lock_.Release();
    108   }
    109 
    110  private:
    111   Lock& lock_;
    112   DISALLOW_COPY_AND_ASSIGN(AutoLock);
    113 };
    114 
    115 // AutoUnlock is a helper that will Release() the |lock| argument in the
    116 // constructor, and re-Acquire() it in the destructor.
    117 class AutoUnlock {
    118  public:
    119   explicit AutoUnlock(Lock& lock) : lock_(lock) {
    120     // We require our caller to have the lock.
    121     lock_.AssertAcquired();
    122     lock_.Release();
    123   }
    124 
    125   ~AutoUnlock() {
    126     lock_.Acquire();
    127   }
    128 
    129  private:
    130   Lock& lock_;
    131   DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
    132 };
    133 
    134 }  // namespace base
    135 
    136 #endif  // BASE_SYNCHRONIZATION_LOCK_H_
    137