ThreadLocal.h revision 53ca1f3190680f3e86aebe0f72f7918d63f71e0d
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 <cassert>
19
20namespace llvm {
21  namespace sys {
22    // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
23    // YOU SHOULD NEVER USE THIS DIRECTLY.
24    class ThreadLocalImpl {
25      void* data;
26    public:
27      ThreadLocalImpl();
28      virtual ~ThreadLocalImpl();
29      void setInstance(const void* d);
30      const void* getInstance();
31      void removeInstance();
32    };
33
34    /// ThreadLocal - A class used to abstract thread-local storage.  It holds,
35    /// for each thread, a pointer a single object of type T.
36    template<class T>
37    class ThreadLocal : public ThreadLocalImpl {
38    public:
39      ThreadLocal() : ThreadLocalImpl() { }
40
41      /// get - Fetches a pointer to the object associated with the current
42      /// thread.  If no object has yet been associated, it returns NULL;
43      T* get() { return static_cast<T*>(getInstance()); }
44
45      // set - Associates a pointer to an object with the current thread.
46      void set(T* d) { setInstance(d); }
47
48      // erase - Removes the pointer associated with the current thread.
49      void erase() { removeInstance(); }
50    };
51  }
52}
53
54#endif
55