Bug 884281 - use mozilla::Atomic in xpcom/; r=bsmedberg,jlebar

This commit is contained in:
Nathan Froyd 2013-08-22 11:14:42 -04:00
parent a81fcc97ef
commit dccf049cdc
12 changed files with 60 additions and 50 deletions

View File

@ -7,7 +7,6 @@
#include "mozilla/AvailableMemoryTracker.h" #include "mozilla/AvailableMemoryTracker.h"
#include "prinrval.h" #include "prinrval.h"
#include "pratom.h"
#include "prenv.h" #include "prenv.h"
#include "nsIMemoryReporter.h" #include "nsIMemoryReporter.h"
@ -19,6 +18,7 @@
#include "nsPrintfCString.h" #include "nsPrintfCString.h"
#include "nsThread.h" #include "nsThread.h"
#include "mozilla/Atomics.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
#include "mozilla/Services.h" #include "mozilla/Services.h"
@ -128,9 +128,9 @@ uint32_t sLowCommitSpaceThreshold = 0;
uint32_t sLowPhysicalMemoryThreshold = 0; uint32_t sLowPhysicalMemoryThreshold = 0;
uint32_t sLowMemoryNotificationIntervalMS = 0; uint32_t sLowMemoryNotificationIntervalMS = 0;
uint32_t sNumLowVirtualMemEvents = 0; Atomic<uint32_t> sNumLowVirtualMemEvents;
uint32_t sNumLowCommitSpaceEvents = 0; Atomic<uint32_t> sNumLowCommitSpaceEvents;
uint32_t sNumLowPhysicalMemEvents = 0; Atomic<uint32_t> sNumLowPhysicalMemEvents;
WindowsDllInterceptor sKernel32Intercept; WindowsDllInterceptor sKernel32Intercept;
WindowsDllInterceptor sGdi32Intercept; WindowsDllInterceptor sGdi32Intercept;
@ -212,19 +212,19 @@ void CheckMemAvailable()
// notification. We'll probably crash if we run out of virtual memory, // notification. We'll probably crash if we run out of virtual memory,
// so don't worry about firing this notification too often. // so don't worry about firing this notification too often.
LOG("Detected low virtual memory."); LOG("Detected low virtual memory.");
PR_ATOMIC_INCREMENT(&sNumLowVirtualMemEvents); ++sNumLowVirtualMemEvents;
NS_DispatchEventualMemoryPressure(MemPressure_New); NS_DispatchEventualMemoryPressure(MemPressure_New);
} }
else if (stat.ullAvailPageFile < sLowCommitSpaceThreshold * 1024 * 1024) { else if (stat.ullAvailPageFile < sLowCommitSpaceThreshold * 1024 * 1024) {
LOG("Detected low available page file space."); LOG("Detected low available page file space.");
if (MaybeScheduleMemoryPressureEvent()) { if (MaybeScheduleMemoryPressureEvent()) {
PR_ATOMIC_INCREMENT(&sNumLowCommitSpaceEvents); ++sNumLowCommitSpaceEvents;
} }
} }
else if (stat.ullAvailPhys < sLowPhysicalMemoryThreshold * 1024 * 1024) { else if (stat.ullAvailPhys < sLowPhysicalMemoryThreshold * 1024 * 1024) {
LOG("Detected low physical memory."); LOG("Detected low physical memory.");
if (MaybeScheduleMemoryPressureEvent()) { if (MaybeScheduleMemoryPressureEvent()) {
PR_ATOMIC_INCREMENT(&sNumLowPhysicalMemEvents); ++sNumLowPhysicalMemEvents;
} }
} }
} }

View File

