Home | History | Annotate | Download | only in Windows
      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 //===----------------------------------------------------------------------===//
     15 //=== WARNING: Implementation here must contain only generic Win32 code that
     16 //===          is guaranteed to work on *all* Win32 variants.
     17 //===----------------------------------------------------------------------===//
     18 
     19 #include "Windows.h"
     20 #include "llvm/Support/ThreadLocal.h"
     21 
     22 namespace llvm {
     23 using namespace sys;
     24 
     25 ThreadLocalImpl::ThreadLocalImpl() {
     26   DWORD* tls = new DWORD;
     27   *tls = TlsAlloc();
     28   assert(*tls != TLS_OUT_OF_INDEXES);
     29   data = tls;
     30 }
     31 
     32 ThreadLocalImpl::~ThreadLocalImpl() {
     33   DWORD* tls = static_cast<DWORD*>(data);
     34   TlsFree(*tls);
     35   delete tls;
     36 }
     37 
     38 const void* ThreadLocalImpl::getInstance() {
     39   DWORD* tls = static_cast<DWORD*>(data);
     40   return TlsGetValue(*tls);
     41 }
     42 
     43 void ThreadLocalImpl::setInstance(const void* d){
     44   DWORD* tls = static_cast<DWORD*>(data);
     45   int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
     46   assert(errorcode != 0);
     47   (void)errorcode;
     48 }
     49 
     50 void ThreadLocalImpl::removeInstance() {
     51   setInstance(0);
     52 }
     53 
     54 }
     55