2009-06-25 21:58:01 +00:00
|
|
|
//===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the llvm::sys::ThreadLocal class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Config/config.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/ThreadLocal.h"
|
2009-06-25 21:58:01 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
|
|
|
//=== independent code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-11-28 00:48:58 +00:00
|
|
|
#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
|
2009-06-25 21:58:01 +00:00
|
|
|
// Define all methods as no-ops if threading is explicitly disabled
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
ThreadLocalImpl::ThreadLocalImpl() { }
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() { }
|
2012-06-13 16:30:06 +00:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) {
|
|
|
|
typedef int SIZE_TOO_BIG[sizeof(d) <= sizeof(data) ? 1 : -1];
|
|
|
|
void **pd = reinterpret_cast<void**>(&data);
|
|
|
|
*pd = const_cast<void*>(d);
|
|
|
|
}
|
2012-06-26 17:13:58 +00:00
|
|
|
const void* ThreadLocalImpl::getInstance() {
|
2012-06-13 16:30:06 +00:00
|
|
|
void **pd = reinterpret_cast<void**>(&data);
|
2012-06-26 17:13:58 +00:00
|
|
|
return *pd;
|
|
|
|
}
|
|
|
|
void ThreadLocalImpl::removeInstance() {
|
|
|
|
setInstance(0);
|
2012-06-13 16:30:06 +00:00
|
|
|
}
|
2009-06-25 21:58:01 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2009-06-25 23:10:26 +00:00
|
|
|
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_GETSPECIFIC)
|
2009-06-25 21:58:01 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
2012-06-12 01:06:16 +00:00
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data() {
|
2012-06-12 00:21:31 +00:00
|
|
|
typedef int SIZE_TOO_BIG[sizeof(pthread_key_t) <= sizeof(data) ? 1 : -1];
|
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
2009-06-25 21:58:01 +00:00
|
|
|
int errorcode = pthread_key_create(key, NULL);
|
|
|
|
assert(errorcode == 0);
|
2009-06-26 01:34:35 +00:00
|
|
|
(void) errorcode;
|
2009-06-25 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() {
|
2012-06-12 00:21:31 +00:00
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
2009-06-25 21:58:01 +00:00
|
|
|
int errorcode = pthread_key_delete(*key);
|
2009-06-25 23:28:28 +00:00
|
|
|
assert(errorcode == 0);
|
2009-06-26 01:34:35 +00:00
|
|
|
(void) errorcode;
|
2009-06-25 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 23:31:18 +00:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) {
|
2012-06-12 00:21:31 +00:00
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
2009-06-25 21:58:01 +00:00
|
|
|
int errorcode = pthread_setspecific(*key, d);
|
|
|
|
assert(errorcode == 0);
|
2009-06-26 01:34:35 +00:00
|
|
|
(void) errorcode;
|
2009-06-25 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 23:31:18 +00:00
|
|
|
const void* ThreadLocalImpl::getInstance() {
|
2012-06-12 00:21:31 +00:00
|
|
|
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
|
2009-06-25 21:58:01 +00:00
|
|
|
return pthread_getspecific(*key);
|
|
|
|
}
|
|
|
|
|
2010-07-28 22:49:43 +00:00
|
|
|
void ThreadLocalImpl::removeInstance() {
|
|
|
|
setInstance(0);
|
|
|
|
}
|
|
|
|
|
2009-06-25 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(LLVM_ON_UNIX)
|
|
|
|
#include "Unix/ThreadLocal.inc"
|
|
|
|
#elif defined( LLVM_ON_WIN32)
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "Windows/ThreadLocal.inc"
|
2009-06-25 21:58:01 +00:00
|
|
|
#else
|
2011-10-11 20:02:52 +00:00
|
|
|
#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp
|
2009-06-25 21:58:01 +00:00
|
|
|
#endif
|
|
|
|
#endif
|