blob: 427a67e2a96d0ead04980385f60f91593a7ba101 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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_SUPPORT_THREADLOCAL_H
15#define LLVM_SUPPORT_THREADLOCAL_H
16
17#include "llvm/Support/DataTypes.h"
18#include "llvm/Support/Threading.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 union {
32 char data[sizeof(ThreadLocalDataTy)];
33 ThreadLocalDataTy align_data;
34 };
35 public:
36 ThreadLocalImpl();
37 virtual ~ThreadLocalImpl();
38 void setInstance(const void* d);
39 void *getInstance();
40 void removeInstance();
41 };
42
43 /// ThreadLocal - A class used to abstract thread-local storage. It holds,
44 /// for each thread, a pointer a single object of type T.
45 template<class T>
46 class ThreadLocal : public ThreadLocalImpl {
47 public:
48 ThreadLocal() : ThreadLocalImpl() { }
49
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()); }
53
54 // set - Associates a pointer to an object with the current thread.
55 void set(T* d) { setInstance(d); }
56
57 // erase - Removes the pointer associated with the current thread.
58 void erase() { removeInstance(); }
59 };
60 }
61}
62
63#endif