mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-25 23:29:55 -06:00
avoid leaking threads in NDSCart_SRAMManager
also atomics
This commit is contained in:
@ -33,7 +33,7 @@ void RenderThreadFunc();
|
|||||||
|
|
||||||
void SoftRenderer::StopRenderThread()
|
void SoftRenderer::StopRenderThread()
|
||||||
{
|
{
|
||||||
if (RenderThreadRunning)
|
if (RenderThreadRunning.load(std::memory_order_relaxed))
|
||||||
{
|
{
|
||||||
RenderThreadRunning = false;
|
RenderThreadRunning = false;
|
||||||
Platform::Semaphore_Post(Sema_RenderStart);
|
Platform::Semaphore_Post(Sema_RenderStart);
|
||||||
@ -46,7 +46,7 @@ void SoftRenderer::SetupRenderThread()
|
|||||||
{
|
{
|
||||||
if (Threaded)
|
if (Threaded)
|
||||||
{
|
{
|
||||||
if (!RenderThreadRunning)
|
if (!RenderThreadRunning.load(std::memory_order_relaxed))
|
||||||
{
|
{
|
||||||
RenderThreadRunning = true;
|
RenderThreadRunning = true;
|
||||||
RenderThread = Platform::Thread_Create(std::bind(&SoftRenderer::RenderThreadFunc, this));
|
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()
|
void SoftRenderer::VCount144()
|
||||||
{
|
{
|
||||||
if (RenderThreadRunning)
|
if (RenderThreadRunning.load(std::memory_order_relaxed))
|
||||||
Platform::Semaphore_Wait(Sema_RenderDone);
|
Platform::Semaphore_Wait(Sema_RenderDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1660,7 +1660,7 @@ void SoftRenderer::RenderFrame()
|
|||||||
|
|
||||||
FrameIdentical = !(textureChanged || texPalChanged) && RenderFrameIdentical;
|
FrameIdentical = !(textureChanged || texPalChanged) && RenderFrameIdentical;
|
||||||
|
|
||||||
if (RenderThreadRunning)
|
if (RenderThreadRunning.load(std::memory_order_relaxed))
|
||||||
{
|
{
|
||||||
Platform::Semaphore_Post(Sema_RenderStart);
|
Platform::Semaphore_Post(Sema_RenderStart);
|
||||||
}
|
}
|
||||||
@ -1701,7 +1701,7 @@ void SoftRenderer::RenderThreadFunc()
|
|||||||
|
|
||||||
u32* SoftRenderer::GetLine(int line)
|
u32* SoftRenderer::GetLine(int line)
|
||||||
{
|
{
|
||||||
if (RenderThreadRunning)
|
if (RenderThreadRunning.load(std::memory_order_relaxed))
|
||||||
{
|
{
|
||||||
if (line < 192)
|
if (line < 192)
|
||||||
Platform::Semaphore_Wait(Sema_ScanlineCount);
|
Platform::Semaphore_Wait(Sema_ScanlineCount);
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include "GPU3D.h"
|
#include "GPU3D.h"
|
||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace GPU3D
|
namespace GPU3D
|
||||||
{
|
{
|
||||||
@ -506,8 +507,8 @@ private:
|
|||||||
|
|
||||||
bool Threaded;
|
bool Threaded;
|
||||||
Platform::Thread* RenderThread;
|
Platform::Thread* RenderThread;
|
||||||
bool RenderThreadRunning;
|
std::atomic_bool RenderThreadRunning;
|
||||||
bool RenderThreadRendering;
|
std::atomic_bool RenderThreadRendering;
|
||||||
Platform::Semaphore* Sema_RenderStart;
|
Platform::Semaphore* Sema_RenderStart;
|
||||||
Platform::Semaphore* Sema_RenderDone;
|
Platform::Semaphore* Sema_RenderDone;
|
||||||
Platform::Semaphore* Sema_ScanlineCount;
|
Platform::Semaphore* Sema_ScanlineCount;
|
||||||
|
@ -20,41 +20,43 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <atomic>
|
||||||
#include "NDSCart_SRAMManager.h"
|
#include "NDSCart_SRAMManager.h"
|
||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
|
|
||||||
namespace NDSCart_SRAMManager
|
namespace NDSCart_SRAMManager
|
||||||
{
|
{
|
||||||
Platform::Thread* FlushThread;
|
|
||||||
bool FlushThreadRunning;
|
|
||||||
Platform::Mutex* SecondaryBufferLock;
|
|
||||||
|
|
||||||
char Path[1024];
|
Platform::Thread* FlushThread;
|
||||||
|
std::atomic_bool FlushThreadRunning;
|
||||||
|
Platform::Mutex* SecondaryBufferLock;
|
||||||
|
|
||||||
u8* Buffer;
|
char Path[1024];
|
||||||
u32 Length;
|
|
||||||
|
|
||||||
u8* SecondaryBuffer;
|
u8* Buffer;
|
||||||
u32 SecondaryBufferLength;
|
u32 Length;
|
||||||
|
|
||||||
time_t TimeAtLastFlushRequest;
|
u8* SecondaryBuffer;
|
||||||
|
u32 SecondaryBufferLength;
|
||||||
|
|
||||||
// We keep versions in case the user closes the application before
|
time_t TimeAtLastFlushRequest;
|
||||||
// a flush cycle is finished.
|
|
||||||
u32 PreviousFlushVersion;
|
|
||||||
u32 FlushVersion;
|
|
||||||
|
|
||||||
void FlushThreadFunc();
|
// We keep versions in case the user closes the application before
|
||||||
|
// a flush cycle is finished.
|
||||||
|
u32 PreviousFlushVersion;
|
||||||
|
u32 FlushVersion;
|
||||||
|
|
||||||
bool Init()
|
void FlushThreadFunc();
|
||||||
{
|
|
||||||
|
bool Init()
|
||||||
|
{
|
||||||
SecondaryBufferLock = Platform::Mutex_Create();
|
SecondaryBufferLock = Platform::Mutex_Create();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeInit()
|
void DeInit()
|
||||||
{
|
{
|
||||||
if (FlushThreadRunning)
|
if (FlushThreadRunning)
|
||||||
{
|
{
|
||||||
FlushThreadRunning = false;
|
FlushThreadRunning = false;
|
||||||
@ -63,14 +65,14 @@ namespace NDSCart_SRAMManager
|
|||||||
FlushSecondaryBuffer();
|
FlushSecondaryBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SecondaryBuffer) delete SecondaryBuffer;
|
if (SecondaryBuffer) delete[] SecondaryBuffer;
|
||||||
SecondaryBuffer = NULL;
|
SecondaryBuffer = NULL;
|
||||||
|
|
||||||
Platform::Mutex_Free(SecondaryBufferLock);
|
Platform::Mutex_Free(SecondaryBufferLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Setup(const char* path, u8* buffer, u32 length)
|
void Setup(const char* path, u8* buffer, u32 length)
|
||||||
{
|
{
|
||||||
// Flush SRAM in case there is unflushed data from previous state.
|
// Flush SRAM in case there is unflushed data from previous state.
|
||||||
FlushSecondaryBuffer();
|
FlushSecondaryBuffer();
|
||||||
|
|
||||||
@ -82,7 +84,7 @@ namespace NDSCart_SRAMManager
|
|||||||
Buffer = buffer;
|
Buffer = buffer;
|
||||||
Length = length;
|
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];
|
SecondaryBuffer = new u8[length];
|
||||||
SecondaryBufferLength = length;
|
SecondaryBufferLength = length;
|
||||||
@ -93,25 +95,31 @@ namespace NDSCart_SRAMManager
|
|||||||
|
|
||||||
Platform::Mutex_Unlock(SecondaryBufferLock);
|
Platform::Mutex_Unlock(SecondaryBufferLock);
|
||||||
|
|
||||||
if (path[0] != '\0')
|
if (path[0] != '\0' && !FlushThreadRunning)
|
||||||
{
|
{
|
||||||
FlushThread = Platform::Thread_Create(FlushThreadFunc);
|
FlushThread = Platform::Thread_Create(FlushThreadFunc);
|
||||||
FlushThreadRunning = true;
|
FlushThreadRunning = true;
|
||||||
}
|
}
|
||||||
}
|
else if (path[0] == '\0' && FlushThreadRunning)
|
||||||
|
|
||||||
void RequestFlush()
|
|
||||||
{
|
{
|
||||||
|
FlushThreadRunning = false;
|
||||||
|
Platform::Thread_Wait(FlushThread);
|
||||||
|
Platform::Thread_Free(FlushThread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RequestFlush()
|
||||||
|
{
|
||||||
Platform::Mutex_Lock(SecondaryBufferLock);
|
Platform::Mutex_Lock(SecondaryBufferLock);
|
||||||
printf("NDS SRAM: Flush requested\n");
|
printf("NDS SRAM: Flush requested\n");
|
||||||
memcpy(SecondaryBuffer, Buffer, Length);
|
memcpy(SecondaryBuffer, Buffer, Length);
|
||||||
FlushVersion++;
|
FlushVersion++;
|
||||||
TimeAtLastFlushRequest = time(NULL);
|
TimeAtLastFlushRequest = time(NULL);
|
||||||
Platform::Mutex_Unlock(SecondaryBufferLock);
|
Platform::Mutex_Unlock(SecondaryBufferLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlushThreadFunc()
|
void FlushThreadFunc()
|
||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
Platform::Sleep(100 * 1000); // 100ms
|
Platform::Sleep(100 * 1000); // 100ms
|
||||||
@ -126,10 +134,10 @@ namespace NDSCart_SRAMManager
|
|||||||
|
|
||||||
FlushSecondaryBuffer();
|
FlushSecondaryBuffer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlushSecondaryBuffer(u8* dst, s32 dstLength)
|
void FlushSecondaryBuffer(u8* dst, s32 dstLength)
|
||||||
{
|
{
|
||||||
// When flushing to a file, there's no point in re-writing the exact same data.
|
// When flushing to a file, there's no point in re-writing the exact same data.
|
||||||
if (!dst && !NeedsFlush()) return;
|
if (!dst && !NeedsFlush()) return;
|
||||||
// When flushing to memory, we don't know if dst already has any data so we only check that we CAN flush.
|
// When flushing to memory, we don't know if dst already has any data so we only check that we CAN flush.
|
||||||
@ -153,15 +161,15 @@ namespace NDSCart_SRAMManager
|
|||||||
PreviousFlushVersion = FlushVersion;
|
PreviousFlushVersion = FlushVersion;
|
||||||
TimeAtLastFlushRequest = 0;
|
TimeAtLastFlushRequest = 0;
|
||||||
Platform::Mutex_Unlock(SecondaryBufferLock);
|
Platform::Mutex_Unlock(SecondaryBufferLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NeedsFlush()
|
bool NeedsFlush()
|
||||||
{
|
{
|
||||||
return FlushVersion != PreviousFlushVersion;
|
return FlushVersion != PreviousFlushVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateBuffer(u8* src, s32 srcLength)
|
void UpdateBuffer(u8* src, s32 srcLength)
|
||||||
{
|
{
|
||||||
if (!src || srcLength != Length) return;
|
if (!src || srcLength != Length) return;
|
||||||
|
|
||||||
// should we create a lock for the primary buffer? this method is not intended to be called from a secondary thread in the way Flush is
|
// should we create a lock for the primary buffer? this method is not intended to be called from a secondary thread in the way Flush is
|
||||||
@ -171,5 +179,6 @@ namespace NDSCart_SRAMManager
|
|||||||
Platform::Mutex_Unlock(SecondaryBufferLock);
|
Platform::Mutex_Unlock(SecondaryBufferLock);
|
||||||
|
|
||||||
PreviousFlushVersion = FlushVersion;
|
PreviousFlushVersion = FlushVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user