LLVM API Documentation

ThreadLocal.h
Go to the documentation of this file.
00001 //===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the llvm::sys::ThreadLocal class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_THREADLOCAL_H
00015 #define LLVM_SUPPORT_THREADLOCAL_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include "llvm/Support/Threading.h"
00019 #include <cassert>
00020 
00021 namespace llvm {
00022   namespace sys {
00023     // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
00024     // YOU SHOULD NEVER USE THIS DIRECTLY.
00025     class ThreadLocalImpl {
00026       typedef uint64_t ThreadLocalDataTy;
00027       /// \brief Platform-specific thread local data.
00028       ///
00029       /// This is embedded in the class and we avoid malloc'ing/free'ing it,
00030       /// to make this class more safe for use along with CrashRecoveryContext.
00031       union {
00032         char data[sizeof(ThreadLocalDataTy)];
00033         ThreadLocalDataTy align_data;
00034       };
00035     public:
00036       ThreadLocalImpl();
00037       virtual ~ThreadLocalImpl();
00038       void setInstance(const void* d);
00039       const void* getInstance();
00040       void removeInstance();
00041     };
00042 
00043     /// ThreadLocal - A class used to abstract thread-local storage.  It holds,
00044     /// for each thread, a pointer a single object of type T.
00045     template<class T>
00046     class ThreadLocal : public ThreadLocalImpl {
00047     public:
00048       ThreadLocal() : ThreadLocalImpl() { }
00049 
00050       /// get - Fetches a pointer to the object associated with the current
00051       /// thread.  If no object has yet been associated, it returns NULL;
00052       T* get() { return static_cast<T*>(getInstance()); }
00053 
00054       // set - Associates a pointer to an object with the current thread.
00055       void set(T* d) { setInstance(d); }
00056 
00057       // erase - Removes the pointer associated with the current thread.
00058       void erase() { removeInstance(); }
00059     };
00060   }
00061 }
00062 
00063 #endif