mirror of
https://git.tesses.org/tesses50/tessesframework.git
synced 2026-09-30 16:54:14 +00:00
Overhaul cmake configuration, add console api, fix http code that caused issues with cgi-bin
This commit is contained in:
+87
-94
@@ -1,106 +1,99 @@
|
||||
/*
|
||||
TessesFramework a library to make C++ easier for me, used in CrossLang:
|
||||
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TessesFramework/Threading/Mutex.hpp"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#undef min
|
||||
#elif defined(GEKKO)
|
||||
#include <ogc/mutex.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_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
|
||||
}
|
||||
};
|
||||
#endif
|
||||
Mutex::Mutex()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md=this->data.AllocField<MutexHiddenFieldData>();
|
||||
#if defined(_WIN32)
|
||||
md->mtx = CreateMutex(NULL,false,NULL);
|
||||
#elif defined(GEKKO)
|
||||
md->mtx = LWP_MUTEX_NULL;
|
||||
LWP_MutexInit(&md->mtx, true);
|
||||
|
||||
#else
|
||||
pthread_mutexattr_init(&md->attr);
|
||||
pthread_mutexattr_settype(&md->attr,PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&md->mtx,&md->attr);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void Mutex::Lock()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData*>();
|
||||
#if defined(_WIN32)
|
||||
WaitForSingleObject(md->mtx, INFINITE);
|
||||
#elif defined(GEKKO)
|
||||
LWP_MutexLock(md->mtx);
|
||||
|
||||
#else
|
||||
pthread_mutex_lock(&md->mtx);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void Mutex::Unlock()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData*>();
|
||||
#if defined(_WIN32)
|
||||
ReleaseMutex(md->mtx);
|
||||
#elif defined(GEKKO)
|
||||
LWP_MutexUnlock(md->mtx);
|
||||
|
||||
#else
|
||||
pthread_mutex_unlock(&md->mtx);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
bool Mutex::TryLock()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData*>();
|
||||
#if defined(_WIN32)
|
||||
return WaitForSingleObject(md->mtx, 100) == WAIT_OBJECT_0;
|
||||
#elif defined(GEKKO)
|
||||
return LWP_MutexTryLock(md->mtx) == 0;
|
||||
|
||||
#else
|
||||
return pthread_mutex_trylock(&md->mtx) == 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
|
||||
namespace Tesses::Framework::Threading {
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
class MutexHiddenFieldData : public HiddenFieldData {
|
||||
public:
|
||||
#if defined(_WIN32)
|
||||
HANDLE mtx;
|
||||
#else
|
||||
pthread_mutex_t mtx;
|
||||
pthread_mutexattr_t attr;
|
||||
#endif
|
||||
~MutexHiddenFieldData() {
|
||||
#if defined(_WIN32)
|
||||
CloseHandle(mtx);
|
||||
#else
|
||||
pthread_mutex_destroy(&mtx);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
#endif
|
||||
Mutex::Mutex() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.AllocField<MutexHiddenFieldData>();
|
||||
#if defined(_WIN32)
|
||||
md->mtx = CreateMutex(NULL, false, NULL);
|
||||
#elif defined(GEKKO)
|
||||
md->mtx = LWP_MUTEX_NULL;
|
||||
LWP_MutexInit(&md->mtx, true);
|
||||
|
||||
#else
|
||||
pthread_mutexattr_init(&md->attr);
|
||||
pthread_mutexattr_settype(&md->attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&md->mtx, &md->attr);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void Mutex::Lock() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData *>();
|
||||
#if defined(_WIN32)
|
||||
WaitForSingleObject(md->mtx, INFINITE);
|
||||
|
||||
#else
|
||||
pthread_mutex_lock(&md->mtx);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void Mutex::Unlock() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData *>();
|
||||
#if defined(_WIN32)
|
||||
ReleaseMutex(md->mtx);
|
||||
#else
|
||||
pthread_mutex_unlock(&md->mtx);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
bool Mutex::TryLock() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto md = this->data.GetField<MutexHiddenFieldData *>();
|
||||
#if defined(_WIN32)
|
||||
return WaitForSingleObject(md->mtx, 100) == WAIT_OBJECT_0;
|
||||
|
||||
#else
|
||||
return pthread_mutex_trylock(&md->mtx) == 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
Mutex::~Mutex() {}
|
||||
}; // namespace Tesses::Framework::Threading
|
||||
|
||||
+192
-197
@@ -1,219 +1,214 @@
|
||||
/*
|
||||
TessesFramework a library to make C++ easier for me, used in CrossLang:
|
||||
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TessesFramework/Threading/Thread.hpp"
|
||||
#include "TessesFramework/Common.hpp"
|
||||
#include "TessesFramework/Threading/Mutex.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "TessesFramework/Threading/Mutex.hpp"
|
||||
#include "TessesFramework/Common.hpp"
|
||||
#if defined(__SWITCH__)
|
||||
extern "C" {
|
||||
#include <switch.h>
|
||||
#include <pthread.h>
|
||||
#include <switch.h>
|
||||
}
|
||||
#endif
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
namespace Tesses::Framework::Threading {
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
#if defined(__SWITCH__)
|
||||
Mutex needed_to_be_joined_mtx;
|
||||
class NeedToBeJoinnedThread {
|
||||
|
||||
#if defined(__SWITCH__) || defined(GEKKO)
|
||||
Mutex needed_to_be_joined_mtx;
|
||||
class NeedToBeJoinnedThread {
|
||||
|
||||
static void* cb(void* data)
|
||||
{
|
||||
|
||||
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;
|
||||
static void *cb(void *data) {
|
||||
|
||||
return NULL;
|
||||
}
|
||||
std::function<void()> _cb;
|
||||
std::atomic<bool> hasInvoked=false;
|
||||
#if defined(__SWITCH__)
|
||||
pthread_t 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, this->cb, static_cast<void*>(this), nullptr,12000, 98);
|
||||
#elif defined(__SWITCH__)
|
||||
pthread_create(&thrd,NULL,this->cb,static_cast<void*>(this));
|
||||
|
||||
#endif
|
||||
}
|
||||
std::atomic<bool> joinned;
|
||||
std::atomic<bool> joinning;
|
||||
std::atomic<bool> hasExited;
|
||||
void Join();
|
||||
void WaitTillInvoked()
|
||||
{
|
||||
while(!hasInvoked);
|
||||
TF_LOG("Invoked!");
|
||||
}
|
||||
};
|
||||
auto ntbjt = static_cast<NeedToBeJoinnedThread *>(data);
|
||||
|
||||
void NeedToBeJoinnedThread::Join()
|
||||
{
|
||||
if(joinning)
|
||||
{
|
||||
return;
|
||||
}
|
||||
joinning=true;
|
||||
#if defined(__SWITCH__)
|
||||
pthread_join(this->thrd,NULL);
|
||||
#elif defined(GEKKO)
|
||||
void* res;
|
||||
LWP_JoinThread(this->thrd,&res);
|
||||
#endif
|
||||
joinned=true;
|
||||
//start the joinning process
|
||||
ntbjt->hasInvoked = true;
|
||||
TF_LOG("About to call thread func");
|
||||
if (ntbjt->_cb)
|
||||
ntbjt->_cb();
|
||||
TF_LOG("Finished calling thread func");
|
||||
ntbjt->hasExited = true;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
std::function<void()> _cb;
|
||||
std::atomic<bool> hasInvoked = false;
|
||||
|
||||
pthread_t thrd;
|
||||
|
||||
public:
|
||||
NeedToBeJoinnedThread(std::function<void()> cb) {
|
||||
this->_cb = cb;
|
||||
joinned = false;
|
||||
joinning = false;
|
||||
hasExited = false;
|
||||
|
||||
pthread_create(&thrd, NULL, this->cb, static_cast<void *>(this));
|
||||
|
||||
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();
|
||||
std::atomic<bool> joinned;
|
||||
std::atomic<bool> joinning;
|
||||
std::atomic<bool> hasExited;
|
||||
void Join();
|
||||
void WaitTillInvoked() {
|
||||
while (!hasInvoked)
|
||||
;
|
||||
TF_LOG("Invoked!");
|
||||
}
|
||||
|
||||
#endif
|
||||
class ThreadHiddenFieldData : public HiddenFieldData {
|
||||
public:
|
||||
#if defined(_WIN32)
|
||||
};
|
||||
|
||||
HANDLE thrd;
|
||||
DWORD thrdId;
|
||||
|
||||
|
||||
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
std::shared_ptr<NeedToBeJoinnedThread> thread;
|
||||
#else
|
||||
pthread_t thrd;
|
||||
#endif
|
||||
|
||||
|
||||
std::function<void()> fn;
|
||||
|
||||
std::atomic<bool> hasInvoked;
|
||||
};
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
static DWORD __stdcall cb(LPVOID data)
|
||||
#elif defined(__SWITCH__)
|
||||
static void cb(void* data)
|
||||
#else
|
||||
static void* cb(void* data)
|
||||
#endif
|
||||
{
|
||||
auto thrd = static_cast<ThreadHiddenFieldData*>(data);
|
||||
|
||||
auto fn = thrd->fn;
|
||||
thrd->hasInvoked=true;
|
||||
fn();
|
||||
#if !defined(_WIN32) && !defined(__SWITCH__)
|
||||
return NULL;
|
||||
#elif(__SWITCH__)
|
||||
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
Thread::Thread(std::function<void()> fn)
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.AllocField<ThreadHiddenFieldData>();
|
||||
data->hasInvoked=false;
|
||||
data->fn = fn;
|
||||
#if defined(_WIN32)
|
||||
data->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(data), 0, &data->thrdId);
|
||||
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
|
||||
|
||||
#endif
|
||||
}
|
||||
void Thread::Detach()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData*>();
|
||||
|
||||
#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
|
||||
void NeedToBeJoinnedThread::Join() {
|
||||
if (joinning) {
|
||||
return;
|
||||
}
|
||||
joinning = true;
|
||||
pthread_join(this->thrd, NULL);
|
||||
|
||||
void Thread::Join()
|
||||
{
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData*>();
|
||||
#if defined(_WIN32)
|
||||
WaitForSingleObject(data->thrd, INFINITE);
|
||||
#elif defined(__SWITCH__) || defined(GEKKO)
|
||||
data->thread->Join();
|
||||
#else
|
||||
pthread_join(data->thrd,NULL);
|
||||
#endif
|
||||
#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)
|
||||
|
||||
HANDLE thrd;
|
||||
DWORD thrdId;
|
||||
|
||||
#elif defined(__SWITCH__)
|
||||
std::shared_ptr<NeedToBeJoinnedThread> thread;
|
||||
#else
|
||||
pthread_t thrd;
|
||||
#endif
|
||||
|
||||
std::function<void()> fn;
|
||||
|
||||
std::atomic<bool> hasInvoked;
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
static DWORD __stdcall cb(LPVOID data)
|
||||
#elif defined(__SWITCH__)
|
||||
static void cb(void *data)
|
||||
#else
|
||||
static void *cb(void *data)
|
||||
#endif
|
||||
{
|
||||
auto thrd = static_cast<ThreadHiddenFieldData *>(data);
|
||||
|
||||
auto fn = thrd->fn;
|
||||
thrd->hasInvoked = true;
|
||||
fn();
|
||||
#if !defined(_WIN32) && !defined(__SWITCH__)
|
||||
return NULL;
|
||||
#elif (__SWITCH__)
|
||||
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
Thread::Thread(std::function<void()> fn) {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.AllocField<ThreadHiddenFieldData>();
|
||||
data->hasInvoked = false;
|
||||
data->fn = fn;
|
||||
#if defined(_WIN32)
|
||||
data->thrd =
|
||||
CreateThread(NULL, 0, cb, static_cast<LPVOID>(data), 0, &data->thrdId);
|
||||
while (!data->hasInvoked)
|
||||
;
|
||||
#elif defined(__SWITCH__)
|
||||
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
|
||||
|
||||
#endif
|
||||
}
|
||||
void Thread::Detach() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData *>();
|
||||
|
||||
#if defined(_WIN32)
|
||||
CloseHandle(data->thrd);
|
||||
#elif defined(__SWITCH__)
|
||||
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
|
||||
}
|
||||
|
||||
void Thread::Join() {
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
auto data = this->data.GetField<ThreadHiddenFieldData *>();
|
||||
#if defined(_WIN32)
|
||||
WaitForSingleObject(data->thrd, INFINITE);
|
||||
#elif defined(__SWITCH__)
|
||||
data->thread->Join();
|
||||
#else
|
||||
pthread_join(data->thrd, NULL);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
} // namespace Tesses::Framework::Threading
|
||||
|
||||
@@ -1,88 +1,93 @@
|
||||
/*
|
||||
TessesFramework a library to make C++ easier for me, used in CrossLang:
|
||||
https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TessesFramework/Threading/ThreadPool.hpp"
|
||||
#if !defined(GEKKO)
|
||||
#include <thread>
|
||||
#endif
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
size_t ThreadPool::GetNumberOfCores()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
return 1;
|
||||
#elif defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
return (size_t)std::thread::hardware_concurrency();
|
||||
#else
|
||||
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,i]()->void{
|
||||
while(true)
|
||||
{
|
||||
this->mtx.Lock();
|
||||
|
||||
if(!this->isRunning)
|
||||
{
|
||||
this->mtx.Unlock();
|
||||
return;
|
||||
}
|
||||
namespace Tesses::Framework::Threading {
|
||||
size_t ThreadPool::GetNumberOfCores() {
|
||||
#if defined(GEKKO)
|
||||
return 1;
|
||||
#elif defined(TESSESFRAMEWORK_ENABLE_THREADING)
|
||||
return (size_t)std::thread::hardware_concurrency();
|
||||
#else
|
||||
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, i]() -> void {
|
||||
while (true) {
|
||||
this->mtx.Lock();
|
||||
|
||||
std::function<void(size_t)> fn=nullptr;
|
||||
|
||||
if(!this->callbacks.empty())
|
||||
{
|
||||
fn=this->callbacks.front();
|
||||
this->callbacks.pop();
|
||||
}
|
||||
if (!this->isRunning) {
|
||||
this->mtx.Unlock();
|
||||
if(fn)
|
||||
fn(i);
|
||||
return;
|
||||
}
|
||||
}));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void ThreadPool::Schedule(std::function<void(size_t)> 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();
|
||||
auto emp=this->callbacks.empty();
|
||||
if(emp) this->isRunning=false;
|
||||
this->mtx.Unlock();
|
||||
if(emp) break;
|
||||
}
|
||||
|
||||
for(auto item : this->threads)
|
||||
{
|
||||
item->Join();
|
||||
delete item;
|
||||
}
|
||||
#endif
|
||||
std::function<void(size_t)> fn = nullptr;
|
||||
|
||||
if (!this->callbacks.empty()) {
|
||||
fn = this->callbacks.front();
|
||||
this->callbacks.pop();
|
||||
}
|
||||
this->mtx.Unlock();
|
||||
if (fn)
|
||||
fn(i);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void ThreadPool::Schedule(std::function<void(size_t)> 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();
|
||||
auto emp = this->callbacks.empty();
|
||||
if (emp)
|
||||
this->isRunning = false;
|
||||
this->mtx.Unlock();
|
||||
if (emp)
|
||||
break;
|
||||
}
|
||||
|
||||
for (auto item : this->threads) {
|
||||
item->Join();
|
||||
delete item;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} // namespace Tesses::Framework::Threading
|
||||
Reference in New Issue
Block a user