1//= llvm/Support/Win32/ThreadLocal.inc - Win32 Thread Local Data -*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Win32 specific (non-pthread) ThreadLocal class.
10//
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
18#include "llvm/Support/Windows/WindowsSupport.h"
19#include "llvm/Support/ThreadLocal.h"
20
21namespace llvm {
22
23sys::ThreadLocalImpl::ThreadLocalImpl() : data() {
24  static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
25  DWORD* tls = reinterpret_cast<DWORD*>(&data);
26  *tls = TlsAlloc();
27  assert(*tls != TLS_OUT_OF_INDEXES);
28}
29
30sys::ThreadLocalImpl::~ThreadLocalImpl() {
31  DWORD* tls = reinterpret_cast<DWORD*>(&data);
32  TlsFree(*tls);
33}
34
35void *sys::ThreadLocalImpl::getInstance() {
36  DWORD* tls = reinterpret_cast<DWORD*>(&data);
37  return TlsGetValue(*tls);
38}
39
40void 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);
44  (void)errorcode;
45}
46
47void sys::ThreadLocalImpl::removeInstance() {
48  setInstance(0);
49}
50
51}
52