1 //=== llvm/Support/Unix/ThreadLocal.inc - Unix Thread Local Data -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific (non-pthread) ThreadLocal class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
19 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
28 ThreadLocalImpl::ThreadLocalImpl() : data() {
29 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
30 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
31 int errorcode = pthread_key_create(key, nullptr);
32 assert(errorcode == 0);
36 ThreadLocalImpl::~ThreadLocalImpl() {
37 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
38 int errorcode = pthread_key_delete(*key);
39 assert(errorcode == 0);
43 void ThreadLocalImpl::setInstance(const void* d) {
44 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
45 int errorcode = pthread_setspecific(*key, d);
46 assert(errorcode == 0);
50 void *ThreadLocalImpl::getInstance() {
51 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
52 return pthread_getspecific(*key);
55 void ThreadLocalImpl::removeInstance() {
63 ThreadLocalImpl::ThreadLocalImpl() : data() { }
64 ThreadLocalImpl::~ThreadLocalImpl() { }
65 void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
66 void *ThreadLocalImpl::getInstance() { return data; }
67 void ThreadLocalImpl::removeInstance() { setInstance(0); }