1 //===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the llvm::sys::ThreadLocal class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Config/config.h" 15 #include "llvm/Support/ThreadLocal.h" 16 17 //===----------------------------------------------------------------------===// 18 //=== WARNING: Implementation here must contain only TRULY operating system 19 //=== independent code. 20 //===----------------------------------------------------------------------===// 21 22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 23 // Define all methods as no-ops if threading is explicitly disabled 24 namespace llvm { 25 using namespace sys; ThreadLocalImpl()26ThreadLocalImpl::ThreadLocalImpl() { } ~ThreadLocalImpl()27ThreadLocalImpl::~ThreadLocalImpl() { } setInstance(const void * d)28void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);} getInstance()29const void* ThreadLocalImpl::getInstance() { return data; } removeInstance()30void ThreadLocalImpl::removeInstance() { data = 0; } 31 } 32 #else 33 34 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC) 35 36 #include <cassert> 37 #include <pthread.h> 38 #include <stdlib.h> 39 40 namespace llvm { 41 using namespace sys; 42 ThreadLocalImpl()43ThreadLocalImpl::ThreadLocalImpl() : data(0) { 44 pthread_key_t* key = new pthread_key_t; 45 int errorcode = pthread_key_create(key, NULL); 46 assert(errorcode == 0); 47 (void) errorcode; 48 data = (void*)key; 49 } 50 ~ThreadLocalImpl()51ThreadLocalImpl::~ThreadLocalImpl() { 52 pthread_key_t* key = static_cast<pthread_key_t*>(data); 53 int errorcode = pthread_key_delete(*key); 54 assert(errorcode == 0); 55 (void) errorcode; 56 delete key; 57 } 58 setInstance(const void * d)59void ThreadLocalImpl::setInstance(const void* d) { 60 pthread_key_t* key = static_cast<pthread_key_t*>(data); 61 int errorcode = pthread_setspecific(*key, d); 62 assert(errorcode == 0); 63 (void) errorcode; 64 } 65 getInstance()66const void* ThreadLocalImpl::getInstance() { 67 pthread_key_t* key = static_cast<pthread_key_t*>(data); 68 return pthread_getspecific(*key); 69 } 70 removeInstance()71void ThreadLocalImpl::removeInstance() { 72 setInstance(0); 73 } 74 75 } 76 77 #elif defined(LLVM_ON_UNIX) 78 #include "Unix/ThreadLocal.inc" 79 #elif defined( LLVM_ON_WIN32) 80 #include "Windows/ThreadLocal.inc" 81 #else 82 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp 83 #endif 84 #endif 85