Make threading and networking optional

This commit is contained in:
2025-02-27 04:17:12 -06:00
parent 29c53b171d
commit 02767f8710
39 changed files with 2054 additions and 99 deletions

View File

@ -1,62 +1,92 @@
#include "TessesFramework/Threading/Mutex.hpp"
#include <cstring>
#include <iostream>
#if defined(_WIN32)
#include <windows.h>
#elif defined(GEKKO)
#include <ogc/mutex.h>
#else
#include <pthread.h>
#endif
namespace Tesses::Framework::Threading
{
class MutexHiddenFieldData : public HiddenFieldData
{
public:
#if defined(_WIN32)
HANDLE mtx;
#elif defined(GEKKO)
mutex_t mtx;
#else
pthread_mutex_t mtx;
pthread_mutexattr_t attr;
#endif
~MutexHiddenFieldData()
{
#if defined(_WIN32)
CloseHandle(mtx);
#elif defined(GEKKO)
LWP_MutexDestroy(mtx);
#else
pthread_mutex_destroy(&mtx);
pthread_mutexattr_destroy(&attr);
#endif
}
};
Mutex::Mutex()
{
auto md=this->data.AllocField<MutexHiddenFieldData*>();
#if defined(_WIN32)
this->mtx = CreateMutex(NULL,false,NULL);
md->mtx = CreateMutex(NULL,false,NULL);
#elif defined(GEKKO)
mtx = LWP_MUTEX_NULL;
LWP_MutexInit(&mtx, true);
md->mtx = LWP_MUTEX_NULL;
LWP_MutexInit(&md->mtx, true);
#else
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mtx,&attr);
pthread_mutexattr_init(&md->attr);
pthread_mutexattr_settype(&md->attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&md->mtx,&md->attr);
#endif
}
void Mutex::Lock()
{
auto md = this->data.GetField<MutexHiddenFieldData*>();
#if defined(_WIN32)
WaitForSingleObject(mtx, INFINITE);
WaitForSingleObject(md->mtx, INFINITE);
#elif defined(GEKKO)
LWP_MutexLock(mtx);
LWP_MutexLock(md->mtx);
#else
pthread_mutex_lock(&mtx);
pthread_mutex_lock(&md->mtx);
#endif
}
void Mutex::Unlock()
{
auto md = this->data.GetField<MutexHiddenFieldData*>();
#if defined(_WIN32)
ReleaseMutex(mtx);
ReleaseMutex(md->mtx);
#elif defined(GEKKO)
LWP_MutexUnlock(mtx);
LWP_MutexUnlock(md->mtx);
#else
pthread_mutex_unlock(&mtx);
pthread_mutex_unlock(&md->mtx);
#endif
}
bool Mutex::TryLock()
{
auto md = this->data.GetField<MutexHiddenFieldData*>();
#if defined(_WIN32)
return WaitForSingleObject(mtx, 100) == WAIT_OBJECT_0;
return WaitForSingleObject(md->mtx, 100) == WAIT_OBJECT_0;
#elif defined(GEKKO)
return LWP_MutexTryLock(mtx) == 0;
return LWP_MutexTryLock(md->mtx) == 0;
#else
return pthread_mutex_trylock(&mtx) == 0;
return pthread_mutex_trylock(&md->mtx) == 0;
#endif
}
Mutex::~Mutex()
{
#if defined(_WIN32)
CloseHandle(mtx);
#elif defined(GEKKO)
LWP_MutexDestroy(mtx);
#else
pthread_mutex_destroy(&mtx);
pthread_mutexattr_destroy(&attr);
#endif
}
};

View File

@ -1,71 +1,95 @@
#include "TessesFramework/Threading/Thread.hpp"
#include <iostream>
namespace Tesses::Framework::Threading
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
class ThreadHiddenFieldData {
public:
#if defined(_WIN32)
HANDLE thrd;
DWORD thrdId;
public:
#elif defined(GEKKO)
lwp_t thrd;
#else
pthread_t thrd;
#endif
std::function<void()> fn;
std::atomic<bool> hasInvoked;
};
#if defined(_WIN32)
static DWORD __stdcall cb(LPVOID data)
#else
static void* cb(void* data)
#endif
{
auto thrd = static_cast<Thread*>(data);
auto thrd = static_cast<ThreadHiddenFieldData*>(data);
auto fn = thrd->fn;
thrd->hasInvoked=true;
fn();
#if defined(GEKKO)
#if !defined(_WIN32)
return NULL;
#else
return 0;
#endif
}
#else
void* Thread::cb(void* data)
{
auto thrd = static_cast<Thread*>(data);
auto fn = thrd->fn;
thrd->hasInvoked=true;
fn();
return NULL;
}
#endif
Thread::Thread(std::function<void()> fn)
{
this->hasInvoked=false;
this->fn = fn;
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
auto data = this->data.AllocField<ThreadHiddenFieldData*>();
data->hasInvoked=false;
data->fn = fn;
#if defined(_WIN32)
this->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(this), 0, &this->thrdId);
data->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(data), 0, &data->thrdId);
#elif defined(GEKKO)
auto res = LWP_CreateThread(&this->thrd, cb, static_cast<void*>(this), nullptr,12000, 98);
auto res = LWP_CreateThread(&data->thrd, cb, static_cast<void*>(data), nullptr,12000, 98);
#else
pthread_create(&thrd,NULL,cb,static_cast<void*>(this));
pthread_create(&data->thrd,NULL,cb,static_cast<void*>(data));
//thrd_create(&thrd, cb, static_cast<void*>(this));
#endif
while(!this->hasInvoked);
while(!data->hasInvoked);
#endif
}
void Thread::Detach()
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
auto data = this->data.AllocField<ThreadHiddenFieldData*>();
#if !defined(GEKKO)
#if defined(_WIN32)
CloseHandle(thrd);
CloseHandle(data->thrd);
#else
pthread_detach(thrd);
pthread_detach(data->thrd);
#endif
#endif
#endif
}
void Thread::Join()
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
auto data = this->data.AllocField<ThreadHiddenFieldData*>();
#if defined(_WIN32)
WaitForSingleObject(this->thrd, INFINITE);
WaitForSingleObject(data->thrd, INFINITE);
#elif defined(GEKKO)
void* res;
LWP_JoinThread(thrd,&res);
LWP_JoinThread(data->thrd,&res);
#else
pthread_join(thrd,NULL);
pthread_join(data->thrd,NULL);
#endif
#endif
}
}

View File

@ -8,12 +8,15 @@ namespace Tesses::Framework::Threading
{
#if defined(GEKKO)
return 1;
#else
#elif defined(TESSESFRAMEWORK_ENABLE_THREADING)
return (size_t)std::thread::hardware_concurrency();
#else
return 1;
#endif
}
ThreadPool::ThreadPool(size_t threads)
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
this->isRunning=true;
for(size_t i = 0; i < threads; i++)
{
@ -41,15 +44,19 @@ namespace Tesses::Framework::Threading
}
}));
}
#endif
}
void ThreadPool::Schedule(std::function<void()> cb)
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
this->mtx.Lock();
this->callbacks.push(cb);
this->mtx.Unlock();
#endif
}
ThreadPool::~ThreadPool()
{
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
while(true)
{
this->mtx.Lock();
@ -64,6 +71,6 @@ namespace Tesses::Framework::Threading
item->Join();
delete item;
}
#endif
}
}