Merge branch 'falco/thread_attributes' into 'main'

Gave threads default attributes for RAM gainz.

See merge request skmp/dca3-game!63
This commit is contained in:
Stefanos Kornilios Mitsis Poiitidis 2025-03-16 14:54:04 +00:00
commit c0e7ddef84
8 changed files with 133 additions and 21 deletions

View file

@ -268,6 +268,7 @@ RE3_OBJS = \
../src/liberty/extras/screendroplets.o \
\
../src/common/vmu/vmu.o \
../src/common/thread/thread.o \
../vendor/miniLZO/minilzo.o \
\

View file

@ -284,7 +284,8 @@ RE3_OBJS = \
\
../vendor/miniLZO/minilzo.o \
\
../src/common/vmu/vmu.o
../src/common/vmu/vmu.o \
../src/common/thread/thread.o
# Excluded \
../src/miami/extras/custompipes.o \

View file

@ -0,0 +1,63 @@
#include "common.h"
#include "thread.h"
#ifdef DC_SH4
# include <kos.h>
#else
# include <thread>
#endif
namespace dc {
Thread::Thread(const char* label, size_t stackSize, bool detached, RunFunction runFunction, void* param) {
spawn(label, stackSize, detached, runFunction, param);
}
Thread::~Thread() {
if(!detached_) {
join();
} else {
#if !defined(DC_SH4)
delete reinterpret_cast<std::thread*>(nativeHandle_);
#endif
}
}
bool Thread::spawn(const char *label, size_t stackSize, bool detached, RunFunction runFunction, void* param) {
#if defined(DC_SH4)
const kthread_attr_t thdAttr = {
.create_detached = detached,
.stack_size = stackSize,
.label = label
};
nativeHandle_ =
reinterpret_cast<uintptr_t>(thd_create_ex(&thdAttr, runFunction, param));
#else
nativeHandle_ =
reinterpret_cast<uintptr_t>(new std::thread(runFunction, param));
#endif
detached_ = detached;
return !!nativeHandle_;
}
bool Thread::join() {
if(!isValid() || detached_)
return false;
#if defined(DC_SH4)
if(thd_join(reinterpret_cast<kthread_t*>(nativeHandle_), nullptr) != 0)
return false;
#else
reinterpret_cast<std::thread*>(nativeHandle_)->join();
delete reinterpret_cast<std::thread*>(nativeHandle_);
#endif
nativeHandle_ = 0;
detached_ = false;
return true;
}
}

View file

@ -0,0 +1,39 @@
#ifndef DC_THREAD_H
#define DC_THREAD_H
namespace dc {
class Thread {
public:
using RunFunction = void* (*)(void*);
Thread() = default;
Thread(const char *label,
size_t stackSize,
bool detached,
RunFunction runFunction,
void* param = nullptr);
~Thread();
bool spawn(const char *label,
size_t stackSize,
bool detached,
RunFunction runFunction,
void* param = nullptr);
bool join();
bool isValid() const { return !!nativeHandle_; }
bool isJoinable() const { return !detached_; }
private:
uintptr_t nativeHandle_ = 0;
bool detached_ = false;
};
}
#endif

View file

@ -24,7 +24,6 @@ VmuProfiler *VmuProfiler::getInstance() {
void VmuProfiler::destroyInstance() {
if(instance_) {
instance_->stopRequest_ = true;
instance_->join();
instance_.reset();
}
}
@ -74,12 +73,15 @@ float VmuProfiler::vertexBufferUtilization() {
// ===== INSTANCE METHODS =====
VmuProfiler::VmuProfiler():
std::thread(std::bind_front(&VmuProfiler::run, this))
{}
VmuProfiler::~VmuProfiler() {
stopRequest_ = true;
}
dc::Thread("VMU Profiler",
1024 * 2,
false,
[](void *param) -> void* {
static_cast<VmuProfiler*>(param)->run();
return nullptr;
},
this)
{}
void VmuProfiler::run() {
while(!stopRequest_) {

View file

@ -4,6 +4,8 @@
#include "common.h"
#include "sampman.h"
#include "thread/thread.h"
#include <thread>
#include <mutex>
#include <shared_mutex>
@ -40,13 +42,13 @@ public:
~RAIIVmuBeeper();
};
class VmuProfiler: public std::thread {
class VmuProfiler: public dc::Thread {
private:
constexpr static auto updateRate_ = std::chrono::milliseconds(100);
constexpr static size_t fpsSamples = 20;
constexpr static size_t fpsSamples = 10;
static inline
std::unique_ptr<VmuProfiler> instance_ = {};
std::unique_ptr<VmuProfiler> instance_ = {};
mutable std::shared_mutex mtx_ = {};
bool updated_ = false;
@ -60,7 +62,7 @@ protected:
VmuProfiler();
// Main entry point and loop for the monitor thread
virtual void run();
void run();
public:
@ -74,9 +76,6 @@ public:
static VmuProfiler *getInstance();
static void destroyInstance();
// Automatically signals thread to exit
~VmuProfiler();
// To be called every frame, so we can update FPS stats too!
void updateVertexBufferUsage();
};

View file

@ -21,6 +21,8 @@
#include "Frontend.h"
#include "Timer.h"
#include "thread/thread.h"
#include <dc/spu.h>
#include <dc/g2bus.h>
#include <dc/sound/aica_comm.h>
@ -312,7 +314,8 @@ static struct {
// return si->buffer;
// }
std::thread snd_thread;
static dc::Thread snd_thread;
bool8
cSampleManager::Initialise(void)
{
@ -345,8 +348,8 @@ cSampleManager::Initialise(void)
if (!InitialiseSampleBanks())
return FALSE;
snd_thread = std::thread([]() {
snd_thread.spawn("Audio Streamer", 1024 * 2, true, [](void*) -> void* {
for(;;) {
{
std::lock_guard<std::mutex> lk(channel_mtx);
@ -446,6 +449,7 @@ cSampleManager::Initialise(void)
}
thd_sleep(50);
}
return nullptr;
});
nPedSfxReqNextId = 1;

View file

@ -9,6 +9,7 @@
#include "common.h"
#include "crossplatform.h"
#include "thread/thread.h"
#if !defined(AUDIO_OAL) && !defined(AUDIO_MSS)
#define verbosef(...) // dbglog(DBG_CRITICAL, __VA_ARGS__)
@ -318,7 +319,8 @@ static struct {
// return si->buffer;
// }
std::thread snd_thread;
static dc::Thread snd_thread;
bool8
cSampleManager::Initialise(void)
{
@ -351,8 +353,8 @@ cSampleManager::Initialise(void)
if (!InitialiseSampleBanks())
return FALSE;
snd_thread = std::thread([]() {
snd_thread.spawn("Audio Streamer", 1024 * 2, true, [](void*) -> void* {
for(;;) {
{
std::lock_guard<std::mutex> lk(channel_mtx);
@ -452,6 +454,7 @@ cSampleManager::Initialise(void)
}
thd_sleep(50);
}
return nullptr;
});
nPedSfxReqNextId = 1;