Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
ThreadSingleton.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Templates/Function.h"
7#include "HAL/PlatformTLS.h"
8#include "HAL/TlsAutoCleanup.h"
9
10template <typename FuncType> class TFunctionRef;
11
12/**
13 * Thread singleton initializer.
14 */
16{
17public:
18
19 /**
20 * @return an instance of a singleton for the current thread.
21 */
22 static FTlsAutoCleanup* Get( TFunctionRef<FTlsAutoCleanup*()> CreateInstance, uint32& TlsSlot );
23
24 /**
25 * @return an instance of the singleton if it exists on the current thread.
26 */
27 static FTlsAutoCleanup* TryGet(uint32& TlsSlot);
28
29 /**
30 * @return sets the TLS store to the instance and returns the previous instance.
31 */
32 static FTlsAutoCleanup* Inject(FTlsAutoCleanup* Instance, uint32& TlsSlot);
33};
34
35
36/**
37 * This a special version of singleton. It means that there is created only one instance for each thread.
38 * Calling Get() method is thread-safe.
39 */
40template < class T >
42{
44 /**
45 * @return TLS slot that holds a TThreadSingleton.
46 */
47 static uint32& GetTlsSlot()
48 {
49 static uint32 TlsSlot = 0xFFFFFFFF;
50 return TlsSlot;
51 }
52#else
53 /**
54 * @return TLS slot that holds a TThreadSingleton.
55 */
58#endif
59 static uint32& GetTlsSlot()
60 {
61 static uint32 TlsSlot = 0xFFFFFFFF;
62 return TlsSlot;
63 }
64#endif
65
66protected:
67
68 /** Default constructor. */
71 {}
72
74 {
75 // Clean the dangling pointer from the TLS.
76 check(GetTlsSlot() != 0xFFFFFFFF);
77 if(((FTlsAutoCleanup*)FPlatformTLS::GetTlsValue(GetTlsSlot())) == static_cast<FTlsAutoCleanup*>(this))
78 {
80 }
81 }
82
83 /**
84 * @return a new instance of the thread singleton.
85 */
87 {
88 return new T();
89 }
90
91 /** Thread ID of this thread singleton. */
92 const uint32 ThreadId;
93
94public:
95
96 /**
97 * @return an instance of a singleton for the current thread.
98 */
99 FORCEINLINE static T& Get()
100 {
101 return *(T*)FThreadSingletonInitializer::Get( [](){ return (FTlsAutoCleanup*)new T(); }, T::GetTlsSlot() ); //-V572
102 }
103
104 /**
105 * @param CreateInstance Function to call when a new instance must be created.
106 * @return an instance of a singleton for the current thread.
107 */
108 FORCEINLINE static T& Get(TFunctionRef<FTlsAutoCleanup*()> CreateInstance)
109 {
111 }
112
113 /**
114 * @return pointer to an instance of a singleton for the current thread. May be nullptr, prefer to use access by reference
115 */
116 FORCEINLINE static T* TryGet()
117 {
119 }
120
121 /**
122 * @return sets the TLS store to the instance and returns the previous instance.
123 */
124 FORCEINLINE static T* Inject(T* Instance)
125 {
127 }
128};
#define check(expr)
#define PLATFORM_APPLE
Definition Platform.h:44
#define PLATFORM_CONSOLE_DYNAMIC_LINK
Definition Platform.h:609
#define FORCEINLINE
Definition Platform.h:644
#define PLATFORM_UNIX
Definition Platform.h:59
static FTlsAutoCleanup * Inject(FTlsAutoCleanup *Instance, uint32 &TlsSlot)
static FTlsAutoCleanup * TryGet(uint32 &TlsSlot)
static FTlsAutoCleanup * Get(TFunctionRef< FTlsAutoCleanup *()> CreateInstance, uint32 &TlsSlot)
virtual ~TThreadSingleton()
static FTlsAutoCleanup * CreateInstance()
static FORCEINLINE T & Get(TFunctionRef< FTlsAutoCleanup *()> CreateInstance)
const uint32 ThreadId
static FORCEINLINE T * Inject(T *Instance)
static FORCEINLINE T * TryGet()
static uint32 & GetTlsSlot()
static FORCEINLINE T & Get()