Bumping manifests a=b2g-bump
[gecko.git] / xpcom / glue / BlockingResourceBase.h
blob3407e8dd94d77e2b66b6b393191311fc3ecef187
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
11 #include "prlog.h"
13 #include "nscore.h"
14 #include "nsDebug.h"
15 #include "nsError.h"
16 #include "nsISupportsImpl.h"
18 #ifdef DEBUG
20 // NB: Comment this out to enable callstack tracking.
21 #define MOZ_CALLSTACK_DISABLED
23 #include "prinit.h"
25 #include "nsStringGlue.h"
27 #ifndef MOZ_CALLSTACK_DISABLED
28 #include "nsTArray.h"
29 #endif
31 #include "nsXPCOM.h"
32 #endif
35 // This header is not meant to be included by client code.
38 namespace mozilla {
40 #ifdef DEBUG
41 template <class T> class DeadlockDetector;
42 #endif
44 /**
45 * BlockingResourceBase
46 * Base class of resources that might block clients trying to acquire them.
47 * Does debugging and deadlock detection in DEBUG builds.
48 **/
49 class BlockingResourceBase
51 public:
52 // Needs to be kept in sync with kResourceTypeNames.
53 enum BlockingResourceType { eMutex, eReentrantMonitor, eCondVar };
55 /**
56 * kResourceTypeName
57 * Human-readable version of BlockingResourceType enum.
59 static const char* const kResourceTypeName[];
62 #ifdef DEBUG
64 static size_t
65 SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf);
67 /**
68 * Print
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
78 * problems.
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;
86 size_t
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);
93 return n;
96 // ``DDT'' = ``Deadlock Detector Type''
97 typedef DeadlockDetector<BlockingResourceBase> DDT;
99 protected:
100 #ifdef MOZ_CALLSTACK_DISABLED
101 typedef bool AcquisitionState;
102 #else
103 typedef nsAutoTArray<void*, 24> AcquisitionState;
104 #endif
107 * BlockingResourceBase
108 * Initialize this blocking resource. Also hooks the resource into
109 * instrumentation code.
111 * Thread safe.
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();
122 * CheckAcquire
124 * Thread safe.
126 void CheckAcquire();
129 * Acquire
131 * *NOT* thread safe. Requires ownership of underlying resource.
133 void Acquire(); //NS_NEEDS_RESOURCE(this)
136 * Release
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)
147 * ResourceChainFront
149 * Thread safe.
151 * @return the front of the resource acquisition chain, i.e., the last
152 * resource acquired.
154 static BlockingResourceBase* ResourceChainFront()
156 return
157 (BlockingResourceBase*)PR_GetThreadPrivate(sResourceAcqnChainFrontTPI);
161 * ResourceChainPrev
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
174 * |this| to |aPrev|.
176 * *NOT* thread safe. Requires ownership of underlying resource.
178 void ResourceChainAppend(BlockingResourceBase* aPrev)
180 mChainPrev = 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()
204 return mAcquired;
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
227 mAcquired = false;
228 #else
229 mAcquired.Clear();
230 #endif
234 * IsAcquired
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
242 return mAcquired;
243 #else
244 return !mAcquired.IsEmpty();
245 #endif
249 * mChainPrev
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;
256 private:
258 * mName
259 * A descriptive name for this resource. Used in error
260 * messages etc.
262 const char* mName;
265 * mType
266 * The more specific type of this resource. Used to implement
267 * special semantics (e.g., reentrancy of monitors).
269 BlockingResourceType mType;
272 * mAcquired
273 * Indicates if this resource is currently acquired.
275 AcquisitionState mAcquired;
277 #ifndef MOZ_CALLSTACK_DISABLED
279 * mFirstSeen
280 * Inidicates where this resource was first acquired.
282 AcquisitionState mFirstSeen;
283 #endif
286 * sCallOnce
287 * Ensures static members are initialized only once, and in a
288 * thread-safe way.
290 static PRCallOnceType sCallOnce;
293 * sResourceAcqnChainFrontTPI
294 * Thread-private index to the front of each thread's resource
295 * acquisition chain.
297 static unsigned sResourceAcqnChainFrontTPI;
300 * sDeadlockDetector
301 * Does as named.
303 static DDT* sDeadlockDetector;
306 * InitStatics
307 * Inititialize static members of BlockingResourceBase that can't
308 * be statically initialized.
310 * *NOT* thread safe.
312 static PRStatus InitStatics();
315 * Shutdown
316 * Free static members.
318 * *NOT* thread safe.
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() {}
337 #endif
341 } // namespace mozilla
344 #endif // mozilla_BlockingResourceBase_h