Bug 587853 - Use PR_ATOMIC_{INCREMENT,DECREMENT} macros for threadsafe addref / release. r+a2.0=bsmedberg

This commit is contained in:
Justin Lebar 2010-08-17 10:25:41 -07:00
parent c359d7e5df
commit 92b6751bd9
7 changed files with 109 additions and 14 deletions

View File

@ -39,6 +39,7 @@
#include "jscntxt.h"
#include "nsDOMWorker.h"
#include "nsAtomicRefcnt.h"
#include "nsIDOMEvent.h"
#include "nsIEventTarget.h"
@ -950,7 +951,7 @@ NS_IMETHODIMP_(nsrefcnt)
nsDOMWorkerFeature::AddRef()
{
NS_ASSERTION(mRefCnt >= 0, "Illegal refcnt!");
return PR_AtomicIncrement((PRInt32*)&mRefCnt);
return NS_AtomicIncrementRefcnt(mRefCnt);
}
// Custom NS_IMPL_THREADSAFE_RELEASE. Checks the mFreeToDie flag before calling
@ -961,7 +962,7 @@ NS_IMETHODIMP_(nsrefcnt)
nsDOMWorkerFeature::Release()
{
NS_ASSERTION(mRefCnt, "Double release!");
nsrefcnt count = PR_AtomicDecrement((PRInt32*)&mRefCnt);
nsrefcnt count = NS_AtomicDecrementRefcnt(mRefCnt);
if (count == 0) {
if (mFreeToDie) {
mRefCnt = 1;

View File

@ -39,6 +39,7 @@
#define GFX_TYPES_H
#include "prtypes.h"
#include "nsAtomicRefcnt.h"
/**
* Currently needs to be 'double' for Cairo compatibility. Could
@ -94,13 +95,13 @@ enum gfxBreakPriority {
public: \
nsrefcnt AddRef(void) { \
NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt"); \
nsrefcnt count = PR_AtomicIncrement((PRInt32*)&mRefCnt); \
nsrefcnt count = NS_AtomicIncrementRefcnt(mRefCnt); \
NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
return count; \
} \
nsrefcnt Release(void) { \
NS_PRECONDITION(0 != mRefCnt, "dup release"); \
nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mRefCnt); \
nsrefcnt count = NS_AtomicDecrementRefcnt(mRefCnt); \
NS_LOG_RELEASE(this, count, #_class); \
if (count == 0) { \
mRefCnt = 1; /* stabilize */ \

View File

@ -42,6 +42,7 @@
/* Class that wraps JS objects to appear as XPCOM objects. */
#include "xpcprivate.h"
#include "nsAtomicRefcnt.h"
// NOTE: much of the fancy footwork is done in xpcstubs.cpp
@ -208,7 +209,7 @@ nsXPCWrappedJS::QueryInterface(REFNSIID aIID, void** aInstancePtr)
nsrefcnt
nsXPCWrappedJS::AddRef(void)
{
nsrefcnt cnt = (nsrefcnt) PR_AtomicIncrement((PRInt32*)&mRefCnt);
nsrefcnt cnt = NS_AtomicIncrementRefcnt(mRefCnt);
NS_LOG_ADDREF(this, cnt, "nsXPCWrappedJS", sizeof(*this));
if(2 == cnt && IsValid())
@ -232,7 +233,7 @@ nsXPCWrappedJS::Release(void)
do_decrement:
nsrefcnt cnt = (nsrefcnt) PR_AtomicDecrement((PRInt32*)&mRefCnt);
nsrefcnt cnt = NS_AtomicDecrementRefcnt(mRefCnt);
NS_LOG_RELEASE(this, cnt, "nsXPCWrappedJS");
if(0 == cnt)

View File

@ -118,6 +118,7 @@ SDK_HEADERS = \
nsError.h \
nsISupportsBase.h \
nscore.h \
nsAtomicRefcnt.h \
nsCycleCollector.h \
nsObjCExceptions.h \

View File

@ -0,0 +1,87 @@
/* ***** 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
* http://www.mozilla.org/MPL/
*
* 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 code.
*
* The Initial Developer of the Original Code is the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Justin Lebar <justin.lebar@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
#ifndef nsAtomicRefcnt_h__
#define nsAtomicRefcnt_h__
#include "nscore.h"
#include "pratom.h"
class nsAutoRefCnt;
// This header defines functions for modifying refcounts which wrap the
// PR_ATOMIC_* macros.
#if defined(XP_WIN)
#if PR_BYTES_PER_LONG == 4
typedef volatile long nsAtomicRefcnt;
#else
#error "Windows should have 4 bytes per long."
#endif
#else /* !defined(XP_WIN) */
typedef PRInt32 nsAtomicRefcnt;
#endif
inline nsrefcnt
NS_AtomicIncrementRefcnt(nsrefcnt &refcnt)
{
return (nsrefcnt) PR_ATOMIC_INCREMENT((nsAtomicRefcnt*)&refcnt);
}
inline nsrefcnt
NS_AtomicIncrementRefcnt(nsAutoRefCnt &refcnt)
{
// This cast is safe since nsAtomicRefCnt contains just one member, its refcount.
return (nsrefcnt) PR_ATOMIC_INCREMENT((nsAtomicRefcnt*)&refcnt);
}
inline nsrefcnt
NS_AtomicDecrementRefcnt(nsrefcnt &refcnt)
{
return (nsrefcnt) PR_ATOMIC_DECREMENT((nsAtomicRefcnt*)&refcnt);
}
inline nsrefcnt
NS_AtomicDecrementRefcnt(nsAutoRefCnt &refcnt)
{
return (nsrefcnt) PR_ATOMIC_DECREMENT((nsAtomicRefcnt*)&refcnt);
}
#endif

View File

@ -53,7 +53,7 @@
#if !defined(XPCOM_GLUE_AVOID_NSPR)
#include "prthread.h" /* needed for thread-safety checks */
#include "pratom.h" /* needed for PR_AtomicIncrement and PR_AtomicDecrement */
#include "nsAtomicRefcnt.h" /* for NS_Atomic{Increment,Decrement}Refcnt */
#endif
#include "nsDebug.h"
@ -1256,23 +1256,26 @@ NS_IMETHODIMP_(nsrefcnt) Class::Release(void) \
NS_IMETHODIMP_(nsrefcnt) _class::AddRef(void) \
{ \
NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt"); \
nsrefcnt count; \
count = PR_AtomicIncrement((PRInt32*)&mRefCnt); \
nsrefcnt count = NS_AtomicIncrementRefcnt(mRefCnt); \
NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
return count; \
return (nsrefcnt) count; \
}
/**
* Use this macro to implement the Release method for a given <i>_class</i>
* @param _class The name of the class implementing the method
*
* Note that we don't need to use an atomic operation to stabilize the refcnt.
* If the refcnt is released to 0, only the current thread has a reference to
* the object; we thus don't have to use an atomic set to inform other threads
* that we've changed the refcnt.
*/
#define NS_IMPL_THREADSAFE_RELEASE(_class) \
NS_IMETHODIMP_(nsrefcnt) _class::Release(void) \
{ \
nsrefcnt count; \
NS_PRECONDITION(0 != mRefCnt, "dup release"); \
count = PR_AtomicDecrement((PRInt32 *)&mRefCnt); \
nsrefcnt count = NS_AtomicDecrementRefcnt(mRefCnt); \
NS_LOG_RELEASE(this, count, #_class); \
if (0 == count) { \
mRefCnt = 1; /* stabilize */ \

View File

@ -40,6 +40,7 @@
/* Implementation of xptiInterfaceEntry and xptiInterfaceInfo. */
#include "xptiprivate.h"
#include "nsAtomicRefcnt.h"
/***************************************************************************/
// Debug Instrumentation...
@ -663,7 +664,7 @@ xptiInterfaceInfo::~xptiInterfaceInfo()
nsrefcnt
xptiInterfaceInfo::AddRef(void)
{
nsrefcnt cnt = (nsrefcnt) PR_AtomicIncrement((PRInt32*)&mRefCnt);
nsrefcnt cnt = NS_AtomicIncrementRefcnt(mRefCnt);
NS_LOG_ADDREF(this, cnt, "xptiInterfaceInfo", sizeof(*this));
return cnt;
}
@ -672,7 +673,7 @@ nsrefcnt
xptiInterfaceInfo::Release(void)
{
xptiInterfaceEntry* entry = mEntry;
nsrefcnt cnt = (nsrefcnt) PR_AtomicDecrement((PRInt32*)&mRefCnt);
nsrefcnt cnt = NS_AtomicDecrementRefcnt(mRefCnt);
NS_LOG_RELEASE(this, cnt, "xptiInterfaceInfo");
if(!cnt)
{