aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PSVita/PSVitaExtras/PSVitaTLSStorage.cpp
blob: 054e9f025d88ee163c075e0836527e98e0e45ab0 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "stdafx.h"
#include "PSVitaTLSStorage.h"

#if 1
static PSVitaTLSStorage Singleton;

#define MAX_THREADS 64

typedef struct
{
	int ThreadID;
	LPVOID Value;
} TLSInfo;

typedef struct
{
	TLSInfo *Array;
	int Size;
} TLSArray;

static std::vector<TLSArray *> PSVitaTLSStorage_ActiveInfos;

void PSVitaTLSStorage::Init()
{
}

PSVitaTLSStorage *PSVitaTLSStorage::Instance()
{
	return &Singleton;
}

DWORD PSVitaTLSStorage::Alloc()
{
	TLSArray* Array = new TLSArray;
	Array->Array = new TLSInfo[MAX_THREADS];
	Array->Size = 0;
	for( int i = 0;i < MAX_THREADS;i += 1 )
	{
		Array->Array[i].ThreadID = -1;
	}

	// add to the active infos
	PSVitaTLSStorage_ActiveInfos.push_back(Array);
	return (DWORD) Array;
}

BOOL PSVitaTLSStorage::Free(DWORD dwTlsIndex)
{
	// remove from the active infos
	std::vector<TLSArray *>::iterator iter = std::find(PSVitaTLSStorage_ActiveInfos.begin(), PSVitaTLSStorage_ActiveInfos.end(), (TLSArray *) dwTlsIndex);
	PSVitaTLSStorage_ActiveInfos.erase(iter);

	delete []((TLSInfo*)dwTlsIndex);
	return 0;
}

void PSVitaTLSStorage::RemoveThread(int threadID)
{
	// remove this thread from all the active Infos
	std::vector<TLSArray *>::iterator iter = PSVitaTLSStorage_ActiveInfos.begin();
	for( ; iter != PSVitaTLSStorage_ActiveInfos.end(); ++iter )
	{
		TLSArray* Array = ((TLSArray*)*iter);

		for( int i = 0;i < Array->Size;i += 1 )
		{
			if( Array->Array[i].ThreadID == threadID )
			{
				// shift all the other entries down
				for( ;i < MAX_THREADS - 1;i += 1 )
				{
					Array->Array[i].ThreadID = Array->Array[i+1].ThreadID;
					Array->Array[i].Value = Array->Array[i+1].Value;
					if( Array->Array[i].ThreadID != -1 )
					{
						Array->Size = i + 1;
					}
				}
				// mark the top one as unused
				Array->Array[i].ThreadID = -1;
				break;
			}
		}
	}
}

LPVOID PSVitaTLSStorage::GetValue(DWORD dwTlsIndex)
{
	if( !dwTlsIndex )
	{
		return 0;
	}

	TLSArray* Array = ((TLSArray*)dwTlsIndex);
	SceUID threadID = sceKernelGetThreadId();
	for( int i = 0;i < Array->Size;i += 1 )
	{
		if( Array->Array[i].ThreadID == threadID )
		{
			return Array->Array[i].Value;
		}
	}

	//assert(0);
	return 0;
}

BOOL PSVitaTLSStorage::SetValue(DWORD dwTlsIndex, LPVOID lpTlsValue)
{
	if( !dwTlsIndex )
	{
		return 0;
	}

	TLSArray* Array = ((TLSArray*)dwTlsIndex);
	SceUID threadID = sceKernelGetThreadId();
	for( int i = 0;i < Array->Size;i += 1 )
	{
		if( Array->Array[i].ThreadID == threadID || Array->Array[i].ThreadID == -1 )
		{
			Array->Array[i].ThreadID = threadID;
			Array->Array[i].Value = lpTlsValue;
			return 0;
		}
	}
	if( Array->Size < MAX_THREADS )
	{
		Array->Array[Array->Size].ThreadID = threadID;
		Array->Array[Array->Size].Value = lpTlsValue;
		Array->Size++;
		return 0;
	}

	assert(0);
	return 0;
}
#else


PSVitaTLSStorage* m_pInstance = nullptr;

#define sc_maxSlots 64
BOOL m_activeList[sc_maxSlots];
//__thread void* m_values[64];

// AP - Oh my. It seems __thread doesn't like cpp files. I couldn't get it to be thread sensitive as it would always return a shared value.
// For now I've stuck m_values and accessor functions into user_malloc.c. Cheap cheap hack.
extern "C"
{
void user_setValue( unsigned int _index, void* _val );
void* user_getValue( unsigned int _index );
}


void PSVitaTLSStorage::Init()
{
	for(int i=0;i<sc_maxSlots; i++)
	{
		m_activeList[i] = false;
//		m_values[i] = nullptr;
	}
}

PSVitaTLSStorage* PSVitaTLSStorage::Instance()
{
	if ( m_pInstance == 0 )			// Is this the first time?
	{
		m_pInstance = new PSVitaTLSStorage;		// Create the singleton instance.
		m_pInstance->Init();
	}
	return m_pInstance;
}


DWORD PSVitaTLSStorage::Alloc()
{
	for(int i=0; i<sc_maxSlots; i++) 
	{
		if(m_activeList[i] == false)
		{
			m_activeList[i] = true;
//			m_values[i] = nullptr;
			return i;
		}
	}
	assert(0);		// we've ran out of slots
	return -1;
}

BOOL PSVitaTLSStorage::Free( DWORD _index )
{
	if(m_activeList[_index] == false)
		return false; // not been allocated

	m_activeList[_index] = false;
//	m_values[_index] = nullptr;
	return true;
}

BOOL PSVitaTLSStorage::SetValue( DWORD _index, LPVOID _val )
{
	if(m_activeList[_index] == false)
		return false;
	user_setValue(_index, _val);
//	m_values[_index] = _val;
	return true;
}

LPVOID PSVitaTLSStorage::GetValue( DWORD _index )
{
	if(m_activeList[_index] == false)
		return nullptr;
	return user_getValue(_index);
//	return m_values[_index];
}

void PSVitaTLSStorage::RemoveThread(int threadID)
{
	for(int i=0; i<sc_maxSlots; i++) 
	{
		//m_values[i] = nullptr;
		user_setValue(i, nullptr);
	}
}

// AP - Oh my. It seems __thread doesn't like cpp files. I couldn't get it to be thread sensitive as it would always return a shared value.
// For now I've stuck m_values and accessor functions into user_malloc.c. Cheap cheap hack.
__thread void* m_values[64];
void user_setValue( unsigned int _index, void* _val )
{
	m_values[_index] = _val;
}
void* user_getValue( unsigned int _index )
{
	return m_values[_index];
}
#endif