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"
|
|
|
|
#include "llvm/System/ThreadLocal.h"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
|
|
|
//=== independent code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
|
|
|
|
// Define all methods as no-ops if threading is explicitly disabled
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
ThreadLocalImpl::ThreadLocalImpl() { }
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() { }
|
2009-06-25 23:31:18 +00:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
|
|
|
|
const void* ThreadLocalImpl::getInstance() { return data; }
|
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;
|
|
|
|
|
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data(0) {
|
|
|
|
pthread_key_t* key = new pthread_key_t;
|
|
|
|
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
|
|
|
data = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() {
|
|
|
|
pthread_key_t* key = static_cast<pthread_key_t*>(data);
|
|
|
|
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
|
|
|
delete key;
|
|
|
|
}
|
|
|
|
|
2009-06-25 23:31:18 +00:00
|
|
|
void ThreadLocalImpl::setInstance(const void* d) {
|
2009-06-25 21:58:01 +00:00
|
|
|
pthread_key_t* key = static_cast<pthread_key_t*>(data);
|
|
|
|
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() {
|
2009-06-25 21:58:01 +00:00
|
|
|
pthread_key_t* key = static_cast<pthread_key_t*>(data);
|
|
|
|
return pthread_getspecific(*key);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(LLVM_ON_UNIX)
|
|
|
|
#include "Unix/ThreadLocal.inc"
|
|
|
|
#elif defined( LLVM_ON_WIN32)
|
|
|
|
#include "Win32/ThreadLocal.inc"
|
|
|
|
#else
|
|
|
|
#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/ThreadLocal.cpp
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|