Bug 1365102 - Convert NS_GetCurrentThread uses in xpcom/ (r=froydnj)
authorBill McCloskey <billm@mozilla.com>
Thu, 01 Jun 2017 13:44:20 -0700
changeset 412326 894903d30183
parent 412325 43fcf7ddd6ba
child 412327 34adc718cf1f
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1365102
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1365102 - Convert NS_GetCurrentThread uses in xpcom/ (r=froydnj) MozReview-Commit-ID: 69qVzf8QJMq
xpcom/base/nsCycleCollector.cpp
xpcom/base/nsInterfaceRequestorAgg.cpp
xpcom/threads/LazyIdleThread.cpp
xpcom/threads/LazyIdleThread.h
xpcom/threads/TaskQueue.cpp
xpcom/threads/TaskQueue.h
xpcom/threads/TimerThread.h
xpcom/threads/nsThreadUtils.cpp
xpcom/threads/nsTimerImpl.cpp
--- a/xpcom/base/nsCycleCollector.cpp
+++ b/xpcom/base/nsCycleCollector.cpp
@@ -171,16 +171,17 @@
 #include "nsThreadUtils.h"
 #include "nsXULAppAPI.h"
 #include "prenv.h"
 #include "nsPrintfCString.h"
 #include "nsTArray.h"
 #include "nsIConsoleService.h"
 #include "mozilla/Attributes.h"
 #include "nsICycleCollectorListener.h"
+#include "nsISerialEventTarget.h"
 #include "nsIMemoryReporter.h"
 #include "nsIFile.h"
 #include "nsDumpUtils.h"
 #include "xpcpublic.h"
 #include "GeckoProfiler.h"
 #include <stdint.h>
 #include <stdio.h>
 
@@ -1258,17 +1259,17 @@ private:
   CycleCollectedJSRuntime* mCCJSRuntime;
 
   ccPhase mIncrementalPhase;
   CCGraph mGraph;
   nsAutoPtr<CCGraphBuilder> mBuilder;
   RefPtr<nsCycleCollectorLogger> mLogger;
 
 #ifdef DEBUG
-  void* mThread;
+  nsISerialEventTarget* mEventTarget;
 #endif
 
   nsCycleCollectorParams mParams;
 
   uint32_t mWhiteNodeCount;
 
   CC_BeforeUnlinkCallback mBeforeUnlinkCB;
   CC_ForgetSkippableCallback mForgetSkippableCB;
