2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-04-19 01:54:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef mozilla_BlockingResourceBase_h
|
|
|
|
#define mozilla_BlockingResourceBase_h
|
|
|
|
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2009-05-05 04:57:15 +00:00
|
|
|
|
|
|
|
#include "nscore.h"
|
2009-05-03 04:13:57 +00:00
|
|
|
#include "nsDebug.h"
|
2009-05-05 04:57:15 +00:00
|
|
|
#include "nsError.h"
|
2014-02-26 21:36:35 +00:00
|
|
|
#include "nsISupportsImpl.h"
|
2009-05-05 04:57:15 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-08-13 19:37:31 +00:00
|
|
|
|
|
|
|
// NB: Comment this out to enable callstack tracking.
|
|
|
|
#define MOZ_CALLSTACK_DISABLED
|
|
|
|
|
2009-05-05 04:57:15 +00:00
|
|
|
#include "prinit.h"
|
|
|
|
|
2017-12-07 00:52:51 +00:00
|
|
|
#include "nsString.h"
|
2014-08-13 19:37:31 +00:00
|
|
|
|
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#endif
|
|
|
|
|
2010-02-03 22:17:09 +00:00
|
|
|
#include "nsXPCOM.h"
|
2009-05-05 04:57:15 +00:00
|
|
|
#endif
|
2009-04-19 01:54:23 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// This header is not meant to be included by client code.
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-12 18:44:04 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
template <class T> class DeadlockDetector;
|
|
|
|
#endif
|
2009-04-19 01:54:23 +00:00
|
|
|
|
|
|
|
/**
|
2009-05-05 04:57:15 +00:00
|
|
|
* BlockingResourceBase
|
2014-06-27 01:35:39 +00:00
|
|
|
* Base class of resources that might block clients trying to acquire them.
|
2009-05-05 04:57:15 +00:00
|
|
|
* Does debugging and deadlock detection in DEBUG builds.
|
2009-04-19 01:54:23 +00:00
|
|
|
**/
|
2014-08-27 22:47:27 +00:00
|
|
|
class BlockingResourceBase
|
2009-04-19 01:54:23 +00:00
|
|
|
{
|
2009-05-05 04:57:15 +00:00
|
|
|
public:
|
2014-06-27 01:35:39 +00:00
|
|
|
// Needs to be kept in sync with kResourceTypeNames.
|
2017-03-14 18:05:51 +00:00
|
|
|
enum BlockingResourceType { eMutex, eReentrantMonitor, eCondVar, eRecursiveMutex };
|
2009-04-19 01:54:23 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
/**
|
|
|
|
* kResourceTypeName
|
|
|
|
* Human-readable version of BlockingResourceType enum.
|
|
|
|
*/
|
|
|
|
static const char* const kResourceTypeName[];
|
2009-05-05 04:57:15 +00:00
|
|
|
|
|
|
|
|
2009-04-19 01:54:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2014-08-04 23:16:09 +00:00
|
|
|
static size_t
|
2014-08-12 18:44:04 +00:00
|
|
|
SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf);
|
2014-08-04 23:16:09 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
/**
|
2014-08-12 18:42:28 +00:00
|
|
|
* Print
|
|
|
|
* Write a description of this blocking resource to |aOut|. If
|
|
|
|
* the resource appears to be currently acquired, the current
|
|
|
|
* acquisition context is printed and true is returned.
|
|
|
|
* Otherwise, we print the context from |aFirstSeen|, the
|
|
|
|
* first acquisition from which the code calling |Print()|
|
|
|
|
* became interested in us, and return false.
|
2014-06-27 01:35:39 +00:00
|
|
|
*
|
2014-08-12 18:42:28 +00:00
|
|
|
* *NOT* thread safe. Reads |mAcquisitionContext| without
|
|
|
|
* synchronization, but this will not cause correctness
|
|
|
|
* problems.
|
|
|
|
*
|
|
|
|
* FIXME bug 456272: hack alert: because we can't write call
|
|
|
|
* contexts into strings, all info is written to stderr, but
|
|
|
|
* only some info is written into |aOut|
|
2014-06-27 01:35:39 +00:00
|
|
|
*/
|
2014-08-12 18:42:28 +00:00
|
|
|
bool Print(nsACString& aOut) const;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2014-06-27 01:35:39 +00:00
|
|
|
{
|
2014-08-12 18:42:28 +00:00
|
|
|
// NB: |mName| is not reported as it's expected to be a static string.
|
|
|
|
// If we switch to a nsString it should be added to the tally.
|
|
|
|
// |mChainPrev| is not reported because its memory is not owned.
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ``DDT'' = ``Deadlock Detector Type''
|
|
|
|
typedef DeadlockDetector<BlockingResourceBase> DDT;
|
2009-05-05 04:57:15 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
protected:
|
2014-08-13 19:37:31 +00:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-13 19:36:41 +00:00
|
|
|
typedef bool AcquisitionState;
|
2014-08-13 19:37:31 +00:00
|
|
|
#else
|
2016-02-02 15:36:30 +00:00
|
|
|
typedef AutoTArray<void*, 24> AcquisitionState;
|
2014-08-13 19:37:31 +00:00
|
|
|
#endif
|
2014-08-13 19:36:41 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
/**
|
|
|
|
* BlockingResourceBase
|
|
|
|
* Initialize this blocking resource. Also hooks the resource into
|
|
|
|
* instrumentation code.
|
|
|
|
*
|
|
|
|
* Thread safe.
|
|
|
|
*
|
|
|
|
* @param aName A meaningful, unique name that can be used in
|
|
|
|
* error messages, et al.
|
|
|
|
* @param aType The specific type of |this|, if any.
|
|
|
|
**/
|
|
|
|
BlockingResourceBase(const char* aName, BlockingResourceType aType);
|
|
|
|
|
|
|
|
~BlockingResourceBase();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CheckAcquire
|
|
|
|
*
|
|
|
|
* Thread safe.
|
|
|
|
**/
|
2014-08-08 18:43:58 +00:00
|
|
|
void CheckAcquire();
|
2014-06-27 01:35:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Acquire
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
**/
|
2014-08-08 18:43:58 +00:00
|
|
|
void Acquire(); //NS_NEEDS_RESOURCE(this)
|
2014-06-27 01:35:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release
|
|
|
|
* Remove this resource from the current thread's acquisition chain.
|
|
|
|
* The resource does not have to be at the front of the chain, although
|
|
|
|
* it is confusing to release resources in a different order than they
|
|
|
|
* are acquired. This generates a warning.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
**/
|
|
|
|
void Release(); //NS_NEEDS_RESOURCE(this)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceChainFront
|
|
|
|
*
|
|
|
|
* Thread safe.
|
|
|
|
*
|
|
|
|
* @return the front of the resource acquisition chain, i.e., the last
|
|
|
|
* resource acquired.
|
|
|
|
*/
|
|
|
|
static BlockingResourceBase* ResourceChainFront()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(BlockingResourceBase*)PR_GetThreadPrivate(sResourceAcqnChainFrontTPI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceChainPrev
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
|
|
|
static BlockingResourceBase* ResourceChainPrev(
|
|
|
|
const BlockingResourceBase* aResource)
|
|
|
|
{
|
|
|
|
return aResource->mChainPrev;
|
|
|
|
} //NS_NEEDS_RESOURCE(this)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceChainAppend
|
|
|
|
* Set |this| to the front of the resource acquisition chain, and link
|
|
|
|
* |this| to |aPrev|.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
|
|
|
void ResourceChainAppend(BlockingResourceBase* aPrev)
|
|
|
|
{
|
|
|
|
mChainPrev = aPrev;
|
|
|
|
PR_SetThreadPrivate(sResourceAcqnChainFrontTPI, this);
|
|
|
|
} //NS_NEEDS_RESOURCE(this)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceChainRemove
|
|
|
|
* Remove |this| from the front of the resource acquisition chain.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
|
|
|
void ResourceChainRemove()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(this == ResourceChainFront(), "not at chain front");
|
|
|
|
PR_SetThreadPrivate(sResourceAcqnChainFrontTPI, mChainPrev);
|
|
|
|
} //NS_NEEDS_RESOURCE(this)
|
|
|
|
|
|
|
|
/**
|
2014-08-08 18:43:26 +00:00
|
|
|
* GetAcquisitionState
|
|
|
|
* Return whether or not this resource was acquired.
|
2014-06-27 01:35:39 +00:00
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
2014-08-13 19:36:41 +00:00
|
|
|
AcquisitionState GetAcquisitionState()
|
2014-06-27 01:35:39 +00:00
|
|
|
{
|
2014-08-12 18:42:28 +00:00
|
|
|
return mAcquired;
|
2014-06-27 01:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-08 18:43:26 +00:00
|
|
|
* SetAcquisitionState
|
|
|
|
* Set whether or not this resource was acquired.
|
2014-06-27 01:35:39 +00:00
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
2014-08-13 19:36:41 +00:00
|
|
|
void SetAcquisitionState(const AcquisitionState& aAcquisitionState)
|
2014-06-27 01:35:39 +00:00
|
|
|
{
|
2014-08-12 18:42:28 +00:00
|
|
|
mAcquired = aAcquisitionState;
|
2014-06-27 01:35:39 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:37:05 +00:00
|
|
|
/**
|
|
|
|
* ClearAcquisitionState
|
|
|
|
* Indicate this resource is not acquired.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
|
|
|
void ClearAcquisitionState()
|
|
|
|
{
|
2014-08-13 19:37:31 +00:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-13 19:37:05 +00:00
|
|
|
mAcquired = false;
|
2014-08-13 19:37:31 +00:00
|
|
|
#else
|
|
|
|
mAcquired.Clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsAcquired
|
|
|
|
* Indicates if this resource is acquired.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
|
|
|
bool IsAcquired() const
|
|
|
|
{
|
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
|
|
|
return mAcquired;
|
|
|
|
#else
|
|
|
|
return !mAcquired.IsEmpty();
|
|
|
|
#endif
|
2014-08-13 19:37:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
/**
|
|
|
|
* mChainPrev
|
|
|
|
* A series of resource acquisitions creates a chain of orders. This
|
|
|
|
* chain is implemented as a linked list; |mChainPrev| points to the
|
|
|
|
* resource most recently Acquire()'d before this one.
|
|
|
|
**/
|
|
|
|
BlockingResourceBase* mChainPrev;
|
2009-04-19 01:54:23 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
private:
|
|
|
|
/**
|
2014-08-12 18:42:28 +00:00
|
|
|
* mName
|
|
|
|
* A descriptive name for this resource. Used in error
|
|
|
|
* messages etc.
|
|
|
|
*/
|
|
|
|
const char* mName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mType
|
|
|
|
* The more specific type of this resource. Used to implement
|
|
|
|
* special semantics (e.g., reentrancy of monitors).
|
|
|
|
**/
|
|
|
|
BlockingResourceType mType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mAcquired
|
|
|
|
* Indicates if this resource is currently acquired.
|
2014-06-27 01:35:39 +00:00
|
|
|
*/
|
2014-08-13 19:36:41 +00:00
|
|
|
AcquisitionState mAcquired;
|
2014-06-27 01:35:39 +00:00
|
|
|
|
2014-08-13 19:37:31 +00:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
/**
|
|
|
|
* mFirstSeen
|
|
|
|
* Inidicates where this resource was first acquired.
|
|
|
|
*/
|
|
|
|
AcquisitionState mFirstSeen;
|
|
|
|
#endif
|
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
/**
|
|
|
|
* sCallOnce
|
|
|
|
* Ensures static members are initialized only once, and in a
|
|
|
|
* thread-safe way.
|
|
|
|
*/
|
|
|
|
static PRCallOnceType sCallOnce;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sResourceAcqnChainFrontTPI
|
|
|
|
* Thread-private index to the front of each thread's resource
|
|
|
|
* acquisition chain.
|
|
|
|
*/
|
|
|
|
static unsigned sResourceAcqnChainFrontTPI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sDeadlockDetector
|
|
|
|
* Does as named.
|
|
|
|
*/
|
|
|
|
static DDT* sDeadlockDetector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InitStatics
|
|
|
|
* Inititialize static members of BlockingResourceBase that can't
|
|
|
|
* be statically initialized.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe.
|
|
|
|
*/
|
2014-08-12 18:44:04 +00:00
|
|
|
static PRStatus InitStatics();
|
2014-06-27 01:35:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown
|
|
|
|
* Free static members.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe.
|
|
|
|
*/
|
2014-08-12 18:44:04 +00:00
|
|
|
static void Shutdown();
|
2009-06-16 19:36:44 +00:00
|
|
|
|
2014-09-11 04:47:01 +00:00
|
|
|
static void StackWalkCallback(uint32_t aFrameNumber, void* aPc,
|
|
|
|
void* aSp, void* aClosure);
|
2014-08-13 19:37:40 +00:00
|
|
|
static void GetStackTrace(AcquisitionState& aState);
|
|
|
|
|
2009-06-16 19:36:44 +00:00
|
|
|
# ifdef MOZILLA_INTERNAL_API
|
2014-06-27 01:35:39 +00:00
|
|
|
// so it can call BlockingResourceBase::Shutdown()
|
|
|
|
friend void LogTerm();
|
2009-06-16 19:36:44 +00:00
|
|
|
# endif // ifdef MOZILLA_INTERNAL_API
|
2009-04-19 01:54:23 +00:00
|
|
|
|
2009-05-05 04:57:15 +00:00
|
|
|
#else // non-DEBUG implementation
|
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
BlockingResourceBase(const char* aName, BlockingResourceType aType) {}
|
2009-05-05 04:57:15 +00:00
|
|
|
|
2014-06-27 01:35:39 +00:00
|
|
|
~BlockingResourceBase() {}
|
2009-04-19 01:54:23 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
|
|
|
#endif // mozilla_BlockingResourceBase_h
|