Bug 1200310 - Instead of thread guard flags for 'is stumbling' AND 'is uploading', we can use just one flag. r=jdm
authorGarvan Keeley <gkeeley@mozilla.com>
Tue, 08 Sep 2015 21:20:56 -0400
changeset 294065 b542d5f36d6bb87025b0287ed89c7f9edb83ff0c
parent 294064 9838257c96b139b41678757f0ec118b74f9f174f
child 294066 d7edc758649a9a5a43fbe038b2e190dca4072f25
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs1200310
milestone43.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 1200310 - Instead of thread guard flags for 'is stumbling' AND 'is uploading', we can use just one flag. r=jdm
dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp
dom/system/gonk/mozstumbler/WriteStumbleOnThread.h
--- a/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp
+++ b/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp
@@ -12,17 +12,16 @@
 #include "nsIFileStreams.h"
 #include "nsIInputStream.h"
 #include "nsPrintfCString.h"
 
 #define MAXFILESIZE_KB (15 * 1024)
 #define ONEDAY_IN_MSEC (24 * 60 * 60 * 1000)
 #define MAX_UPLOAD_ATTEMPTS 20
 
-mozilla::Atomic<bool> WriteStumbleOnThread::sIsUploading(false);
 mozilla::Atomic<bool> WriteStumbleOnThread::sIsAlreadyRunning(false);
 WriteStumbleOnThread::UploadFreqGuard WriteStumbleOnThread::sUploadFreqGuard = {0};
 
 #define FILENAME_INPROGRESS NS_LITERAL_CSTRING("stumbles.json.gz")
 #define FILENAME_COMPLETED NS_LITERAL_CSTRING("stumbles.done.json.gz")
 #define OUTPUT_DIR NS_LITERAL_CSTRING("mozstumbler")
 
 class DeleteRunnable : public nsRunnable
@@ -37,29 +36,29 @@ class DeleteRunnable : public nsRunnable
       nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED,
                                               getter_AddRefs(tmpFile),
                                               OUTPUT_DIR,
                                               nsDumpUtils::CREATE);
       if (NS_SUCCEEDED(rv)) {
         tmpFile->Remove(true);
       }
       // critically, this sets this flag to false so writing can happen again
-      WriteStumbleOnThread::sIsUploading = false;
+      WriteStumbleOnThread::sIsAlreadyRunning = false;
       return NS_OK;
     }
 
   private:
     ~DeleteRunnable() {}
 };
 
 void
 WriteStumbleOnThread::UploadEnded(bool deleteUploadFile)
 {
   if (!deleteUploadFile) {
-    sIsUploading = false;
+    sIsAlreadyRunning = false;
     return;
   }
 
   nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
   MOZ_ASSERT(target);
   nsCOMPtr<nsIRunnable> event = new DeleteRunnable();
   target->Dispatch(event, NS_DISPATCH_NORMAL);
 }
@@ -191,16 +190,17 @@ WriteStumbleOnThread::Run()
     return NS_OK;
   }
 
   UploadFileStatus status = GetUploadFileStatus();
 
   if (UploadFileStatus::NoFile != status) {
     if (UploadFileStatus::ExistsAndReadyToUpload == status) {
       Upload();
+      return NS_OK;
     }
   } else {
     Partition partition = GetWritePosition();
     if (partition == Partition::Unknown) {
       STUMBLER_ERR("GetWritePosition failed, skip once");
     } else {
       WriteJSON(partition);
     }
@@ -245,55 +245,50 @@ WriteStumbleOnThread::GetUploadFileStatu
   return UploadFileStatus::Exists;
 }
 
 void
 WriteStumbleOnThread::Upload()
 {
   MOZ_ASSERT(!NS_IsMainThread());
 
-  bool b = sIsUploading.exchange(true);
-  if (b) {
-    return;
-  }
-
   time_t seconds = time(0);
   int day = seconds / (60 * 60 * 24);
 
   if (sUploadFreqGuard.daySinceEpoch < day) {
     sUploadFreqGuard.daySinceEpoch = day;
     sUploadFreqGuard.attempts = 0;
   }
 
   sUploadFreqGuard.attempts++;
   if (sUploadFreqGuard.attempts > MAX_UPLOAD_ATTEMPTS) {
     STUMBLER_ERR("Too many upload attempts today");
-    sIsUploading = false;
+    sIsAlreadyRunning = false;
     return;
   }
 
   nsCOMPtr<nsIFile> tmpFile;
   nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED, getter_AddRefs(tmpFile),
                                           OUTPUT_DIR, nsDumpUtils::CREATE);
   int64_t fileSize;
   rv = tmpFile->GetFileSize(&fileSize);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     STUMBLER_ERR("GetFileSize failed");
-    sIsUploading = false;
+    sIsAlreadyRunning = false;
     return;
   }
 
   if (fileSize <= 0) {
-    sIsUploading = false;
+    sIsAlreadyRunning = false;
     return;
   }
 
   // prepare json into nsIInputStream
   nsCOMPtr<nsIInputStream> inStream;
   rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), tmpFile);
   if (NS_FAILED(rv)) {
-    sIsUploading = false;
+    sIsAlreadyRunning = false;
     return;
   }
 
   nsRefPtr<nsIRunnable> uploader = new UploadStumbleRunnable(inStream);
   NS_DispatchToMainThread(uploader);
 }
--- a/dom/system/gonk/mozstumbler/WriteStumbleOnThread.h
+++ b/dom/system/gonk/mozstumbler/WriteStumbleOnThread.h
@@ -4,16 +4,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef WriteStumbleOnThread_H
 #define WriteStumbleOnThread_H
 
 #include "mozilla/Atomics.h"
 
+class DeleteRunnable;
+
 /*
  This class is the entry point to stumbling, in that it
  receives the location+cell+wifi string and writes it
  to disk, or instead, it calls UploadStumbleRunnable
  to upload the data.
 
  Writes will happen until the file is a max size, then stop.
  Uploads will happen only when the file is one day old.
@@ -38,20 +40,19 @@ class WriteStumbleOnThread : public nsRu
 public:
   explicit WriteStumbleOnThread(const nsCString& aDesc)
   : mDesc(aDesc)
   {}
 
   NS_IMETHODIMP Run() override;
 
   static void UploadEnded(bool deleteUploadFile);
-  // Don't write while uploading is happening
-  static mozilla::Atomic<bool> sIsUploading;
 
 private:
+  friend class DeleteRunnable;
 
   enum class Partition {
     Begining,
     Middle,
     End,
     Unknown
   };