Bug 1186750 part 7 - Convert DispatchToOwningThread and DispatchOrAbandon to take already_AddRefed&&. r=dhylands
authorXidorn Quan <quanxunzhen@gmail.com>
Fri, 20 Nov 2015 18:47:49 +1100
changeset 273515 cbd3f6d49362e867b8e9e9bdfea3a18c970d3a6b
parent 273514 dc63519e3cae242606a1009f707af3d0f6943344
child 273516 ebb8a91b830ab636683df1aa684f002caef9438e
push id29705
push userkwierso@gmail.com
push dateFri, 20 Nov 2015 22:36:48 +0000
treeherdermozilla-central@e3d9cf3dc326 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands
bugs1186750
milestone45.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 1186750 part 7 - Convert DispatchToOwningThread and DispatchOrAbandon to take already_AddRefed&&. r=dhylands
dom/devicestorage/DeviceStorage.h
dom/devicestorage/nsDeviceStorage.cpp
dom/devicestorage/nsDeviceStorage.h
--- a/dom/devicestorage/DeviceStorage.h
+++ b/dom/devicestorage/DeviceStorage.h
@@ -305,17 +305,17 @@ public:
                            nsDOMDeviceStorageCursor** aCursor,
                            ErrorResult& aRv);
   already_AddRefed<DOMRequest> CreateAndRejectDOMRequest(const char *aReason,
                                                          ErrorResult& aRv);
 
   nsresult CheckPermission(already_AddRefed<DeviceStorageRequest>&& aRequest);
 
   bool IsOwningThread();
-  nsresult DispatchToOwningThread(nsIRunnable* aRunnable);
+  nsresult DispatchToOwningThread(already_AddRefed<nsIRunnable>&& aRunnable);
 
 private:
   ~nsDOMDeviceStorage();
 
   static nsresult CheckPrincipal(nsPIDOMWindow* aWindow, bool aIsAppsStorage,
                                  nsIPrincipal** aPrincipal);
 
   already_AddRefed<DOMRequest>
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -2590,19 +2590,20 @@ bool
 nsDOMDeviceStorage::IsOwningThread()
 {
   bool owner = false;
   mOwningThread->IsOnCurrentThread(&owner);
   return owner;
 }
 
 nsresult
-nsDOMDeviceStorage::DispatchToOwningThread(nsIRunnable* aRunnable)
+nsDOMDeviceStorage::DispatchToOwningThread(
+  already_AddRefed<nsIRunnable>&& aRunnable)
 {
-  return mOwningThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
+  return mOwningThread->Dispatch(Move(aRunnable), NS_DISPATCH_NORMAL);
 }
 
 /* virtual */ JSObject*
 nsDOMDeviceStorage::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return DeviceStorageBinding::Wrap(aCx, this, aGivenProto);
 }
 
@@ -3749,36 +3750,38 @@ bool
 DeviceStorageRequestManager::IsOwningThread()
 {
   bool owner = false;
   mOwningThread->IsOnCurrentThread(&owner);
   return owner;
 }
 
 nsresult
-DeviceStorageRequestManager::DispatchToOwningThread(nsIRunnable* aRunnable)
+DeviceStorageRequestManager::DispatchToOwningThread(
+  already_AddRefed<nsIRunnable>&& aRunnable)
 {
-  return mOwningThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
+  return mOwningThread->Dispatch(Move(aRunnable), NS_DISPATCH_NORMAL);
 }
 
 nsresult
