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"
20 // NB: Comment this out to enable callstack tracking.
21 # define MOZ_CALLSTACK_DISABLED
25 # ifndef MOZ_CALLSTACK_DISABLED
26 # include "mozilla/Maybe.h"
27 # include "nsTArray.h"
33 // This header is not meant to be included by client code.
40 class DeadlockDetector
;
44 * BlockingResourceBase
45 * Base class of resources that might block clients trying to acquire them.
46 * Does debugging and deadlock detection in DEBUG builds.
48 class BlockingResourceBase
{
50 // Needs to be kept in sync with kResourceTypeNames.
51 enum BlockingResourceType
{
60 * Human-readable version of BlockingResourceType enum.
62 static const char* const kResourceTypeName
[];
66 static size_t SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf
);
70 * Write a description of this blocking resource to |aOut|. If
71 * the resource appears to be currently acquired, the current
72 * acquisition context is printed and true is returned.
73 * Otherwise, we print the context from |aFirstSeen|, the
74 * first acquisition from which the code calling |Print()|
75 * became interested in us, and return false.
77 * *NOT* thread safe. Reads |mAcquisitionContext| without
78 * synchronization, but this will not cause correctness
81 * FIXME bug 456272: hack alert: because we can't write call
82 * contexts into strings, all info is written to stderr, but
83 * only some info is written into |aOut|
85 bool Print(nsACString
& aOut
) const;
87 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
88 // NB: |mName| is not reported as it's expected to be a static string.
89 // If we switch to a nsString it should be added to the tally.
90 // |mChainPrev| is not reported because its memory is not owned.
91 size_t n
= aMallocSizeOf(this);
95 // ``DDT'' = ``Deadlock Detector Type''
96 typedef DeadlockDetector
<BlockingResourceBase
> DDT
;
99 # ifdef MOZ_CALLSTACK_DISABLED
100 typedef bool AcquisitionState
;
102 // Using maybe to use emplacement as the acquisition state flag; we may not
103 // always get a stack trace because of possible stack walk suppression or
104 // errors, hence can't use !IsEmpty() on the array itself as indication.
105 static size_t const kAcquisitionStateStackSize
= 24;
106 typedef Maybe
<AutoTArray
<void*, kAcquisitionStateStackSize
> >
111 * BlockingResourceBase
112 * Initialize this blocking resource. Also hooks the resource into
113 * instrumentation code.
117 * @param aName A meaningful, unique name that can be used in
118 * error messages, et al.
119 * @param aType The specific type of |this|, if any.
121 BlockingResourceBase(const char* aName
, BlockingResourceType aType
);
123 ~BlockingResourceBase();
135 * *NOT* thread safe. Requires ownership of underlying resource.
137 void Acquire(); // NS_NEEDS_RESOURCE(this)
141 * Remove this resource from the current thread's acquisition chain.
142 * The resource does not have to be at the front of the chain, although
143 * it is confusing to release resources in a different order than they
144 * are acquired. This generates a warning.
146 * *NOT* thread safe. Requires ownership of underlying resource.
148 void Release(); // NS_NEEDS_RESOURCE(this)
155 * @return the front of the resource acquisition chain, i.e., the last
158 static BlockingResourceBase
* ResourceChainFront() {
159 return sResourceAcqnChainFront
.get();
165 * *NOT* thread safe. Requires ownership of underlying resource.
167 static BlockingResourceBase
* ResourceChainPrev(
168 const BlockingResourceBase
* aResource
) {
169 return aResource
->mChainPrev
;
170 } // NS_NEEDS_RESOURCE(this)
173 * ResourceChainAppend
174 * Set |this| to the front of the resource acquisition chain, and link
177 * *NOT* thread safe. Requires ownership of underlying resource.
179 void ResourceChainAppend(BlockingResourceBase
* aPrev
) {
181 sResourceAcqnChainFront
.set(this);
182 } // NS_NEEDS_RESOURCE(this)
185 * ResourceChainRemove
186 * Remove |this| from the front of the resource acquisition chain.
188 * *NOT* thread safe. Requires ownership of underlying resource.
190 void ResourceChainRemove() {
191 NS_ASSERTION(this == ResourceChainFront(), "not at chain front");
192 sResourceAcqnChainFront
.set(mChainPrev
);
193 } // NS_NEEDS_RESOURCE(this)
196 * TakeAcquisitionState
197 * Return whether or not this resource was acquired and mark the resource
198 * as not acquired for subsequent uses.
200 * *NOT* thread safe. Requires ownership of underlying resource.
202 AcquisitionState
TakeAcquisitionState() {
203 # ifdef MOZ_CALLSTACK_DISABLED
204 bool acquired
= mAcquired
;
205 ClearAcquisitionState();
208 return mAcquired
.take();
213 * SetAcquisitionState
214 * Set whether or not this resource was acquired.
216 * *NOT* thread safe. Requires ownership of underlying resource.
218 void SetAcquisitionState(AcquisitionState
&& aAcquisitionState
) {
219 mAcquired
= std::move(aAcquisitionState
);
223 * ClearAcquisitionState
224 * Indicate this resource is not acquired.
226 * *NOT* thread safe. Requires ownership of underlying resource.
228 void ClearAcquisitionState() {
229 # ifdef MOZ_CALLSTACK_DISABLED
238 * Indicates if this resource is acquired.
240 * *NOT* thread safe. Requires ownership of underlying resource.
242 bool IsAcquired() const { return (bool)mAcquired
; }
246 * A series of resource acquisitions creates a chain of orders. This
247 * chain is implemented as a linked list; |mChainPrev| points to the
248 * resource most recently Acquire()'d before this one.
250 BlockingResourceBase
* mChainPrev
;
255 * A descriptive name for this resource. Used in error
262 * The more specific type of this resource. Used to implement
263 * special semantics (e.g., reentrancy of monitors).
265 BlockingResourceType mType
;
269 * Indicates if this resource is currently acquired.
271 AcquisitionState mAcquired
;
273 # ifndef MOZ_CALLSTACK_DISABLED
276 * Inidicates where this resource was first acquired.
278 AcquisitionState mFirstSeen
;
283 * Ensures static members are initialized only once, and in a
286 static PRCallOnceType sCallOnce
;
289 * Thread-private pointer to the front of each thread's resource
292 static MOZ_THREAD_LOCAL(BlockingResourceBase
*) sResourceAcqnChainFront
;
298 static DDT
* sDeadlockDetector
;
302 * Inititialize static members of BlockingResourceBase that can't
303 * be statically initialized.
307 static PRStatus
InitStatics();
311 * Free static members.
315 static void Shutdown();
317 static void StackWalkCallback(uint32_t aFrameNumber
, void* aPc
, void* aSp
,
319 static void GetStackTrace(AcquisitionState
& aState
,
320 const void* aFirstFramePC
);
322 # ifdef MOZILLA_INTERNAL_API
323 // so it can call BlockingResourceBase::Shutdown()
324 friend void LogTerm();
325 # endif // ifdef MOZILLA_INTERNAL_API
327 #else // non-DEBUG implementation
329 BlockingResourceBase(const char* aName
, BlockingResourceType aType
) {}
331 ~BlockingResourceBase() {}
336 } // namespace mozilla
338 #endif // mozilla_BlockingResourceBase_h