1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Fixes from valgrind

This commit is contained in:
Brian Fiete 2022-03-18 18:06:14 -07:00
parent 0feaaded22
commit 676e7988fb
31 changed files with 243 additions and 147 deletions

View file

@ -547,8 +547,11 @@ void Internal::Dbg_ObjectPreCustomDelete(bf::System::Object* object)
{
BF_ASSERT((BFRTFLAGS & BfRtFlags_ObjectHasDebugFlags) != 0);
const char* errorPtr = NULL;
if ((object->mObjectFlags & BfObjectFlag_AppendAlloc) != 0)
return;
const char* errorPtr = NULL;
if ((object->mObjectFlags & BfObjectFlag_StackAlloc) != 0)
errorPtr = "Attempting to delete stack-allocated object";
if ((object->mObjectFlags & BfObjectFlag_Deleted) != 0)
@ -564,8 +567,7 @@ void Internal::Dbg_ObjectPreCustomDelete(bf::System::Object* object)
errorStr += StrFormat(" (%s)0x%@\n", typeName.c_str(), object);
SETUP_ERROR(errorStr.c_str(), 2);
BF_DEBUG_BREAK();
BFRTCALLBACKS.DebugMessageData_Fatal();
return;
BFRTCALLBACKS.DebugMessageData_Fatal();
}
}

View file

@ -120,16 +120,21 @@ static void BF_CALLTYPE CStartProc(void* threadParam)
#endif
auto internalThread = thread->GetInternalThread();
// Hold lock until we get ThreadStarted callback
internalThread->mCritSect.Lock();
internalThread->mStartedEvent.Set(true);
internalThread->mThreadHandle = BfpThread_GetCurrent();
internalThread->mStackStart = (intptr)&thread;
internalThread->ThreadStarted();
bool isAutoDelete = gBfRtCallbacks.Thread_IsAutoDelete(thread);
gBfRtCallbacks.Thread_ThreadProc(thread);
bool isLastThread = BfpSystem_InterlockedExchangeAdd32((uint32*)&gLiveThreadCount, -1) == 1;
//printf("Stopping thread\n");
bool wantsDelete = false;
//
{
@ -143,8 +148,7 @@ static void BF_CALLTYPE CStartProc(void* threadParam)
if (internalThread->mThread == NULL)
{
// If the thread was already deleted then we need to delete ourselves now
wantsDelete = true;
internalThread = NULL;
wantsDelete = true;
}
}
@ -194,6 +198,9 @@ void Thread::StartInternal()
BfpSystem_InterlockedExchangeAdd32((uint32*)&gLiveThreadCount, 1);
BfInternalThread* internalThread = SetupInternalThread();
Beefy::AutoCrit autoCrit(internalThread->mCritSect);
internalThread->mStarted = true;
internalThread->mThread = this;
#ifdef _WIN32
internalThread->mThreadHandle = BfpThread_Create(CStartProc, (void*)this, GetMaxStackSize(), (BfpThreadCreateFlags)(BfpThreadCreateFlag_StackSizeReserve | BfpThreadCreateFlag_Suspended), &internalThread->mThreadId);
@ -205,6 +212,12 @@ void Thread::StartInternal()
#endif
}
void Thread::ThreadStarted()
{
auto internalThread = GetInternalThread();
internalThread->mCritSect.Unlock();
}
int Thread::GetThreadId()
{
return (int)GetInternalThread()->mThreadId;
@ -221,11 +234,22 @@ void Thread::InternalFinalize()
auto internalThread = GetInternalThread();
if (internalThread == NULL)
return;
bool wantsJoin = false;
bool started = false;
//
{
Beefy::AutoCrit autoCrit(internalThread->mCritSect);
started = internalThread->mStarted;
}
if (started)
internalThread->mStartedEvent.WaitFor();
//
{
Beefy::AutoCrit autoCrit(internalThread->mCritSect);
if ((!internalThread->mDone) && (internalThread->mJoinOnDelete))
{
if (this != BfGetCurrentThread())
@ -255,6 +279,9 @@ void Thread::InternalFinalize()
SetInternalThread(NULL);
}
if (internalThread->mIsManualInit)
wantsDelete = true;
if (wantsDelete)
delete internalThread;
}
@ -283,53 +310,3 @@ void Thread::MemoryBarrier()
{
BF_FULL_MEMORY_FENCE();
}
//////////////////////////////////////////////////////////////////////////
// BfInternalThread::BfInternalThread()
// {
// #ifdef BF_GC_SUPPORTED
// mBFIThreadData = NULL;
// #endif
//
// mThread = NULL;
// mThreadHandle = 0;
// mStarted = false;
// mRunning = false;
// mDone = false;
// mIsSuspended = false;
// }
//
// BfInternalThread::~BfInternalThread()
// {
// if (mThreadHandle != 0)
// {
// BfpThread_Release(mThreadHandle);
// }
// }
//
// void BfInternalThread::ManualThreadInit(bf::System::Threading::Thread* thread)
// {
// bf::System::Threading::Thread* newThread = thread;
//
// mStarted = true;
// mThread = newThread;
// newThread->SetInternalThread(this);
// mThreadId = BfpThread_GetCurrentId();
// mThreadHandle = BfpThread_GetCurrent();
// mStackStart = ((intptr)&newThread + 0xFFF) & ~(intptr)0xFFF;
// ThreadStarted();
// }
// void BfInternalThread::ThreadStarted()
// {
// BF_ASSERT((gBfRtFlags & BfRtFlags_ObjectHasDebugFlags) == 0);
// int threadPriority = BfpThread_GetPriority(mThreadHandle, NULL);
// mRunning = true;
// }
//
// void BfInternalThread::ThreadStopped()
// {
// BF_ASSERT((gBfRtFlags & BfRtFlags_ObjectHasDebugFlags) == 0);
// mRunning = false;
// }

View file

@ -55,6 +55,7 @@ namespace bf
BFRT_EXPORT static Thread* GetCurrentThreadNative();
BFRT_EXPORT unsigned long GetProcessDefaultStackSize();
BFRT_EXPORT void StartInternal();
BFRT_EXPORT void ThreadStarted();
BFRT_EXPORT void SetStackStart(void* ptr);
BFRT_EXPORT void InternalFinalize();
BFRT_EXPORT bool IsBackgroundNative();
@ -107,9 +108,11 @@ public:
bool mDone;
bool mStarted;
bool mJoinOnDelete;
bool mIsManualInit;
BfpThread* mThreadHandle;
intptr mThreadId;
Beefy::CritSect mCritSect;
Beefy::SyncEvent mStartedEvent;
BfInternalThread()
{
@ -120,6 +123,7 @@ public:
mDone = false;
mIsSuspended = false;
mJoinOnDelete = true;
mIsManualInit = false;
mStackStart = 0;
mThreadId = 0;
}
@ -136,7 +140,9 @@ public:
{
bf::System::Threading::Thread* newThread = thread;
mIsManualInit = true;
mStarted = true;
mStartedEvent.Set(true);
mThread = newThread;
newThread->SetInternalThread(this);
mThreadId = BfpThread_GetCurrentId();