Add switch support (BROKEN) and TessesFrameworkFeatures.h and CertificateChain.hpp will be installed properly
This commit is contained in:
@ -5,6 +5,10 @@
|
||||
#include <windows.h>
|
||||
#elif defined(GEKKO)
|
||||
#include <ogc/mutex.h>
|
||||
#elif defined(__SWITCH__)
|
||||
extern "C" {
|
||||
#include <switch/kernel/mutex.h>
|
||||
}
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
@ -19,6 +23,8 @@ 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;
|
||||
@ -29,6 +35,8 @@ namespace Tesses::Framework::Threading
|
||||
CloseHandle(mtx);
|
||||
#elif defined(GEKKO)
|
||||
LWP_MutexDestroy(mtx);
|
||||
#elif defined(__SWITCH__)
|
||||
|
||||
#else
|
||||
pthread_mutex_destroy(&mtx);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
@ -45,6 +53,8 @@ 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);
|
||||
@ -61,6 +71,8 @@ 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
|
||||
@ -74,6 +86,8 @@ 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
|
||||
@ -87,6 +101,8 @@ 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
|
||||
|
||||
@ -1,10 +1,150 @@
|
||||
|
||||
#include "TessesFramework/Threading/Thread.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include "TessesFramework/Threading/Mutex.hpp"
|
||||
#include "TessesFramework/Common.hpp"
|
||||
#if defined(__SWITCH__)
|
||||
extern "C" {
|
||||
#include <switch.h>
|
||||
#include <switch/kernel/thread.h>
|
||||
}
|
||||
using NxThread = Thread;
|
||||
#endif
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
|
||||
#if defined(__SWITCH__) || defined(GEKKO)
|
||||
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);
|
||||
|
||||
ntbjt->hasInvoked=true;
|
||||
TF_LOG("About to call thread func");
|
||||
if(ntbjt->_cb)
|
||||
ntbjt->_cb();
|
||||
TF_LOG("Finished calling thread func");
|
||||
ntbjt->hasExited=true;
|
||||
|
||||
#if defined(GEKKO)
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
std::function<void()> _cb;
|
||||
std::atomic<bool> hasInvoked=false;
|
||||
#if defined(__SWITCH__)
|
||||
NxThread thrd;
|
||||
#elif defined(GEKKO)
|
||||
lwp_t thrd;
|
||||
#endif
|
||||
public:
|
||||
NeedToBeJoinnedThread(std::function<void()> cb)
|
||||
{
|
||||
this->_cb = cb;
|
||||
joinned=false;
|
||||
joinning=false;
|
||||
hasExited=false;
|
||||
#if defined(GEKKO)
|
||||
LWP_CreateThread(&thrd, 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");
|
||||
#endif
|
||||
}
|
||||
std::atomic<bool> joinned;
|
||||
std::atomic<bool> joinning;
|
||||
std::atomic<bool> hasExited;
|
||||
void Join();
|
||||
void WaitTillInvoked()
|
||||
{
|
||||
while(!hasInvoked);
|
||||
TF_LOG("Invoked!");
|
||||
}
|
||||
};
|
||||
|
||||
void NeedToBeJoinnedThread::Join()
|
||||
{
|
||||
if(joinning)
|
||||
{
|
||||
return;
|
||||
}
|
||||
joinning=true;
|
||||
#if defined(__SWITCH__)
|
||||
threadWaitForExit(&this->thrd);
|
||||
threadClose(&this->thrd);
|
||||
#elif defined(GEKKO)
|
||||
void* res;
|
||||
LWP_JoinThread(this->thrd,&res);
|
||||
#endif
|
||||
joinned=true;
|
||||
//start the joinning process
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<NeedToBeJoinnedThread>> needToBeJoinnedThread;
|
||||
void JoinAllThreads()
|
||||
{
|
||||
needed_to_be_joined_mtx.Lock();
|
||||
for(auto item : needToBeJoinnedThread)
|
||||
{
|
||||
item->Join();
|
||||
}
|
||||
needToBeJoinnedThread.clear();
|
||||
needed_to_be_joined_mtx.Unlock();
|
||||
|
||||
}
|
||||
void LookForFinishedThreads()
|
||||
{
|
||||
|
||||
needed_to_be_joined_mtx.Lock();
|
||||
for(auto index = needToBeJoinnedThread.begin(); index < needToBeJoinnedThread.end(); index++)
|
||||
{
|
||||
auto& idx = *index;
|
||||
if(idx->hasExited)
|
||||
{
|
||||
if(idx->joinning) while(!idx->joinned);
|
||||
TF_LOG("ABOUT TO JOIN");
|
||||
idx->Join();
|
||||
TF_LOG("JOINNED");
|
||||
needToBeJoinnedThread.erase(index);
|
||||
index--;
|
||||
}
|
||||
}
|
||||
needed_to_be_joined_mtx.Unlock();
|
||||
}
|
||||
|
||||
#endif
|
||||
class ThreadHiddenFieldData : public HiddenFieldData {
|
||||
public:
|
||||
#if defined(_WIN32)
|
||||
@ -16,7 +156,8 @@ namespace Tesses::Framework::Threading
|
||||
#elif defined(GEKKO)
|
||||
lwp_t thrd;
|
||||
|
||||
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
std::shared_ptr<NeedToBeJoinnedThread> thread;
|
||||
#else
|
||||
pthread_t thrd;
|
||||
#endif
|
||||
@ -30,6 +171,8 @@ namespace Tesses::Framework::Threading
|
||||
|
||||
#if defined(_WIN32)
|
||||
static DWORD __stdcall cb(LPVOID data)
|
||||
#elif defined(__SWITCH__)
|
||||
static void cb(void* data)
|
||||
#else
|
||||
static void* cb(void* data)
|
||||
#endif
|
||||
@ -39,8 +182,10 @@ namespace Tesses::Framework::Threading
|
||||
auto fn = thrd->fn;
|
||||
thrd->hasInvoked=true;
|
||||
fn();
|
||||
#if !defined(_WIN32)
|
||||
#if !defined(_WIN32) && !defined(__SWITCH__)
|
||||
return NULL;
|
||||
#elif(__SWITCH__)
|
||||
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
@ -55,26 +200,37 @@ namespace Tesses::Framework::Threading
|
||||
data->fn = fn;
|
||||
#if defined(_WIN32)
|
||||
data->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(data), 0, &data->thrdId);
|
||||
#elif defined(GEKKO)
|
||||
auto res = LWP_CreateThread(&data->thrd, cb, static_cast<void*>(data), nullptr,12000, 98);
|
||||
while(!data->hasInvoked);
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
data->thread = std::make_shared<NeedToBeJoinnedThread>(fn);
|
||||
data->thread->WaitTillInvoked();
|
||||
//threadCreate(,cb,static_cast<void*>(data),NULL,8000000,0x00,-2);
|
||||
#else
|
||||
pthread_create(&data->thrd,NULL,cb,static_cast<void*>(data));
|
||||
while(!data->hasInvoked);
|
||||
//thrd_create(&thrd, cb, static_cast<void*>(this));
|
||||
#endif
|
||||
while(!data->hasInvoked);
|
||||
|
||||
#endif
|
||||
}
|
||||
void Thread::Detach()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData*>();
|
||||
#if !defined(GEKKO)
|
||||
|
||||
#if defined(_WIN32)
|
||||
CloseHandle(data->thrd);
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
TF_LOG("Detaching");
|
||||
needed_to_be_joined_mtx.Lock();
|
||||
needToBeJoinnedThread.push_back(data->thread);
|
||||
needed_to_be_joined_mtx.Unlock();
|
||||
|
||||
TF_LOG("Detached!");
|
||||
#else
|
||||
pthread_detach(data->thrd);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -84,9 +240,8 @@ namespace Tesses::Framework::Threading
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData*>();
|
||||
#if defined(_WIN32)
|
||||
WaitForSingleObject(data->thrd, INFINITE);
|
||||
#elif defined(GEKKO)
|
||||
void* res;
|
||||
LWP_JoinThread(data->thrd,&res);
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
data->thread->Join();
|
||||
#else
|
||||
pthread_join(data->thrd,NULL);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user