aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/memtrack/platformtls.h
blob: f134e68a8add7a4c78de0a1ae41f3b9a8d745fb5 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zenbase/zenbase.h>

/**
 * It should be possible to provide a generic implementation as long as a threadID is provided. We don't do that yet.
 */
struct FGenericPlatformTLS
{
	static const uint32_t InvalidTlsSlot = 0xFFFFFFFF;

	/**
	 * Return false if this is an invalid TLS slot
	 * @param SlotIndex the TLS index to check
	 * @return true if this looks like a valid slot
	 */
	static bool IsValidTlsSlot(uint32_t SlotIndex) { return SlotIndex != InvalidTlsSlot; }
};

#if ZEN_PLATFORM_WINDOWS

#	include <zencore/windows.h>

class FWindowsPlatformTLS : public FGenericPlatformTLS
{
public:
	static uint32_t AllocTlsSlot() { return ::TlsAlloc(); }

	static void FreeTlsSlot(uint32_t SlotIndex) { ::TlsFree(SlotIndex); }

	static void SetTlsValue(uint32_t SlotIndex, void* Value) { ::TlsSetValue(SlotIndex, Value); }

	/**
	 * Reads the value stored at the specified TLS slot
	 *
	 * @return the value stored in the slot
	 */
	static void* GetTlsValue(uint32_t SlotIndex) { return ::TlsGetValue(SlotIndex); }

	/**
	 * Return false if this is an invalid TLS slot
	 * @param SlotIndex the TLS index to check
	 * @return true if this looks like a valid slot
	 */
	static bool IsValidTlsSlot(uint32_t SlotIndex) { return SlotIndex != InvalidTlsSlot; }
};

typedef FWindowsPlatformTLS FPlatformTLS;

#elif ZEN_PLATFORM_MAC

#	include <pthread.h

/**
 * Apple implementation of the TLS OS functions
 **/
struct FApplePlatformTLS : public FGenericPlatformTLS
{
	/**
	 * Returns the currently executing thread's id
	 */
	static uint32_t GetCurrentThreadId(void) { return (uint32_t)pthread_mach_thread_np(pthread_self()); }

	/**
	 * Allocates a thread local store slot
	 */
	static uint32_t AllocTlsSlot(void)
	{
		// allocate a per-thread mem slot
		pthread_key_t SlotKey = 0;
		if (pthread_key_create(&SlotKey, NULL) != 0)
		{
			SlotKey = InvalidTlsSlot;  // matches the Windows TlsAlloc() retval.
		}
		return SlotKey;
	}

	/**
	 * Sets a value in the specified TLS slot
	 *
	 * @param SlotIndex the TLS index to store it in
	 * @param Value the value to store in the slot
	 */
	static void SetTlsValue(uint32_t SlotIndex, void* Value) { pthread_setspecific((pthread_key_t)SlotIndex, Value); }

	/**
	 * Reads the value stored at the specified TLS slot
	 *
	 * @return the value stored in the slot
	 */
	static void* GetTlsValue(uint32_t SlotIndex) { return pthread_getspecific((pthread_key_t)SlotIndex); }

	/**
	 * Frees a previously allocated TLS slot
	 *
	 * @param SlotIndex the TLS index to store it in
	 */
	static void FreeTlsSlot(uint32_t SlotIndex) { pthread_key_delete((pthread_key_t)SlotIndex); }
};

typedef FApplePlatformTLS FPlatformTLS;

#else
#	error Platform not yet supported
#endif