summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Winapi/PlatformThreadSpecificValue.h
blob: 8e6d42a3ea0ca414099f3052cd67bfabf8bfc91f (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
#ifndef PLATFORMTHREADSPECIFICVALUE_H
#define PLATFORMTHREADSPECIFICVALUE_H

#if UNITY_DYNAMIC_TLS

#if UNITY_WINRT
#include "PlatformDependent/MetroPlayer/Win32Threads.h"
using win32::TlsAlloc;
using win32::TlsFree;
using win32::TlsGetValue;
using win32::TlsSetValue;
#endif

class PlatformThreadSpecificValue
{
public:
	PlatformThreadSpecificValue();
	~PlatformThreadSpecificValue();

	void* GetValue() const;
	void SetValue(void* value);

private:
	DWORD	m_TLSKey;
};

inline PlatformThreadSpecificValue::PlatformThreadSpecificValue ()
{
	m_TLSKey = TlsAlloc();
	AssertIf( m_TLSKey == TLS_OUT_OF_INDEXES );
}

inline PlatformThreadSpecificValue::~PlatformThreadSpecificValue ()
{
	TlsFree( m_TLSKey );
}

inline void* PlatformThreadSpecificValue::GetValue () const
{
#if UNITY_WIN
	void* result = TlsGetValue(m_TLSKey);
	DebugAssertIf( result == NULL && GetLastError() != ERROR_SUCCESS );
	return result;
#elif UNITY_XENON
	return TlsGetValue(m_TLSKey);	// on XENON TlsGetValue does not call SetLastError
#endif
}

inline void PlatformThreadSpecificValue::SetValue (void* value)
{
	BOOL ok = TlsSetValue( m_TLSKey, value );
	DebugAssertIf( !ok );
}

#else

	#define UNITY_TLS_VALUE(type) __declspec(thread) type

#endif // UNITY_DYNAMIC_TLS

#endif // PLATFORMTHREADSPECIFICVALUE_H