@@ -3389,17 +3390,17 @@ nsCycleCollector::CollectReports(nsIHand
 
 nsCycleCollector::nsCycleCollector() :
   mActivelyCollecting(false),
   mFreeingSnowWhite(false),
   mScanInProgress(false),
   mCCJSRuntime(nullptr),
   mIncrementalPhase(IdlePhase),
 #ifdef DEBUG
-  mThread(NS_GetCurrentThread()),
+  mEventTarget(GetCurrentThreadSerialEventTarget()),
 #endif
   mWhiteNodeCount(0),
   mBeforeUnlinkCB(nullptr),
   mForgetSkippableCB(nullptr),
   mUnmergedNeeded(0),
   mMergedInARow(0)
 {
 }
@@ -3466,20 +3467,17 @@ nsCycleCollector::Suspect(void* aPtr, ns
 
   mPurpleBuf.Put(aPtr, aParti, aRefCnt);
 }
 
 void
 nsCycleCollector::CheckThreadSafety()
 {
 #ifdef DEBUG
-  nsIThread* currentThread = NS_GetCurrentThread();
-  // XXXkhuey we can be called so late in shutdown that NS_GetCurrentThread
-  // returns null (after the thread manager has shut down)
-  MOZ_ASSERT(mThread == currentThread || !currentThread);
+  MOZ_ASSERT(mEventTarget->IsOnCurrentThread());
 #endif
 }
 
 // The cycle collector uses the mark bitmap to discover what JS objects
 // were reachable only from XPConnect roots that might participate in
 // cycles. We ask the JS context whether we need to force a GC before
 // this CC. It returns true on startup (before the mark bits have been set),
 // and also when UnmarkGray has run out of stack.  We also force GCs on shut
--- a/xpcom/base/nsInterfaceRequestorAgg.cpp
+++ b/xpcom/base/nsInterfaceRequestorAgg.cpp
@@ -21,17 +21,17 @@ public:
   nsInterfaceRequestorAgg(nsIInterfaceRequestor* aFirst,
                           nsIInterfaceRequestor* aSecond,
                           nsIEventTarget* aConsumerTarget = nullptr)
     : mFirst(aFirst)
     , mSecond(aSecond)
     , mConsumerTarget(aConsumerTarget)
   {
     if (!mConsumerTarget) {
-      mConsumerTarget = NS_GetCurrentThread();
+      mConsumerTarget = GetCurrentThreadEventTarget();
     }
   }
 
 private:
   ~nsInterfaceRequestorAgg();
 
   nsCOMPtr<nsIInterfaceRequestor> mFirst, mSecond;
   nsCOMPtr<nsIEventTarget> mConsumerTarget;
--- a/xpcom/threads/LazyIdleThread.cpp
+++ b/xpcom/threads/LazyIdleThread.cpp
@@ -13,47 +13,42 @@
 #include "nsIIdlePeriod.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "mozilla/Services.h"
 
 #ifdef DEBUG
 #define ASSERT_OWNING_THREAD()                                                 \
   do {                                                                         \
-    nsIThread* currentThread = NS_GetCurrentThread();                          \
-    if (currentThread) {                                                       \
-      nsCOMPtr<nsISupports> current(do_QueryInterface(currentThread));         \
-      nsCOMPtr<nsISupports> test(do_QueryInterface(mOwningThread));            \
-      MOZ_ASSERT(current == test, "Wrong thread!");                            \
-    }                                                                          \
+    MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());               \
   } while(0)
 #else
 #define ASSERT_OWNING_THREAD() /* nothing */
 #endif
 
 namespace mozilla {
 
 LazyIdleThread::LazyIdleThread(uint32_t aIdleTimeoutMS,
                                const nsCSubstring& aName,
                                ShutdownMethod aShutdownMethod,
                                nsIObserver* aIdleObserver)
   : mMutex("LazyIdleThread::mMutex")
-  , mOwningThread(NS_GetCurrentThread())
+  , mOwningEventTarget(GetCurrentThreadSerialEventTarget())
   , mIdleObserver(aIdleObserver)
   , mQueuedRunnables(nullptr)
   , mIdleTimeoutMS(aIdleTimeoutMS)
   , mPendingEventCount(0)
   , mIdleNotificationCount(0)
   , mShutdownMethod(aShutdownMethod)
   , mShutdown(false)
   , mThreadIsShuttingDown(false)
   , mIdleTimeoutEnabled(true)
   , mName(aName)
 {
-  MOZ_ASSERT(mOwningThread, "Need owning thread!");
+  MOZ_ASSERT(mOwningEventTarget, "Need owning thread!");
 }
 
 LazyIdleThread::~LazyIdleThread()
 {
   ASSERT_OWNING_THREAD();
 
   Shutdown();
 }
@@ -560,17 +555,17 @@ LazyIdleThread::Notify(nsITimer* aTimer)
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 LazyIdleThread::OnDispatchedEvent(nsIThreadInternal* /*aThread */)
 {
-  MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread, "Wrong thread!");
+  MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
   return NS_OK;
 }
 
 NS_IMETHODIMP
 LazyIdleThread::OnProcessNextEvent(nsIThreadInternal* /* aThread */,
                                    bool /* aMayWait */)
 {
   return NS_OK;
@@ -603,17 +598,17 @@ LazyIdleThread::AfterProcessNextEvent(ns
 
   if (shouldNotifyIdle) {
     nsCOMPtr<nsIRunnable> runnable =
       NewRunnableMethod(this, &LazyIdleThread::ScheduleTimer);
     if (NS_WARN_IF(!runnable)) {
       return NS_ERROR_UNEXPECTED;
     }
 
-    nsresult rv = mOwningThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
+    nsresult rv = mOwningEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   return NS_OK;
 }
 
--- a/xpcom/threads/LazyIdleThread.h
+++ b/xpcom/threads/LazyIdleThread.h
@@ -139,17 +139,17 @@ private:
    * Protects data that is accessed on both threads.
    */
   mozilla::Mutex mMutex;
 
   /**
    * Touched on both threads but set before mThread is created. Used to direct
    * timer events to the owning thread.
    */
-  nsCOMPtr<nsIThread> mOwningThread;
+  nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
 
   /**
    * Only accessed on the owning thread. Set by EnsureThread().
    */
   nsCOMPtr<nsIThread> mThread;
 
   /**
    * Protected by mMutex. Created when mThread has no pending events and fired
--- a/xpcom/threads/TaskQueue.cpp
+++ b/xpcom/threads/TaskQueue.cpp
@@ -204,17 +204,17 @@ TaskQueue::ImpreciseLengthForHeuristics(
 {
   MonitorAutoLock mon(mQueueMonitor);
   return mTasks.size();
 }
 
 bool
 TaskQueue::IsCurrentThreadIn()
 {
-  bool in = NS_GetCurrentThread() == mRunningThread;
+  bool in = mRunningThread == GetCurrentPhysicalThread();
   return in;
 }
 
 already_AddRefed<nsISerialEventTarget>
 TaskQueue::WrapAsEventTarget()
 {
   nsCOMPtr<nsISerialEventTarget> ref = new EventTargetWrapper(this);
   return ref.forget();
--- a/xpcom/threads/TaskQueue.h
+++ b/xpcom/threads/TaskQueue.h
@@ -141,17 +141,17 @@ protected:
   // The thread currently running the task queue. We store a reference
   // to this so that IsCurrentThreadIn() can tell if the current thread
   // is the thread currently running in the task queue.
   //
   // This may be read on any thread, but may only be written on mRunningThread.
   // The thread can't die while we're running in it, and we only use it for
   // pointer-comparison with the current thread anyway - so we make it atomic
   // and don't refcount it.
-  Atomic<nsIThread*> mRunningThread;
+  Atomic<PRThread*> mRunningThread;
 
   // RAII class that gets instantiated for each dispatched task.
   class AutoTaskGuard : public AutoTaskDispatcher
   {
   public:
     explicit AutoTaskGuard(TaskQueue* aQueue)
       : AutoTaskDispatcher(/* aIsTailDispatcher = */ true), mQueue(aQueue)
       , mLastCurrentThread(nullptr)
@@ -160,24 +160,24 @@ protected:
       // might require it.
       MOZ_ASSERT(!mQueue->mTailDispatcher);
       mQueue->mTailDispatcher = this;
 
       mLastCurrentThread = sCurrentThreadTLS.get();
       sCurrentThreadTLS.set(aQueue);
 
       MOZ_ASSERT(mQueue->mRunningThread == nullptr);
-      mQueue->mRunningThread = NS_GetCurrentThread();
+      mQueue->mRunningThread = GetCurrentPhysicalThread();
     }
 
     ~AutoTaskGuard()
     {
       DrainDirectTasks();
 
-      MOZ_ASSERT(mQueue->mRunningThread == NS_GetCurrentThread());
+      MOZ_ASSERT(mQueue->mRunningThread == GetCurrentPhysicalThread());
       mQueue->mRunningThread = nullptr;
 
       sCurrentThreadTLS.set(mLastCurrentThread);
       mQueue->mTailDispatcher = nullptr;
     }
 
   private:
   TaskQueue* mQueue;
