1 //===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::ThreadLocal class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_THREADLOCAL_H
15 #define LLVM_SUPPORT_THREADLOCAL_H
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Threading.h"
23 // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
24 // YOU SHOULD NEVER USE THIS DIRECTLY.
25 class ThreadLocalImpl {
26 typedef uint64_t ThreadLocalDataTy;
27 /// \brief Platform-specific thread local data.
29 /// This is embedded in the class and we avoid malloc'ing/free'ing it,
30 /// to make this class more safe for use along with CrashRecoveryContext.
32 char data[sizeof(ThreadLocalDataTy)];
33 ThreadLocalDataTy align_data;
37 virtual ~ThreadLocalImpl();
38 void setInstance(const void* d);
39 const void* getInstance();
40 void removeInstance();
43 /// ThreadLocal - A class used to abstract thread-local storage. It holds,
44 /// for each thread, a pointer a single object of type T.
46 class ThreadLocal : public ThreadLocalImpl {
48 ThreadLocal() : ThreadLocalImpl() { }
50 /// get - Fetches a pointer to the object associated with the current
51 /// thread. If no object has yet been associated, it returns NULL;
52 T* get() { return static_cast<T*>(getInstance()); }
54 // set - Associates a pointer to an object with the current thread.
55 void set(T* d) { setInstance(d); }
57 // erase - Removes the pointer associated with the current thread.
58 void erase() { removeInstance(); }