Bug 1321617 - Reduce the scope of the lock in profiler_get_profile. r?njn draft
authorMarkus Stange <mstange@themasta.com>
Sat, 08 Apr 2017 21:33:32 -0400
changeset 559165 6860c3092eaefbcbf96dd5b2e80991c3087249a6
parent 559164 a579a9e7e6a76ecf0a6e948f45a974fed5ed4dd5
child 559166 543b4a5112b0fbf25f7ea5f13c7f5c46952f3bbf
push id53008
push userbmo:mstange@themasta.com
push dateSun, 09 Apr 2017 03:27:44 +0000
reviewersnjn
bugs1321617
milestone55.0a1
Bug 1321617 - Reduce the scope of the lock in profiler_get_profile. r?njn MozReview-Commit-ID: 5OmVMwy1yMl
tools/profiler/core/platform.cpp
--- a/tools/profiler/core/platform.cpp
+++ b/tools/profiler/core/platform.cpp
@@ -1415,16 +1415,34 @@ locked_profiler_stream_json_for_this_pro
     }
 #endif
 
     gPS->SetIsPaused(aLock, false);
   }
   aWriter.EndArray();
 }
 
+static bool
+profiler_stream_json_for_this_process(SpliceableJSONWriter& aWriter, double aSinceTime)
+{
+  LOG("locked_profiler_stream_json_for_this_process");
+
+  MOZ_RELEASE_ASSERT(NS_IsMainThread());
+  MOZ_RELEASE_ASSERT(gPS);
+
+  PS::AutoLock lock(gPSMutex);
+
+  if (!gPS->IsActive(lock)) {
+    return false;
+  }
+
+  locked_profiler_stream_json_for_this_process(lock, aWriter, aSinceTime);
+  return true;
+}
+
 // END saving/streaming code
 ////////////////////////////////////////////////////////////////////////
 
 ProfilerMarker::ProfilerMarker(const char* aMarkerName,
                                ProfilerMarkerPayload* aPayload,
                                double aTime)
   : mMarkerName(strdup(aMarkerName))
   , mPayload(aPayload)
@@ -2093,32 +2111,24 @@ profiler_shutdown()
 UniquePtr<char[]>
 profiler_get_profile(double aSinceTime)
 {
   LOG("profiler_get_profile");
 
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   MOZ_RELEASE_ASSERT(gPS);
 
-  PS::AutoLock lock(gPSMutex);
-
-  if (!gPS->IsActive(lock)) {
-    return nullptr;
-  }
-
   SpliceableChunkedJSONWriter b;
   b.Start(SpliceableJSONWriter::SingleLineStyle);
   {
-    locked_profiler_stream_json_for_this_process(lock, b, aSinceTime);
+    if (!profiler_stream_json_for_this_process(b, aSinceTime)) {
+      return nullptr;
+    }
 
     b.StartArrayProperty("processes");
-    // When notifying observers in other places in this file we are careful
-    // to do it when gPSMutex is unlocked, to avoid deadlocks. But that's not
-    // necessary here, because "profiler-subprocess" observers just call back
-    // into SubprocessCallback, which is simple and doesn't lock gPSMutex.
     if (CanNotifyObservers()) {
       // Send a event asking any subprocesses (plugins) to
       // give us their information
       SubprocessClosure closure(&b);
       nsCOMPtr<nsIObserverService> os =
         mozilla::services::GetObserverService();
       if (os) {
         RefPtr<ProfileSaveEvent> pse =