1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_BlockingResourceBase_h
8 #define mozilla_BlockingResourceBase_h
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/ThreadLocal.h"
12 #include "mozilla/Attributes.h"
21 // NB: Comment this out to enable callstack tracking.
22 # define MOZ_CALLSTACK_DISABLED
26 # ifndef MOZ_CALLSTACK_DISABLED
27 # include "mozilla/Maybe.h"
28 # include "nsTArray.h"
34 // This header is not meant to be included by client code.
41 class DeadlockDetector
;
45 * BlockingResourceBase
46 * Base class of resources that might block clients trying to acquire them.
47 * Does debugging and deadlock detection in DEBUG builds.
49 class BlockingResourceBase
{
51 // Needs to be kept in sync with kResourceTypeNames.
52 enum BlockingResourceType
{
61 * Human-readable version of BlockingResourceType enum.
63 static const char* const kResourceTypeName
[];
67 static size_t SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf
);
71 * Write a description of this blocking resource to |aOut|. If
72 * the resource appears to be currently acquired, the current
73 * acquisition context is printed and true is returned.
74 * Otherwise, we print the context from |aFirstSeen|, the
75 * first acquisition from which the code calling |Print()|
76 * became interested in us, and return false.
78 * *NOT* thread safe. Reads |mAcquisitionContext| without
79 * synchronization, but this will not cause correctness
82 * FIXME bug 456272: hack alert: because we can't write call
83 * contexts into strings, all info is written to stderr, but
84 * only some info is written into |aOut|
86 bool Print(nsACString
& aOut
) const;
88 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
89 // NB: |mName| is not reported as it's expected to be a static string.
90 // If we switch to a nsString it should be added to the tally.
91 // |mChainPrev| is not reported because its memory is not owned.
92 size_t n
= aMallocSizeOf(this);
96 // ``DDT'' = ``Deadlock Detector Type''
97 typedef DeadlockDetector
<BlockingResourceBase
> DDT
;
100 # ifdef MOZ_CALLSTACK_DISABLED
101 typedef bool AcquisitionState
;
103 // Using maybe to use emplacement as the acquisition state flag; we may not
104 // always get a stack trace because of possible stack walk suppression or
105 // errors, hence can't use !IsEmpty() on the array itself as indication.
106 static size_t const kAcquisitionStateStackSize
= 24;
107 typedef Maybe
<AutoTArray
<void*, kAcquisitionStateStackSize
> >
112 * BlockingResourceBase
113 * Initialize this blocking resource. Also hooks the resource into
114 * instrumentation code.
118 * @param aName A meaningful, unique name that can be used in
119 * error messages, et al.
120 * @param aType The specific type of |this|, if any.
122 BlockingResourceBase(const char* aName
, BlockingResourceType aType
);
124 ~BlockingResourceBase();
136 * *NOT* thread safe. Requires ownership of underlying resource.
138 void Acquire(); // NS_NEEDS_RESOURCE(this)
142 * Remove this resource from the current thread's acquisition chain.
143 * The resource does not have to be at the front of the chain, although
144 * it is confusing to release resources in a different order than they
145 * are acquired. This generates a warning.
147 * *NOT* thread safe. Requires ownership of underlying resource.
149 void Release(); // NS_NEEDS_RESOURCE(this)
156 * @return the front of the resource acquisition chain, i.e., the last
159 static BlockingResourceBase
* ResourceChainFront() {
160 return sResourceAcqnChainFront
.get();
166 * *NOT* thread safe. Requires ownership of underlying resource.
168 static BlockingResourceBase
* ResourceChainPrev(
169 const BlockingResourceBase
* aResource
) {
170 return aResource
->mChainPrev
;
171 } // NS_NEEDS_RESOURCE(this)
174 * ResourceChainAppend
175 * Set |this| to the front of the resource acquisition chain, and link
178 * *NOT* thread safe. Requires ownership of underlying resource.
180 void ResourceChainAppend(BlockingResourceBase
* aPrev
) {
182 sResourceAcqnChainFront
.set(this);
183 } // NS_NEEDS_RESOURCE(this)
186 * ResourceChainRemove
187 * Remove |this| from the front of the resource acquisition chain.
189 * *NOT* thread safe. Requires ownership of underlying resource.
191 void ResourceChainRemove() {
192 NS_ASSERTION(this == ResourceChainFront(), "not at chain front");
193 sResourceAcqnChainFront
.set(mChainPrev
);
194 } // NS_NEEDS_RESOURCE(this)
197 * TakeAcquisitionState
198 * Return whether or not this resource was acquired and mark the resource
199 * as not acquired for subsequent uses.
201 * *NOT* thread safe. Requires ownership of underlying resource.
203 AcquisitionState
TakeAcquisitionState() {
204 # ifdef MOZ_CALLSTACK_DISABLED
205 bool acquired
= mAcquired
;
206 ClearAcquisitionState();
209 return mAcquired
.take();
214 * SetAcquisitionState
215 * Set whether or not this resource was acquired.
217 * *NOT* thread safe. Requires ownership of underlying resource.
219 void SetAcquisitionState(AcquisitionState
&& aAcquisitionState
) {
220 mAcquired
= std::move(aAcquisitionState
);
224 * ClearAcquisitionState
225 * Indicate this resource is not acquired.
227 * *NOT* thread safe. Requires ownership of underlying resource.
229 void ClearAcquisitionState() {
230 # ifdef MOZ_CALLSTACK_DISABLED
239 * Indicates if this resource is acquired.
241 * *NOT* thread safe. Requires ownership of underlying resource.
243 bool IsAcquired() const { return (bool)mAcquired
; }
247 * A series of resource acquisitions creates a chain of orders. This
248 * chain is implemented as a linked list; |mChainPrev| points to the
249 * resource most recently Acquire()'d before this one.
251 BlockingResourceBase
* mChainPrev
;
256 * A descriptive name for this resource. Used in error
263 * The more specific type of this resource. Used to implement
264 * special semantics (e.g., reentrancy of monitors).
266 BlockingResourceType mType
;
270 * Indicates if this resource is currently acquired.
272 AcquisitionState mAcquired
;
274 # ifndef MOZ_CALLSTACK_DISABLED
277 * Inidicates where this resource was first acquired.
279 AcquisitionState mFirstSeen
;
284 * Ensures static members are initialized only once, and in a
287 static PRCallOnceType sCallOnce
;
290 * Thread-private pointer to the front of each thread's resource
293 static MOZ_THREAD_LOCAL(BlockingResourceBase
*) sResourceAcqnChainFront
;
299 static DDT
* sDeadlockDetector
;
303 * Inititialize static members of BlockingResourceBase that can't
304 * be statically initialized.
308 static PRStatus
InitStatics();
312 * Free static members.
316 static void Shutdown();
318 static void StackWalkCallback(uint32_t aFrameNumber
, void* aPc
, void* aSp
,
320 static void GetStackTrace(AcquisitionState
& aState
,
321 const void* aFirstFramePC
);
323 # ifdef MOZILLA_INTERNAL_API
324 // so it can call BlockingResourceBase::Shutdown()
325 friend void LogTerm();
326 # endif // ifdef MOZILLA_INTERNAL_API
328 #else // non-DEBUG implementation
330 BlockingResourceBase(const char* aName
, BlockingResourceType aType
) {}
332 ~BlockingResourceBase() {}
337 } // namespace mozilla
339 #endif // mozilla_BlockingResourceBase_h