2010-11-29 20:44:50 +01:00
|
|
|
//===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
|
2009-06-25 23:58:01 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the llvm::sys::ThreadLocal class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SYSTEM_THREAD_LOCAL_H
|
|
|
|
#define LLVM_SYSTEM_THREAD_LOCAL_H
|
|
|
|
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Threading.h"
|
2012-06-12 02:21:31 +02:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2009-06-25 23:58:01 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
2010-07-29 00:30:53 +02:00
|
|
|
// ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
|
|
|
|
// YOU SHOULD NEVER USE THIS DIRECTLY.
|
2009-06-25 23:58:01 +02:00
|
|
|
class ThreadLocalImpl {
|
2012-06-12 02:21:31 +02:00
|
|
|
typedef uint64_t ThreadLocalDataTy;
|
|
|
|
/// \brief Platform-specific thread local data.
|
|
|
|
///
|
|
|
|
/// This is embedded in the class and we avoid malloc'ing/free'ing it,
|
|
|
|
/// to make this class more safe for use along with CrashRecoveryContext.
|
2012-06-12 03:06:16 +02:00
|
|
|
union {
|
|
|
|
char data[sizeof(ThreadLocalDataTy)];
|
2012-06-12 19:06:32 +02:00
|
|
|
ThreadLocalDataTy align_data;
|
2012-06-12 03:06:16 +02:00
|
|
|
};
|
2009-06-25 23:58:01 +02:00
|
|
|
public:
|
|
|
|
ThreadLocalImpl();
|
|
|
|
virtual ~ThreadLocalImpl();
|
2009-06-26 01:31:18 +02:00
|
|
|
void setInstance(const void* d);
|
|
|
|
const void* getInstance();
|
2010-07-29 00:49:43 +02:00
|
|
|
void removeInstance();
|
2009-06-25 23:58:01 +02:00
|
|
|
};
|
2010-11-29 19:16:10 +01:00
|
|
|
|
2010-07-29 00:30:53 +02:00
|
|
|
/// ThreadLocal - A class used to abstract thread-local storage. It holds,
|
|
|
|
/// for each thread, a pointer a single object of type T.
|
2009-06-25 23:58:01 +02:00
|
|
|
template<class T>
|
|
|
|
class ThreadLocal : public ThreadLocalImpl {
|
|
|
|
public:
|
|
|
|
ThreadLocal() : ThreadLocalImpl() { }
|
2010-11-29 19:16:10 +01:00
|
|
|
|
2010-07-29 00:30:53 +02:00
|
|
|
/// get - Fetches a pointer to the object associated with the current
|
|
|
|
/// thread. If no object has yet been associated, it returns NULL;
|
2009-06-25 23:58:01 +02:00
|
|
|
T* get() { return static_cast<T*>(getInstance()); }
|
2010-11-29 19:16:10 +01:00
|
|
|
|
2010-07-29 00:30:53 +02:00
|
|
|
// set - Associates a pointer to an object with the current thread.
|
2009-06-25 23:58:01 +02:00
|
|
|
void set(T* d) { setInstance(d); }
|
2010-11-29 19:16:10 +01:00
|
|
|
|
2010-07-29 00:49:43 +02:00
|
|
|
// erase - Removes the pointer associated with the current thread.
|
|
|
|
void erase() { removeInstance(); }
|
2009-06-25 23:58:01 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|