@ -6,6 +6,8 @@
// Chromium headers must come before Mozilla headers. // Chromium headers must come before Mozilla headers.
#include "base/process_util.h" #include "base/process_util.h"
#include "mozilla/Atomics.h"
#include "nsDebugImpl.h" #include "nsDebugImpl.h"
#include "nsDebug.h" #include "nsDebug.h"
#ifdef MOZ_CRASHREPORTER #ifdef MOZ_CRASHREPORTER
@ -20,7 +22,6 @@
#include "prerror.h" #include "prerror.h"
#include "prerr.h" #include "prerr.h"
#include "prenv.h" #include "prenv.h"
#include "pratom.h"
#ifdef ANDROID #ifdef ANDROID
#include <android/log.h> #include <android/log.h>
@ -83,7 +84,7 @@ using namespace mozilla;
static bool sIsMultiprocess = false; static bool sIsMultiprocess = false;
static const char *sMultiprocessDescription = NULL; static const char *sMultiprocessDescription = NULL;
static int32_t gAssertionCount = 0; static Atomic<int32_t> gAssertionCount;
NS_IMPL_QUERY_INTERFACE2(nsDebugImpl, nsIDebug, nsIDebug2) NS_IMPL_QUERY_INTERFACE2(nsDebugImpl, nsIDebug, nsIDebug2)
@ -390,7 +391,7 @@ NS_DebugBreak(uint32_t aSeverity, const char *aStr, const char *aExpr,
} }
// Now we deal with assertions // Now we deal with assertions
PR_ATOMIC_INCREMENT(&gAssertionCount); gAssertionCount++;
switch (GetAssertBehavior()) { switch (GetAssertBehavior()) {
case NS_ASSERT_WARN: case NS_ASSERT_WARN:

View File

@ -36,7 +36,7 @@ public:
nsExceptionManager *mNextThread; // not ref-counted. nsExceptionManager *mNextThread; // not ref-counted.
nsExceptionService *mService; // not ref-counted nsExceptionService *mService; // not ref-counted
#ifdef DEBUG #ifdef DEBUG
static int32_t totalInstances; static Atomic<int32_t> totalInstances;
#endif #endif
private: private:
@ -45,7 +45,7 @@ private:
#ifdef DEBUG #ifdef DEBUG
int32_t nsExceptionManager::totalInstances = 0; Atomic<int32_t> nsExceptionManager::totalInstances;
#endif #endif
// Note this object is single threaded - the service itself ensures // Note this object is single threaded - the service itself ensures
@ -60,7 +60,7 @@ nsExceptionManager::nsExceptionManager(nsExceptionService *svc) :
{ {
/* member initializers and constructor code */ /* member initializers and constructor code */
#ifdef DEBUG #ifdef DEBUG
PR_ATOMIC_INCREMENT(&totalInstances); ++totalInstances;
#endif #endif
} }
@ -68,7 +68,7 @@ nsExceptionManager::~nsExceptionManager()
{ {
/* destructor code */ /* destructor code */
#ifdef DEBUG #ifdef DEBUG
PR_ATOMIC_DECREMENT(&totalInstances); --totalInstances;
#endif // DEBUG #endif // DEBUG
} }
@ -104,7 +104,7 @@ Mutex *nsExceptionService::sLock = nullptr;
nsExceptionManager *nsExceptionService::firstThread = nullptr; nsExceptionManager *nsExceptionService::firstThread = nullptr;
#ifdef DEBUG #ifdef DEBUG
int32_t nsExceptionService::totalInstances = 0; Atomic<int32_t> nsExceptionService::totalInstances;
#endif #endif
NS_IMPL_ISUPPORTS3(nsExceptionService, NS_IMPL_ISUPPORTS3(nsExceptionService,
@ -115,7 +115,7 @@ NS_IMPL_ISUPPORTS3(nsExceptionService,
nsExceptionService::nsExceptionService() nsExceptionService::nsExceptionService()
{ {
#ifdef DEBUG #ifdef DEBUG
if (PR_ATOMIC_INCREMENT(&totalInstances)!=1) { if (++totalInstances != 1) {
NS_ERROR("The nsExceptionService is a singleton!"); NS_ERROR("The nsExceptionService is a singleton!");
} }
#endif #endif
@ -140,7 +140,7 @@ nsExceptionService::~nsExceptionService()
Shutdown(); Shutdown();
/* destructor code */ /* destructor code */
#ifdef DEBUG #ifdef DEBUG
PR_ATOMIC_DECREMENT(&totalInstances); --totalInstances;
#endif #endif
} }

View File

@ -6,6 +6,7 @@
#ifndef nsExceptionService_h__ #ifndef nsExceptionService_h__
#define nsExceptionService_h__ #define nsExceptionService_h__
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/Mutex.h" #include "mozilla/Mutex.h"
@ -49,7 +50,7 @@ public:
static unsigned tlsIndex; static unsigned tlsIndex;
static void ThreadDestruct( void *data ); static void ThreadDestruct( void *data );
#ifdef DEBUG #ifdef DEBUG
static int32_t totalInstances; static mozilla::Atomic<int32_t> totalInstances;
#endif #endif
private: private:

View File

@ -114,7 +114,7 @@ nsMemoryImpl::FlushMemory(const PRUnichar* aReason, bool aImmediate)
} }
} }
int32_t lastVal = PR_ATOMIC_SET(&sIsFlushing, 1); int32_t lastVal = sIsFlushing.exchange(1);
if (lastVal) if (lastVal)
return NS_OK; return NS_OK;
@ -183,8 +183,8 @@ nsMemoryImpl::FlushEvent::Run()
return NS_OK; return NS_OK;
} }
int32_t mozilla::Atomic<int32_t>
nsMemoryImpl::sIsFlushing = 0; nsMemoryImpl::sIsFlushing;
PRIntervalTime PRIntervalTime
nsMemoryImpl::sLastFlushTime = 0; nsMemoryImpl::sLastFlushTime = 0;

