Skip to content

Commit

Permalink
Use pthread instead of thread_local
Browse files Browse the repository at this point in the history
Some buggy versions of Android NDK don't support thread_local properly (see android/ndk#8). So use pthreads here.
  • Loading branch information
kwasimensah committed Nov 22, 2017
1 parent f681549 commit 13db535
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions profiling/instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ using ::uintptr_t;
#include <set>
#endif

// We should always use C++11 thread_local; unfortunately that
// isn't fully supported on Apple yet.
#ifdef __APPLE__
#define GEMMLOWP_THREAD_LOCAL static __thread
#define GEMMLOWP_USING_OLD_THREAD_LOCAL
#else
#define GEMMLOWP_THREAD_LOCAL thread_local
#endif

namespace gemmlowp {

inline void ReleaseBuildAssertion(bool condition, const char* msg) {
Expand Down Expand Up @@ -184,18 +175,22 @@ struct ThreadInfo {
};

inline ThreadInfo& ThreadLocalThreadInfo() {
#ifdef GEMMLOWP_USING_OLD_THREAD_LOCAL
// We're leaking this ThreadInfo structure, because Apple doesn't support
// non-trivial constructors or destructors for their __thread type modifier.
GEMMLOWP_THREAD_LOCAL ThreadInfo* i = nullptr;
if (i == nullptr) {
i = new ThreadInfo();
static pthread_key_t key;
static auto DeleteThreadInfo = [](void* threadInfoPtr) {
ThreadInfo* threadInfo = static_cast<ThreadInfo*>(threadInfoPtr);
if (threadInfo) {
delete threadInfo;
}
};

static int key_result = pthread_key_create(&key, DeleteThreadInfo);

ThreadInfo* threadInfo = static_cast<ThreadInfo*>(pthread_getspecific(key));
if (!threadInfo) {
threadInfo = new ThreadInfo();
pthread_setspecific(key, threadInfo);
}
return *i;
#else
GEMMLOWP_THREAD_LOCAL ThreadInfo i;
return i;
#endif
return *threadInfo;
}

// ScopedProfilingLabel is how one instruments code for profiling
Expand Down

0 comments on commit 13db535

Please sign in to comment.