1 //===- llvm/Support/ThreadLocal.h - Thread Local Data ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the llvm::sys::ThreadLocal class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_THREADLOCAL_H
14 #define LLVM_SUPPORT_THREADLOCAL_H
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Threading.h"
22 // ThreadLocalImpl - Common base class of all ThreadLocal instantiations.
23 // YOU SHOULD NEVER USE THIS DIRECTLY.
24 class ThreadLocalImpl
{
25 typedef uint64_t ThreadLocalDataTy
;
26 /// Platform-specific thread local data.
28 /// This is embedded in the class and we avoid malloc'ing/free'ing it,
29 /// to make this class more safe for use along with CrashRecoveryContext.
31 char data
[sizeof(ThreadLocalDataTy
)];
32 ThreadLocalDataTy align_data
;
36 virtual ~ThreadLocalImpl();
37 void setInstance(const void* d
);
39 void removeInstance();
42 /// ThreadLocal - A class used to abstract thread-local storage. It holds,
43 /// for each thread, a pointer a single object of type T.
45 class ThreadLocal
: public ThreadLocalImpl
{
47 ThreadLocal() : ThreadLocalImpl() { }
49 /// get - Fetches a pointer to the object associated with the current
50 /// thread. If no object has yet been associated, it returns NULL;
51 T
* get() { return static_cast<T
*>(getInstance()); }
53 // set - Associates a pointer to an object with the current thread.
54 void set(T
* d
) { setInstance(d
); }
56 // erase - Removes the pointer associated with the current thread.
57 void erase() { removeInstance(); }