--- a/xpcom/threads/TimerThread.h
+++ b/xpcom/threads/TimerThread.h
@@ -48,17 +48,17 @@ public:
   nsresult RemoveTimer(nsTimerImpl* aTimer);
   TimeStamp FindNextFireTimeForCurrentThread(TimeStamp aDefault, uint32_t aSearchBound);
 
   void DoBeforeSleep();
   void DoAfterSleep();
 
   bool IsOnTimerThread() const
   {
-    return mThread == NS_GetCurrentThread();
+    return mThread->SerialEventTarget()->IsOnCurrentThread();
   }
 
   uint32_t
   AllowedEarlyFiringMicroseconds() const;
 
 private:
   ~TimerThread();
 
--- a/xpcom/threads/nsThreadUtils.cpp
+++ b/xpcom/threads/nsThreadUtils.cpp
@@ -191,17 +191,17 @@ NS_IsMainThread()
 #endif
 
 nsresult
 NS_DispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent)
 {
   nsresult rv;
   nsCOMPtr<nsIRunnable> event(aEvent);
 #ifdef MOZILLA_INTERNAL_API
-  nsIThread* thread = NS_GetCurrentThread();
+  nsIEventTarget* thread = GetCurrentThreadEventTarget();
   if (!thread) {
     return NS_ERROR_UNEXPECTED;
   }
 #else
   nsCOMPtr<nsIThread> thread;
   rv = NS_GetCurrentThread(getter_AddRefs(thread));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
@@ -257,17 +257,17 @@ NS_DispatchToMainThread(nsIRunnable* aEv
   return NS_DispatchToMainThread(event.forget(), aDispatchFlags);
 }
 
 nsresult
 NS_DelayedDispatchToCurrentThread(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs)
 {
   nsCOMPtr<nsIRunnable> event(aEvent);
 #ifdef MOZILLA_INTERNAL_API
-  nsIThread* thread = NS_GetCurrentThread();
+  nsIEventTarget* thread = GetCurrentThreadEventTarget();
   if (!thread) {
     return NS_ERROR_UNEXPECTED;
   }
 #else
   nsresult rv;
   nsCOMPtr<nsIThread> thread;
   rv = NS_GetCurrentThread(getter_AddRefs(thread));
   if (NS_WARN_IF(NS_FAILED(rv))) {
--- a/xpcom/threads/nsTimerImpl.cpp
+++ b/xpcom/threads/nsTimerImpl.cpp
@@ -148,17 +148,17 @@ nsTimer::Release(void)
 
 nsTimerImpl::nsTimerImpl(nsITimer* aTimer) :
   mHolder(nullptr),
   mGeneration(0),
   mITimer(aTimer),
   mMutex("nsTimerImpl::mMutex")
 {
   // XXXbsmedberg: shouldn't this be in Init()?
-  mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
+  mEventTarget = GetCurrentThreadEventTarget();
 }
 
 //static
 nsresult
 nsTimerImpl::Startup()
 {
   nsresult rv;
 
@@ -448,17 +448,17 @@ nsTimerImpl::SetTarget(nsIEventTarget* a
   MutexAutoLock lock(mMutex);
   if (NS_WARN_IF(mCallback.mType != Callback::Type::Unknown)) {
     return NS_ERROR_ALREADY_INITIALIZED;
   }
 
   if (aTarget) {
     mEventTarget = aTarget;
   } else {
-    mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
+    mEventTarget = GetCurrentThreadEventTarget();
   }
   return NS_OK;
 }
 
 nsresult
 nsTimerImpl::GetAllowedEarlyFiringMicroseconds(uint32_t* aValueOut)
 {
   *aValueOut = gThread ? gThread->AllowedEarlyFiringMicroseconds() : 0;