xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Support/ThreadLocal.cpp (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg //===- ThreadLocal.cpp - Thread Local Data ----------------------*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg //
9*7330f729Sjoerg // This file implements the llvm::sys::ThreadLocal class.
10*7330f729Sjoerg //
11*7330f729Sjoerg //===----------------------------------------------------------------------===//
12*7330f729Sjoerg 
13*7330f729Sjoerg #include "llvm/Support/ThreadLocal.h"
14*7330f729Sjoerg #include "llvm/Config/llvm-config.h"
15*7330f729Sjoerg #include "llvm/Support/Compiler.h"
16*7330f729Sjoerg 
17*7330f729Sjoerg //===----------------------------------------------------------------------===//
18*7330f729Sjoerg //=== WARNING: Implementation here must contain only TRULY operating system
19*7330f729Sjoerg //===          independent code.
20*7330f729Sjoerg //===----------------------------------------------------------------------===//
21*7330f729Sjoerg 
22*7330f729Sjoerg #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23*7330f729Sjoerg // Define all methods as no-ops if threading is explicitly disabled
24*7330f729Sjoerg namespace llvm {
25*7330f729Sjoerg using namespace sys;
ThreadLocalImpl()26*7330f729Sjoerg ThreadLocalImpl::ThreadLocalImpl() : data() { }
~ThreadLocalImpl()27*7330f729Sjoerg ThreadLocalImpl::~ThreadLocalImpl() { }
setInstance(const void * d)28*7330f729Sjoerg void ThreadLocalImpl::setInstance(const void* d) {
29*7330f729Sjoerg   static_assert(sizeof(d) <= sizeof(data), "size too big");
30*7330f729Sjoerg   void **pd = reinterpret_cast<void**>(&data);
31*7330f729Sjoerg   *pd = const_cast<void*>(d);
32*7330f729Sjoerg }
getInstance()33*7330f729Sjoerg void *ThreadLocalImpl::getInstance() {
34*7330f729Sjoerg   void **pd = reinterpret_cast<void**>(&data);
35*7330f729Sjoerg   return *pd;
36*7330f729Sjoerg }
removeInstance()37*7330f729Sjoerg void ThreadLocalImpl::removeInstance() {
38*7330f729Sjoerg   setInstance(nullptr);
39*7330f729Sjoerg }
40*7330f729Sjoerg }
41*7330f729Sjoerg #elif defined(LLVM_ON_UNIX)
42*7330f729Sjoerg #include "Unix/ThreadLocal.inc"
43*7330f729Sjoerg #elif defined( _WIN32)
44*7330f729Sjoerg #include "Windows/ThreadLocal.inc"
45*7330f729Sjoerg #else
46*7330f729Sjoerg #warning Neither LLVM_ON_UNIX nor _WIN32 set in Support/ThreadLocal.cpp
47*7330f729Sjoerg #endif
48