This commit is contained in:
brendan%mozilla.org 1999-09-17 23:21:29 +00:00
Родитель 586a744c0d
Коммит f7ba68844b
2 изменённых файлов: 127 добавлений и 48 удалений

Просмотреть файл

@ -436,6 +436,10 @@ nsresult NS_COM NS_ShutdownXPCOM(nsIServiceManager* servMgr)
{
NS_RELEASE(nsServiceManager::mGlobalServiceManager);
NS_RELEASE(nsComponentManagerImpl::gComponentManager);
#ifdef DEBUG
extern void _FreeAutoLockStatics();
_FreeAutoLockStatics();
#endif
return NS_OK;
}

Просмотреть файл

@ -22,31 +22,97 @@
#include "plhash.h"
#include "prprf.h"
#include "prlock.h"
#include "prthread.h"
#include "nsDebug.h"
#include "nsVector.h"
static PRUintn LockStackTPI = (PRUintn)-1;
static PLHashTable* OrderTable = 0;
static PRLock* OrderTableLock = 0;
static const char* LockTypeNames[] = {"Lock", "Monitor", "CMonitor"};
struct nsNamedVector : public nsVector {
const char* mName;
nsNamedVector(const char* name, PRUint32 initialSize = 0)
nsNamedVector(const char* name = 0, PRUint32 initialSize = 0)
: nsVector(initialSize),
mName(name)
{
}
};
PR_STATIC_CALLBACK(PRIntn)
_purge_one(PLHashEntry* he, PRIntn cnt, void* arg)
{
nsNamedVector* vec = (nsNamedVector*) he->value;
if (he->key == arg) {
he->value = 0;
delete vec;
return HT_ENUMERATE_REMOVE;
}
for (PRUint32 i = 0, n = vec->GetSize(); i < n; i++) {
if (vec->Get(i) == arg) {
vec->Remove(i);
break;
}
}
return HT_ENUMERATE_NEXT;
}
PR_STATIC_CALLBACK(void)
OnMonitorRecycle(void* addr)
{
PR_Lock(OrderTableLock);
PL_HashTableEnumerateEntries(OrderTable, _purge_one, addr);
PR_Unlock(OrderTableLock);
}
PR_STATIC_CALLBACK(PLHashNumber)
_hash_pointer(const void *key)
_hash_pointer(const void* key)
{
return PLHashNumber(key) >> 2;
}
// Must be single-threaded here, early in primordial thread.
static void InitAutoLockStatics()
{
(void) PR_NewThreadPrivateIndex(&LockStackTPI, 0);
OrderTable = PL_NewHashTable(64, _hash_pointer,
PL_CompareValues, PL_CompareValues,
0, 0);
if (OrderTable && !(OrderTableLock = PR_NewLock())) {
PL_HashTableDestroy(OrderTable);
OrderTable = 0;
}
PR_CSetOnMonitorRecycle(OnMonitorRecycle);
}
PR_STATIC_CALLBACK(PRIntn)
_purge_all(PLHashEntry* he, PRIntn cnt, void* arg)
{
nsNamedVector* vec = (nsNamedVector*) he->value;
he->value = 0;
delete vec;
return HT_ENUMERATE_REMOVE;
}
void _FreeAutoLockStatics()
{
PLHashTable* table = OrderTable;
if (!table) return;
// Called at shutdown, so we don't need to lock.
PR_CSetOnMonitorRecycle(0);
PR_DestroyLock(OrderTableLock);
OrderTableLock = 0;
PL_HashTableEnumerateEntries(table, _purge_all, 0);
PL_HashTableDestroy(table);
OrderTable = 0;
}
static nsNamedVector* GetVector(PLHashTable* table, const void* key)
{
PLHashNumber hash = _hash_pointer(key);
@ -54,51 +120,75 @@ static nsNamedVector* GetVector(PLHashTable* table, const void* key)
PLHashEntry* he = *hep;
if (he)
return (nsNamedVector*) he->value;
nsNamedVector* vec = new nsNamedVector(0, 1);
nsNamedVector* vec = new nsNamedVector();
if (vec)
PL_HashTableRawAdd(table, hep, hash, key, vec);
return vec;
}
// We maintain an acyclic graph in order table, so recursion can't diverge
static PRBool Reachable(PLHashTable* table, const void* goal, const void* start)
{
PR_ASSERT(goal);
PR_ASSERT(start);
nsNamedVector* vec = GetVector(table, start);
for (PRUint32 i = 0, n = vec->GetSize(); i < n; i++) {
void* addr = vec->Get(i);
if (addr == goal || Reachable(table, goal, addr))
return PR_TRUE;
}
return PR_FALSE;
}
static PRBool WellOrdered(const void* addr1, const void* addr2,
nsNamedVector** vec1p, nsNamedVector** vec2p)
{
PRBool rv = PR_TRUE;
PLHashTable* table = OrderTable;
if (!table) return PR_TRUE;
if (!table) return rv;
PR_Lock(OrderTableLock);
PRUint32 i, n;
// Check whether we've already asserted (addr1 < addr2).
nsNamedVector* vec1 = GetVector(table, addr1);
if (!vec1) return PR_TRUE;
for (i = 0, n = vec1->GetSize(); i < n; i++)
if (vec1->Get(i) == addr2)
return PR_TRUE;
if (vec1) {
for (i = 0, n = vec1->GetSize(); i < n; i++)
if (vec1->Get(i) == addr2)
break;
// Now check for (addr2 < addr1) and return false if so.
nsNamedVector* vec2 = GetVector(table, addr2);
if (!vec2) return PR_TRUE;
for (i = 0, n = vec2->GetSize(); i < n; i++)
if (vec2->Get(i) == addr1) {
*vec1p = vec1;
*vec2p = vec2;
return PR_FALSE;
if (i == n) {
// Now check for (addr2 < addr1) and return false if so.
nsNamedVector* vec2 = GetVector(table, addr2);
if (vec2) {
for (i = 0, n = vec2->GetSize(); i < n; i++) {
void* addri = vec2->Get(i);
PR_ASSERT(addri);
if (addri == addr1 || Reachable(table, addr1, addri)) {
*vec1p = vec1;
*vec2p = vec2;
rv = PR_FALSE;
break;
}
}
if (rv) {
// Assert (addr1 < addr2) into the order table.
// XXX fix plvector/nsVector to use const void*
vec1->Add((void*) addr2);
}
}
}
}
// Assert (addr1 < addr2) into the order table.
// XXX fix plvector/nsVector to use const void*
vec1->Add((void*) addr2);
return PR_TRUE;
PR_Unlock(OrderTableLock);
return rv;
}
nsAutoLockBase::nsAutoLockBase(void* addr, nsAutoLockType type)
{
if (LockStackTPI == PRUintn(-1)) {
(void) PR_NewThreadPrivateIndex(&LockStackTPI, 0);
OrderTable = PL_NewHashTable(64, _hash_pointer,
PL_CompareValues, PL_CompareValues,
0, 0);
}
if (LockStackTPI == PRUintn(-1))
InitAutoLockStatics();
nsAutoLockBase* stackTop =
(nsAutoLockBase*) PR_GetThreadPrivate(LockStackTPI);
if (stackTop) {
@ -109,7 +199,7 @@ nsAutoLockBase::nsAutoLockBase(void* addr, nsAutoLockType type)
nsNamedVector* vec1;
nsNamedVector* vec2;
if (!WellOrdered(stackTop->mAddr, addr, &vec1, &vec2)) {
char buf[200];
char buf[128];
PR_snprintf(buf, sizeof buf,
"potential deadlock between %s%s@%p and %s%s@%p",
vec1->mName ? vec1->mName : "",
@ -140,37 +230,22 @@ PRMonitor* nsAutoMonitor::NewMonitor(const char* name)
PRMonitor* mon = PR_NewMonitor();
#ifdef DEBUG
if (mon && OrderTable) {
nsNamedVector* value = new nsNamedVector(name, 0);
if (value)
nsNamedVector* value = new nsNamedVector(name);
if (value) {
PR_Lock(OrderTableLock);
PL_HashTableAdd(OrderTable, mon, value);
PR_Unlock(OrderTableLock);
}
}
#endif
return mon;
}
#ifdef DEBUG
PR_STATIC_CALLBACK(PRIntn)
_purge_mon(PLHashEntry *he, PRIntn cnt, void* arg)
{
nsNamedVector* vec = (nsNamedVector*) he->value;
if (he->key == arg)
return HT_ENUMERATE_REMOVE;
for (PRUint32 i = 0, n = vec->GetSize(); i < n; i++) {
if (vec->Get(i) == arg) {
vec->Remove(i);
break;
}
}
return HT_ENUMERATE_NEXT;
}
#endif
void nsAutoMonitor::DestroyMonitor(PRMonitor* mon)
{
#ifdef DEBUG
if (OrderTable)
PL_HashTableEnumerateEntries(OrderTable, _purge_mon, mon);
OnMonitorRecycle(mon);
#endif
PR_DestroyMonitor(mon);
}