ThreadLocal.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //= llvm/Support/Win32/ThreadLocal.inc - Win32 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 Win32 specific (non-pthread) ThreadLocal class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. //===----------------------------------------------------------------------===//
  13. //=== WARNING: Implementation here must contain only generic Win32 code that
  14. //=== is guaranteed to work on *all* Win32 variants.
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Support/Windows/WindowsSupport.h"
  17. #include "llvm/Support/ThreadLocal.h"
  18. namespace llvm {
  19. sys::ThreadLocalImpl::ThreadLocalImpl() : data() {
  20. static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
  21. DWORD* tls = reinterpret_cast<DWORD*>(&data);
  22. *tls = TlsAlloc();
  23. assert(*tls != TLS_OUT_OF_INDEXES);
  24. }
  25. sys::ThreadLocalImpl::~ThreadLocalImpl() {
  26. DWORD* tls = reinterpret_cast<DWORD*>(&data);
  27. TlsFree(*tls);
  28. }
  29. void *sys::ThreadLocalImpl::getInstance() {
  30. DWORD* tls = reinterpret_cast<DWORD*>(&data);
  31. return TlsGetValue(*tls);
  32. }
  33. void sys::ThreadLocalImpl::setInstance(const void* d){
  34. DWORD* tls = reinterpret_cast<DWORD*>(&data);
  35. int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
  36. assert(errorcode != 0);
  37. (void)errorcode;
  38. }
  39. void sys::ThreadLocalImpl::removeInstance() {
  40. setInstance(0);
  41. }
  42. }