/src/mozilla-central/security/sandbox/chromium/base/synchronization/lock.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | Lock() : lock_() {} |
25 | 0 | ~Lock() {} |
26 | 0 | void Acquire() { lock_.Lock(); } |
27 | 0 | 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 | 0 | bool Try() { return lock_.Try(); } |
34 | | |
35 | | // Null implementation if not debug. |
36 | 0 | 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 | | // Whether Lock mitigates priority inversion when used from different thread |
65 | | // priorities. |
66 | 0 | static bool HandlesMultipleThreadPriorities() { |
67 | 0 | #if defined(OS_POSIX) |
68 | 0 | // POSIX mitigates priority inversion by setting the priority of a thread |
69 | 0 | // holding a Lock to the maximum priority of any other thread waiting on it. |
70 | 0 | return internal::LockImpl::PriorityInheritanceAvailable(); |
71 | 0 | #elif defined(OS_WIN) |
72 | 0 | // Windows mitigates priority inversion by randomly boosting the priority of |
73 | 0 | // ready threads. |
74 | 0 | // https://msdn.microsoft.com/library/windows/desktop/ms684831.aspx |
75 | 0 | return true; |
76 | 0 | #else |
77 | 0 | #error Unsupported platform |
78 | 0 | #endif |
79 | 0 | } |
80 | | |
81 | | #if defined(OS_POSIX) || defined(OS_WIN) |
82 | | // Both Windows and POSIX implementations of ConditionVariable need to be |
83 | | // able to see our lock and tweak our debugging counters, as they release and |
84 | | // acquire locks inside of their condition variable APIs. |
85 | | friend class ConditionVariable; |
86 | | #endif |
87 | | |
88 | | private: |
89 | | #if DCHECK_IS_ON() |
90 | | // Members and routines taking care of locks assertions. |
91 | | // Note that this checks for recursive locks and allows them |
92 | | // if the variable is set. This is allowed by the underlying implementation |
93 | | // on windows but not on Posix, so we're doing unneeded checks on Posix. |
94 | | // It's worth it to share the code. |
95 | | void CheckHeldAndUnmark(); |
96 | | void CheckUnheldAndMark(); |
97 | | |
98 | | // All private data is implicitly protected by lock_. |
99 | | // Be VERY careful to only access members under that lock. |
100 | | base::PlatformThreadRef owning_thread_ref_; |
101 | | #endif // DCHECK_IS_ON() |
102 | | |
103 | | // Platform specific underlying lock implementation. |
104 | | internal::LockImpl lock_; |
105 | | |
106 | | DISALLOW_COPY_AND_ASSIGN(Lock); |
107 | | }; |
108 | | |
109 | | // A helper class that acquires the given Lock while the AutoLock is in scope. |
110 | | class AutoLock { |
111 | | public: |
112 | | struct AlreadyAcquired {}; |
113 | | |
114 | 0 | explicit AutoLock(Lock& lock) : lock_(lock) { |
115 | 0 | lock_.Acquire(); |
116 | 0 | } |
117 | | |
118 | 0 | AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { |
119 | 0 | lock_.AssertAcquired(); |
120 | 0 | } |
121 | | |
122 | 0 | ~AutoLock() { |
123 | 0 | lock_.AssertAcquired(); |
124 | 0 | lock_.Release(); |
125 | 0 | } |
126 | | |
127 | | private: |
128 | | Lock& lock_; |
129 | | DISALLOW_COPY_AND_ASSIGN(AutoLock); |
130 | | }; |
131 | | |
132 | | // AutoUnlock is a helper that will Release() the |lock| argument in the |
133 | | // constructor, and re-Acquire() it in the destructor. |
134 | | class AutoUnlock { |
135 | | public: |
136 | 0 | explicit AutoUnlock(Lock& lock) : lock_(lock) { |
137 | 0 | // We require our caller to have the lock. |
138 | 0 | lock_.AssertAcquired(); |
139 | 0 | lock_.Release(); |
140 | 0 | } |
141 | | |
142 | 0 | ~AutoUnlock() { |
143 | 0 | lock_.Acquire(); |
144 | 0 | } |
145 | | |
146 | | private: |
147 | | Lock& lock_; |
148 | | DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
149 | | }; |
150 | | |
151 | | } // namespace base |
152 | | |
153 | | #endif // BASE_SYNCHRONIZATION_LOCK_H_ |