]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
trim: Greatly expand the list of calls considered to have no side effects
[apitrace] / common / trace_writer_local.cpp
index 52962c8b50e256adbc2841b9ad2af6c440834132..feb5c912dfd06b2a61f2a003f9f9759bc23255e0 100644 (file)
@@ -89,7 +89,11 @@ LocalWriter::open(void) {
 #endif
         process.trimDirectory();
 
-        os::String prefix = os::getCurrentDir();
+#ifdef ANDROID
+       os::String prefix = "/data";
+#else
+       os::String prefix = os::getCurrentDir();
+#endif
         prefix.join(process);
 
         for (;;) {
@@ -124,39 +128,35 @@ LocalWriter::open(void) {
 #endif
 }
 
-static unsigned next_thread_id = 0;
-static os::thread_specific_ptr<unsigned> thread_id_specific_ptr;
+static unsigned next_thread_num = 1;
+static thread_specific unsigned thread_num = 0;
 
 unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
-    os::acquireMutex();
+    mutex.lock();
     ++acquired;
 
     if (!m_file->isOpened()) {
         open();
     }
 
-    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);
+    unsigned this_thread_num = thread_num;
+    if (!this_thread_num) {
+        this_thread_num = thread_num = next_thread_num++;
     }
 
+    assert(thread_num > 0);
+    unsigned thread_id = thread_num - 1;
     return Writer::beginEnter(sig, thread_id);
 }
 
 void LocalWriter::endEnter(void) {
     Writer::endEnter();
     --acquired;
-    os::releaseMutex();
+    mutex.unlock();
 }
 
 void LocalWriter::beginLeave(unsigned call) {
-    os::acquireMutex();
+    mutex.lock();
     ++acquired;
     Writer::beginLeave(call);
 }
@@ -164,16 +164,17 @@ void LocalWriter::beginLeave(unsigned call) {
 void LocalWriter::endLeave(void) {
     Writer::endLeave();
     --acquired;
-    os::releaseMutex();
+    mutex.unlock();
 }
 
 void LocalWriter::flush(void) {
     /*
      * Do nothing if the mutex is already acquired (e.g., if a segfault happen
-     * while writing the file) to prevent dead-lock.
+     * while writing the file) as state could be inconsistent, therefore yield
+     * inconsistent trace files and/or repeated segfaults till infinity.
      */
 
-    os::acquireMutex();
+    mutex.lock();
     if (acquired) {
         os::log("apitrace: ignoring exception while tracing\n");
     } else {
@@ -184,7 +185,7 @@ void LocalWriter::flush(void) {
         }
         --acquired;
     }
-    os::releaseMutex();
+    mutex.unlock();
 }