Move things from crosslang to here

This commit is contained in:
2025-06-21 23:05:12 -05:00
parent 71a2c83e5a
commit 81f0d3be2e
41 changed files with 275029 additions and 96 deletions

View File

@@ -6,10 +6,6 @@
#undef min
#elif defined(GEKKO)
#include <ogc/mutex.h>
#elif defined(__SWITCH__)
extern "C" {
#include <switch/kernel/mutex.h>
}
#else
#include <pthread.h>
#endif
@@ -24,8 +20,6 @@ namespace Tesses::Framework::Threading
HANDLE mtx;
#elif defined(GEKKO)
mutex_t mtx;
#elif defined(__SWITCH__)
RMutex mtx;
#else
pthread_mutex_t mtx;
pthread_mutexattr_t attr;
@@ -36,7 +30,6 @@ namespace Tesses::Framework::Threading
CloseHandle(mtx);
#elif defined(GEKKO)
LWP_MutexDestroy(mtx);
#elif defined(__SWITCH__)
#else
pthread_mutex_destroy(&mtx);
@@ -54,8 +47,7 @@ namespace Tesses::Framework::Threading
#elif defined(GEKKO)
md->mtx = LWP_MUTEX_NULL;
LWP_MutexInit(&md->mtx, true);
#elif defined(__SWITCH__)
rmutexInit(&md->mtx);
#else
pthread_mutexattr_init(&md->attr);
pthread_mutexattr_settype(&md->attr,PTHREAD_MUTEX_RECURSIVE);
@@ -72,8 +64,7 @@ namespace Tesses::Framework::Threading
WaitForSingleObject(md->mtx, INFINITE);
#elif defined(GEKKO)
LWP_MutexLock(md->mtx);
#elif defined(__SWITCH__)
rmutexLock(&md->mtx);
#else
pthread_mutex_lock(&md->mtx);
#endif
@@ -87,8 +78,7 @@ namespace Tesses::Framework::Threading
ReleaseMutex(md->mtx);
#elif defined(GEKKO)
LWP_MutexUnlock(md->mtx);
#elif defined(__SWITCH__)
rmutexUnlock(&md->mtx);
#else
pthread_mutex_unlock(&md->mtx);
#endif
@@ -102,8 +92,7 @@ namespace Tesses::Framework::Threading
return WaitForSingleObject(md->mtx, 100) == WAIT_OBJECT_0;
#elif defined(GEKKO)
return LWP_MutexTryLock(md->mtx) == 0;
#elif defined(__SWITCH__)
return rmutexTryLock(&md->mtx);
#else
return pthread_mutex_trylock(&md->mtx) == 0;
#endif

View File

@@ -7,9 +7,8 @@
#if defined(__SWITCH__)
extern "C" {
#include <switch.h>
#include <switch/kernel/thread.h>
#include <pthread.h>
}
using NxThread = Thread;
#endif
namespace Tesses::Framework::Threading
{
@@ -21,12 +20,8 @@ namespace Tesses::Framework::Threading
Mutex needed_to_be_joined_mtx;
class NeedToBeJoinnedThread {
#if defined(GEKKO)
static void* cb(void* data)
#elif defined(__SWITCH__)
static void cb(void* data)
#endif
{
{
auto ntbjt = static_cast<NeedToBeJoinnedThread*>(data);
@@ -37,14 +32,12 @@ namespace Tesses::Framework::Threading
TF_LOG("Finished calling thread func");
ntbjt->hasExited=true;
#if defined(GEKKO)
return NULL;
#endif
return NULL;
}
std::function<void()> _cb;
std::atomic<bool> hasInvoked=false;
#if defined(__SWITCH__)
NxThread thrd;
pthread_t thrd;
#elif defined(GEKKO)
lwp_t thrd;
#endif
@@ -58,29 +51,8 @@ namespace Tesses::Framework::Threading
#if defined(GEKKO)
LWP_CreateThread(&thrd, this->cb, static_cast<void*>(this), nullptr,12000, 98);
#elif defined(__SWITCH__)
TF_LOG("Before Thread create");
Result rc = threadCreate(&thrd,this->cb,
static_cast<void*>(this), NULL, 0x100000,
0x20 , 2);
if (R_FAILED(rc))
{
this->hasExited=true;
this->joinned=true;
TF_LOG("Failed to create Thread");
return;
}
TF_LOG("After Thread create, before starting");
rc = threadStart(&thrd);
if (R_FAILED(rc))
{
TF_LOG("Failed to start thread");
threadClose(&thrd);
this->hasExited=true;
this->joinned=true;
return;
}
TF_LOG("Starting");
pthread_create(&thrd,NULL,this->cb,static_cast<void*>(this));
#endif
}
std::atomic<bool> joinned;
@@ -102,8 +74,7 @@ namespace Tesses::Framework::Threading
}
joinning=true;
#if defined(__SWITCH__)
threadWaitForExit(&this->thrd);
threadClose(&this->thrd);
pthread_join(this->thrd,NULL);
#elif defined(GEKKO)
void* res;
LWP_JoinThread(this->thrd,&res);

View File

@@ -14,13 +14,25 @@ namespace Tesses::Framework::Threading
return 1;
#endif
}
size_t ThreadPool::ThreadCount()
{
return this->threads.size();
}
bool ThreadPool::Empty()
{
bool qie;
this->mtx.Lock();
qie = this->callbacks.empty();
this->mtx.Unlock();
return qie;
}
ThreadPool::ThreadPool(size_t threads)
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
this->isRunning=true;
for(size_t i = 0; i < threads; i++)
{
this->threads.push_back(new Thread([this]()->void{
this->threads.push_back(new Thread([this,i]()->void{
while(true)
{
this->mtx.Lock();
@@ -31,22 +43,22 @@ namespace Tesses::Framework::Threading
return;
}
std::function<void()> fn=nullptr;
std::function<void(size_t)> fn=nullptr;
if(this->callbacks.empty())
if(!this->callbacks.empty())
{
fn=this->callbacks.front();
this->callbacks.pop();
}
this->mtx.Unlock();
if(fn)
fn();
fn(i);
}
}));
}
#endif
}
void ThreadPool::Schedule(std::function<void()> cb)
void ThreadPool::Schedule(std::function<void(size_t)> cb)
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
this->mtx.Lock();