avoid leaking threads in NDSCart_SRAMManager

also atomics
This commit is contained in:
RSDuck 2021-03-11 16:54:27 +01:00
parent ae7761c33e
commit bc63531e00
3 changed files with 150 additions and 140 deletions

View File

@ -33,7 +33,7 @@ void RenderThreadFunc();
void SoftRenderer::StopRenderThread()
{
if (RenderThreadRunning)
if (RenderThreadRunning.load(std::memory_order_relaxed))
{
RenderThreadRunning = false;
Platform::Semaphore_Post(Sema_RenderStart);
@ -46,7 +46,7 @@ void SoftRenderer::SetupRenderThread()
{
if (Threaded)
{
if (!RenderThreadRunning)
if (!RenderThreadRunning.load(std::memory_order_relaxed))
{
RenderThreadRunning = true;
RenderThread = Platform::Thread_Create(std::bind(&SoftRenderer::RenderThreadFunc, this));
@ -1646,7 +1646,7 @@ void SoftRenderer::RenderPolygons(bool threaded, Polygon** polygons, int npolys)
void SoftRenderer::VCount144()
{
if (RenderThreadRunning)
if (RenderThreadRunning.load(std::memory_order_relaxed))
Platform::Semaphore_Wait(Sema_RenderDone);
}
@ -1660,7 +1660,7 @@ void SoftRenderer::RenderFrame()
FrameIdentical = !(textureChanged || texPalChanged) && RenderFrameIdentical;
if (RenderThreadRunning)
if (RenderThreadRunning.load(std::memory_order_relaxed))
{
Platform::Semaphore_Post(Sema_RenderStart);
}
@ -1701,7 +1701,7 @@ void SoftRenderer::RenderThreadFunc()
u32* SoftRenderer::GetLine(int line)
{
if (RenderThreadRunning)
if (RenderThreadRunning.load(std::memory_order_relaxed))
{
if (line < 192)
Platform::Semaphore_Wait(Sema_ScanlineCount);

View File

@ -21,6 +21,7 @@
#include "GPU3D.h"
#include "Platform.h"
#include <thread>
#include <atomic>
namespace GPU3D
{
@ -506,8 +507,8 @@ private:
bool Threaded;
Platform::Thread* RenderThread;
bool RenderThreadRunning;
bool RenderThreadRendering;
std::atomic_bool RenderThreadRunning;
std::atomic_bool RenderThreadRendering;
Platform::Semaphore* Sema_RenderStart;
Platform::Semaphore* Sema_RenderDone;
Platform::Semaphore* Sema_ScanlineCount;

View File

@ -20,13 +20,15 @@
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <atomic>
#include "NDSCart_SRAMManager.h"
#include "Platform.h"
namespace NDSCart_SRAMManager
{
Platform::Thread* FlushThread;
bool FlushThreadRunning;
std::atomic_bool FlushThreadRunning;
Platform::Mutex* SecondaryBufferLock;
char Path[1024];
@ -63,7 +65,7 @@ namespace NDSCart_SRAMManager
FlushSecondaryBuffer();
}
if (SecondaryBuffer) delete SecondaryBuffer;
if (SecondaryBuffer) delete[] SecondaryBuffer;
SecondaryBuffer = NULL;
Platform::Mutex_Free(SecondaryBufferLock);
@ -82,7 +84,7 @@ namespace NDSCart_SRAMManager
Buffer = buffer;
Length = length;
if(SecondaryBuffer) delete SecondaryBuffer; // Delete secondary buffer, there might be previous state.
if(SecondaryBuffer) delete[] SecondaryBuffer; // Delete secondary buffer, there might be previous state.
SecondaryBuffer = new u8[length];
SecondaryBufferLength = length;
@ -93,11 +95,17 @@ namespace NDSCart_SRAMManager
Platform::Mutex_Unlock(SecondaryBufferLock);
if (path[0] != '\0')
if (path[0] != '\0' && !FlushThreadRunning)
{
FlushThread = Platform::Thread_Create(FlushThreadFunc);
FlushThreadRunning = true;
}
else if (path[0] == '\0' && FlushThreadRunning)
{
FlushThreadRunning = false;
Platform::Thread_Wait(FlushThread);
Platform::Thread_Free(FlushThread);
}
}
void RequestFlush()
@ -172,4 +180,5 @@ namespace NDSCart_SRAMManager
PreviousFlushVersion = FlushVersion;
}
}