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 #include "llvm/Config/config.h"
21 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
30 ThreadLocalImpl::ThreadLocalImpl() : data() {
31 static_assert(sizeof(pthread_key_t) <= sizeof(data), "size too big");
32 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
33 int errorcode = pthread_key_create(key, nullptr);
34 assert(errorcode == 0);
38 ThreadLocalImpl::~ThreadLocalImpl() {
39 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
40 int errorcode = pthread_key_delete(*key);
41 assert(errorcode == 0);
45 void ThreadLocalImpl::setInstance(const void* d) {
46 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
47 int errorcode = pthread_setspecific(*key, d);
48 assert(errorcode == 0);
52 void *ThreadLocalImpl::getInstance() {
53 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
54 return pthread_getspecific(*key);
57 void ThreadLocalImpl::removeInstance() {
65 ThreadLocalImpl::ThreadLocalImpl() : data() { }
66 ThreadLocalImpl::~ThreadLocalImpl() { }
67 void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
68 void *ThreadLocalImpl::getInstance() { return data; }
69 void ThreadLocalImpl::removeInstance() { setInstance(0); }