2010-11-29 19:44:50 +00:00
|
|
|
//===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
|
2009-06-25 21:58:01 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_SUPPORT_THREADLOCAL_H
|
|
|
|
#define LLVM_SUPPORT_THREADLOCAL_H
|
2009-06-25 21:58:01 +00:00
|
|
|
|
2012-06-12 00:21:31 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Support/Threading.h"
|
2009-06-25 21:58:01 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace sys {
|
2010-07-28 22:30:53 +00:00
|
|
|
// ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
|
|
|
|
// YOU SHOULD NEVER USE THIS DIRECTLY.
|
2009-06-25 21:58:01 +00:00
|
|
|
class ThreadLocalImpl {
|
2012-06-12 00:21:31 +00: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 01:06:16 +00:00
|
|
|
union {
|
|
|
|
char data[sizeof(ThreadLocalDataTy)];
|
2012-06-12 17:06:32 +00:00
|
|
|
ThreadLocalDataTy align_data;
|
2012-06-12 01:06:16 +00:00
|
|
|
};
|
2009-06-25 21:58:01 +00:00
|
|
|
public:
|
|
|
|
ThreadLocalImpl();
|
|
|
|
virtual ~ThreadLocalImpl();
|
2009-06-25 23:31:18 +00:00
|
|
|
void setInstance(const void* d);
|
2014-12-15 01:04:45 +00:00
|
|
|
void *getInstance();
|
2010-07-28 22:49:43 +00:00
|
|
|
void removeInstance();
|
2009-06-25 21:58:01 +00:00
|
|
|
};
|
2010-11-29 18:16:10 +00:00
|
|
|
|
2010-07-28 22:30:53 +00: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 21:58:01 +00:00
|
|
|
template<class T>
|
|
|
|
class ThreadLocal : public ThreadLocalImpl {
|
|
|
|
public:
|
|
|
|
ThreadLocal() : ThreadLocalImpl() { }
|
2010-11-29 18:16:10 +00:00
|
|
|
|
2010-07-28 22:30:53 +00: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 21:58:01 +00:00
|
|
|
T* get() { return static_cast<T*>(getInstance()); }
|
2010-11-29 18:16:10 +00:00
|
|
|
|
2010-07-28 22:30:53 +00:00
|
|
|
// set - Associates a pointer to an object with the current thread.
|
2009-06-25 21:58:01 +00:00
|
|
|
void set(T* d) { setInstance(d); }
|
2010-11-29 18:16:10 +00:00
|
|
|
|
2010-07-28 22:49:43 +00:00
|
|
|
// erase - Removes the pointer associated with the current thread.
|
|
|
|
void erase() { removeInstance(); }
|
2009-06-25 21:58:01 +00:00
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-25 21:58:01 +00:00
|
|
|
|
|
|
|
#endif
|