ThreadLocal.h revision 218885
1218885Sdim//===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This file declares the llvm::sys::ThreadLocal class.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14218885Sdim#ifndef LLVM_SYSTEM_THREAD_LOCAL_H
15218885Sdim#define LLVM_SYSTEM_THREAD_LOCAL_H
16218885Sdim
17218885Sdim#include "llvm/Support/Threading.h"
18218885Sdim#include <cassert>
19218885Sdim
20218885Sdimnamespace llvm {
21218885Sdim  namespace sys {
22218885Sdim    // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
23218885Sdim    // YOU SHOULD NEVER USE THIS DIRECTLY.
24218885Sdim    class ThreadLocalImpl {
25218885Sdim      void* data;
26218885Sdim    public:
27218885Sdim      ThreadLocalImpl();
28218885Sdim      virtual ~ThreadLocalImpl();
29218885Sdim      void setInstance(const void* d);
30218885Sdim      const void* getInstance();
31218885Sdim      void removeInstance();
32218885Sdim    };
33218885Sdim
34218885Sdim    /// ThreadLocal - A class used to abstract thread-local storage.  It holds,
35218885Sdim    /// for each thread, a pointer a single object of type T.
36218885Sdim    template<class T>
37218885Sdim    class ThreadLocal : public ThreadLocalImpl {
38218885Sdim    public:
39218885Sdim      ThreadLocal() : ThreadLocalImpl() { }
40218885Sdim
41218885Sdim      /// get - Fetches a pointer to the object associated with the current
42218885Sdim      /// thread.  If no object has yet been associated, it returns NULL;
43218885Sdim      T* get() { return static_cast<T*>(getInstance()); }
44218885Sdim
45218885Sdim      // set - Associates a pointer to an object with the current thread.
46218885Sdim      void set(T* d) { setInstance(d); }
47218885Sdim
48218885Sdim      // erase - Removes the pointer associated with the current thread.
49218885Sdim      void erase() { removeInstance(); }
50218885Sdim    };
51218885Sdim  }
52218885Sdim}
53218885Sdim
54218885Sdim#endif
55