1 //= llvm/Support/Win32/ThreadLocal.inc - Win32 Thread Local Data -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Win32 specific (non-pthread) ThreadLocal class.
11 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
14 //=== WARNING: Implementation here must contain only generic Win32 code that
15 //=== is guaranteed to work on *all* Win32 variants.
16 //===----------------------------------------------------------------------===//
18 #include "WindowsSupport.h"
19 #include "llvm/Support/ThreadLocal.h"
23 sys::ThreadLocalImpl::ThreadLocalImpl() : data() {
24 static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
25 DWORD* tls = reinterpret_cast<DWORD*>(&data);
27 assert(*tls != TLS_OUT_OF_INDEXES);
30 sys::ThreadLocalImpl::~ThreadLocalImpl() {
31 DWORD* tls = reinterpret_cast<DWORD*>(&data);
35 void *sys::ThreadLocalImpl::getInstance() {
36 DWORD* tls = reinterpret_cast<DWORD*>(&data);
37 return TlsGetValue(*tls);
40 void sys::ThreadLocalImpl::setInstance(const void* d){
41 DWORD* tls = reinterpret_cast<DWORD*>(&data);
42 int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
43 assert(errorcode != 0);
47 void sys::ThreadLocalImpl::removeInstance() {