Bug 1072903 - TraceLogger: Part 6: Add locking logic for TraceLoggerGraph, r=bbouvier
authorHannes Verschore <hv1989@gmail.com>
Thu, 20 Nov 2014 17:44:01 +0100
changeset 247496 20dc3d4a930353882f05243e01de935774f549a4
parent 247495 aad64d6ad822f2c9a69e7fb81456161fcb590052
child 247497 2d0b50d683201ad827260b454aed1a940775f1cf
push id4489
push userraliiev@mozilla.com
push dateMon, 23 Feb 2015 15:17:55 +0000
treeherdermozilla-beta@fd7c3dc24146 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbbouvier
bugs1072903
milestone37.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 1072903 - TraceLogger: Part 6: Add locking logic for TraceLoggerGraph, r=bbouvier
js/src/vm/TraceLoggingGraph.cpp
js/src/vm/TraceLoggingGraph.h
--- a/js/src/vm/TraceLoggingGraph.cpp
+++ b/js/src/vm/TraceLoggingGraph.cpp
@@ -17,16 +17,43 @@
 #  define TRACE_LOG_DIR "/tmp/"
 # endif
 #endif
 
 using mozilla::NativeEndian;
 
 TraceLoggerGraphState traceLoggersGraph;
 
+class AutoTraceLoggerGraphStateLock
+{
+  TraceLoggerGraphState *graph;
+
+  public:
+    AutoTraceLoggerGraphStateLock(TraceLoggerGraphState *graph MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
+      : graph(graph)
+    {
+        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
+        PR_Lock(graph->lock);
+    }
+    ~AutoTraceLoggerGraphStateLock() {
+        PR_Unlock(graph->lock);
+    }
+  private:
+    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+};
+
+TraceLoggerGraphState::TraceLoggerGraphState()
+  : numLoggers(0),
+    out(nullptr)
+{
+    lock = PR_NewLock();
+    if (!lock)
+        MOZ_CRASH();
+}
+
 bool
 TraceLoggerGraphState::ensureInitialized()
 {
     if (out)
         return true;
 
     out = fopen(TRACE_LOG_DIR "tl-data.json", "w");
     if (!out)
@@ -38,22 +65,28 @@ TraceLoggerGraphState::ensureInitialized
 
 TraceLoggerGraphState::~TraceLoggerGraphState()
 {
     if (out) {
         fprintf(out, "]");
         fclose(out);
         out = nullptr;
     }
+
+    if (lock) {
+        PR_DestroyLock(lock);
+        lock = nullptr;
+    }
 }
 
 uint32_t
 TraceLoggerGraphState::nextLoggerId()
 {
-// TODO: lock
+    AutoTraceLoggerGraphStateLock lock(this);
+
     if (!ensureInitialized()) {
         fprintf(stderr, "TraceLogging: Couldn't create the main log file.");
         return uint32_t(-1);
     }
 
     if (numLoggers > 999) {
         fprintf(stderr, "TraceLogging: Can't create more than 999 different loggers.");
         return uint32_t(-1);
--- a/js/src/vm/TraceLoggingGraph.h
+++ b/js/src/vm/TraceLoggingGraph.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 TraceLoggingGraph_h
 #define TraceLoggingGraph_h
 
 #include "mozilla/DebugOnly.h"
 
+#include "jslock.h"
+
 #include "js/TypeDecls.h"
 #include "vm/TraceLoggingTypes.h"
 
 /*
  * The output of a tracelogging session is saved in /tmp/tl-data.json.
  * The format of that file is a JS array per tracelogger (=thread), with a map
  * containing:
  *  - dict:   Name of the file containing a json table with the log text.
@@ -63,20 +65,20 @@
 class TraceLoggerGraphState
 {
     uint32_t numLoggers;
 
     // File pointer to the "tl-data.json" file. (Explained above).
     FILE *out;
 
   public:
-    TraceLoggerGraphState()
-      : numLoggers(0),
-        out(nullptr)
-    { }
+    PRLock *lock;
+
+  public:
+    TraceLoggerGraphState();
     ~TraceLoggerGraphState();
 
     uint32_t nextLoggerId();
 
   private:
     bool ensureInitialized();
 };