]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
Move local writer definitions to a separate header file.
[apitrace] / common / trace_writer_local.cpp
index 0730150e0f228d5fbb0fb6d8e9d6c611d1383002..52962c8b50e256adbc2841b9ad2af6c440834132 100644 (file)
 #include <string.h>
 
 #include "os.hpp"
+#include "os_thread.hpp"
 #include "os_string.hpp"
 #include "trace_file.hpp"
-#include "trace_writer.hpp"
+#include "trace_writer_local.hpp"
 #include "trace_format.hpp"
 
 
@@ -123,6 +124,9 @@ LocalWriter::open(void) {
 #endif
 }
 
+static unsigned next_thread_id = 0;
+static os::thread_specific_ptr<unsigned> thread_id_specific_ptr;
+
 unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
     os::acquireMutex();
     ++acquired;
@@ -131,7 +135,18 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
         open();
     }
 
-    return Writer::beginEnter(sig);
+    unsigned *thread_id_ptr = thread_id_specific_ptr.get();
+    unsigned thread_id;
+    if (thread_id_ptr) {
+        thread_id = *thread_id_ptr;
+    } else {
+        thread_id = next_thread_id++;
+        thread_id_ptr = new unsigned;
+        *thread_id_ptr = thread_id;
+        thread_id_specific_ptr.reset(thread_id_ptr);
+    }
+
+    return Writer::beginEnter(sig, thread_id);
 }
 
 void LocalWriter::endEnter(void) {
@@ -158,14 +173,18 @@ void LocalWriter::flush(void) {
      * while writing the file) to prevent dead-lock.
      */
 
-    if (!acquired) {
-        os::acquireMutex();
+    os::acquireMutex();
+    if (acquired) {
+        os::log("apitrace: ignoring exception while tracing\n");
+    } else {
+        ++acquired;
         if (m_file->isOpened()) {
             os::log("apitrace: flushing trace due to an exception\n");
             m_file->flush();
         }
-        os::releaseMutex();
+        --acquired;
     }
+    os::releaseMutex();
 }