diff --git a/xpcom/base/AvailableMemoryTracker.cpp b/xpcom/base/AvailableMemoryTracker.cpp index 75f31aea9b9..b1985db99c4 100644 --- a/xpcom/base/AvailableMemoryTracker.cpp +++ b/xpcom/base/AvailableMemoryTracker.cpp @@ -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 sNumLowVirtualMemEvents; +Atomic sNumLowCommitSpaceEvents; +Atomic 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; } } } diff --git a/xpcom/base/nsDebugImpl.cpp b/xpcom/base/nsDebugImpl.cpp index e751d993b0d..16f243aa64f 100644 --- a/xpcom/base/nsDebugImpl.cpp +++ b/xpcom/base/nsDebugImpl.cpp @@ -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 @@ -83,7 +84,7 @@ using namespace mozilla; static bool sIsMultiprocess = false; static const char *sMultiprocessDescription = NULL; -static int32_t gAssertionCount = 0; +static Atomic 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: diff --git a/xpcom/base/nsExceptionService.cpp b/xpcom/base/nsExceptionService.cpp index 430eccd0857..706e33fd4a3 100644 --- a/xpcom/base/nsExceptionService.cpp +++ b/xpcom/base/nsExceptionService.cpp @@ -36,7 +36,7 @@ public: nsExceptionManager *mNextThread; // not ref-counted. nsExceptionService *mService; // not ref-counted #ifdef DEBUG - static int32_t totalInstances; + static Atomic totalInstances; #endif private: @@ -45,7 +45,7 @@ private: #ifdef DEBUG -int32_t nsExceptionManager::totalInstances = 0; +Atomic 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 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 } diff --git a/xpcom/base/nsExceptionService.h b/xpcom/base/nsExceptionService.h index b4a2c52d306..eb89c9c181b 100644 --- a/xpcom/base/nsExceptionService.h +++ b/xpcom/base/nsExceptionService.h @@ -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 totalInstances; #endif private: diff --git a/xpcom/base/nsMemoryImpl.cpp b/xpcom/base/nsMemoryImpl.cpp index a67372a6d2c..d09a9136af8 100644 --- a/xpcom/base/nsMemoryImpl.cpp +++ b/xpcom/base/nsMemoryImpl.cpp @@ -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 +nsMemoryImpl::sIsFlushing; PRIntervalTime nsMemoryImpl::sLastFlushTime = 0; diff --git a/xpcom/base/nsMemoryImpl.h b/xpcom/base/nsMemoryImpl.h index c73b8e2e6ee..ab27d01c199 100644 --- a/xpcom/base/nsMemoryImpl.h +++ b/xpcom/base/nsMemoryImpl.h @@ -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 sIsFlushing; static FlushEvent sFlushEvent; static PRIntervalTime sLastFlushTime; }; diff --git a/xpcom/base/nsMemoryInfoDumper.cpp b/xpcom/base/nsMemoryInfoDumper.cpp index 417e28b05ab..728edf18aff 100644 --- a/xpcom/base/nsMemoryInfoDumper.cpp +++ b/xpcom/base/nsMemoryInfoDumper.cpp @@ -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 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(); diff --git a/xpcom/string/public/nsStringBuffer.h b/xpcom/string/public/nsStringBuffer.h index dbd92759825..8f8cf0f9c26 100644 --- a/xpcom/string/public/nsStringBuffer.h +++ b/xpcom/string/public/nsStringBuffer.h @@ -7,6 +7,7 @@ #ifndef nsStringBuffer_h__ #define nsStringBuffer_h__ +#include "mozilla/Atomics.h" #include "mozilla/MemoryReporting.h" template struct already_AddRefed; @@ -25,7 +26,7 @@ class nsStringBuffer private: friend class CheckStaticAtomSizes; - int32_t mRefCount; + mozilla::Atomic mRefCount; uint32_t mStorageSize; public: diff --git a/xpcom/string/src/nsSubstring.cpp b/xpcom/string/src/nsSubstring.cpp index 1a9a730cd20..7a9df8b3e1c 100644 --- a/xpcom/string/src/nsSubstring.cpp +++ b/xpcom/string/src/nsSubstring.cpp @@ -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 mAllocCount; + Atomic mReallocCount; + Atomic mFreeCount; + Atomic mShareCount; + Atomic mAdoptCount; + Atomic 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) { diff --git a/xpcom/threads/TimerThread.cpp b/xpcom/threads/TimerThread.cpp index 9053f8f4118..68803aeeb94 100644 --- a/xpcom/threads/TimerThread.cpp +++ b/xpcom/threads/TimerThread.cpp @@ -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)) { diff --git a/xpcom/threads/TimerThread.h b/xpcom/threads/TimerThread.h index a9699c29f3c..aba1e4ba8ed 100644 --- a/xpcom/threads/TimerThread.h +++ b/xpcom/threads/TimerThread.h @@ -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 mInitInProgress; bool mInitialized; // These two internal helper methods must be called while mLock is held. diff --git a/xpcom/threads/nsTimerImpl.cpp b/xpcom/threads/nsTimerImpl.cpp index 0f1f1412902..bb59fc98bf1 100644 --- a/xpcom/threads/nsTimerImpl.cpp +++ b/xpcom/threads/nsTimerImpl.cpp @@ -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 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 mTimer; int32_t mGeneration; static TimerEventAllocator* sAllocator; - static int32_t sAllocatorUsers; + static Atomic sAllocatorUsers; static bool sCanDeleteAllocator; }; TimerEventAllocator* nsTimerEvent::sAllocator = nullptr; -int32_t nsTimerEvent::sAllocatorUsers = 0; +Atomic 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);