//===----------------------------------------------------------------------===//
#include "llvm/Config/config.h"
+#include "llvm/Support/Compiler.h"
#include "llvm/Support/ThreadLocal.h"
//===----------------------------------------------------------------------===//
// Define all methods as no-ops if threading is explicitly disabled
namespace llvm {
using namespace sys;
-ThreadLocalImpl::ThreadLocalImpl() { }
+ThreadLocalImpl::ThreadLocalImpl() : data() { }
ThreadLocalImpl::~ThreadLocalImpl() { }
void ThreadLocalImpl::setInstance(const void* d) {
- typedef int SIZE_TOO_BIG[sizeof(d) <= sizeof(data) ? 1 : -1];
+ static_assert(sizeof(d) <= sizeof(data), "size too big");
void **pd = reinterpret_cast<void**>(&data);
*pd = const_cast<void*>(d);
}
-const void* ThreadLocalImpl::getInstance() { return data; }
-void ThreadLocalImpl::removeInstance() {
+const void* ThreadLocalImpl::getInstance() {
void **pd = reinterpret_cast<void**>(&data);
- *pd = 0;
+ return *pd;
+}
+void ThreadLocalImpl::removeInstance() {
+ setInstance(0);
}
}
#else
using namespace sys;
ThreadLocalImpl::ThreadLocalImpl() : data() {
- typedef int SIZE_TOO_BIG[sizeof(pthread_key_t) <= sizeof(data) ? 1 : -1];
+ static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
- int errorcode = pthread_key_create(key, NULL);
+ int errorcode = pthread_key_create(key, nullptr);
assert(errorcode == 0);
(void) errorcode;
}
}
void ThreadLocalImpl::removeInstance() {
- setInstance(0);
+ setInstance(nullptr);
}
}