2
0

ThreadLocal.inc 1.6 KB

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