blob: 9a75c02b351f884f53862f6b346881ef31ddda1e [file] [log] [blame]
Owen Andersonf17f6f02009-06-25 21:58:01 +00001//===- ThreadLocal.cpp - 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 implements the llvm::sys::ThreadLocal class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Support/ThreadLocal.h"
Owen Andersonf17f6f02009-06-25 21:58:01 +000015#include "llvm/Config/config.h"
Alp Toker1bcdd6a2013-12-31 03:16:55 +000016#include "llvm/Support/Compiler.h"
Owen Andersonf17f6f02009-06-25 21:58:01 +000017
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
Dylan Noblesmithefddf202011-11-28 00:48:58 +000023#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Andersonf17f6f02009-06-25 21:58:01 +000024// Define all methods as no-ops if threading is explicitly disabled
25namespace llvm {
26using namespace sys;
Hans Wennborgfabf8bf2013-12-19 20:32:44 +000027ThreadLocalImpl::ThreadLocalImpl() : data() { }
Owen Andersonf17f6f02009-06-25 21:58:01 +000028ThreadLocalImpl::~ThreadLocalImpl() { }
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000029void ThreadLocalImpl::setInstance(const void* d) {
Chandler Carruthcf018002014-03-02 13:10:45 +000030 static_assert(sizeof(d) <= sizeof(data), "size too big");
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000031 void **pd = reinterpret_cast<void**>(&data);
32 *pd = const_cast<void*>(d);
33}
David Majnemer421c89d2014-12-15 01:04:45 +000034void *ThreadLocalImpl::getInstance() {
Argyrios Kyrtzidis444fd422012-06-13 16:30:06 +000035 void **pd = reinterpret_cast<void**>(&data);
Argyrios Kyrtzidis46785f92012-06-26 17:13:58 +000036 return *pd;
37}
38void ThreadLocalImpl::removeInstance() {
Craig Topperc10719f2014-04-07 04:17:22 +000039 setInstance(nullptr);
Owen Andersoncfc2a572010-07-28 22:49:43 +000040}
Owen Andersonf17f6f02009-06-25 21:58:01 +000041}
Owen Andersonf17f6f02009-06-25 21:58:01 +000042#elif defined(LLVM_ON_UNIX)
43#include "Unix/ThreadLocal.inc"
44#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "Windows/ThreadLocal.inc"
Owen Andersonf17f6f02009-06-25 21:58:01 +000046#else
Daniel Dunbar037fc932011-10-11 20:02:52 +000047#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 set in Support/ThreadLocal.cpp
Owen Andersonf17f6f02009-06-25 21:58:01 +000048#endif