View File

@ -6,6 +6,8 @@
#ifndef nsMemoryImpl_h__ #ifndef nsMemoryImpl_h__
#define nsMemoryImpl_h__ #define nsMemoryImpl_h__
#include "mozilla/Atomics.h"
#include "nsIMemory.h" #include "nsIMemory.h"
#include "nsIRunnable.h" #include "nsIRunnable.h"
#include "prtime.h" #include "prtime.h"
@ -37,7 +39,7 @@ protected:
const PRUnichar* mReason; const PRUnichar* mReason;
}; };
static int32_t sIsFlushing; static mozilla::Atomic<int32_t> sIsFlushing;
static FlushEvent sFlushEvent; static FlushEvent sFlushEvent;
static PRIntervalTime sLastFlushTime; static PRIntervalTime sLastFlushTime;
}; };

View File

@ -6,6 +6,7 @@
#include "mozilla/nsMemoryInfoDumper.h" #include "mozilla/nsMemoryInfoDumper.h"
#include "mozilla/Atomics.h"
#include "mozilla/ClearOnShutdown.h" #include "mozilla/ClearOnShutdown.h"
#include "mozilla/FileUtils.h" #include "mozilla/FileUtils.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
@ -142,7 +143,7 @@ static int sGCAndCCDumpSignum; // SIGRTMIN + 2
// This is the write-end of a pipe that we use to notice when a // This is the write-end of a pipe that we use to notice when a
// dump-about-memory signal occurs. // dump-about-memory signal occurs.
static int sDumpAboutMemoryPipeWriteFd = -1; static Atomic<int> sDumpAboutMemoryPipeWriteFd(-1);
void void
DumpAboutMemorySignalHandler(int aSignum) DumpAboutMemorySignalHandler(int aSignum)
@ -329,8 +330,7 @@ public:
// 2) open a new fd with the same number as sDumpAboutMemoryPipeWriteFd // 2) open a new fd with the same number as sDumpAboutMemoryPipeWriteFd
// had. // had.
// 3) receive a signal, then write to the fd. // 3) receive a signal, then write to the fd.
int pipeWriteFd = sDumpAboutMemoryPipeWriteFd; int pipeWriteFd = sDumpAboutMemoryPipeWriteFd.exchange(-1);
PR_ATOMIC_SET(&sDumpAboutMemoryPipeWriteFd, -1);
close(pipeWriteFd); close(pipeWriteFd);
FdWatcher::StopWatching(); FdWatcher::StopWatching();

View File

@ -7,6 +7,7 @@
#ifndef nsStringBuffer_h__ #ifndef nsStringBuffer_h__
#define nsStringBuffer_h__ #define nsStringBuffer_h__
#include "mozilla/Atomics.h"
#include "mozilla/MemoryReporting.h" #include "mozilla/MemoryReporting.h"
template<class T> struct already_AddRefed; template<class T> struct already_AddRefed;
@ -25,7 +26,7 @@ class nsStringBuffer
private: private:
friend class CheckStaticAtomSizes; friend class CheckStaticAtomSizes;
int32_t mRefCount; mozilla::Atomic<int32_t> mRefCount;
uint32_t mStorageSize; uint32_t mStorageSize;
public: public:

View File

