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
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#include "mozilla/BlockingResourceBase.h"
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#ifdef DEBUG
|
2014-08-12 11:44:04 -07:00
|
|
|
#include "prthread.h"
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-08-13 12:37:40 -07:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
2014-08-13 13:06:07 -07:00
|
|
|
#include "CodeAddressService.h"
|
|
|
|
#include "nsHashKeys.h"
|
2014-08-13 12:37:40 -07:00
|
|
|
#include "nsStackWalk.h"
|
2014-08-13 13:06:07 -07:00
|
|
|
#include "nsTHashtable.h"
|
2014-08-13 12:37:40 -07:00
|
|
|
#endif
|
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
#include "mozilla/CondVar.h"
|
2014-08-12 11:44:04 -07:00
|
|
|
#include "mozilla/DeadlockDetector.h"
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2009-04-18 18:54:23 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-03-28 13:08:22 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#endif // ifdef DEBUG
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
namespace mozilla {
|
|
|
|
//
|
|
|
|
// BlockingResourceBase implementation
|
|
|
|
//
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
// static members
|
|
|
|
const char* const BlockingResourceBase::kResourceTypeName[] =
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
// needs to be kept in sync with BlockingResourceType
|
|
|
|
"Mutex", "ReentrantMonitor", "CondVar"
|
2009-04-18 18:54:23 -07:00
|
|
|
};
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
#ifdef DEBUG
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
PRCallOnceType BlockingResourceBase::sCallOnce;
|
2012-08-22 08:56:38 -07:00
|
|
|
unsigned BlockingResourceBase::sResourceAcqnChainFrontTPI = (unsigned)-1;
|
2009-06-16 12:36:44 -07:00
|
|
|
BlockingResourceBase::DDT* BlockingResourceBase::sDeadlockDetector;
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-08-12 11:43:27 -07:00
|
|
|
|
2014-08-13 12:37:40 -07:00
|
|
|
void
|
|
|
|
BlockingResourceBase::StackWalkCallback(void* aPc, void* aSp, void* aClosure)
|
|
|
|
{
|
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
AcquisitionState* state = (AcquisitionState*)aClosure;
|
|
|
|
state->AppendElement(aPc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BlockingResourceBase::GetStackTrace(AcquisitionState& aState)
|
|
|
|
{
|
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
2014-08-13 13:06:07 -07:00
|
|
|
// Skip this function and the calling function.
|
2014-08-13 12:37:40 -07:00
|
|
|
const uint32_t kSkipFrames = 2;
|
|
|
|
|
|
|
|
aState.Clear();
|
|
|
|
|
|
|
|
// NB: Ignore the return value, there's nothing useful we can do if this
|
|
|
|
// this fails.
|
|
|
|
NS_StackWalk(StackWalkCallback, kSkipFrames,
|
|
|
|
24, &aState, 0, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-12 11:43:27 -07:00
|
|
|
/**
|
|
|
|
* PrintCycle
|
|
|
|
* Append to |aOut| detailed information about the circular
|
|
|
|
* dependency in |aCycle|. Returns true if it *appears* that this
|
|
|
|
* cycle may represent an imminent deadlock, but this is merely a
|
|
|
|
* heuristic; the value returned may be a false positive or false
|
|
|
|
* negative.
|
|
|
|
*
|
|
|
|
* *NOT* thread safe. Calls |Print()|.
|
|
|
|
*
|
|
|
|
* 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|
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
PrintCycle(const BlockingResourceBase::DDT::ResourceAcquisitionArray* aCycle, nsACString& aOut)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCycle->Length() > 1, "need > 1 element for cycle!");
|
|
|
|
|
|
|
|
bool maybeImminent = true;
|
|
|
|
|
|
|
|
fputs("=== Cyclical dependency starts at\n", stderr);
|
|
|
|
aOut += "Cyclical dependency starts at\n";
|
|
|
|
|
|
|
|
const BlockingResourceBase::DDT::ResourceAcquisitionArray::elem_type res = aCycle->ElementAt(0);
|
|
|
|
maybeImminent &= res->Print(aOut);
|
|
|
|
|
|
|
|
BlockingResourceBase::DDT::ResourceAcquisitionArray::index_type i;
|
|
|
|
BlockingResourceBase::DDT::ResourceAcquisitionArray::size_type len = aCycle->Length();
|
|
|
|
const BlockingResourceBase::DDT::ResourceAcquisitionArray::elem_type* it = 1 + aCycle->Elements();
|
|
|
|
for (i = 1; i < len - 1; ++i, ++it) {
|
|
|
|
fputs("\n--- Next dependency:\n", stderr);
|
|
|
|
aOut += "\nNext dependency:\n";
|
|
|
|
|
|
|
|
maybeImminent &= (*it)->Print(aOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs("\n=== Cycle completed at\n", stderr);
|
|
|
|
aOut += "Cycle completed at\n";
|
|
|
|
(*it)->Print(aOut);
|
|
|
|
|
|
|
|
return maybeImminent;
|
|
|
|
}
|
|
|
|
|
2014-08-13 13:06:07 -07:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
class CodeAddressServiceWriter MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit CodeAddressServiceWriter(nsACString& aOut) : mOut(aOut) {}
|
|
|
|
|
|
|
|
void Write(const char* aFmt, ...) const
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aFmt);
|
|
|
|
|
|
|
|
const size_t kMaxLength = 4096;
|
|
|
|
char buffer[kMaxLength];
|
|
|
|
|
|
|
|
vsnprintf(buffer, kMaxLength, aFmt, ap);
|
|
|
|
mOut += buffer;
|
|
|
|
fprintf(stderr, "%s", buffer);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsACString& mOut;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CodeAddressServiceLock MOZ_FINAL
|
|
|
|
{
|
|
|
|
static void Unlock() { }
|
|
|
|
static void Lock() { }
|
|
|
|
static bool IsLocked() { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CodeAddressServiceStringAlloc MOZ_FINAL
|
|
|
|
{
|
|
|
|
static char* copy(const char* aString) { return ::strdup(aString); }
|
|
|
|
static void free(char* aString) { ::free(aString); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class CodeAddressServiceStringTable MOZ_FINAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CodeAddressServiceStringTable() : mSet(32) {}
|
|
|
|
|
|
|
|
const char* Intern(const char* aString)
|
|
|
|
{
|
|
|
|
nsCharPtrHashKey* e = mSet.PutEntry(aString);
|
|
|
|
return e->GetKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mSet.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef nsTHashtable<nsCharPtrHashKey> StringSet;
|
|
|
|
StringSet mSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef CodeAddressService<CodeAddressServiceStringTable,
|
|
|
|
CodeAddressServiceStringAlloc,
|
|
|
|
CodeAddressServiceWriter,
|
|
|
|
CodeAddressServiceLock> WalkTheStackCodeAddressService;
|
|
|
|
#endif
|
2014-08-12 11:43:27 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
bool
|
2014-08-12 11:42:28 -07:00
|
|
|
BlockingResourceBase::Print(nsACString& aOut) const
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
fprintf(stderr, "--- %s : %s",
|
|
|
|
kResourceTypeName[mType], mName);
|
|
|
|
aOut += BlockingResourceBase::kResourceTypeName[mType];
|
|
|
|
aOut += " : ";
|
|
|
|
aOut += mName;
|
|
|
|
|
2014-08-13 12:37:31 -07:00
|
|
|
bool acquired = IsAcquired();
|
|
|
|
|
|
|
|
if (acquired) {
|
2014-06-26 18:35:39 -07:00
|
|
|
fputs(" (currently acquired)\n", stderr);
|
|
|
|
aOut += " (currently acquired)\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" calling context\n", stderr);
|
2014-08-13 13:06:07 -07:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-08 11:43:19 -07:00
|
|
|
fputs(" [stack trace unavailable]\n", stderr);
|
2014-08-13 13:06:07 -07:00
|
|
|
#else
|
|
|
|
const AcquisitionState& state = acquired ? mAcquired : mFirstSeen;
|
|
|
|
|
|
|
|
WalkTheStackCodeAddressService addressService;
|
|
|
|
CodeAddressServiceWriter writer(aOut);
|
|
|
|
for (uint32_t i = 0; i < state.Length(); i++) {
|
|
|
|
addressService.WriteLocation(writer, state[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-06-26 18:35:39 -07:00
|
|
|
|
2014-08-13 12:37:31 -07:00
|
|
|
return acquired;
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
2009-05-02 21:13:57 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
BlockingResourceBase::BlockingResourceBase(
|
|
|
|
const char* aName,
|
|
|
|
BlockingResourceBase::BlockingResourceType aType)
|
2014-08-12 11:42:28 -07:00
|
|
|
: mName(aName)
|
|
|
|
, mType(aType)
|
2014-08-13 12:37:31 -07:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-12 11:42:28 -07:00
|
|
|
, mAcquired(false)
|
2014-08-13 12:37:31 -07:00
|
|
|
#else
|
|
|
|
, mAcquired()
|
|
|
|
#endif
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
NS_ABORT_IF_FALSE(mName, "Name must be nonnull");
|
2014-06-26 18:35:39 -07:00
|
|
|
// PR_CallOnce guaranatees that InitStatics is called in a
|
|
|
|
// thread-safe way
|
|
|
|
if (PR_SUCCESS != PR_CallOnce(&sCallOnce, InitStatics)) {
|
|
|
|
NS_RUNTIMEABORT("can't initialize blocking resource static members");
|
|
|
|
}
|
|
|
|
|
|
|
|
mChainPrev = 0;
|
2014-08-12 11:42:28 -07:00
|
|
|
sDeadlockDetector->Add(this);
|
2009-05-02 21:13:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
BlockingResourceBase::~BlockingResourceBase()
|
2009-05-02 21:13:57 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
// we don't check for really obviously bad things like freeing
|
|
|
|
// Mutexes while they're still locked. it is assumed that the
|
|
|
|
// base class, or its underlying primitive, will check for such
|
|
|
|
// stupid mistakes.
|
|
|
|
mChainPrev = 0; // racy only for stupidly buggy client code
|
2014-08-12 11:42:28 -07:00
|
|
|
sDeadlockDetector->Remove(this);
|
2009-05-02 21:13:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-12 11:44:04 -07:00
|
|
|
size_t
|
|
|
|
BlockingResourceBase::SizeOfDeadlockDetector(MallocSizeOf aMallocSizeOf)
|
|
|
|
{
|
|
|
|
return sDeadlockDetector ?
|
|
|
|
sDeadlockDetector->SizeOfIncludingThis(aMallocSizeOf) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRStatus
|
|
|
|
BlockingResourceBase::InitStatics()
|
|
|
|
{
|
|
|
|
PR_NewThreadPrivateIndex(&sResourceAcqnChainFrontTPI, 0);
|
|
|
|
sDeadlockDetector = new DDT();
|
|
|
|
if (!sDeadlockDetector) {
|
|
|
|
NS_RUNTIMEABORT("can't allocate deadlock detector");
|
|
|
|
}
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BlockingResourceBase::Shutdown()
|
|
|
|
{
|
|
|
|
delete sDeadlockDetector;
|
|
|
|
sDeadlockDetector = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
void
|
2014-08-08 11:43:58 -07:00
|
|
|
BlockingResourceBase::CheckAcquire()
|
2009-05-02 21:13:57 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
if (mType == eCondVar) {
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_NOTYETIMPLEMENTED(
|
|
|
|
"FIXME bug 456272: annots. to allow CheckAcquire()ing condvars");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
|
|
|
nsAutoPtr<DDT::ResourceAcquisitionArray> cycle(
|
|
|
|
sDeadlockDetector->CheckAcquisition(
|
2014-08-12 11:42:28 -07:00
|
|
|
chainFront ? chainFront : 0, this));
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!cycle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 12:37:40 -07:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
// Update the current stack before printing.
|
|
|
|
GetStackTrace(mAcquired);
|
|
|
|
#endif
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
fputs("###!!! ERROR: Potential deadlock detected:\n", stderr);
|
|
|
|
nsAutoCString out("Potential deadlock detected:\n");
|
|
|
|
bool maybeImminent = PrintCycle(cycle, out);
|
|
|
|
|
|
|
|
if (maybeImminent) {
|
|
|
|
fputs("\n###!!! Deadlock may happen NOW!\n\n", stderr);
|
|
|
|
out.AppendLiteral("\n###!!! Deadlock may happen NOW!\n\n");
|
|
|
|
} else {
|
|
|
|
fputs("\nDeadlock may happen for some other execution\n\n",
|
|
|
|
stderr);
|
|
|
|
out.AppendLiteral("\nDeadlock may happen for some other execution\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX can customize behavior on whether we /think/ deadlock is
|
|
|
|
// XXX about to happen. for example:
|
|
|
|
// XXX if (maybeImminent)
|
|
|
|
// NS_RUNTIMEABORT(out.get());
|
|
|
|
NS_ERROR(out.get());
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-08-08 11:43:58 -07:00
|
|
|
BlockingResourceBase::Acquire()
|
2009-05-02 21:13:57 -07:00
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
if (mType == eCondVar) {
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_NOTYETIMPLEMENTED(
|
|
|
|
"FIXME bug 456272: annots. to allow Acquire()ing condvars");
|
|
|
|
return;
|
|
|
|
}
|
2014-08-13 12:37:31 -07:00
|
|
|
NS_ASSERTION(!IsAcquired(),
|
2014-06-26 18:35:39 -07:00
|
|
|
"reacquiring already acquired resource");
|
|
|
|
|
|
|
|
ResourceChainAppend(ResourceChainFront());
|
2014-08-13 12:37:31 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-12 11:42:28 -07:00
|
|
|
mAcquired = true;
|
2014-08-13 12:37:31 -07:00
|
|
|
#else
|
2014-08-13 12:37:40 -07:00
|
|
|
// Take a stack snapshot.
|
|
|
|
GetStackTrace(mAcquired);
|
|
|
|
if (mFirstSeen.IsEmpty()) {
|
|
|
|
mFirstSeen = mAcquired;
|
|
|
|
}
|
2014-08-13 12:37:31 -07:00
|
|
|
#endif
|
2009-05-02 21:13:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
void
|
|
|
|
BlockingResourceBase::Release()
|
|
|
|
{
|
2014-08-12 11:42:28 -07:00
|
|
|
if (mType == eCondVar) {
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_NOTYETIMPLEMENTED(
|
|
|
|
"FIXME bug 456272: annots. to allow Release()ing condvars");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
2014-08-13 12:37:31 -07:00
|
|
|
NS_ASSERTION(chainFront && IsAcquired(),
|
2014-06-26 18:35:39 -07:00
|
|
|
"Release()ing something that hasn't been Acquire()ed");
|
|
|
|
|
|
|
|
if (chainFront == this) {
|
|
|
|
ResourceChainRemove();
|
|
|
|
} else {
|
|
|
|
// not an error, but makes code hard to reason about.
|
|
|
|
NS_WARNING("Resource acquired at calling context\n");
|
2014-08-08 11:43:19 -07:00
|
|
|
NS_WARNING(" [stack trace unavailable]\n");
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_WARNING("\nis being released in non-LIFO order; why?");
|
|
|
|
|
|
|
|
// remove this resource from wherever it lives in the chain
|
|
|
|
// we walk backwards in order of acquisition:
|
|
|
|
// (1) ...node<-prev<-curr...
|
|
|
|
// / /
|
|
|
|
// (2) ...prev<-curr...
|
|
|
|
BlockingResourceBase* curr = chainFront;
|
|
|
|
BlockingResourceBase* prev = nullptr;
|
|
|
|
while (curr && (prev = curr->mChainPrev) && (prev != this)) {
|
|
|
|
curr = prev;
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
if (prev == this) {
|
|
|
|
curr->mChainPrev = prev->mChainPrev;
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-08-13 12:37:31 -07:00
|
|
|
ClearAcquisitionState();
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
//
|
2013-05-24 10:10:47 -07:00
|
|
|
// Debug implementation of (OffTheBooks)Mutex
|
2009-04-18 18:54:23 -07:00
|
|
|
void
|
2013-05-24 10:10:47 -07:00
|
|
|
OffTheBooksMutex::Lock()
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-08-08 11:43:58 -07:00
|
|
|
CheckAcquire();
|
2014-06-26 18:35:39 -07:00
|
|
|
PR_Lock(mLock);
|
2014-08-08 11:43:58 -07:00
|
|
|
Acquire(); // protected by mLock
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-24 10:10:47 -07:00
|
|
|
OffTheBooksMutex::Unlock()
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
Release(); // protected by mLock
|
|
|
|
PRStatus status = PR_Unlock(mLock);
|
|
|
|
NS_ASSERTION(PR_SUCCESS == status, "bad Mutex::Unlock()");
|
2009-05-02 21:13:57 -07:00
|
|
|
}
|
2009-05-02 20:54:05 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2009-04-18 18:54:23 -07:00
|
|
|
//
|
2011-04-29 12:21:57 -07:00
|
|
|
// Debug implementation of ReentrantMonitor
|
2009-05-04 21:57:15 -07:00
|
|
|
void
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor::Enter()
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// the code below implements monitor reentrancy semantics
|
|
|
|
|
|
|
|
if (this == chainFront) {
|
|
|
|
// immediately re-entered the monitor: acceptable
|
|
|
|
PR_EnterMonitor(mReentrantMonitor);
|
|
|
|
++mEntryCount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is sort of a hack around not recording the thread that
|
|
|
|
// owns this monitor
|
|
|
|
if (chainFront) {
|
|
|
|
for (BlockingResourceBase* br = ResourceChainPrev(chainFront);
|
|
|
|
br;
|
|
|
|
br = ResourceChainPrev(br)) {
|
|
|
|
if (br == this) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Re-entering ReentrantMonitor after acquiring other resources.\n"
|
2014-08-08 11:43:19 -07:00
|
|
|
"At calling context\n"
|
|
|
|
" [stack trace unavailable]\n");
|
2014-06-26 18:35:39 -07:00
|
|
|
|
|
|
|
// show the caller why this is potentially bad
|
2014-08-08 11:43:58 -07:00
|
|
|
CheckAcquire();
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
PR_EnterMonitor(mReentrantMonitor);
|
2009-05-04 21:57:15 -07:00
|
|
|
++mEntryCount;
|
|
|
|
return;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2009-05-04 21:57:15 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-08-08 11:43:58 -07:00
|
|
|
CheckAcquire();
|
2014-06-26 18:35:39 -07:00
|
|
|
PR_EnterMonitor(mReentrantMonitor);
|
|
|
|
NS_ASSERTION(mEntryCount == 0, "ReentrantMonitor isn't free!");
|
2014-08-08 11:43:58 -07:00
|
|
|
Acquire(); // protected by mReentrantMonitor
|
2014-06-26 18:35:39 -07:00
|
|
|
mEntryCount = 1;
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor::Exit()
|
2009-04-18 18:54:23 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
if (--mEntryCount == 0) {
|
|
|
|
Release(); // protected by mReentrantMonitor
|
|
|
|
}
|
|
|
|
PRStatus status = PR_ExitMonitor(mReentrantMonitor);
|
|
|
|
NS_ASSERTION(PR_SUCCESS == status, "bad ReentrantMonitor::Exit()");
|
2009-05-04 21:57:15 -07:00
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
nsresult
|
2014-06-26 18:35:39 -07:00
|
|
|
ReentrantMonitor::Wait(PRIntervalTime aInterval)
|
2009-05-04 21:57:15 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
AssertCurrentThreadIn();
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// save monitor state and reset it to empty
|
|
|
|
int32_t savedEntryCount = mEntryCount;
|
2014-08-13 12:36:41 -07:00
|
|
|
AcquisitionState savedAcquisitionState = GetAcquisitionState();
|
2014-06-26 18:35:39 -07:00
|
|
|
BlockingResourceBase* savedChainPrev = mChainPrev;
|
|
|
|
mEntryCount = 0;
|
2014-08-13 12:37:05 -07:00
|
|
|
ClearAcquisitionState();
|
2014-06-26 18:35:39 -07:00
|
|
|
mChainPrev = 0;
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsresult rv;
|
2014-03-28 13:08:22 -07:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-06-26 18:35:39 -07:00
|
|
|
{
|
|
|
|
GeckoProfilerSleepRAII profiler_sleep;
|
2014-03-28 13:08:22 -07:00
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
// give up the monitor until we're back from Wait()
|
2014-06-26 18:35:39 -07:00
|
|
|
rv = PR_Wait(mReentrantMonitor, aInterval) == PR_SUCCESS ? NS_OK :
|
|
|
|
NS_ERROR_FAILURE;
|
2014-03-28 13:08:22 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2014-03-28 13:08:22 -07:00
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// restore saved state
|
|
|
|
mEntryCount = savedEntryCount;
|
2014-08-08 11:43:26 -07:00
|
|
|
SetAcquisitionState(savedAcquisitionState);
|
2014-06-26 18:35:39 -07:00
|
|
|
mChainPrev = savedChainPrev;
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return rv;
|
2009-04-18 18:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 21:57:15 -07:00
|
|
|
//
|
|
|
|
// Debug implementation of CondVar
|
|
|
|
nsresult
|
2014-06-26 18:35:39 -07:00
|
|
|
CondVar::Wait(PRIntervalTime aInterval)
|
2009-05-04 21:57:15 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
AssertCurrentThreadOwnsMutex();
|
2009-04-18 18:54:23 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// save mutex state and reset to empty
|
2014-08-13 12:36:41 -07:00
|
|
|
AcquisitionState savedAcquisitionState = mLock->GetAcquisitionState();
|
2014-06-26 18:35:39 -07:00
|
|
|
BlockingResourceBase* savedChainPrev = mLock->mChainPrev;
|
2014-08-13 12:37:05 -07:00
|
|
|
mLock->ClearAcquisitionState();
|
2014-06-26 18:35:39 -07:00
|
|
|
mLock->mChainPrev = 0;
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// give up mutex until we're back from Wait()
|
|
|
|
nsresult rv =
|
|
|
|
PR_WaitCondVar(mCvar, aInterval) == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// restore saved state
|
2014-08-08 11:43:26 -07:00
|
|
|
mLock->SetAcquisitionState(savedAcquisitionState);
|
2014-06-26 18:35:39 -07:00
|
|
|
mLock->mChainPrev = savedChainPrev;
|
2009-05-04 21:57:15 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
return rv;
|
2009-05-04 21:57:15 -07:00
|
|
|
}
|
2009-04-18 18:54:23 -07:00
|
|
|
|
|
|
|
#endif // ifdef DEBUG
|
2009-05-04 21:57:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|