From dad2946b51de3226184ca0aff641537f3e532857 Mon Sep 17 00:00:00 2001 From: Dean Michael Berris Date: Tue, 3 Oct 2017 06:11:20 +0000 Subject: fixup: use UNUSED, restore alignment for cache-line friendliness, and report on errors found when pthread_create_key fails git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@314765 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/xray/xray_fdr_logging_impl.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/xray/xray_fdr_logging_impl.h b/lib/xray/xray_fdr_logging_impl.h index 28e70f891..aec205e8a 100644 --- a/lib/xray/xray_fdr_logging_impl.h +++ b/lib/xray/xray_fdr_logging_impl.h @@ -93,8 +93,10 @@ static void writeEOBMetadata(); static void writeTSCWrapMetadata(uint64_t TSC); // Group together thread-local-data in a struct, then hide it behind a function -// call so that it can be initialized on first use instead of as a global. -struct ThreadLocalData { +// call so that it can be initialized on first use instead of as a global. We +// force the alignment to 64-bytes for x86 cache line alignment, as this +// structure is used in the hot path of implementation. +struct ALIGNED(64) ThreadLocalData { BufferQueue::Buffer Buffer; char *RecordPtr = nullptr; // The number of FunctionEntry records immediately preceding RecordPtr. @@ -174,12 +176,13 @@ static ThreadLocalData &getThreadLocalData() { // We need aligned, uninitialized storage for the TLS object which is // trivially destructible. We're going to use this as raw storage and // placement-new the ThreadLocalData object into it later. - thread_local std::aligned_union<1, ThreadLocalData>::type TLSBuffer; + thread_local std::aligned_storage::type TLSBuffer; // Ensure that we only actually ever do the pthread initialization once. - thread_local bool unused = [] { + thread_local bool UNUSED Unused = [] { new (&TLSBuffer) ThreadLocalData(); - pthread_key_create(&key, +[](void *) { + auto result = pthread_key_create(&key, +[](void *) { auto &TLD = *reinterpret_cast(&TLSBuffer); auto &RecordPtr = TLD.RecordPtr; auto &Buffers = TLD.LocalBQ; @@ -203,10 +206,14 @@ static ThreadLocalData &getThreadLocalData() { return; } }); + if (result != 0) { + Report("Failed to allocate thread-local data through pthread; error=%d", + result); + return false; + } pthread_setspecific(key, &TLSBuffer); return true; }(); - (void)unused; return *reinterpret_cast(&TLSBuffer); } -- cgit v1.2.3