While flushing, reset the input_buf_index_ so that it will not be reused.
[chromium-blink-merge.git] / base / synchronization / lock.h
blob81e274809f8a93436889af756aaf85157791eb2e
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.
5 #ifndef BASE_SYNCHRONIZATION_LOCK_H_
6 #define BASE_SYNCHRONIZATION_LOCK_H_
8 #include "base/base_export.h"
9 #include "base/logging.h"
10 #include "base/synchronization/lock_impl.h"
11 #include "base/threading/platform_thread.h"
13 namespace base {
15 // A convenient wrapper for an OS specific critical section. The only real
16 // intelligence in this class is in debug mode for the support for the
17 // AssertAcquired() method.
18 class BASE_EXPORT Lock {
19 public:
20 #if !DCHECK_IS_ON()
21 // Optimized wrapper implementation
22 Lock() : lock_() {}
23 ~Lock() {}
24 void Acquire() { lock_.Lock(); }
25 void Release() { lock_.Unlock(); }
27 // If the lock is not held, take it and return true. If the lock is already
28 // held by another thread, immediately return false. This must not be called
29 // by a thread already holding the lock (what happens is undefined and an
30 // assertion may fail).
31 bool Try() { return lock_.Try(); }
33 // Null implementation if not debug.
34 void AssertAcquired() const {}
35 #else
36 Lock();
37 ~Lock();
39 // NOTE: Although windows critical sections support recursive locks, we do not
40 // allow this, and we will commonly fire a DCHECK() if a thread attempts to
41 // acquire the lock a second time (while already holding it).
42 void Acquire() {
43 lock_.Lock();
44 CheckUnheldAndMark();
46 void Release() {
47 CheckHeldAndUnmark();
48 lock_.Unlock();
51 bool Try() {
52 bool rv = lock_.Try();
53 if (rv) {
54 CheckUnheldAndMark();
56 return rv;
59 void AssertAcquired() const;
60 #endif // DCHECK_IS_ON()
62 #if defined(OS_POSIX)
63 // The posix implementation of ConditionVariable needs to be able
64 // to see our lock and tweak our debugging counters, as it releases
65 // and acquires locks inside of pthread_cond_{timed,}wait.
66 friend class ConditionVariable;
67 #elif defined(OS_WIN)
68 // The Windows Vista implementation of ConditionVariable needs the
69 // native handle of the critical section.
70 friend class WinVistaCondVar;
71 #endif
73 private:
74 #if DCHECK_IS_ON()
75 // Members and routines taking care of locks assertions.
76 // Note that this checks for recursive locks and allows them
77 // if the variable is set. This is allowed by the underlying implementation
78 // on windows but not on Posix, so we're doing unneeded checks on Posix.
79 // It's worth it to share the code.
80 void CheckHeldAndUnmark();
81 void CheckUnheldAndMark();
83 // All private data is implicitly protected by lock_.
84 // Be VERY careful to only access members under that lock.
85 base::PlatformThreadRef owning_thread_ref_;
86 #endif // DCHECK_IS_ON()
88 // Platform specific underlying lock implementation.
89 internal::LockImpl lock_;
91 DISALLOW_COPY_AND_ASSIGN(Lock);
94 // A helper class that acquires the given Lock while the AutoLock is in scope.
95 class AutoLock {
96 public:
97 struct AlreadyAcquired {};
99 explicit AutoLock(Lock& lock) : lock_(lock) {
100 lock_.Acquire();
103 AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) {
104 lock_.AssertAcquired();
107 ~AutoLock() {
108 lock_.AssertAcquired();
109 lock_.Release();
112 private:
113 Lock& lock_;
114 DISALLOW_COPY_AND_ASSIGN(AutoLock);
117 // AutoUnlock is a helper that will Release() the |lock| argument in the
118 // constructor, and re-Acquire() it in the destructor.
119 class AutoUnlock {
120 public:
121 explicit AutoUnlock(Lock& lock) : lock_(lock) {
122 // We require our caller to have the lock.
123 lock_.AssertAcquired();
124 lock_.Release();
127 ~AutoUnlock() {
128 lock_.Acquire();
131 private:
132 Lock& lock_;
133 DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
136 } // namespace base
138 #endif // BASE_SYNCHRONIZATION_LOCK_H_