2014-06-30 08:39:45 -07: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 04:12:37 -07: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-18 18:54:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef mozilla_BlockingResourceBase_h
|
|
|
|
#define mozilla_BlockingResourceBase_h
|
|
|
|
|
2009-05-02 21:13:57 -07:00
|
|
|
#include "prlog.h"
|
2009-05-04 21:57:15 -07:00
|
|
|
|
|
|
|
#include "nscore.h"
|
2009-05-02 21:13:57 -07:00
|
|
|
#include "nsDebug.h"
|
2009-05-04 21:57:15 -07:00
|
|
|
#include "nsError.h"
|
2014-02-26 13:36:35 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
2009-05-04 21:57:15 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include "prinit.h"
|
|
|
|
|
|
|
|
#include "nsStringGlue.h"
|
2010-02-03 14:17:09 -08:00
|
|
|
#include "nsXPCOM.h"
|
2009-05-04 21:57:15 -07:00
|
|
|
#endif
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// This header is not meant to be included by client code.
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-12 11:44:04 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
template <class T> class DeadlockDetector;
|
|
|
|
#endif
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
/**
|
2009-05-04 21:57:15 -07:00
|
|
|
* BlockingResourceBase
|
2014-06-26 18:35:39 -07:00
|
|
|
* Base class of resources that might block clients trying to acquire them.
|
2009-05-04 21:57:15 -07:00
|
|
|
* Does debugging and deadlock detection in DEBUG builds.
|
2009-04-18 18:54:23 -07:00
|
|
|
**/
|
|
|
|
class NS_COM_GLUE BlockingResourceBase
|
|
|
|
{
|
2009-05-04 21:57:15 -07:00
|
|
|
public:
|
2014-06-26 18:35:39 -07:00
|
|
|
// Needs to be kept in sync with kResourceTypeNames.
|
|
|
|
enum BlockingResourceType { eMutex, eReentrantMonitor, eCondVar };
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* kResourceTypeName
|
|
|
|
* Human-readable version of BlockingResourceType enum.
|
|
|
|
*/
|
|
|
|
static const char* const kResourceTypeName[];
|
2009-05-04 21:57:15 -07:00
|
|
|
|
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2014-08-04 16:16:09 -07:00
|
|
|
static size_t
|
2014-08-12 11:44:04 -07:00
|
|
|
SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf);
|
2014-08-04 16:16:09 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
2014-08-12 11:42:28 -07: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-26 18:35:39 -07:00
|
|
|
*
|
2014-08-12 11:42:28 -07: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-26 18:35:39 -07:00
|
|
|
*/
|
2014-08-12 11:42:28 -07:00
|
|
|
bool Print(nsACString& aOut) const;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2014-06-26 18:35:39 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07: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-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* 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 11:43:58 -07:00
|
|
|
void CheckAcquire();
|
2014-06-26 18:35:39 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Acquire
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
**/
|
2014-08-08 11:43:58 -07:00
|
|
|
void Acquire(); //NS_NEEDS_RESOURCE(this)
|
2014-06-26 18:35:39 -07: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 11:43:26 -07:00
|
|
|
* GetAcquisitionState
|
|
|
|
* Return whether or not this resource was acquired.
|
2014-06-26 18:35:39 -07:00
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
2014-08-08 11:43:26 -07:00
|
|
|
bool GetAcquisitionState()
|
2014-06-26 18:35:39 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
return mAcquired;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-08 11:43:26 -07:00
|
|
|
* SetAcquisitionState
|
|
|
|
* Set whether or not this resource was acquired.
|
2014-06-26 18:35:39 -07:00
|
|
|
*
|
|
|
|
* *NOT* thread safe. Requires ownership of underlying resource.
|
|
|
|
*/
|
2014-08-08 11:43:26 -07:00
|
|
|
void SetAcquisitionState(bool aAcquisitionState)
|
2014-06-26 18:35:39 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
mAcquired = aAcquisitionState;
|
2014-06-26 18:35:39 -07: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-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
private:
|
|
|
|
/**
|
2014-08-12 11:42:28 -07: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-26 18:35:39 -07:00
|
|
|
*/
|
2014-08-12 11:42:28 -07:00
|
|
|
bool mAcquired;
|
2014-06-26 18:35:39 -07: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 11:44:04 -07:00
|
|
|
static PRStatus InitStatics();
|
2014-06-26 18:35:39 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown
|
|
|
|
* Free static members.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe.
|
|
|
|
*/
|
2014-08-12 11:44:04 -07:00
|
|
|
static void Shutdown();
|
2009-06-16 12:36:44 -07:00
|
|
|
|
|
|
|
# ifdef MOZILLA_INTERNAL_API
|
2014-06-26 18:35:39 -07:00
|
|
|
// so it can call BlockingResourceBase::Shutdown()
|
|
|
|
friend void LogTerm();
|
2009-06-16 12:36:44 -07:00
|
|
|
# endif // ifdef MOZILLA_INTERNAL_API
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#else // non-DEBUG implementation
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
BlockingResourceBase(const char* aName, BlockingResourceType aType) {}
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
~BlockingResourceBase() {}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
|
|
|
#endif // mozilla_BlockingResourceBase_h
|