xref: /aosp_15_r20/external/llvm/lib/Support/Windows/ThreadLocal.inc (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker//= llvm/Support/Win32/ThreadLocal.inc - Win32 Thread Local Data -*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker//
3*9880d681SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker//
5*9880d681SAndroid Build Coastguard Worker// This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker// License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker//
8*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker//
10*9880d681SAndroid Build Coastguard Worker// This file implements the Win32 specific (non-pthread) ThreadLocal class.
11*9880d681SAndroid Build Coastguard Worker//
12*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker//=== WARNING: Implementation here must contain only generic Win32 code that
16*9880d681SAndroid Build Coastguard Worker//===          is guaranteed to work on *all* Win32 variants.
17*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker#include "WindowsSupport.h"
20*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/ThreadLocal.h"
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Workernamespace llvm {
23*9880d681SAndroid Build Coastguard Workerusing namespace sys;
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard WorkerThreadLocalImpl::ThreadLocalImpl() : data() {
26*9880d681SAndroid Build Coastguard Worker  static_assert(sizeof(DWORD) <= sizeof(data), "size too big");
27*9880d681SAndroid Build Coastguard Worker  DWORD* tls = reinterpret_cast<DWORD*>(&data);
28*9880d681SAndroid Build Coastguard Worker  *tls = TlsAlloc();
29*9880d681SAndroid Build Coastguard Worker  assert(*tls != TLS_OUT_OF_INDEXES);
30*9880d681SAndroid Build Coastguard Worker}
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard WorkerThreadLocalImpl::~ThreadLocalImpl() {
33*9880d681SAndroid Build Coastguard Worker  DWORD* tls = reinterpret_cast<DWORD*>(&data);
34*9880d681SAndroid Build Coastguard Worker  TlsFree(*tls);
35*9880d681SAndroid Build Coastguard Worker}
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Workervoid *ThreadLocalImpl::getInstance() {
38*9880d681SAndroid Build Coastguard Worker  DWORD* tls = reinterpret_cast<DWORD*>(&data);
39*9880d681SAndroid Build Coastguard Worker  return TlsGetValue(*tls);
40*9880d681SAndroid Build Coastguard Worker}
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Workervoid ThreadLocalImpl::setInstance(const void* d){
43*9880d681SAndroid Build Coastguard Worker  DWORD* tls = reinterpret_cast<DWORD*>(&data);
44*9880d681SAndroid Build Coastguard Worker  int errorcode = TlsSetValue(*tls, const_cast<void*>(d));
45*9880d681SAndroid Build Coastguard Worker  assert(errorcode != 0);
46*9880d681SAndroid Build Coastguard Worker  (void)errorcode;
47*9880d681SAndroid Build Coastguard Worker}
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Workervoid ThreadLocalImpl::removeInstance() {
50*9880d681SAndroid Build Coastguard Worker  setInstance(0);
51*9880d681SAndroid Build Coastguard Worker}
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker}
54