2011-02-20 07:57:14 -05:00
|
|
|
//= llvm/Support/Win32/ThreadLocal.inc - Win32 Thread Local Data -*- C++ -*-===//
|
|
|
|
|
//
|
2009-06-27 06:44:33 -04:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
2011-02-20 07:57:14 -05:00
|
|
|
//
|
2009-06-27 06:44:33 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This file implements the Win32 specific (non-pthread) ThreadLocal class.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//=== WARNING: Implementation here must contain only generic Win32 code that
|
|
|
|
|
//=== is guaranteed to work on *all* Win32 variants.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2014-11-24 04:08:18 -05:00
|
|
|
#include "WindowsSupport.h"
|
2011-02-20 07:57:14 -05:00
|
|
|
#include "llvm/Support/ThreadLocal.h"
|
2009-06-27 06:44:33 -04:00
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
using namespace sys;
|
|
|
|
|
|
2012-08-15 15:34:23 -04:00
|
|
|
ThreadLocalImpl::ThreadLocalImpl() : data() {
|
2014-11-24 04:08:18 -05:00
|
|
|
static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
|
2012-08-15 15:34:23 -04:00
|
|
|
DWORD* tls = reinterpret_cast<DWORD*>(&data);
|
2009-06-27 06:44:33 -04:00
|
|
|
*tls = TlsAlloc();
|
|
|
|
|
assert(*tls != TLS_OUT_OF_INDEXES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadLocalImpl::~ThreadLocalImpl() {
|
2012-08-15 15:34:23 -04:00
|
|
|
DWORD* tls = reinterpret_cast<DWORD*>(&data);
|
2009-06-27 06:44:33 -04:00
|
|
|
TlsFree(*tls);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-18 11:17:27 -05:00
|
|
|
void *ThreadLocalImpl::getInstance() {
|
2012-08-15 15:34:23 -04:00
|
|
|
DWORD* tls = reinterpret_cast<DWORD*>(&data);
|
2009-06-27 06:44:33 -04:00
|
|
|
return TlsGetValue(*tls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ThreadLocalImpl::setInstance(const void* d){
|
2012-08-15 15:34:23 -04:00
|
|
|
DWORD* tls = reinterpret_cast<DWORD*>(&data);
|
2009-06-27 06:44:33 -04:00
|
|
|
int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
|
2009-07-04 09:58:26 -04:00
|
|
|
assert(errorcode != 0);
|
2011-02-20 07:57:14 -05:00
|
|
|
(void)errorcode;
|
2009-06-27 06:44:33 -04:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 11:48:55 -04:00
|
|
|
void ThreadLocalImpl::removeInstance() {
|
|
|
|
|
setInstance(0);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-27 06:44:33 -04:00
|
|
|
}
|