#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"
#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;
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) {
* 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();
}