Skip to content

Commit

Permalink
Remove home-grown thread-local storage wrappers
Browse files Browse the repository at this point in the history
Summary:
Use c++11 thread_local variables instead. As far as I am aware, they are
supported by all compilers/targets we care about.

Reviewers: zturner, jingham

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D34274

llvm-svn: 305779
  • Loading branch information
labath committed Jun 20, 2017
1 parent 6c5c542 commit f891812
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 71 deletions.
9 changes: 0 additions & 9 deletions lldb/include/lldb/Host/Host.h
Expand Up @@ -132,15 +132,6 @@ class Host {

static const char *GetSignalAsCString(int signo);

typedef void (*ThreadLocalStorageCleanupCallback)(void *p);

static lldb::thread_key_t
ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback);

static void *ThreadLocalStorageGet(lldb::thread_key_t key);

static void ThreadLocalStorageSet(lldb::thread_key_t key, void *value);

//------------------------------------------------------------------
/// Given an address in the current process (the process that
/// is running the LLDB code), return the name of the module that
Expand Down
44 changes: 14 additions & 30 deletions lldb/source/Core/Timer.cpp
Expand Up @@ -38,20 +38,9 @@ static std::mutex &GetFileMutex() {
return *g_file_mutex_ptr;
}

static void ThreadSpecificCleanup(void *p) {
delete static_cast<TimerStack *>(p);
}

static TimerStack *GetTimerStackForCurrentThread() {
static lldb::thread_key_t g_key =
Host::ThreadLocalStorageCreate(ThreadSpecificCleanup);

void *timer_stack = Host::ThreadLocalStorageGet(g_key);
if (timer_stack == NULL) {
Host::ThreadLocalStorageSet(g_key, new TimerStack);
timer_stack = Host::ThreadLocalStorageGet(g_key);
}
return (TimerStack *)timer_stack;
static TimerStack &GetTimerStackForCurrentThread() {
static thread_local TimerStack g_stack;
return g_stack;
}

Timer::Category::Category(const char *cat) : m_name(cat) {
Expand All @@ -66,16 +55,14 @@ void Timer::SetQuiet(bool value) { g_quiet = value; }

Timer::Timer(Timer::Category &category, const char *format, ...)
: m_category(category), m_total_start(std::chrono::steady_clock::now()) {
TimerStack *stack = GetTimerStackForCurrentThread();
if (!stack)
return;
TimerStack &stack = GetTimerStackForCurrentThread();

stack->push_back(this);
if (g_quiet && stack->size() <= g_display_depth) {
stack.push_back(this);
if (g_quiet && stack.size() <= g_display_depth) {
std::lock_guard<std::mutex> lock(GetFileMutex());

// Indent
::fprintf(stdout, "%*s", int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "");
::fprintf(stdout, "%*s", int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "");
// Print formatted string
va_list args;
va_start(args, format);
Expand All @@ -90,26 +77,23 @@ Timer::Timer(Timer::Category &category, const char *format, ...)
Timer::~Timer() {
using namespace std::chrono;

TimerStack *stack = GetTimerStackForCurrentThread();
if (!stack)
return;

auto stop_time = steady_clock::now();
auto total_dur = stop_time - m_total_start;
auto timer_dur = total_dur - m_child_duration;

if (g_quiet && stack->size() <= g_display_depth) {
TimerStack &stack = GetTimerStackForCurrentThread();
if (g_quiet && stack.size() <= g_display_depth) {
std::lock_guard<std::mutex> lock(GetFileMutex());
::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n",
int(stack->size() - 1) * TIMER_INDENT_AMOUNT, "",
int(stack.size() - 1) * TIMER_INDENT_AMOUNT, "",
duration<double>(total_dur).count(),
duration<double>(timer_dur).count());
}

assert(stack->back() == this);
stack->pop_back();
if (!stack->empty())
stack->back()->ChildDuration(total_dur);
assert(stack.back() == this);
stack.pop_back();
if (!stack.empty())
stack.back()->ChildDuration(total_dur);

// Keep total results for each category so we can dump results.
m_category.m_nanos += std::chrono::nanoseconds(timer_dur).count();
Expand Down
19 changes: 0 additions & 19 deletions lldb/source/Host/common/Host.cpp
Expand Up @@ -406,25 +406,6 @@ const char *Host::GetSignalAsCString(int signo) {

#endif

#ifndef _WIN32

lldb::thread_key_t
Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) {
pthread_key_t key;
::pthread_key_create(&key, callback);
return key;
}

void *Host::ThreadLocalStorageGet(lldb::thread_key_t key) {
return ::pthread_getspecific(key);
}

void Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) {
::pthread_setspecific(key, value);
}

#endif

#if !defined(__APPLE__) // see Host.mm

bool Host::GetBundleDirectory(const FileSpec &file, FileSpec &bundle) {
Expand Down
13 changes: 0 additions & 13 deletions lldb/source/Host/windows/Host.cpp
Expand Up @@ -101,19 +101,6 @@ lldb::thread_t Host::GetCurrentThread() {
return lldb::thread_t(::GetCurrentThread());
}

lldb::thread_key_t
Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) {
return TlsAlloc();
}

void *Host::ThreadLocalStorageGet(lldb::thread_key_t key) {
return ::TlsGetValue(key);
}

void Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) {
::TlsSetValue(key, value);
}

void Host::Kill(lldb::pid_t pid, int signo) {
TerminateProcess((HANDLE)pid, 1);
}
Expand Down

0 comments on commit f891812

Please sign in to comment.