1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Lazy<T>, LazyTLS<T>, thread dtors

This commit is contained in:
Brian Fiete 2022-07-04 10:20:38 -07:00
parent cf269db0eb
commit a27ef9beda
17 changed files with 437 additions and 30 deletions

View file

@ -3,7 +3,7 @@
#include "BeefySysLib/Common.h"
#include "BeefySysLib/util/String.h"
#define BFRT_VERSION 9
#define BFRT_VERSION 10
#ifdef BFRT_DYNAMIC
#define BFRT_EXPORT __declspec(dllexport)
@ -99,6 +99,7 @@ namespace bf
bool(*Thread_IsAutoDelete)(bf::System::Threading::Thread* thread);
void(*Thread_AutoDelete)(bf::System::Threading::Thread* thread);
int32(*Thread_GetMaxStackSize)(bf::System::Threading::Thread* thread);
void(*Thread_Exiting)();
void(*GC_MarkAllStaticMembers)();
bool(*GC_CallRootCallbacks)();
void(*GC_Shutdown)();
@ -107,7 +108,7 @@ namespace bf
void(*DebugMessageData_SetupProfilerCmd)(const char* str);
void(*DebugMessageData_Fatal)();
void(*DebugMessageData_Clear)();
int(*CheckErrorHandler)(const char* kind, const char* arg1, const char* arg2, intptr arg3);
int(*CheckErrorHandler)(const char* kind, const char* arg1, const char* arg2, intptr arg3);
};
public:

View file

@ -55,6 +55,12 @@ static Beefy::StringT<0> gCmdLineString;
bf::System::Runtime::BfRtCallbacks gBfRtCallbacks;
BfRtFlags gBfRtFlags = (BfRtFlags)0;
#ifdef BF_PLATFORM_WINDOWS
DWORD gBfTLSKey = 0;
#else
pthread_key_t gBfTLSKey = 0;
#endif
static int gTestMethodIdx = -1;
static uint32 gTestStartTick = 0;
static bool gTestBreakOnFailure = false;
@ -287,6 +293,11 @@ static void GetCrashInfo()
}
}
static void NTAPI TlsFreeFunc(void* ptr)
{
gBfRtCallbacks.Thread_Exiting();
}
void bf::System::Runtime::Init(int version, int flags, BfRtCallbacks* callbacks)
{
BfpSystemInitFlags sysInitFlags = BfpSystemInitFlag_InstallCrashCatcher;
@ -343,6 +354,12 @@ void bf::System::Runtime::Init(int version, int flags, BfRtCallbacks* callbacks)
useCmdLineStr++;
}
gCmdLineString = useCmdLineStr;
#ifdef BF_PLATFORM_WINDOWS
gBfTLSKey = FlsAlloc(TlsFreeFunc);
#else
pthread_key_create(&gBfTLSKey, TlsFreeFunc);
#endif
}
void bf::System::Runtime::SetErrorString(char* errorStr)

View file

@ -19,6 +19,12 @@ BF_TLS_DECLSPEC Thread* Thread::sCurrentThread;
static volatile int gLiveThreadCount;
static Beefy::SyncEvent gThreadsDoneEvent;
#ifdef BF_PLATFORM_WINDOWS
extern DWORD gBfTLSKey;
#else
extern pthread_key_t gBfTLSKey;
#endif
bf::System::Threading::Thread* BfGetCurrentThread()
{
#ifdef BF_THREAD_TLS
@ -133,7 +139,8 @@ static void BF_CALLTYPE CStartProc(void* threadParam)
bool wantsDelete = false;
//
{
internalThread->ThreadStopped();
internalThread->ThreadStopped();
Beefy::AutoCrit autoCrit(internalThread->mCritSect);
if (isAutoDelete)
gBfRtCallbacks.Thread_AutoDelete(thread);
@ -207,15 +214,28 @@ void Thread::StartInternal()
#endif
}
void Thread::RequestExitNotify()
{
// Do we already have implicit exiting notification?
if (BfGetCurrentThread() != NULL)
return;
#ifdef BF_PLATFORM_WINDOWS
FlsSetValue(gBfTLSKey, (void*)&gBfRtCallbacks);
#else
pthread_setspecific(gBfTLSKey, (void*)&gBfRtCallbacks);
#endif
}
void Thread::ThreadStarted()
{
auto internalThread = GetInternalThread();
internalThread->mCritSect.Unlock();
}
int Thread::GetThreadId()
intptr Thread::GetThreadId()
{
return (int)GetInternalThread()->mThreadId;
return GetInternalThread()->mThreadId;
}
void Thread::SetStackStart(void* ptr)

View file

@ -39,7 +39,7 @@ namespace bf
private:
BfInternalThread* SetupInternalThread();
BFRT_EXPORT void ManualThreadInit();
BFRT_EXPORT void ManualThreadInit();
BFRT_EXPORT int GetPriorityNative();
BFRT_EXPORT void SetPriorityNative(int priority);
BFRT_EXPORT void SetJoinOnDelete(bool joinOnDelete);
@ -59,7 +59,7 @@ namespace bf
BFRT_EXPORT void SetBackgroundNative(bool isBackground);
BFRT_EXPORT int GetThreadStateNative();
BFRT_EXPORT void InformThreadNameChange(String* name);
BFRT_EXPORT int GetThreadId();
BFRT_EXPORT intptr GetThreadId();
BFRT_EXPORT void Dbg_CreateInternal();
@ -68,6 +68,7 @@ namespace bf
BFRT_EXPORT void Resume();
BFRT_EXPORT void Abort();
BFRT_EXPORT static void RequestExitNotify();
BFRT_EXPORT static void MemoryBarrier();
static Thread* Alloc()

View file

@ -35,7 +35,7 @@ uint32 BfTLSManager::Alloc()
}
}
mAllocatedKeys[idx] = BfpTLS_Create();
mAllocatedKeys[idx] = BfpTLS_Create(NULL);
mAssociatedTLSDatums[idx] = NULL;
return idx;
}

View file

@ -43,7 +43,7 @@ public:
BfTLSManager()
{
sInternalThreadKey = BfpTLS_Create();
sInternalThreadKey = BfpTLS_Create(NULL);
mAssociatedTLSDatums = NULL;
mAllocSize = 0;
mAllocIdx = 1;