2014-06-30 19:39:45 +04: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 15:12:37 +04: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 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
#include "mozilla/BlockingResourceBase.h"
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
#ifdef DEBUG
|
2014-08-12 22:44:04 +04:00
|
|
|
#include "prthread.h"
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2014-08-13 23:37:40 +04:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
2014-08-14 00:06:07 +04:00
|
|
|
#include "CodeAddressService.h"
|
|
|
|
#include "nsHashKeys.h"
|
2015-06-10 23:32:45 +03:00
|
|
|
#include "mozilla/StackWalk.h"
|
2014-08-14 00:06:07 +04:00
|
|
|
#include "nsTHashtable.h"
|
2014-08-13 23:37:40 +04:00
|
|
|
#endif
|
|
|
|
|
2009-04-19 05:54:23 +04:00
|
|
|
#include "mozilla/CondVar.h"
|
2014-08-12 22:44:04 +04:00
|
|
|
#include "mozilla/DeadlockDetector.h"
|
2017-03-14 21:05:51 +03:00
|
|
|
#include "mozilla/RecursiveMutex.h"
|
2011-04-29 23:21:57 +04:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2009-04-19 05:54:23 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-05-23 04:26:26 +03:00
|
|
|
#include "mozilla/RWLock.h"
|
2014-03-29 00:08:22 +04:00
|
|
|
|
2016-02-27 05:10:56 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
2014-03-29 00:08:22 +04:00
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
#endif // ifdef DEBUG
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
namespace mozilla {
|
|
|
|
//
|
|
|
|
// BlockingResourceBase implementation
|
|
|
|
//
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
// static members
|
2014-08-25 23:17:15 +04:00
|
|
|
const char* const BlockingResourceBase::kResourceTypeName[] = {
|
2014-06-27 05:35:39 +04:00
|
|
|
// needs to be kept in sync with BlockingResourceType
|
2017-03-14 21:05:51 +03:00
|
|
|
"Mutex", "ReentrantMonitor", "CondVar", "RecursiveMutex"
|
2009-04-19 05:54:23 +04:00
|
|
|
};
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
#ifdef DEBUG
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
PRCallOnceType BlockingResourceBase::sCallOnce;
|
2012-08-22 19:56:38 +04:00
|
|
|
unsigned BlockingResourceBase::sResourceAcqnChainFrontTPI = (unsigned)-1;
|
2009-06-16 23:36:44 +04:00
|
|
|
BlockingResourceBase::DDT* BlockingResourceBase::sDeadlockDetector;
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2014-08-12 22:43:27 +04:00
|
|
|
|
2014-08-13 23:37:40 +04:00
|
|
|
void
|
2014-09-11 08:47:01 +04:00
|
|
|
BlockingResourceBase::StackWalkCallback(uint32_t aFrameNumber, void* aPc,
|
|
|
|
void* aSp, void* aClosure)
|
2014-08-13 23:37:40 +04:00
|
|
|
{
|
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
AcquisitionState* state = (AcquisitionState*)aClosure;
|
|
|
|
state->AppendElement(aPc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BlockingResourceBase::GetStackTrace(AcquisitionState& aState)
|
|
|
|
{
|
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
2014-08-14 00:06:07 +04:00
|
|
|
// Skip this function and the calling function.
|
2014-08-13 23:37:40 +04:00
|
|
|
const uint32_t kSkipFrames = 2;
|
|
|
|
|
|
|
|
aState.Clear();
|
|
|
|
|
|
|
|
// NB: Ignore the return value, there's nothing useful we can do if this
|
|
|
|
// this fails.
|
2017-07-27 05:46:47 +03:00
|
|
|
MozStackWalk(StackWalkCallback, kSkipFrames, 24, &aState);
|
2014-08-13 23:37:40 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:43:27 +04: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
|
2014-08-25 23:17:15 +04:00
|
|
|
PrintCycle(const BlockingResourceBase::DDT::ResourceAcquisitionArray* aCycle,
|
|
|
|
nsACString& aOut)
|
2014-08-12 22:43:27 +04:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
const BlockingResourceBase::DDT::ResourceAcquisitionArray::elem_type res =
|
|
|
|
aCycle->ElementAt(0);
|
2014-08-12 22:43:27 +04:00
|
|
|
maybeImminent &= res->Print(aOut);
|
|
|
|
|
|
|
|
BlockingResourceBase::DDT::ResourceAcquisitionArray::index_type i;
|
2014-08-25 23:17:15 +04:00
|
|
|
BlockingResourceBase::DDT::ResourceAcquisitionArray::size_type len =
|
|
|
|
aCycle->Length();
|
|
|
|
const BlockingResourceBase::DDT::ResourceAcquisitionArray::elem_type* it =
|
|
|
|
1 + aCycle->Elements();
|
2014-08-12 22:43:27 +04:00
|
|
|
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-14 00:06:07 +04:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
2015-03-21 19:28:04 +03:00
|
|
|
struct CodeAddressServiceLock final
|
2014-08-14 00:06:07 +04:00
|
|
|
{
|
|
|
|
static void Unlock() { }
|
|
|
|
static void Lock() { }
|
|
|
|
static bool IsLocked() { return true; }
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
struct CodeAddressServiceStringAlloc final
|
2014-08-14 00:06:07 +04:00
|
|
|
{
|
|
|
|
static char* copy(const char* aString) { return ::strdup(aString); }
|
|
|
|
static void free(char* aString) { ::free(aString); }
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CodeAddressServiceStringTable final
|
2014-08-14 00:06:07 +04:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
CodeAddressServiceLock> WalkTheStackCodeAddressService;
|
|
|
|
#endif
|
2014-08-12 22:43:27 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
bool
|
2014-08-12 22:42:28 +04:00
|
|
|
BlockingResourceBase::Print(nsACString& aOut) const
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
fprintf(stderr, "--- %s : %s",
|
|
|
|
kResourceTypeName[mType], mName);
|
|
|
|
aOut += BlockingResourceBase::kResourceTypeName[mType];
|
|
|
|
aOut += " : ";
|
|
|
|
aOut += mName;
|
|
|
|
|
2014-08-13 23:37:31 +04:00
|
|
|
bool acquired = IsAcquired();
|
|
|
|
|
|
|
|
if (acquired) {
|
2014-06-27 05:35:39 +04:00
|
|
|
fputs(" (currently acquired)\n", stderr);
|
|
|
|
aOut += " (currently acquired)\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" calling context\n", stderr);
|
2014-08-14 00:06:07 +04:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-08 22:43:19 +04:00
|
|
|
fputs(" [stack trace unavailable]\n", stderr);
|
2014-08-14 00:06:07 +04:00
|
|
|
#else
|
|
|
|
const AcquisitionState& state = acquired ? mAcquired : mFirstSeen;
|
|
|
|
|
|
|
|
WalkTheStackCodeAddressService addressService;
|
2014-09-02 07:03:06 +04:00
|
|
|
|
2014-08-14 00:06:07 +04:00
|
|
|
for (uint32_t i = 0; i < state.Length(); i++) {
|
2014-09-02 09:56:05 +04:00
|
|
|
const size_t kMaxLength = 1024;
|
2014-09-02 07:03:06 +04:00
|
|
|
char buffer[kMaxLength];
|
2014-11-27 08:52:56 +03:00
|
|
|
addressService.GetLocation(i + 1, state[i], buffer, kMaxLength);
|
2014-09-02 07:03:06 +04:00
|
|
|
const char* fmt = " %s\n";
|
2014-11-27 08:52:56 +03:00
|
|
|
aOut.AppendLiteral(" ");
|
|
|
|
aOut.Append(buffer);
|
|
|
|
aOut.AppendLiteral("\n");
|
2014-09-02 07:03:06 +04:00
|
|
|
fprintf(stderr, fmt, buffer);
|
2014-08-14 00:06:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2014-08-13 23:37:31 +04:00
|
|
|
return acquired;
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
2009-05-03 08:13:57 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
BlockingResourceBase::BlockingResourceBase(
|
|
|
|
const char* aName,
|
|
|
|
BlockingResourceBase::BlockingResourceType aType)
|
2014-08-12 22:42:28 +04:00
|
|
|
: mName(aName)
|
|
|
|
, mType(aType)
|
2014-08-13 23:37:31 +04:00
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-12 22:42:28 +04:00
|
|
|
, mAcquired(false)
|
2014-08-13 23:37:31 +04:00
|
|
|
#else
|
|
|
|
, mAcquired()
|
|
|
|
#endif
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mName, "Name must be nonnull");
|
2014-06-27 05:35:39 +04:00
|
|
|
// PR_CallOnce guaranatees that InitStatics is called in a
|
|
|
|
// thread-safe way
|
|
|
|
if (PR_SUCCESS != PR_CallOnce(&sCallOnce, InitStatics)) {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("can't initialize blocking resource static members");
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mChainPrev = 0;
|
2014-08-12 22:42:28 +04:00
|
|
|
sDeadlockDetector->Add(this);
|
2009-05-03 08:13:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
BlockingResourceBase::~BlockingResourceBase()
|
2009-05-03 08:13:57 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04: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-10-21 19:16:31 +04:00
|
|
|
if (sDeadlockDetector) {
|
|
|
|
sDeadlockDetector->Remove(this);
|
|
|
|
}
|
2009-05-03 08:13:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-12 22:44:04 +04: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) {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("can't allocate deadlock detector");
|
2014-08-12 22:44:04 +04:00
|
|
|
}
|
|
|
|
return PR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BlockingResourceBase::Shutdown()
|
|
|
|
{
|
|
|
|
delete sDeadlockDetector;
|
|
|
|
sDeadlockDetector = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
void
|
2014-08-08 22:43:58 +04:00
|
|
|
BlockingResourceBase::CheckAcquire()
|
2009-05-03 08:13:57 +04:00
|
|
|
{
|
2014-08-12 22:42:28 +04:00
|
|
|
if (mType == eCondVar) {
|
2014-06-27 05:35:39 +04: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 22:42:28 +04:00
|
|
|
chainFront ? chainFront : 0, this));
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!cycle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:37:40 +04:00
|
|
|
#ifndef MOZ_CALLSTACK_DISABLED
|
|
|
|
// Update the current stack before printing.
|
|
|
|
GetStackTrace(mAcquired);
|
|
|
|
#endif
|
|
|
|
|
2014-06-27 05:35:39 +04: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");
|
|
|
|
}
|
|
|
|
|
2017-06-10 02:09:17 +03:00
|
|
|
// Only error out if we think a deadlock is imminent.
|
|
|
|
if (maybeImminent) {
|
|
|
|
NS_ERROR(out.get());
|
|
|
|
} else {
|
|
|
|
NS_WARNING(out.get());
|
|
|
|
}
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-08-08 22:43:58 +04:00
|
|
|
BlockingResourceBase::Acquire()
|
2009-05-03 08:13:57 +04:00
|
|
|
{
|
2014-08-12 22:42:28 +04:00
|
|
|
if (mType == eCondVar) {
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_NOTYETIMPLEMENTED(
|
|
|
|
"FIXME bug 456272: annots. to allow Acquire()ing condvars");
|
|
|
|
return;
|
|
|
|
}
|
2014-08-13 23:37:31 +04:00
|
|
|
NS_ASSERTION(!IsAcquired(),
|
2014-06-27 05:35:39 +04:00
|
|
|
"reacquiring already acquired resource");
|
|
|
|
|
|
|
|
ResourceChainAppend(ResourceChainFront());
|
2014-08-13 23:37:31 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_CALLSTACK_DISABLED
|
2014-08-12 22:42:28 +04:00
|
|
|
mAcquired = true;
|
2014-08-13 23:37:31 +04:00
|
|
|
#else
|
2014-08-13 23:37:40 +04:00
|
|
|
// Take a stack snapshot.
|
|
|
|
GetStackTrace(mAcquired);
|
|
|
|
if (mFirstSeen.IsEmpty()) {
|
|
|
|
mFirstSeen = mAcquired;
|
|
|
|
}
|
2014-08-13 23:37:31 +04:00
|
|
|
#endif
|
2009-05-03 08:13:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
void
|
|
|
|
BlockingResourceBase::Release()
|
|
|
|
{
|
2014-08-12 22:42:28 +04:00
|
|
|
if (mType == eCondVar) {
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_NOTYETIMPLEMENTED(
|
|
|
|
"FIXME bug 456272: annots. to allow Release()ing condvars");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
2014-08-13 23:37:31 +04:00
|
|
|
NS_ASSERTION(chainFront && IsAcquired(),
|
2014-06-27 05:35:39 +04: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.
|
2016-05-24 00:59:30 +03:00
|
|
|
NS_WARNING("Resource acquired is being released in non-LIFO order; why?\n");
|
|
|
|
nsCString tmp;
|
|
|
|
Print(tmp);
|
2014-06-27 05:35:39 +04:00
|
|
|
|
|
|
|
// 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-19 05:54:23 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
if (prev == this) {
|
|
|
|
curr->mChainPrev = prev->mChainPrev;
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-08-13 23:37:31 +04:00
|
|
|
ClearAcquisitionState();
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2009-04-19 05:54:23 +04:00
|
|
|
//
|
2013-05-24 21:10:47 +04:00
|
|
|
// Debug implementation of (OffTheBooks)Mutex
|
2009-04-19 05:54:23 +04:00
|
|
|
void
|
2013-05-24 21:10:47 +04:00
|
|
|
OffTheBooksMutex::Lock()
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2014-08-08 22:43:58 +04:00
|
|
|
CheckAcquire();
|
2017-03-21 18:20:36 +03:00
|
|
|
this->lock();
|
|
|
|
mOwningThread = PR_GetCurrentThread();
|
|
|
|
Acquire();
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-24 21:10:47 +04:00
|
|
|
OffTheBooksMutex::Unlock()
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2017-03-21 18:20:36 +03:00
|
|
|
Release();
|
|
|
|
mOwningThread = nullptr;
|
|
|
|
this->unlock();
|
2009-05-03 08:13:57 +04:00
|
|
|
}
|
2009-05-03 07:54:05 +04:00
|
|
|
|
2017-03-21 18:20:36 +03:00
|
|
|
void
|
|
|
|
OffTheBooksMutex::AssertCurrentThreadOwns() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsAcquired() && mOwningThread == PR_GetCurrentThread());
|
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2017-05-23 04:26:26 +03:00
|
|
|
//
|
|
|
|
// Debug implementation of RWLock
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
RWLock::ReadLock()
|
|
|
|
{
|
|
|
|
// All we want to ensure here is that we're not attempting to acquire the
|
|
|
|
// read lock while this thread is holding the write lock.
|
|
|
|
CheckAcquire();
|
|
|
|
this->ReadLockInternal();
|
|
|
|
MOZ_ASSERT(mOwningThread == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RWLock::ReadUnlock()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningThread == nullptr);
|
|
|
|
this->ReadUnlockInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RWLock::WriteLock()
|
|
|
|
{
|
|
|
|
CheckAcquire();
|
|
|
|
this->WriteLockInternal();
|
|
|
|
mOwningThread = PR_GetCurrentThread();
|
|
|
|
Acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RWLock::WriteUnlock()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
mOwningThread = nullptr;
|
|
|
|
this->WriteUnlockInternal();
|
|
|
|
}
|
|
|
|
|
2009-04-19 05:54:23 +04:00
|
|
|
//
|
2011-04-29 23:21:57 +04:00
|
|
|
// Debug implementation of ReentrantMonitor
|
2009-05-05 08:57:15 +04:00
|
|
|
void
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitor::Enter()
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04: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(
|
2016-05-24 00:59:30 +03:00
|
|
|
"Re-entering ReentrantMonitor after acquiring other resources.");
|
2014-06-27 05:35:39 +04:00
|
|
|
|
|
|
|
// show the caller why this is potentially bad
|
2014-08-08 22:43:58 +04:00
|
|
|
CheckAcquire();
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
PR_EnterMonitor(mReentrantMonitor);
|
2009-05-05 08:57:15 +04:00
|
|
|
++mEntryCount;
|
|
|
|
return;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-08-08 22:43:58 +04:00
|
|
|
CheckAcquire();
|
2014-06-27 05:35:39 +04:00
|
|
|
PR_EnterMonitor(mReentrantMonitor);
|
|
|
|
NS_ASSERTION(mEntryCount == 0, "ReentrantMonitor isn't free!");
|
2014-08-08 22:43:58 +04:00
|
|
|
Acquire(); // protected by mReentrantMonitor
|
2014-06-27 05:35:39 +04:00
|
|
|
mEntryCount = 1;
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitor::Exit()
|
2009-04-19 05:54:23 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (--mEntryCount == 0) {
|
|
|
|
Release(); // protected by mReentrantMonitor
|
|
|
|
}
|
|
|
|
PRStatus status = PR_ExitMonitor(mReentrantMonitor);
|
|
|
|
NS_ASSERTION(PR_SUCCESS == status, "bad ReentrantMonitor::Exit()");
|
2009-05-05 08:57:15 +04:00
|
|
|
}
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
ReentrantMonitor::Wait(PRIntervalTime aInterval)
|
2009-05-05 08:57:15 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
AssertCurrentThreadIn();
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// save monitor state and reset it to empty
|
|
|
|
int32_t savedEntryCount = mEntryCount;
|
2014-08-13 23:36:41 +04:00
|
|
|
AcquisitionState savedAcquisitionState = GetAcquisitionState();
|
2014-06-27 05:35:39 +04:00
|
|
|
BlockingResourceBase* savedChainPrev = mChainPrev;
|
|
|
|
mEntryCount = 0;
|
2014-08-13 23:37:05 +04:00
|
|
|
ClearAcquisitionState();
|
2014-06-27 05:35:39 +04:00
|
|
|
mChainPrev = 0;
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult rv;
|
2016-02-27 05:10:56 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2017-06-07 05:33:19 +03:00
|
|
|
AutoProfilerThreadSleep sleep;
|
2014-03-29 00:08:22 +04:00
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
// give up the monitor until we're back from Wait()
|
2014-06-27 05:35:39 +04:00
|
|
|
rv = PR_Wait(mReentrantMonitor, aInterval) == PR_SUCCESS ? NS_OK :
|
|
|
|
NS_ERROR_FAILURE;
|
2014-03-29 00:08:22 +04:00
|
|
|
|
2016-02-27 05:10:56 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2014-03-29 00:08:22 +04:00
|
|
|
#endif //MOZILLA_INTERNAL_API
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// restore saved state
|
|
|
|
mEntryCount = savedEntryCount;
|
2014-08-08 22:43:26 +04:00
|
|
|
SetAcquisitionState(savedAcquisitionState);
|
2014-06-27 05:35:39 +04:00
|
|
|
mChainPrev = savedChainPrev;
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
return rv;
|
2009-04-19 05:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-14 21:05:51 +03:00
|
|
|
//
|
|
|
|
// Debug implementation of RecursiveMutex
|
|
|
|
void
|
|
|
|
RecursiveMutex::Lock()
|
|
|
|
{
|
|
|
|
BlockingResourceBase* chainFront = ResourceChainFront();
|
|
|
|
|
|
|
|
// the code below implements mutex reentrancy semantics
|
|
|
|
|
|
|
|
if (this == chainFront) {
|
|
|
|
// immediately re-entered the mutex: acceptable
|
|
|
|
LockInternal();
|
|
|
|
++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 RecursiveMutex after acquiring other resources.");
|
|
|
|
|
|
|
|
// show the caller why this is potentially bad
|
|
|
|
CheckAcquire();
|
|
|
|
|
|
|
|
LockInternal();
|
|
|
|
++mEntryCount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckAcquire();
|
|
|
|
LockInternal();
|
|
|
|
NS_ASSERTION(mEntryCount == 0, "RecursiveMutex isn't free!");
|
|
|
|
Acquire(); // protected by us
|
|
|
|
mOwningThread = PR_GetCurrentThread();
|
|
|
|
mEntryCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RecursiveMutex::Unlock()
|
|
|
|
{
|
|
|
|
if (--mEntryCount == 0) {
|
|
|
|
Release(); // protected by us
|
|
|
|
mOwningThread = nullptr;
|
|
|
|
}
|
|
|
|
UnlockInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RecursiveMutex::AssertCurrentThreadIn()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsAcquired() && mOwningThread == PR_GetCurrentThread());
|
|
|
|
}
|
|
|
|
|
2009-05-05 08:57:15 +04:00
|
|
|
//
|
|
|
|
// Debug implementation of CondVar
|
|
|
|
nsresult
|
2014-06-27 05:35:39 +04:00
|
|
|
CondVar::Wait(PRIntervalTime aInterval)
|
2009-05-05 08:57:15 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
AssertCurrentThreadOwnsMutex();
|
2009-04-19 05:54:23 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// save mutex state and reset to empty
|
2014-08-13 23:36:41 +04:00
|
|
|
AcquisitionState savedAcquisitionState = mLock->GetAcquisitionState();
|
2014-06-27 05:35:39 +04:00
|
|
|
BlockingResourceBase* savedChainPrev = mLock->mChainPrev;
|
2017-03-21 18:20:36 +03:00
|
|
|
PRThread* savedOwningThread = mLock->mOwningThread;
|
2014-08-13 23:37:05 +04:00
|
|
|
mLock->ClearAcquisitionState();
|
2014-06-27 05:35:39 +04:00
|
|
|
mLock->mChainPrev = 0;
|
2017-03-21 18:20:36 +03:00
|
|
|
mLock->mOwningThread = nullptr;
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// give up mutex until we're back from Wait()
|
2017-03-21 18:20:36 +03:00
|
|
|
if (aInterval == PR_INTERVAL_NO_TIMEOUT) {
|
|
|
|
mImpl.wait(*mLock);
|
|
|
|
} else {
|
|
|
|
mImpl.wait_for(*mLock, TimeDuration::FromMilliseconds(double(aInterval)));
|
|
|
|
}
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// restore saved state
|
2014-08-08 22:43:26 +04:00
|
|
|
mLock->SetAcquisitionState(savedAcquisitionState);
|
2014-06-27 05:35:39 +04:00
|
|
|
mLock->mChainPrev = savedChainPrev;
|
2017-03-21 18:20:36 +03:00
|
|
|
mLock->mOwningThread = savedOwningThread;
|
2009-05-05 08:57:15 +04:00
|
|
|
|
2017-03-21 18:20:36 +03:00
|
|
|
return NS_OK;
|
2009-05-05 08:57:15 +04:00
|
|
|
}
|
2009-04-19 05:54:23 +04:00
|
|
|
|
|
|
|
#endif // ifdef DEBUG
|
2009-05-05 08:57:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|