-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathTls.h
More file actions
executable file
·165 lines (126 loc) · 3.71 KB
/
Tls.h
File metadata and controls
executable file
·165 lines (126 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifdef HX_TLS_H_OVERRIDE
// Users can define their own header to use here, but there is no API
// compatibility guarantee for future changes.
#include HX_TLS_H_OVERRIDE
#else
#ifndef HX_TLS_INCLUDED
#define HX_TLS_INCLUDED
#ifdef HX_WINDOWS
#ifdef HX_WINRT
// Nothing
#else
#if ! defined(__GNUC__) && !defined(__BORLANDC__)
#include <intrin.h>
#endif
extern "C"
{
__declspec(dllimport)
int __stdcall TlsSetValue(unsigned long dwTlsIndex, void *lpTlsValue);
__declspec(dllimport)
void * __stdcall TlsGetValue(unsigned long dwTlsIndex);
__declspec(dllimport)
unsigned long __stdcall TlsAlloc(void);
}
namespace hx {
template<typename DATA,bool FAST=false>
struct TLSData
{
static const size_t kMaxInlineSlots = 64;
TLSData()
{
mSlot = TlsAlloc();
TlsSetValue(mSlot,0);
#ifdef HXCPP_M64
mFastOffset = mSlot*sizeof(void *) + 0x1480;
#else
if (FAST || mSlot < kMaxInlineSlots)
mFastOffset = mSlot*sizeof(void *) + 0xE10;
else
mFastOffset = mSlot - kMaxInlineSlots;
#endif
}
inline DATA *operator=(DATA *inData)
{
TlsSetValue(mSlot,inData);
return inData;
}
inline operator DATA *()
{
#if !defined(HXCPP_M64) && (_MSC_VER >= 1400)
const size_t kTibExtraTlsOffset = 0xF94;
if (FAST || mSlot < kMaxInlineSlots)
return (DATA *)__readfsdword(mFastOffset);
DATA **extra = (DATA **)(__readfsdword(kTibExtraTlsOffset));
return extra[mFastOffset];
#elif (_MSC_VER >= 1400) & !defined(HXCPP_DEBUG) && !defined(HXCPP_ARM64)// 64 bit version...
if (mSlot < 64)
return (DATA *)__readgsqword(mFastOffset);
else
return (DATA *)TlsGetValue(mSlot);
#else
return (DATA *)TlsGetValue(mSlot);
#endif
}
int mSlot;
int mFastOffset;
};
} // end namespace hx
#define DECLARE_TLS_DATA(TYPE,NAME) \
hx::TLSData<TYPE> NAME;
#define DECLARE_FAST_TLS_DATA(TYPE,NAME) \
hx::TLSData<TYPE,true> NAME;
#define EXTERN_TLS_DATA(TYPE,NAME) \
extern hx::TLSData<TYPE> NAME;
#define EXTERN_FAST_TLS_DATA(TYPE,NAME) \
extern hx::TLSData<TYPE,true> NAME;
#endif
#else // not HX_WINDOWS
#include <pthread.h>
namespace hx
{
template<typename DATA,bool FAST=false>
struct TLSData
{
TLSData()
{
pthread_key_create(&mSlot, 0);
}
DATA *Get()
{
return (DATA *)pthread_getspecific(mSlot);
}
void Set(DATA *inData)
{
pthread_setspecific(mSlot,inData);
}
inline DATA *operator=(DATA *inData)
{
pthread_setspecific(mSlot,inData);
return inData;
}
inline operator DATA *() { return (DATA *)pthread_getspecific(mSlot); }
pthread_key_t mSlot;
};
} // end namespace hx
#endif
#ifdef HX_WINRT
#define DECLARE_TLS_DATA(TYPE,NAME) \
__declspec(thread) TYPE * NAME = nullptr;
#define DECLARE_FAST_TLS_DATA(TYPE,NAME) \
__declspec(thread) TYPE * NAME = nullptr;
#define EXTERN_TLS_DATA(TYPE,NAME) \
__declspec(thread) extern TYPE * NAME;
#define EXTERN_FAST_TLS_DATA(TYPE,NAME) \
__declspec(thread) extern TYPE * NAME;
#else
#define DECLARE_TLS_DATA(TYPE,NAME) \
hx::TLSData<TYPE> NAME;
#define DECLARE_FAST_TLS_DATA(TYPE,NAME) \
hx::TLSData<TYPE,true> NAME;
#define EXTERN_TLS_DATA(TYPE,NAME) \
extern hx::TLSData<TYPE> NAME;
#define EXTERN_FAST_TLS_DATA(TYPE,NAME) \
extern hx::TLSData<TYPE,true> NAME;
#endif
#endif
#endif