• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
Lock()24   Lock() : lock_() {}
~Lock()25   ~Lock() {}
Acquire()26   void Acquire() { lock_.Lock(); }
Release()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).
Try()33   bool Try() { return lock_.Try(); }
34 
35   // Null implementation if not debug.
AssertAcquired()36   void AssertAcquired() const {}
37 #else
38   Lock();
39   ~Lock();
40 
41   // NOTE: Although windows critical sections support recursive locks, we do not
42   // allow this, and we will commonly fire a DCHECK() if a thread attempts to
43   // acquire the lock a second time (while already holding 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)
65   // The posix implementation of ConditionVariable needs to be able
66   // to see our lock and tweak our debugging counters, as it releases
67   // and acquires locks inside of pthread_cond_{timed,}wait.
68   friend class ConditionVariable;
69 #elif defined(OS_WIN)
70   // The Windows Vista implementation of ConditionVariable needs the
71   // native handle of the critical section.
72   friend class WinVistaCondVar;
73 #endif
74 
75  private:
76 #if DCHECK_IS_ON()
77   // Members and routines taking care of locks assertions.
78   // Note that this checks for recursive locks and allows them
79   // if the variable is set.  This is allowed by the underlying implementation
80   // on windows but not on Posix, so we're doing unneeded checks on Posix.
81   // It's worth it to share the code.
82   void CheckHeldAndUnmark();
83   void CheckUnheldAndMark();
84 
85   // All private data is implicitly protected by lock_.
86   // Be VERY careful to only access members under that lock.
87   base::PlatformThreadRef owning_thread_ref_;
88 #endif  // DCHECK_IS_ON()
89 
90   // Platform specific underlying lock implementation.
91   internal::LockImpl lock_;
92 
93   DISALLOW_COPY_AND_ASSIGN(Lock);
94 };
95 
96 // A helper class that acquires the given Lock while the AutoLock is in scope.
97 class AutoLock {
98  public:
99   struct AlreadyAcquired {};
100 
AutoLock(Lock & lock)101   explicit AutoLock(Lock& lock) : lock_(lock) {
102     lock_.Acquire();
103   }
104 
AutoLock(Lock & lock,const AlreadyAcquired &)105   AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) {
106     lock_.AssertAcquired();
107   }
108 
~AutoLock()109   ~AutoLock() {
110     lock_.AssertAcquired();
111     lock_.Release();
112   }
113 
114  private:
115   Lock& lock_;
116   DISALLOW_COPY_AND_ASSIGN(AutoLock);
117 };
118 
119 // AutoUnlock is a helper that will Release() the |lock| argument in the
120 // constructor, and re-Acquire() it in the destructor.
121 class AutoUnlock {
122  public:
AutoUnlock(Lock & lock)123   explicit AutoUnlock(Lock& lock) : lock_(lock) {
124     // We require our caller to have the lock.
125     lock_.AssertAcquired();
126     lock_.Release();
127   }
128 
~AutoUnlock()129   ~AutoUnlock() {
130     lock_.Acquire();
131   }
132 
133  private:
134   Lock& lock_;
135   DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
136 };
137 
138 }  // namespace base
139 
140 #endif  // BASE_SYNCHRONIZATION_LOCK_H_
141