LLVM API Documentation

Windows/ThreadLocal.inc
Go to the documentation of this file.
00001 //= llvm/Support/Win32/ThreadLocal.inc - Win32 Thread Local Data -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Win32 specific (non-pthread) ThreadLocal class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 //===----------------------------------------------------------------------===//
00015 //=== WARNING: Implementation here must contain only generic Win32 code that
00016 //===          is guaranteed to work on *all* Win32 variants.
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "WindowsSupport.h"
00020 #include "llvm/Support/ThreadLocal.h"
00021 
00022 namespace llvm {
00023 using namespace sys;
00024 
00025 ThreadLocalImpl::ThreadLocalImpl() : data() {
00026   static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
00027   DWORD* tls = reinterpret_cast<DWORD*>(&data);
00028   *tls = TlsAlloc();
00029   assert(*tls != TLS_OUT_OF_INDEXES);
00030 }
00031 
00032 ThreadLocalImpl::~ThreadLocalImpl() {
00033   DWORD* tls = reinterpret_cast<DWORD*>(&data);
00034   TlsFree(*tls);
00035 }
00036 
00037 const void* ThreadLocalImpl::getInstance() {
00038   DWORD* tls = reinterpret_cast<DWORD*>(&data);
00039   return TlsGetValue(*tls);
00040 }
00041 
00042 void ThreadLocalImpl::setInstance(const void* d){
00043   DWORD* tls = reinterpret_cast<DWORD*>(&data);
00044   int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
00045   assert(errorcode != 0);
00046   (void)errorcode;
00047 }
00048 
00049 void ThreadLocalImpl::removeInstance() {
00050   setInstance(0);
00051 }
00052 
00053 }