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
Родитель 739307aed0
Коммит 122242a163
12 изменённых файлов: 60 добавлений и 50 удалений

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

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

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

@ -6,6 +6,8 @@
// Chromium headers must come before Mozilla headers.
#include "base/process_util.h"
#include "mozilla/Atomics.h"
#include "nsDebugImpl.h"
#include "nsDebug.h"
#ifdef MOZ_CRASHREPORTER
@ -20,7 +22,6 @@
#include "prerror.h"
#include "prerr.h"
#include "prenv.h"
#include "pratom.h"
#ifdef ANDROID
#include <android/log.h>
@ -83,7 +84,7 @@ using namespace mozilla;
static bool sIsMultiprocess = false;
static const char *sMultiprocessDescription = NULL;
static int32_t gAssertionCount = 0;
static Atomic<int32_t> gAssertionCount;
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
PR_ATOMIC_INCREMENT(&gAssertionCount);
gAssertionCount++;
switch (GetAssertBehavior()) {
case NS_ASSERT_WARN:

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

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

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

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

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

@ -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)
return NS_OK;
@ -183,8 +183,8 @@ nsMemoryImpl::FlushEvent::Run()
return NS_OK;
}
int32_t
nsMemoryImpl::sIsFlushing = 0;
mozilla::Atomic<int32_t>
nsMemoryImpl::sIsFlushing;
PRIntervalTime
nsMemoryImpl::sLastFlushTime = 0;

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

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

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

@ -6,6 +6,7 @@
#include "mozilla/nsMemoryInfoDumper.h"
#include "mozilla/Atomics.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/FileUtils.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
// dump-about-memory signal occurs.
static int sDumpAboutMemoryPipeWriteFd = -1;
static Atomic<int> sDumpAboutMemoryPipeWriteFd(-1);
void
DumpAboutMemorySignalHandler(int aSignum)
@ -329,8 +330,7 @@ public:
// 2) open a new fd with the same number as sDumpAboutMemoryPipeWriteFd
// had.
// 3) receive a signal, then write to the fd.
int pipeWriteFd = sDumpAboutMemoryPipeWriteFd;
PR_ATOMIC_SET(&sDumpAboutMemoryPipeWriteFd, -1);
int pipeWriteFd = sDumpAboutMemoryPipeWriteFd.exchange(-1);
close(pipeWriteFd);
FdWatcher::StopWatching();

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

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

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

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

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

@ -84,7 +84,7 @@ nsresult TimerThread::Init()
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.
nsresult rv = NS_NewThread(getter_AddRefs(mThread), this);
if (NS_FAILED(rv)) {

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

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

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

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