ThreadLocal.cpp revision 226633
1218885Sdim//===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===// 2218885Sdim// 3218885Sdim// The LLVM Compiler Infrastructure 4218885Sdim// 5218885Sdim// This file is distributed under the University of Illinois Open Source 6218885Sdim// License. See LICENSE.TXT for details. 7218885Sdim// 8218885Sdim//===----------------------------------------------------------------------===// 9218885Sdim// 10218885Sdim// This file implements the llvm::sys::ThreadLocal class. 11218885Sdim// 12218885Sdim//===----------------------------------------------------------------------===// 13218885Sdim 14218885Sdim#include "llvm/Config/config.h" 15218885Sdim#include "llvm/Support/ThreadLocal.h" 16218885Sdim 17218885Sdim//===----------------------------------------------------------------------===// 18218885Sdim//=== WARNING: Implementation here must contain only TRULY operating system 19218885Sdim//=== independent code. 20218885Sdim//===----------------------------------------------------------------------===// 21218885Sdim 22218885Sdim#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0 23218885Sdim// Define all methods as no-ops if threading is explicitly disabled 24218885Sdimnamespace llvm { 25218885Sdimusing namespace sys; 26218885SdimThreadLocalImpl::ThreadLocalImpl() { } 27218885SdimThreadLocalImpl::~ThreadLocalImpl() { } 28218885Sdimvoid ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);} 29218885Sdimconst void* ThreadLocalImpl::getInstance() { return data; } 30218885Sdimvoid ThreadLocalImpl::removeInstance() { data = 0; } 31218885Sdim} 32218885Sdim#else 33218885Sdim 34218885Sdim#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC) 35218885Sdim 36218885Sdim#include <cassert> 37218885Sdim#include <pthread.h> 38218885Sdim#include <stdlib.h> 39218885Sdim 40218885Sdimnamespace llvm { 41218885Sdimusing namespace sys; 42218885Sdim 43218885SdimThreadLocalImpl::ThreadLocalImpl() : data(0) { 44218885Sdim pthread_key_t* key = new pthread_key_t; 45218885Sdim int errorcode = pthread_key_create(key, NULL); 46218885Sdim assert(errorcode == 0); 47218885Sdim (void) errorcode; 48218885Sdim data = (void*)key; 49218885Sdim} 50218885Sdim 51218885SdimThreadLocalImpl::~ThreadLocalImpl() { 52218885Sdim pthread_key_t* key = static_cast<pthread_key_t*>(data); 53218885Sdim int errorcode = pthread_key_delete(*key); 54218885Sdim assert(errorcode == 0); 55218885Sdim (void) errorcode; 56218885Sdim delete key; 57218885Sdim} 58218885Sdim 59218885Sdimvoid ThreadLocalImpl::setInstance(const void* d) { 60218885Sdim pthread_key_t* key = static_cast<pthread_key_t*>(data); 61218885Sdim int errorcode = pthread_setspecific(*key, d); 62218885Sdim assert(errorcode == 0); 63218885Sdim (void) errorcode; 64218885Sdim} 65218885Sdim 66218885Sdimconst void* ThreadLocalImpl::getInstance() { 67218885Sdim pthread_key_t* key = static_cast<pthread_key_t*>(data); 68218885Sdim return pthread_getspecific(*key); 69218885Sdim} 70218885Sdim 71218885Sdimvoid ThreadLocalImpl::removeInstance() { 72218885Sdim setInstance(0); 73218885Sdim} 74218885Sdim 75218885Sdim} 76218885Sdim 77218885Sdim#elif defined(LLVM_ON_UNIX) 78218885Sdim#include "Unix/ThreadLocal.inc" 79218885Sdim#elif defined( LLVM_ON_WIN32) 80218885Sdim#include "Windows/ThreadLocal.inc" 81218885Sdim#else 82226633Sdim#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp 83218885Sdim#endif 84218885Sdim#endif 85