-DeviceStorageRequestManager::DispatchOrAbandon(uint32_t aId,
-                                               nsIRunnable* aRunnable)
+DeviceStorageRequestManager::DispatchOrAbandon(
+  uint32_t aId, already_AddRefed<nsIRunnable>&& aRunnable)
 {
   MutexAutoLock lock(mMutex);
   if (mShutdown) {
     /* Dispatching in this situation may result in the runnable being
        silently discarded but not freed. The runnables themselves are
        safe to be freed off the owner thread but the dispatch method
        does not know that. */
     DS_LOG_DEBUG("shutdown %u", aId);
+    nsCOMPtr<nsIRunnable> runnable(aRunnable);
     return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
   }
 
-  nsresult rv = DispatchToOwningThread(aRunnable);
+  nsresult rv = DispatchToOwningThread(Move(aRunnable));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     DS_LOG_ERROR("abandon %u", aId);
   }
   return rv;
 }
 
 uint32_t
 DeviceStorageRequestManager::Create(nsDOMDeviceStorage* aDeviceStorage,
@@ -3835,17 +3838,17 @@ DeviceStorageRequestManager::Resolve(uin
 {
   if (aForceDispatch || !IsOwningThread()) {
     DS_LOG_DEBUG("recv %u", aId);
     RefPtr<DeviceStorageRequestManager> self = this;
     nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([self, aId] () -> void
     {
       self->Resolve(aId, false);
     });
-    return DispatchOrAbandon(aId, r);
+    return DispatchOrAbandon(aId, r.forget());
   }
 
   DS_LOG_INFO("posted %u", aId);
 
   if (NS_WARN_IF(aId == INVALID_ID)) {
     DS_LOG_ERROR("invalid");
     MOZ_ASSERT_UNREACHABLE("resolve invalid request");
     return NS_OK;
@@ -3867,17 +3870,17 @@ DeviceStorageRequestManager::Resolve(uin
     DS_LOG_DEBUG("recv %u", aId);
     RefPtr<DeviceStorageRequestManager> self = this;
     nsString result = aResult;
     nsCOMPtr<nsIRunnable> r
       = NS_NewRunnableFunction([self, aId, result] () -> void
     {
       self->Resolve(aId, result, false);
     });
-    return DispatchOrAbandon(aId, r);
+    return DispatchOrAbandon(aId, r.forget());
   }
 
   DS_LOG_INFO("posted %u w/ %s", aId,
               NS_LossyConvertUTF16toASCII(aResult).get());
 
   if (NS_WARN_IF(aId == INVALID_ID)) {
     DS_LOG_WARN("invalid");
     MOZ_ASSERT_UNREACHABLE("resolve invalid request");
@@ -3912,17 +3915,17 @@ DeviceStorageRequestManager::Resolve(uin
   if (aForceDispatch || !IsOwningThread()) {
     DS_LOG_DEBUG("recv %u w/ %" PRIu64, aId, aValue);
     RefPtr<DeviceStorageRequestManager> self = this;
     nsCOMPtr<nsIRunnable> r
       = NS_NewRunnableFunction([self, aId, aValue] () -> void
     {
       self->Resolve(aId, aValue, false);
     });
-    return DispatchOrAbandon(aId, r);
+    return DispatchOrAbandon(aId, r.forget());
   }
 
   DS_LOG_INFO("posted %u w/ %" PRIu64, aId, aValue);
 
   if (NS_WARN_IF(aId == INVALID_ID)) {
     DS_LOG_ERROR("invalid");
     MOZ_ASSERT_UNREACHABLE("resolve invalid request");
     return NS_OK;
@@ -3992,17 +3995,17 @@ DeviceStorageRequestManager::Resolve(uin
     DS_LOG_DEBUG("recv %u w/ %p", aId, aBlobImpl);
     RefPtr<DeviceStorageRequestManager> self = this;
     RefPtr<BlobImpl> blobImpl = aBlobImpl;
     nsCOMPtr<nsIRunnable> r
       = NS_NewRunnableFunction([self, aId, blobImpl] () -> void
     {
       self->Resolve(aId, blobImpl, false);
     });
-    return DispatchOrAbandon(aId, r);
+    return DispatchOrAbandon(aId, r.forget());
   }
 
   DS_LOG_INFO("posted %u w/ %p", aId, aBlobImpl);
 
   if (NS_WARN_IF(aId == INVALID_ID)) {
     DS_LOG_ERROR("invalid");
     MOZ_ASSERT_UNREACHABLE("resolve invalid request");
     return NS_OK;
@@ -4120,17 +4123,17 @@ DeviceStorageRequestManager::Reject(uint
 
     ListIndex i = self->Find(aId);
     if (NS_WARN_IF(i == self->mPending.Length())) {
       return;
     }
 
     self->RejectInternal(i, reason);
   });
-  return DispatchOrAbandon(aId, r);
+  return DispatchOrAbandon(aId, r.forget());
 }
 
 nsresult
 DeviceStorageRequestManager::Reject(uint32_t aId, const char* aReason)
 {
   nsString reason;
   reason.AssignASCII(aReason);
   return Reject(aId, reason);
--- a/dom/devicestorage/nsDeviceStorage.h
+++ b/dom/devicestorage/nsDeviceStorage.h
@@ -240,17 +240,17 @@ class DeviceStorageRequestManager final
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeviceStorageRequestManager)
 
   static const uint32_t INVALID_ID = 0;
 
   DeviceStorageRequestManager();
 
   bool IsOwningThread();
-  nsresult DispatchToOwningThread(nsIRunnable* aRunnable);
+  nsresult DispatchToOwningThread(already_AddRefed<nsIRunnable>&& aRunnable);
 
   void StorePermission(size_t aAccess, bool aAllow);
   uint32_t CheckPermission(size_t aAccess);
 
   /* These must be called on the owning thread context of the device
      storage object. It will hold onto a device storage reference until
      all of the pending requests are completed or shutdown is called. */
   uint32_t Create(nsDOMDeviceStorage* aDeviceStorage,
@@ -285,17 +285,18 @@ private:
 
   typedef nsTArray<ListEntry> ListType;
   typedef ListType::index_type ListIndex;
 
   virtual ~DeviceStorageRequestManager();
   uint32_t CreateInternal(mozilla::dom::DOMRequest* aRequest, bool aCursor);
   nsresult ResolveInternal(ListIndex aIndex, JS::HandleValue aResult);
   nsresult RejectInternal(ListIndex aIndex, const nsString& aReason);
-  nsresult DispatchOrAbandon(uint32_t aId, nsIRunnable* aRunnable);
+  nsresult DispatchOrAbandon(uint32_t aId,
+                             already_AddRefed<nsIRunnable>&& aRunnable);
   ListType::index_type Find(uint32_t aId);
 
   nsCOMPtr<nsIThread> mOwningThread;
   ListType mPending; // owning thread or destructor only
 
   mozilla::Mutex mMutex;
   uint32_t mPermissionCache[DEVICE_STORAGE_ACCESS_COUNT];
   bool mShutdown;