]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
Use thread local storage to specify unique and low integer thread ids.
[apitrace] / common / trace_writer_local.cpp
index bb06d90f74783f53f9d7bed795811fc7ac9fa251..77c8bd67de3785db908e02a7dbf574f2385f0167 100644 (file)
@@ -124,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;
@@ -132,9 +135,18 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
         open();
     }
 
-    os::thread::id id = os::this_thread::get_id();
+    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, static_cast<unsigned>(id));
+    return Writer::beginEnter(sig, thread_id);
 }
 
 void LocalWriter::endEnter(void) {