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 void AssertSafeToProcessEventLoop();
69 static size_t SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf
);
73 * Write a description of this blocking resource to |aOut|. If
74 * the resource appears to be currently acquired, the current
75 * acquisition context is printed and true is returned.
76 * Otherwise, we print the context from |aFirstSeen|, the
77 * first acquisition from which the code calling |Print()|
78 * became interested in us, and return false.
80 * *NOT* thread safe. Reads |mAcquisitionContext| without
81 * synchronization, but this will not cause correctness
84 * FIXME bug 456272: hack alert: because we can't write call
85 * contexts into strings, all info is written to stderr, but
86 * only some info is written into |aOut|
88 bool Print(nsACString
& aOut
) const;
90 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
91 // NB: |mName| is not reported as it's expected to be a static string.
92 // If we switch to a nsString it should be added to the tally.
93 // |mChainPrev| is not reported because its memory is not owned.
94 size_t n
= aMallocSizeOf(this);
98 // ``DDT'' = ``Deadlock Detector Type''
99 typedef DeadlockDetector
<BlockingResourceBase
> DDT
;
102 # ifdef MOZ_CALLSTACK_DISABLED
103 typedef bool AcquisitionState
;
105 // Using maybe to use emplacement as the acquisition state flag; we may not
106 // always get a stack trace because of possible stack walk suppression or
107 // errors, hence can't use !IsEmpty() on the array itself as indication.
108 static size_t const kAcquisitionStateStackSize
= 24;
109 typedef Maybe
<AutoTArray
<void*, kAcquisitionStateStackSize
> >
114 * BlockingResourceBase
115 * Initialize this blocking resource. Also hooks the resource into
116 * instrumentation code.
120 * @param aName A meaningful, unique name that can be used in
121 * error messages, et al.
122 * @param aType The specific type of |this|, if any.
124 BlockingResourceBase(const char* aName
, BlockingResourceType aType
);
126 ~BlockingResourceBase();
138 * *NOT* thread safe. Requires ownership of underlying resource.
140 void Acquire(); // NS_NEEDS_RESOURCE(this)
144 * Remove this resource from the current thread's acquisition chain.
145 * The resource does not have to be at the front of the chain, although
146 * it is confusing to release resources in a different order than they
147 * are acquired. This generates a warning.
149 * *NOT* thread safe. Requires ownership of underlying resource.
151 void Release(); // NS_NEEDS_RESOURCE(this)
158 * @return the front of the resource acquisition chain, i.e., the last
161 static BlockingResourceBase
* ResourceChainFront() {
162 return sResourceAcqnChainFront
.get();
168 * *NOT* thread safe. Requires ownership of underlying resource.
170 static BlockingResourceBase
* ResourceChainPrev(
171 const BlockingResourceBase
* aResource
) {
172 return aResource
->mChainPrev
;
173 } // NS_NEEDS_RESOURCE(this)
176 * ResourceChainAppend
177 * Set |this| to the front of the resource acquisition chain, and link
180 * *NOT* thread safe. Requires ownership of underlying resource.
182 void ResourceChainAppend(BlockingResourceBase
* aPrev
) {
184 sResourceAcqnChainFront
.set(this);
185 } // NS_NEEDS_RESOURCE(this)
188 * ResourceChainRemove
189 * Remove |this| from the front of the resource acquisition chain.
191 * *NOT* thread safe. Requires ownership of underlying resource.
193 void ResourceChainRemove() {
194 NS_ASSERTION(this == ResourceChainFront(), "not at chain front");
195 sResourceAcqnChainFront
.set(mChainPrev
);
196 } // NS_NEEDS_RESOURCE(this)
199 * TakeAcquisitionState
200 * Return whether or not this resource was acquired and mark the resource
201 * as not acquired for subsequent uses.
203 * *NOT* thread safe. Requires ownership of underlying resource.
205 AcquisitionState
TakeAcquisitionState() {
206 # ifdef MOZ_CALLSTACK_DISABLED
207 bool acquired
= mAcquired
;
208 ClearAcquisitionState();
211 return mAcquired
.take();
216 * SetAcquisitionState
217 * Set whether or not this resource was acquired.
219 * *NOT* thread safe. Requires ownership of underlying resource.
221 void SetAcquisitionState(AcquisitionState
&& aAcquisitionState
) {
222 mAcquired
= std::move(aAcquisitionState
);
226 * ClearAcquisitionState
227 * Indicate this resource is not acquired.
229 * *NOT* thread safe. Requires ownership of underlying resource.
231 void ClearAcquisitionState() {
232 # ifdef MOZ_CALLSTACK_DISABLED
241 * Indicates if this resource is acquired.
243 * *NOT* thread safe. Requires ownership of underlying resource.
245 bool IsAcquired() const { return (bool)mAcquired
; }
249 * A series of resource acquisitions creates a chain of orders. This
250 * chain is implemented as a linked list; |mChainPrev| points to the
251 * resource most recently Acquire()'d before this one.
253 BlockingResourceBase
* mChainPrev
;
258 * A descriptive name for this resource. Used in error
265 * The more specific type of this resource. Used to implement
266 * special semantics (e.g., reentrancy of monitors).
268 BlockingResourceType mType
;
272 * Indicates if this resource is currently acquired.
274 AcquisitionState mAcquired
;
276 # ifndef MOZ_CALLSTACK_DISABLED
279 * Inidicates where this resource was first acquired.
281 AcquisitionState mFirstSeen
;
286 * Ensures static members are initialized only once, and in a
289 static PRCallOnceType sCallOnce
;
292 * Thread-private pointer to the front of each thread's resource
295 static MOZ_THREAD_LOCAL(BlockingResourceBase
*) sResourceAcqnChainFront
;
301 static DDT
* sDeadlockDetector
;
305 * Inititialize static members of BlockingResourceBase that can't
306 * be statically initialized.
310 static PRStatus
InitStatics();
314 * Free static members.
318 static void Shutdown();
320 static void StackWalkCallback(uint32_t aFrameNumber
, void* aPc
, void* aSp
,
322 static void GetStackTrace(AcquisitionState
& aState
,
323 const void* aFirstFramePC
);
325 # ifdef MOZILLA_INTERNAL_API
326 // so it can call BlockingResourceBase::Shutdown()
327 friend void LogTerm();
328 # endif // ifdef MOZILLA_INTERNAL_API
330 #else // non-DEBUG implementation
332 BlockingResourceBase(const char* aName
, BlockingResourceType aType
) {}
334 ~BlockingResourceBase() {}
339 } // namespace mozilla
341 #endif // mozilla_BlockingResourceBase_h