ThreadLocal.h revision 793537d21f8aa46458a5733d96816ba8bddeef50
1//===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the llvm::sys::ThreadLocal class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_SYSTEM_THREAD_LOCAL_H 15#define LLVM_SYSTEM_THREAD_LOCAL_H 16 17#include "llvm/Support/Threading.h" 18#include "llvm/Support/DataTypes.h" 19#include <cassert> 20 21namespace llvm { 22 namespace sys { 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. 28 /// 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. 31 ThreadLocalDataTy data; 32 public: 33 ThreadLocalImpl(); 34 virtual ~ThreadLocalImpl(); 35 void setInstance(const void* d); 36 const void* getInstance(); 37 void removeInstance(); 38 }; 39 40 /// ThreadLocal - A class used to abstract thread-local storage. It holds, 41 /// for each thread, a pointer a single object of type T. 42 template<class T> 43 class ThreadLocal : public ThreadLocalImpl { 44 public: 45 ThreadLocal() : ThreadLocalImpl() { } 46 47 /// get - Fetches a pointer to the object associated with the current 48 /// thread. If no object has yet been associated, it returns NULL; 49 T* get() { return static_cast<T*>(getInstance()); } 50 51 // set - Associates a pointer to an object with the current thread. 52 void set(T* d) { setInstance(d); } 53 54 // erase - Removes the pointer associated with the current thread. 55 void erase() { removeInstance(); } 56 }; 57 } 58} 59 60#endif 61