Bug 1141775 - One wifi monitor thread. r=hurley, a=sledru
authorPatrick McManus <mcmanus@ducksong.com>
Thu, 02 Apr 2015 13:58:14 -0400
changeset 258298 ce2f9cedb975
parent 258297 f9441895096d
child 258299 b4e6da60e6d4
push id4638
push userryanvm@gmail.com
push date2015-04-06 20:41 +0000
treeherdermozilla-beta@499e1c563939 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershurley, sledru
bugs1141775
milestone38.0
Bug 1141775 - One wifi monitor thread. r=hurley, a=sledru
netwerk/wifi/nsWifiMonitor.cpp
netwerk/wifi/nsWifiMonitor.h
netwerk/wifi/nsWifiScannerWin.cpp
--- a/netwerk/wifi/nsWifiMonitor.cpp
+++ b/netwerk/wifi/nsWifiMonitor.cpp
@@ -26,16 +26,17 @@ PRLogModuleInfo *gWifiMonitorLog;
 
 NS_IMPL_ISUPPORTS(nsWifiMonitor,
                   nsIRunnable,
                   nsIObserver,
                   nsIWifiMonitor)
 
 nsWifiMonitor::nsWifiMonitor()
 : mKeepGoing(true)
+, mThreadComplete(false)
 , mReentrantMonitor("nsWifiMonitor.mReentrantMonitor")
 {
 #if defined(PR_LOGGING)
   gWifiMonitorLog = PR_NewLogModule("WifiMonitor");
 #endif
 
   nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
   if (obsSvc)
@@ -49,71 +50,83 @@ nsWifiMonitor::~nsWifiMonitor()
 }
 
 NS_IMETHODIMP
 nsWifiMonitor::Observe(nsISupports *subject, const char *topic,
                      const char16_t *data)
 {
   if (!strcmp(topic, "xpcom-shutdown")) {
     LOG(("Shutting down\n"));
-    mKeepGoing = false;
 
     ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    mKeepGoing = false;
     mon.Notify();
+    mThread = nullptr;
   }
   return NS_OK;
 }
 
 
 NS_IMETHODIMP nsWifiMonitor::StartWatching(nsIWifiListener *aListener)
 {
+  LOG(("nsWifiMonitor::StartWatching %p thread %p listener %p\n",
+       this, mThread.get(), aListener));
+  MOZ_ASSERT(NS_IsMainThread());
+
   if (!aListener)
     return NS_ERROR_NULL_POINTER;
+  if (!mKeepGoing) {
+      return NS_ERROR_NOT_AVAILABLE;
+  }
 
   nsresult rv = NS_OK;
+
+  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+  if (mThreadComplete) {
+      // generally there is just one thread for the lifetime of the service,
+      // but if DoScan returns with an error before shutdown (i.e. !mKeepGoing)
+      // then we will respawn the thread.
+      LOG(("nsWifiMonitor::StartWatching %p restarting thread\n", this));
+      mThreadComplete = false;
+      mThread = nullptr;
+  }
+
   if (!mThread) {
     rv = NS_NewThread(getter_AddRefs(mThread), this);
     if (NS_FAILED(rv))
       return rv;
   }
 
-  ReentrantMonitorAutoEnter mon(mReentrantMonitor);
-
-  mKeepGoing = true;
 
   mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener)));
 
   // tell ourselves that we have a new watcher.
   mon.Notify();
   return NS_OK;
 }
 
 NS_IMETHODIMP nsWifiMonitor::StopWatching(nsIWifiListener *aListener)
 {
+  LOG(("nsWifiMonitor::StopWatching %p thread %p listener %p\n",
+       this, mThread.get(), aListener));
+  MOZ_ASSERT(NS_IsMainThread());
+
   if (!aListener)
     return NS_ERROR_NULL_POINTER;
 
-  LOG(("removing listener\n"));
-
   ReentrantMonitorAutoEnter mon(mReentrantMonitor);
 
   for (uint32_t i = 0; i < mListeners.Length(); i++) {
 
     if (mListeners[i].mListener == aListener) {
       mListeners.RemoveElementAt(i);
       break;
     }
   }
 
-  if (mListeners.Length() == 0) {
-    mKeepGoing = false;
-    mon.Notify();
-    mThread = nullptr;
-  }
-
   return NS_OK;
 }
 
 typedef nsTArray<nsMainThreadPtrHandle<nsIWifiListener> > WifiListenerArray;
 
 class nsPassErrorToWifiListeners final : public nsIRunnable
 {
  public:
@@ -146,37 +159,45 @@ NS_IMETHODIMP nsPassErrorToWifiListeners
 
 NS_IMETHODIMP nsWifiMonitor::Run()
 {
   LOG(("@@@@@ wifi monitor run called\n"));
 
   PR_SetCurrentThreadName("Wifi Monitor");
 
   nsresult rv = DoScan();
+  LOG(("@@@@@ wifi monitor run::doscan complete %x\n", rv));
 
-  if (mKeepGoing && NS_FAILED(rv)) {
-    nsAutoPtr<WifiListenerArray> currentListeners(
-                           new WifiListenerArray(mListeners.Length()));
-    if (!currentListeners)
-      return NS_ERROR_OUT_OF_MEMORY;
+  nsAutoPtr<WifiListenerArray> currentListeners;
+  bool doError = false;
 
-    for (uint32_t i = 0; i < mListeners.Length(); i++)
-      currentListeners->AppendElement(mListeners[i].mListener);
+  {
+      ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+      if (mKeepGoing && NS_FAILED(rv)) {
+          doError = true;
+          currentListeners = new WifiListenerArray(mListeners.Length());
+          for (uint32_t i = 0; i < mListeners.Length(); i++)
+              currentListeners->AppendElement(mListeners[i].mListener);
+      }
+      mThreadComplete = true;
+  }
 
+  if (doError) {
     nsCOMPtr<nsIThread> thread = do_GetMainThread();
     if (!thread)
       return NS_ERROR_UNEXPECTED;
 
     nsCOMPtr<nsIRunnable> runnable(new nsPassErrorToWifiListeners(currentListeners, rv));
     if (!runnable)
       return NS_ERROR_OUT_OF_MEMORY;
 
     thread->Dispatch(runnable, NS_DISPATCH_SYNC);
   }
 
+  LOG(("@@@@@ wifi monitor run complete\n"));
   return NS_OK;
 }
 
 class nsCallWifiListeners final : public nsIRunnable
 {
  public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIRUNNABLE
@@ -204,24 +225,22 @@ NS_IMETHODIMP nsCallWifiListeners::Run()
   }
   return NS_OK;
 }
 
 nsresult
 nsWifiMonitor::CallWifiListeners(const nsCOMArray<nsWifiAccessPoint> &aAccessPoints,
                                  bool aAccessPointsChanged)
 {
-    nsAutoPtr<WifiListenerArray> currentListeners(
-                           new WifiListenerArray(mListeners.Length()));
-    if (!currentListeners)
-      return NS_ERROR_OUT_OF_MEMORY;
-
+    nsAutoPtr<WifiListenerArray> currentListeners;
     {
       ReentrantMonitorAutoEnter mon(mReentrantMonitor);
 
+      currentListeners = new WifiListenerArray(mListeners.Length());
+
       for (uint32_t i = 0; i < mListeners.Length(); i++) {
         if (!mListeners[i].mHasSentData || aAccessPointsChanged) {
           mListeners[i].mHasSentData = true;
           currentListeners->AppendElement(mListeners[i].mListener);
         }
       }
     }
 
--- a/netwerk/wifi/nsWifiMonitor.h
+++ b/netwerk/wifi/nsWifiMonitor.h
@@ -8,16 +8,17 @@
 #include "nsIWifiMonitor.h"
 #include "nsCOMPtr.h"
 #include "nsAutoPtr.h"
 #include "nsProxyRelease.h"
 #include "nsIThread.h"
 #include "nsIRunnable.h"
 #include "nsCOMArray.h"
 #include "nsIWifiListener.h"
+#include "mozilla/Atomics.h"
 #include "mozilla/ReentrantMonitor.h"
 #include "prlog.h"
 #include "nsIObserver.h"
 #include "nsTArray.h"
 #include "nsITimer.h"
 #include "mozilla/Attributes.h"
 #include "nsIInterfaceRequestor.h"
 
@@ -63,17 +64,18 @@ class nsWifiMonitor final : nsIRunnable,
  private:
   ~nsWifiMonitor();
 
   nsresult DoScan();
 
   nsresult CallWifiListeners(const nsCOMArray<nsWifiAccessPoint> &aAccessPoints,
                              bool aAccessPointsChanged);
 
-  bool mKeepGoing;
+  mozilla::Atomic<bool> mKeepGoing;
+  mozilla::Atomic<bool> mThreadComplete;
   nsCOMPtr<nsIThread> mThread;
 
   nsTArray<nsWifiListener> mListeners;
 
   mozilla::ReentrantMonitor mReentrantMonitor;
 
 #ifdef XP_WIN
   nsAutoPtr<WinWifiScanner> mWinWifiScanner;
--- a/netwerk/wifi/nsWifiScannerWin.cpp
+++ b/netwerk/wifi/nsWifiScannerWin.cpp
@@ -53,14 +53,16 @@ nsWifiMonitor::DoScan()
 
       rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
       NS_ENSURE_SUCCESS(rv, rv);
 
       // wait for some reasonable amount of time.  pref?
       LOG(("waiting on monitor\n"));
 
       ReentrantMonitorAutoEnter mon(mReentrantMonitor);
-      mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
+      if (mKeepGoing) {
+          mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
+      }
     }
     while (mKeepGoing);
 
     return NS_OK;
 }