[asan] move TSD code into asan_posix.cc

llvm-svn: 147913
This commit is contained in:
Kostya Serebryany 2012-01-11 02:21:06 +00:00
parent 8b1d023a4a
commit 1dd51b53b2
4 changed files with 37 additions and 24 deletions

View File

@ -108,6 +108,12 @@ int AsanClose(int fd);
bool AsanInterceptsSignal(int signum);
void InstallSignalHandlers();
int GetPid();
uintptr_t GetThreadSelf();
// Wrapper for TLS/TSD.
void AsanTSDInit();
void *AsanTSDGet();
void AsanTSDSet(void *tsd);
// Opens the file 'file_name" and reads up to 'max_len' bytes.
// The resulting buffer is mmaped and stored in '*buff'.

View File

@ -18,6 +18,7 @@
#include "asan_stack.h"
#include "asan_thread_registry.h"
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
@ -72,6 +73,30 @@ int GetPid() {
return getpid();
}
uintptr_t GetThreadSelf() {
return (uintptr_t)pthread_self();
}
// ---------------------- TSD ---------------- {{{1
static pthread_key_t tsd_key;
static bool tsd_key_inited = false;
void AsanTSDInit() {
CHECK(!tsd_key_inited);
tsd_key_inited = true;
CHECK(0 == pthread_key_create(&tsd_key, 0));
}
void *AsanTSDGet() {
CHECK(tsd_key_inited);
return pthread_getspecific(tsd_key);
}
void AsanTSDSet(void *tsd) {
CHECK(tsd_key_inited);
pthread_setspecific(tsd_key, tsd);
}
} // namespace __asan
#endif // __linux__ || __APPLE_

View File

@ -17,9 +17,6 @@
#include "asan_thread.h"
#include "asan_thread_registry.h"
#include <limits.h>
#include <pthread.h>
namespace __asan {
static AsanThreadRegistry asan_thread_registry(__asan::LINKER_INITIALIZED);
@ -35,8 +32,7 @@ AsanThreadRegistry::AsanThreadRegistry(LinkerInitialized x)
mu_(x) { }
void AsanThreadRegistry::Init() {
CHECK(0 == pthread_key_create(&tls_key_, 0));
tls_key_created_ = true;
AsanTSDInit();
main_thread_.set_summary(&main_thread_summary_);
main_thread_summary_.set_thread(&main_thread_);
SetCurrent(&main_thread_);
@ -70,9 +66,7 @@ AsanThread *AsanThreadRegistry::GetMain() {
}
AsanThread *AsanThreadRegistry::GetCurrent() {
CHECK(tls_key_created_);
AsanThreadSummary *summary =
(AsanThreadSummary *)pthread_getspecific(tls_key_);
AsanThreadSummary *summary = (AsanThreadSummary *)AsanTSDGet();
if (!summary) return 0;
return summary->thread();
}
@ -80,17 +74,12 @@ AsanThread *AsanThreadRegistry::GetCurrent() {
void AsanThreadRegistry::SetCurrent(AsanThread *t) {
CHECK(t->summary());
if (FLAG_v >= 2) {
Report("SetCurrent: %p for thread %p\n", t->summary(), pthread_self());
Report("SetCurrent: %p for thread %p\n", t->summary(), GetThreadSelf());
}
// Make sure we do not reset the current AsanThread.
intptr_t old_key = (intptr_t)pthread_getspecific(tls_key_);
CHECK(!old_key);
CHECK(0 == pthread_setspecific(tls_key_, t->summary()));
CHECK(pthread_getspecific(tls_key_) == t->summary());
}
pthread_key_t AsanThreadRegistry::GetTlsKey() {
return tls_key_;
CHECK(AsanTSDGet() == 0);
AsanTSDSet(t->summary());
CHECK(AsanTSDGet() == t->summary());
}
AsanStats &AsanThreadRegistry::GetCurrentThreadStats() {

View File

@ -38,7 +38,6 @@ class AsanThreadRegistry {
// Get the current thread. May return NULL.
AsanThread *GetCurrent();
void SetCurrent(AsanThread *t);
pthread_key_t GetTlsKey();
int GetCurrentTidOrMinusOne() {
AsanThread *t = GetCurrent();
@ -71,12 +70,6 @@ class AsanThreadRegistry {
AsanStats accumulated_stats_;
int n_threads_;
AsanLock mu_;
// For each thread tls_key_ stores the pointer to the corresponding
// AsanThread.
pthread_key_t tls_key_;
// This flag is updated only once at program startup, and then read
// by concurrent threads.
bool tls_key_created_;
};
// Returns a single instance of registry.