nsAutoLock.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsAutoLock.h"
#ifdef DEBUG
#include "plhash.h"
#include "prprf.h"
#include "prlock.h"
#include "prthread.h"
#include "nsDebug.h"
#include "nsVoidArray.h"
#ifdef NS_TRACE_MALLOC_XXX
# include <stdio.h>
# include "nsTraceMalloc.h"
#endif
static PLHashTable* OrderTable = 0;
static PRLock* OrderTableLock = 0;
struct nsNamedVector : public nsVoidArray {
const char* mName;
#ifdef NS_TRACE_MALLOC_XXX
// This array parallels our base nsVoidArray.
#endif
{
}
};
static void * PR_CALLBACK
{
return operator new(size);
}
static void PR_CALLBACK
{
operator delete(item);
}
static PLHashEntry * PR_CALLBACK
{
return new PLHashEntry;
}
/*
* Because monitors and locks may be associated with an nsAutoLockBase,
* without having had their associated nsNamedVector created explicitly in
* nsAutoMonitor::NewMonitor/DeleteMonitor, we need to provide a freeEntry
* PLHashTable hook, to avoid leaking nsNamedVectors which are replaced by
* nsAutoMonitor::NewMonitor.
*
* There is still a problem with the OrderTable containing orphaned
* nsNamedVector entries, for manually created locks wrapped by nsAutoLocks.
* (there should be no manually created monitors wrapped by nsAutoMonitors:
* you should use nsAutoMonitor::NewMonitor and nsAutoMonitor::DestroyMonitor
* instead of PR_NewMonitor and PR_DestroyMonitor). These lock vectors don't
* strictly leak, as they are killed on shutdown, but there are unnecessary
* named vectors in the hash table that outlive their associated locks.
*
* XXX so we should have nsLock, nsMonitor, etc. and strongly type their
* XXX nsAutoXXX counterparts to take only the non-auto types as inputs
*/
static void PR_CALLBACK
{
if (vec) {
delete vec;
}
if (flag == HT_FREE_ENTRY)
delete entry;
}
static const PLHashAllocOps _hash_alloc_ops = {
};
{
return HT_ENUMERATE_REMOVE;
return HT_ENUMERATE_NEXT;
}
PR_STATIC_CALLBACK(void)
OnMonitorRecycle(void* addr)
{
}
_hash_pointer(const void* key)
{
}
// Must be single-threaded here, early in primordial thread.
static void InitAutoLockStatics()
{
(void) PR_NewThreadPrivateIndex(&LockStackTPI, 0);
&_hash_alloc_ops, 0);
OrderTable = 0;
}
}
void _FreeAutoLockStatics()
{
if (!table) return;
// Called at shutdown, so we don't need to lock.
OrderTableLock = 0;
OrderTable = 0;
}
{
if (he)
if (vec)
return vec;
}
// We maintain an acyclic graph in OrderTable, so recursion can't diverge.
{
return PR_TRUE;
}
return PR_FALSE;
}
{
// Check whether we've already asserted (addr1 < addr2).
if (vec1) {
PRUint32 i, n;
break;
if (i == n) {
// Now check for (addr2 < addr1) and return false if so.
if (vec2) {
*index2p = i;
break;
}
}
if (rv) {
// Assert (addr1 < addr2) into the order table.
#ifdef NS_TRACE_MALLOC_XXX
#endif
}
}
}
}
return rv;
}
{
if (stackTop) {
// Ignore reentry: it's legal for monitors, and NSPR will assert
// if you reenter a PRLock.
} else if (!addr) {
// Ignore null addresses: the caller promises not to use the
// lock at all, and NSPR will assert if you enter it.
} else {
const void* node =
#ifdef NS_TRACE_MALLOC_XXX
#else
#endif
;
char buf[128];
"Potential deadlock between %s%s@%p and %s%s@%p",
addr);
#ifdef NS_TRACE_MALLOC_XXX
#endif
}
}
}
if (mAddr)
(void) PR_SetThreadPrivate(LockStackTPI, this);
}
{
if (mAddr)
}
void nsAutoLockBase::Show()
{
if (!mAddr)
return;
}
if (!prev)
PR_SetThreadPrivate(LockStackTPI, this);
else
}
void nsAutoLockBase::Hide()
{
if (!mAddr)
return;
while (curr != this) {
}
if (!prev)
else
}
#endif /* DEBUG */
{
#ifdef DEBUG
if (mon && OrderTable) {
if (value) {
}
}
#endif
return mon;
}
{
#ifdef DEBUG
if (OrderTable)
#endif
}
void nsAutoMonitor::Enter()
{
#ifdef DEBUG
if (!mAddr) {
NS_ERROR("It is not legal to enter a null monitor");
return;
}
(void) PR_SetThreadPrivate(LockStackTPI, this);
#endif
mLockCount += 1;
}
void nsAutoMonitor::Exit()
{
#ifdef DEBUG
if (!mAddr) {
NS_ERROR("It is not legal to exit a null monitor");
return;
}
#endif
mLockCount -= 1;
}
// XXX we don't worry about cached monitors being destroyed behind our back.
// XXX a cached monitor! potential resource pig in conjunction with necko...
void nsAutoCMonitor::Enter()
{
#ifdef DEBUG
(void) PR_SetThreadPrivate(LockStackTPI, this);
#endif
mLockCount += 1;
}
void nsAutoCMonitor::Exit()
{
#ifdef DEBUG
#endif
mLockCount -= 1;
}