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

#if UNITY_DYNAMIC_TLS

#include <pthread.h>
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Utilities/Utility.h"

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

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

private:
	pthread_key_t m_TLSKey;
};

inline PlatformThreadSpecificValue::PlatformThreadSpecificValue()
{
	int rc = pthread_key_create(&m_TLSKey, NULL);
	DebugAssertIf(rc != 0);
	UNUSED(rc);
}

inline PlatformThreadSpecificValue::~PlatformThreadSpecificValue()
{
	int rc = pthread_key_delete(m_TLSKey);
	DebugAssertIf(rc != 0);
	UNUSED(rc);
}

inline void* PlatformThreadSpecificValue::GetValue() const
{
#if !UNITY_LINUX
	// 0 is a valid key on Linux and POSIX specifies keys as opaque objects,
	// so technically we have no business snopping in them anyway...
	DebugAssertIf(m_TLSKey == 0);
#endif
	return pthread_getspecific(m_TLSKey);
}

inline void PlatformThreadSpecificValue::SetValue(void* value)
{
#if !UNITY_LINUX
	// 0 is a valid key on Linux and POSIX specifies keys as opaque objects,
	// so technically we have no business snopping in them anyway...
	DebugAssertIf(m_TLSKey == 0);
#endif
	pthread_setspecific(m_TLSKey, value);
}

#else

	#error "POSIX doesn't define a static TLS path"

#endif // UNITY_DYNAMIC_TLS

#endif // PLATFORMTHREADSPECIFICVALUE_H