@ -8,6 +8,7 @@
#define ENABLE_STRING_STATS #define ENABLE_STRING_STATS
#endif #endif
#include "mozilla/Atomics.h"
#include "mozilla/MemoryReporting.h" #include "mozilla/MemoryReporting.h"
#ifdef ENABLE_STRING_STATS #ifdef ENABLE_STRING_STATS
@ -20,11 +21,12 @@
#include "nsStringBuffer.h" #include "nsStringBuffer.h"
#include "nsDependentString.h" #include "nsDependentString.h"
#include "nsMemory.h" #include "nsMemory.h"
#include "pratom.h"
#include "prprf.h" #include "prprf.h"
#include "nsStaticAtom.h" #include "nsStaticAtom.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
using mozilla::Atomic;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static PRUnichar gNullChar = 0; static PRUnichar gNullChar = 0;
@ -50,31 +52,31 @@ class nsStringStats
return; return;
printf("nsStringStats\n"); printf("nsStringStats\n");
printf(" => mAllocCount: % 10d\n", mAllocCount); printf(" => mAllocCount: % 10d\n", int(mAllocCount));
printf(" => mReallocCount: % 10d\n", mReallocCount); printf(" => mReallocCount: % 10d\n", int(mReallocCount));
printf(" => mFreeCount: % 10d", mFreeCount); printf(" => mFreeCount: % 10d", int(mFreeCount));
if (mAllocCount > mFreeCount) if (mAllocCount > mFreeCount)
printf(" -- LEAKED %d !!!\n", mAllocCount - mFreeCount); printf(" -- LEAKED %d !!!\n", mAllocCount - mFreeCount);
else else
printf("\n"); printf("\n");
printf(" => mShareCount: % 10d\n", mShareCount); printf(" => mShareCount: % 10d\n", int(mShareCount));
printf(" => mAdoptCount: % 10d\n", mAdoptCount); printf(" => mAdoptCount: % 10d\n", int(mAdoptCount));
printf(" => mAdoptFreeCount: % 10d", mAdoptFreeCount); printf(" => mAdoptFreeCount: % 10d", int(mAdoptFreeCount));
if (mAdoptCount > mAdoptFreeCount) if (mAdoptCount > mAdoptFreeCount)
printf(" -- LEAKED %d !!!\n", mAdoptCount - mAdoptFreeCount); printf(" -- LEAKED %d !!!\n", mAdoptCount - mAdoptFreeCount);
else else
printf("\n"); printf("\n");
} }
int32_t mAllocCount; Atomic<int32_t> mAllocCount;
int32_t mReallocCount; Atomic<int32_t> mReallocCount;
int32_t mFreeCount; Atomic<int32_t> mFreeCount;
int32_t mShareCount; Atomic<int32_t> mShareCount;
int32_t mAdoptCount; Atomic<int32_t> mAdoptCount;
int32_t mAdoptFreeCount; Atomic<int32_t> mAdoptFreeCount;
}; };
static nsStringStats gStringStats; static nsStringStats gStringStats;
#define STRING_STAT_INCREMENT(_s) PR_ATOMIC_INCREMENT(&gStringStats.m ## _s ## Count) #define STRING_STAT_INCREMENT(_s) (gStringStats.m ## _s ## Count)++
#else #else
#define STRING_STAT_INCREMENT(_s) #define STRING_STAT_INCREMENT(_s)
#endif #endif
@ -148,7 +150,7 @@ class nsACStringAccessor : public nsACString
void void
nsStringBuffer::AddRef() nsStringBuffer::AddRef()
{ {
PR_ATOMIC_INCREMENT(&mRefCount); ++mRefCount;
STRING_STAT_INCREMENT(Share); STRING_STAT_INCREMENT(Share);
NS_LOG_ADDREF(this, mRefCount, "nsStringBuffer", sizeof(*this)); NS_LOG_ADDREF(this, mRefCount, "nsStringBuffer", sizeof(*this));
} }
@ -156,7 +158,7 @@ nsStringBuffer::AddRef()
void void
nsStringBuffer::Release() nsStringBuffer::Release()
{ {
int32_t count = PR_ATOMIC_DECREMENT(&mRefCount); int32_t count = --mRefCount;
NS_LOG_RELEASE(this, count, "nsStringBuffer"); NS_LOG_RELEASE(this, count, "nsStringBuffer");
if (count == 0) if (count == 0)
{ {

View File

@ -84,7 +84,7 @@ nsresult TimerThread::Init()
return NS_OK; return NS_OK;
} }
if (PR_ATOMIC_SET(&mInitInProgress, 1) == 0) { if (mInitInProgress.exchange(1) == 0) {
// We hold on to mThread to keep the thread alive. // We hold on to mThread to keep the thread alive.
nsresult rv = NS_NewThread(getter_AddRefs(mThread), this); nsresult rv = NS_NewThread(getter_AddRefs(mThread), this);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {

View File

@ -15,6 +15,7 @@
#include "nsTArray.h" #include "nsTArray.h"
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/Monitor.h" #include "mozilla/Monitor.h"
#include "mozilla/TimeStamp.h" #include "mozilla/TimeStamp.h"
@ -52,7 +53,7 @@ public:
private: private:
~TimerThread(); ~TimerThread();
int32_t mInitInProgress; mozilla::Atomic<int32_t> mInitInProgress;
bool mInitialized; bool mInitialized;
// These two internal helper methods must be called while mLock is held. // These two internal helper methods must be called while mLock is held.

View File

@ -12,11 +12,13 @@
#include "plarena.h" #include "plarena.h"
#include "pratom.h" #include "pratom.h"
#include "GeckoProfiler.h" #include "GeckoProfiler.h"
#include "mozilla/Atomics.h"
using mozilla::Atomic;
using mozilla::TimeDuration; using mozilla::TimeDuration;
using mozilla::TimeStamp; using mozilla::TimeStamp;
static int32_t gGenerator = 0; static Atomic<int32_t> gGenerator;
static TimerThread* gThread = nullptr; static TimerThread* gThread = nullptr;
#ifdef DEBUG_TIMERS #ifdef DEBUG_TIMERS
@ -114,7 +116,7 @@ public:
MOZ_ASSERT(gThread->IsOnTimerThread(), MOZ_ASSERT(gThread->IsOnTimerThread(),
"nsTimer must always be allocated on the timer thread"); "nsTimer must always be allocated on the timer thread");
PR_ATOMIC_INCREMENT(&sAllocatorUsers); sAllocatorUsers++;
} }
#ifdef DEBUG_TIMERS #ifdef DEBUG_TIMERS
@ -140,19 +142,19 @@ private:
MOZ_ASSERT(!sCanDeleteAllocator || sAllocatorUsers > 0, MOZ_ASSERT(!sCanDeleteAllocator || sAllocatorUsers > 0,
"This will result in us attempting to deallocate the nsTimerEvent allocator twice"); "This will result in us attempting to deallocate the nsTimerEvent allocator twice");
PR_ATOMIC_DECREMENT(&sAllocatorUsers); sAllocatorUsers--;
} }
nsRefPtr<nsTimerImpl> mTimer; nsRefPtr<nsTimerImpl> mTimer;
int32_t mGeneration; int32_t mGeneration;
static TimerEventAllocator* sAllocator; static TimerEventAllocator* sAllocator;
static int32_t sAllocatorUsers; static Atomic<int32_t> sAllocatorUsers;
static bool sCanDeleteAllocator; static bool sCanDeleteAllocator;
}; };
TimerEventAllocator* nsTimerEvent::sAllocator = nullptr; TimerEventAllocator* nsTimerEvent::sAllocator = nullptr;
int32_t nsTimerEvent::sAllocatorUsers = 0; Atomic<int32_t> nsTimerEvent::sAllocatorUsers;
bool nsTimerEvent::sCanDeleteAllocator = false; bool nsTimerEvent::sCanDeleteAllocator = false;
namespace { namespace {
@ -342,7 +344,7 @@ nsresult nsTimerImpl::InitCommon(uint32_t aType, uint32_t aDelay)
gThread->RemoveTimer(this); gThread->RemoveTimer(this);
mCanceled = false; mCanceled = false;
mTimeout = TimeStamp(); mTimeout = TimeStamp();
mGeneration = PR_ATOMIC_INCREMENT(&gGenerator); mGeneration = gGenerator++;
mType = (uint8_t)aType; mType = (uint8_t)aType;
SetDelayInternal(aDelay); SetDelayInternal(aDelay);