LLVM API Documentation

ThreadLocal.cpp

Go to the documentation of this file.
00001 //===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the llvm::sys::ThreadLocal class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Config/config.h"
00015 #include "llvm/System/ThreadLocal.h"
00016 
00017 //===----------------------------------------------------------------------===//
00018 //=== WARNING: Implementation here must contain only TRULY operating system
00019 //===          independent code.
00020 //===----------------------------------------------------------------------===//
00021 
00022 #if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
00023 // Define all methods as no-ops if threading is explicitly disabled
00024 namespace llvm {
00025 using namespace sys;
00026 ThreadLocalImpl::ThreadLocalImpl() { }
00027 ThreadLocalImpl::~ThreadLocalImpl() { }
00028 void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
00029 const void* ThreadLocalImpl::getInstance() { return data; }
00030 }
00031 #else
00032 
00033 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
00034 
00035 #include <cassert>
00036 #include <pthread.h>
00037 #include <stdlib.h>
00038 
00039 namespace llvm {
00040 using namespace sys;
00041 
00042 ThreadLocalImpl::ThreadLocalImpl() : data(0) {
00043   pthread_key_t* key = new pthread_key_t;
00044   int errorcode = pthread_key_create(key, NULL);
00045   assert(errorcode == 0);
00046   (void) errorcode;
00047   data = (void*)key;
00048 }
00049 
00050 ThreadLocalImpl::~ThreadLocalImpl() {
00051   pthread_key_t* key = static_cast<pthread_key_t*>(data);
00052   int errorcode = pthread_key_delete(*key);
00053   assert(errorcode == 0);
00054   (void) errorcode;
00055   delete key;
00056 }
00057 
00058 void ThreadLocalImpl::setInstance(const void* d) {
00059   pthread_key_t* key = static_cast<pthread_key_t*>(data);
00060   int errorcode = pthread_setspecific(*key, d);
00061   assert(errorcode == 0);
00062   (void) errorcode;
00063 }
00064 
00065 const void* ThreadLocalImpl::getInstance() {
00066   pthread_key_t* key = static_cast<pthread_key_t*>(data);
00067   return pthread_getspecific(*key);
00068 }
00069 
00070 }
00071 
00072 #elif defined(LLVM_ON_UNIX)
00073 #include "Unix/ThreadLocal.inc"
00074 #elif defined( LLVM_ON_WIN32)
00075 #include "Win32/ThreadLocal.inc"
00076 #else
00077 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/ThreadLocal.cpp
00078 #endif
00079 #endif
00080