mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-08-19 05:02:12 -04:00
Merge pull request #205 from TakaRikka/shutdown
Allow threads to gracefully shutdown
This commit is contained in:
@@ -72,6 +72,15 @@ static PCCondData& GetCondData(OSCond* cond) {
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
void ClearCondMap() {
|
||||
std::lock_guard<std::mutex> lock(GetCondMapMutex());
|
||||
auto& map = GetCondMap();
|
||||
for (auto& pair : map) {
|
||||
pair.second->cv.notify_all();
|
||||
}
|
||||
map.clear();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// C API functions
|
||||
// ============================================================================
|
||||
|
||||
@@ -223,104 +223,3 @@ namespace dusk {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Limiter
|
||||
{
|
||||
using delta_clock = std::chrono::high_resolution_clock;
|
||||
using duration_t = std::chrono::nanoseconds;
|
||||
|
||||
public:
|
||||
void Reset()
|
||||
{
|
||||
m_oldTime = delta_clock::now();
|
||||
}
|
||||
|
||||
void Sleep(duration_t targetFrameTime)
|
||||
{
|
||||
if (targetFrameTime.count() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto start = delta_clock::now();
|
||||
duration_t adjustedSleepTime = SleepTime(targetFrameTime);
|
||||
if (adjustedSleepTime.count() > 0)
|
||||
{
|
||||
NanoSleep(adjustedSleepTime);
|
||||
duration_t overslept = TimeSince(start) - adjustedSleepTime;
|
||||
if (overslept < duration_t{ targetFrameTime })
|
||||
{
|
||||
m_overheadTimes[m_overheadTimeIdx] = overslept;
|
||||
m_overheadTimeIdx = (m_overheadTimeIdx + 1) % m_overheadTimes.size();
|
||||
}
|
||||
}
|
||||
Reset();
|
||||
}
|
||||
|
||||
duration_t SleepTime(duration_t targetFrameTime)
|
||||
{
|
||||
const auto sleepTime = duration_t{ targetFrameTime } - TimeSince(m_oldTime);
|
||||
m_overhead = std::accumulate(m_overheadTimes.begin(), m_overheadTimes.end(), duration_t{}) /
|
||||
m_overheadTimes.size();
|
||||
if (sleepTime > m_overhead)
|
||||
{
|
||||
return sleepTime - m_overhead;
|
||||
}
|
||||
return duration_t{ 0 };
|
||||
}
|
||||
|
||||
private:
|
||||
delta_clock::time_point m_oldTime;
|
||||
std::array<duration_t, 4> m_overheadTimes{};
|
||||
size_t m_overheadTimeIdx = 0;
|
||||
duration_t m_overhead = duration_t{ 0 };
|
||||
|
||||
duration_t TimeSince(delta_clock::time_point start)
|
||||
{
|
||||
return std::chrono::duration_cast<duration_t>(delta_clock::now() - start);
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
bool m_initialized;
|
||||
double m_countPerNs;
|
||||
|
||||
void NanoSleep(const duration_t duration)
|
||||
{
|
||||
if (!m_initialized)
|
||||
{
|
||||
LARGE_INTEGER freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
m_countPerNs = static_cast<double>(freq.QuadPart) / 1000000000.0;
|
||||
m_initialized = true;
|
||||
}
|
||||
|
||||
DWORD ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
auto tickCount =
|
||||
static_cast<LONGLONG>(static_cast<double>(duration.count()) * m_countPerNs);
|
||||
LARGE_INTEGER count;
|
||||
QueryPerformanceCounter(&count);
|
||||
if (ms > 10)
|
||||
{
|
||||
// Adjust for Sleep overhead
|
||||
::Sleep(ms - 10);
|
||||
}
|
||||
auto end = count.QuadPart + tickCount;
|
||||
do
|
||||
{
|
||||
QueryPerformanceCounter(&count);
|
||||
} while (count.QuadPart < end);
|
||||
}
|
||||
#else
|
||||
void NanoSleep(const duration_t duration)
|
||||
{
|
||||
std::this_thread::sleep_for(duration);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
static Limiter g_frameLimiter;
|
||||
void frame_limiter()
|
||||
{
|
||||
g_frameLimiter.Sleep(
|
||||
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds{ 1 }) / 60);
|
||||
}
|
||||
|
||||
+28
-5
@@ -11,7 +11,7 @@
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <dusk/logging.h>
|
||||
|
||||
#include <dusk/main.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/time.h>
|
||||
@@ -84,6 +84,16 @@ static PCMessageQueueData& GetMsgQueueData(OSMessageQueue* mq) {
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
static void ClearMsgQueueMap() {
|
||||
std::lock_guard<std::mutex> lock(GetMsgQueueMapMutex());
|
||||
auto& map = GetMsgQueueMap();
|
||||
for (auto & [_, value] : map) {
|
||||
value->cvReceive.notify_all();
|
||||
value->cvSend.notify_all();
|
||||
}
|
||||
map.clear();
|
||||
}
|
||||
|
||||
void OSInitMessageQueue(OSMessageQueue* mq, void* msgArray, s32 msgCount) {
|
||||
if (!mq) return;
|
||||
mq->queueSend.head = mq->queueSend.tail = nullptr;
|
||||
@@ -104,7 +114,10 @@ int OSSendMessage(OSMessageQueue* mq, void* msg, s32 flags) {
|
||||
if (mq->usedCount >= mq->msgCount) {
|
||||
if (flags == OS_MESSAGE_NOBLOCK) return 0;
|
||||
// BLOCK: wait until space is available
|
||||
data.cvSend.wait(lock, [mq]() { return mq->usedCount < mq->msgCount; });
|
||||
data.cvSend.wait(lock, [mq] { return mq->usedCount < mq->msgCount || dusk::IsShuttingDown; });
|
||||
}
|
||||
if (dusk::IsShuttingDown) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
s32 idx = (mq->firstIndex + mq->usedCount) % mq->msgCount;
|
||||
@@ -124,7 +137,10 @@ int OSReceiveMessage(OSMessageQueue* mq, void* msg, s32 flags) {
|
||||
if (mq->usedCount == 0) {
|
||||
if (flags == OS_MESSAGE_NOBLOCK) return 0;
|
||||
// BLOCK: wait until a message arrives
|
||||
data.cvReceive.wait(lock, [mq]() { return mq->usedCount > 0; });
|
||||
data.cvReceive.wait(lock, [mq] { return mq->usedCount > 0 || dusk::IsShuttingDown; });
|
||||
}
|
||||
if (dusk::IsShuttingDown) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (msg) {
|
||||
@@ -146,7 +162,10 @@ int OSJamMessage(OSMessageQueue* mq, void* msg, s32 flags) {
|
||||
if (mq->usedCount >= mq->msgCount) {
|
||||
if (flags == OS_MESSAGE_NOBLOCK) return 0;
|
||||
// BLOCK: wait until space is available
|
||||
data.cvSend.wait(lock, [mq]() { return mq->usedCount < mq->msgCount; });
|
||||
data.cvSend.wait(lock, [mq] { return mq->usedCount < mq->msgCount || dusk::IsShuttingDown; });
|
||||
}
|
||||
if (dusk::IsShuttingDown) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Jam inserts at the front of the queue
|
||||
@@ -185,8 +204,12 @@ BOOL OSGetResetButtonState() { return FALSE; }
|
||||
BOOL OSInitFont(OSFontHeader* fontData) { return FALSE; }
|
||||
BOOL OSLink(OSModuleInfo* newModule, void* bss) { return TRUE; }
|
||||
|
||||
void ClearCondMap();
|
||||
void OSResetSystem(int reset, u32 resetCode, BOOL forceMenu) {
|
||||
OSReport("[PC] OSResetSystem called (reset=%d, code=%u)\n", reset, resetCode);
|
||||
dusk::IsShuttingDown = true;
|
||||
ClearMsgQueueMap();
|
||||
ClearCondMap();
|
||||
}
|
||||
|
||||
void OSSetStringTable(void* stringTable) {}
|
||||
@@ -998,7 +1021,7 @@ f32 GXGetYScaleFactor(u16 efbHeight, u16 xfbHeight) {
|
||||
void GXInitTexCacheRegion(GXTexRegion* region, GXBool is_32b_mipmap, u32 tmem_even,
|
||||
GXTexCacheSize size_even, u32 tmem_odd, GXTexCacheSize size_odd) {
|
||||
STUB_LOG();
|
||||
}
|
||||
}
|
||||
// XXX, this should be some struct?
|
||||
// GXRenderModeObj GXNtsc480IntDf;
|
||||
//GXRenderModeObj GXNtsc480Int;
|
||||
|
||||
Reference in New Issue
Block a user