1 //===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the llvm::sys::ThreadLocal class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/ThreadLocal.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/Compiler.h"
16 
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //===          independent code.
20 //===----------------------------------------------------------------------===//
21 
22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
24 namespace llvm {
25 using namespace sys;
ThreadLocalImpl()26 ThreadLocalImpl::ThreadLocalImpl() : data() { }
~ThreadLocalImpl()27 ThreadLocalImpl::~ThreadLocalImpl() { }
setInstance(const void * d)28 void ThreadLocalImpl::setInstance(const void* d) {
29   static_assert(sizeof(d) <= sizeof(data), "size too big");
30   void **pd = reinterpret_cast<void**>(&data);
31   *pd = const_cast<void*>(d);
32 }
getInstance()33 void *ThreadLocalImpl::getInstance() {
34   void **pd = reinterpret_cast<void**>(&data);
35   return *pd;
36 }
removeInstance()37 void ThreadLocalImpl::removeInstance() {
38   setInstance(nullptr);
39 }
40 }
41 #elif defined(LLVM_ON_UNIX)
42 #include "Unix/ThreadLocal.inc"
43 #elif defined( _WIN32)
44 #include "Windows/ThreadLocal.inc"
45 #else
46 #warning Neither LLVM_ON_UNIX nor _WIN32 set in Support/ThreadLocal.cpp
47 #endif
48