ThreadLocal.cpp 1.7 KB

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