-
Notifications
You must be signed in to change notification settings - Fork 444
/
tlsdata.h
80 lines (73 loc) · 1.25 KB
/
tlsdata.h
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
#pragma once
template <class T>
class CTlsData
{
private:
DWORD tlsindex;
CPtrArray<T>* pArray;
enum { INVALID_TLS_VALUE = 0xffffffff };
public:
T* GetPtr() const
{
if(tlsindex == INVALID_TLS_VALUE)
return NULL;
T* pT = reinterpret_cast<T*>(::TlsGetValue(tlsindex));
if(pT)
return pT;
pT = new T;
if(!pT)
return NULL;
if(!::TlsSetValue(tlsindex, pT)) {
delete pT;
return NULL;
}
if(pArray) {
pArray->Add(pT);
}
return pT;
}
bool ProcessInit()
{
tlsindex = ::TlsAlloc();
if (tlsindex == INVALID_TLS_VALUE) {
return false;
}
pArray = new CPtrArray<T>;
if (!pArray) {
::TlsFree(tlsindex);
tlsindex = INVALID_TLS_VALUE;
return false;
}
return true;
}
void ProcessTerm()
{
if(tlsindex == INVALID_TLS_VALUE) {
return;
}
ThreadTerm(); //これ入れないとリークする
::TlsFree(tlsindex);
tlsindex = INVALID_TLS_VALUE;
if (pArray) {
for(int i = 0; i < pArray->GetSize(); i++) {
delete pArray->operator[](i);
}
delete pArray;
pArray = NULL;
}
}
// bool ThreadInit()
// {
// }
void ThreadTerm()
{
T* pT = reinterpret_cast<T*>(::TlsGetValue(tlsindex));
if(pT) {
if (pArray) {
pArray->Remove(pT);
}
delete pT;
}
::TlsSetValue(tlsindex, NULL);
}
};