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/. */
8 #ifndef mozilla_BlockingResourceBase_h
9 #define mozilla_BlockingResourceBase_h
16 #include "nsISupportsImpl.h"
20 // NB: Comment this out to enable callstack tracking.
21 #define MOZ_CALLSTACK_DISABLED
25 #include "nsStringGlue.h"
27 #ifndef MOZ_CALLSTACK_DISABLED
35 // This header is not meant to be included by client code.
41 template <class T
> 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
52 // Needs to be kept in sync with kResourceTypeNames.
53 enum BlockingResourceType
{ eMutex
, eReentrantMonitor
, eCondVar
};
57 * Human-readable version of BlockingResourceType enum.
59 static const char* const kResourceTypeName
[];
65 SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf
);
69 * Write a description of this blocking resource to |aOut|. If
70 * the resource appears to be currently acquired, the current
71 * acquisition context is printed and true is returned.
72 * Otherwise, we print the context from |aFirstSeen|, the
73 * first acquisition from which the code calling |Print()|
74 * became interested in us, and return false.
76 * *NOT* thread safe. Reads |mAcquisitionContext| without
77 * synchronization, but this will not cause correctness
80 * FIXME bug 456272: hack alert: because we can't write call
81 * contexts into strings, all info is written to stderr, but
82 * only some info is written into |aOut|
84 bool Print(nsACString
& aOut
) const;
87 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 typedef nsAutoTArray
<void*, 24> AcquisitionState
;
107 * BlockingResourceBase
108 * Initialize this blocking resource. Also hooks the resource into
109 * instrumentation code.
113 * @param aName A meaningful, unique name that can be used in
114 * error messages, et al.
115 * @param aType The specific type of |this|, if any.
117 BlockingResourceBase(const char* aName
, BlockingResourceType aType
);
119 ~BlockingResourceBase();
131 * *NOT* thread safe. Requires ownership of underlying resource.
133 void Acquire(); //NS_NEEDS_RESOURCE(this)
137 * Remove this resource from the current thread's acquisition chain.
138 * The resource does not have to be at the front of the chain, although
139 * it is confusing to release resources in a different order than they
140 * are acquired. This generates a warning.
142 * *NOT* thread safe. Requires ownership of underlying resource.
144 void Release(); //NS_NEEDS_RESOURCE(this)
151 * @return the front of the resource acquisition chain, i.e., the last
154 static BlockingResourceBase
* ResourceChainFront()
157 (BlockingResourceBase
*)PR_GetThreadPrivate(sResourceAcqnChainFrontTPI
);
163 * *NOT* thread safe. Requires ownership of underlying resource.
165 static BlockingResourceBase
* ResourceChainPrev(
166 const BlockingResourceBase
* aResource
)
168 return aResource
->mChainPrev
;
169 } //NS_NEEDS_RESOURCE(this)
172 * ResourceChainAppend
173 * Set |this| to the front of the resource acquisition chain, and link
176 * *NOT* thread safe. Requires ownership of underlying resource.
178 void ResourceChainAppend(BlockingResourceBase
* aPrev
)
181 PR_SetThreadPrivate(sResourceAcqnChainFrontTPI
, 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()
192 NS_ASSERTION(this == ResourceChainFront(), "not at chain front");
193 PR_SetThreadPrivate(sResourceAcqnChainFrontTPI
, mChainPrev
);
194 } //NS_NEEDS_RESOURCE(this)
197 * GetAcquisitionState
198 * Return whether or not this resource was acquired.
200 * *NOT* thread safe. Requires ownership of underlying resource.
202 AcquisitionState
GetAcquisitionState()
208 * SetAcquisitionState
209 * Set whether or not this resource was acquired.
211 * *NOT* thread safe. Requires ownership of underlying resource.
213 void SetAcquisitionState(const AcquisitionState
& aAcquisitionState
)
215 mAcquired
= aAcquisitionState
;
219 * ClearAcquisitionState
220 * Indicate this resource is not acquired.
222 * *NOT* thread safe. Requires ownership of underlying resource.
224 void ClearAcquisitionState()
226 #ifdef MOZ_CALLSTACK_DISABLED
235 * Indicates if this resource is acquired.
237 * *NOT* thread safe. Requires ownership of underlying resource.
239 bool IsAcquired() const
241 #ifdef MOZ_CALLSTACK_DISABLED
244 return !mAcquired
.IsEmpty();
250 * A series of resource acquisitions creates a chain of orders. This
251 * chain is implemented as a linked list; |mChainPrev| points to the
252 * resource most recently Acquire()'d before this one.
254 BlockingResourceBase
* mChainPrev
;
259 * A descriptive name for this resource. Used in error
266 * The more specific type of this resource. Used to implement
267 * special semantics (e.g., reentrancy of monitors).
269 BlockingResourceType mType
;
273 * Indicates if this resource is currently acquired.
275 AcquisitionState mAcquired
;
277 #ifndef MOZ_CALLSTACK_DISABLED
280 * Inidicates where this resource was first acquired.
282 AcquisitionState mFirstSeen
;
287 * Ensures static members are initialized only once, and in a
290 static PRCallOnceType sCallOnce
;
293 * sResourceAcqnChainFrontTPI
294 * Thread-private index to the front of each thread's resource
297 static unsigned sResourceAcqnChainFrontTPI
;
303 static DDT
* sDeadlockDetector
;
307 * Inititialize static members of BlockingResourceBase that can't
308 * be statically initialized.
312 static PRStatus
InitStatics();
316 * Free static members.
320 static void Shutdown();
322 static void StackWalkCallback(uint32_t aFrameNumber
, void* aPc
,
323 void* aSp
, void* aClosure
);
324 static void GetStackTrace(AcquisitionState
& aState
);
326 # ifdef MOZILLA_INTERNAL_API
327 // so it can call BlockingResourceBase::Shutdown()
328 friend void LogTerm();
329 # endif // ifdef MOZILLA_INTERNAL_API
331 #else // non-DEBUG implementation
333 BlockingResourceBase(const char* aName
, BlockingResourceType aType
) {}
335 ~BlockingResourceBase() {}
341 } // namespace mozilla
344 #endif // mozilla_BlockingResourceBase_h