first commit
This commit is contained in:
49
src/Threading/Mutex.cpp
Normal file
49
src/Threading/Mutex.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "TessesFramework/Threading/Mutex.hpp"
|
||||
#include <cstring>
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
Mutex::Mutex()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
mtx = LWP_MUTEX_NULL;
|
||||
LWP_MutexInit(&mtx, true);
|
||||
#else
|
||||
memset(&mtx, 0, sizeof(mtx_t));
|
||||
mtx_init(&mtx, mtx_recursive);
|
||||
|
||||
#endif
|
||||
}
|
||||
void Mutex::Lock()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
LWP_MutexLock(mtx);
|
||||
#else
|
||||
mtx_lock(&mtx);
|
||||
#endif
|
||||
}
|
||||
void Mutex::Unlock()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
LWP_MutexUnlock(mtx);
|
||||
#else
|
||||
mtx_unlock(&mtx);
|
||||
#endif
|
||||
}
|
||||
bool Mutex::TryLock()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
return LWP_MutexTryLock(mtx) == 0;
|
||||
#else
|
||||
return mtx_trylock(&mtx) == thrd_success;
|
||||
#endif
|
||||
}
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
LWP_MutexDestroy(mtx);
|
||||
#else
|
||||
mtx_destroy(&mtx);
|
||||
#endif
|
||||
|
||||
}
|
||||
};
|
||||
53
src/Threading/Thread.cpp
Normal file
53
src/Threading/Thread.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "TessesFramework/Threading/Thread.hpp"
|
||||
#include <iostream>
|
||||
namespace Tesses::Framework::Threading
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
void* Thread::cb(void* data)
|
||||
#else
|
||||
int Thread::cb(void* data)
|
||||
#endif
|
||||
{
|
||||
auto thrd = static_cast<Thread*>(data);
|
||||
|
||||
auto fn = thrd->fn;
|
||||
thrd->hasInvoked=true;
|
||||
fn();
|
||||
#if defined(GEKKO)
|
||||
return NULL;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
Thread::Thread(std::function<void()> fn)
|
||||
{
|
||||
this->hasInvoked=false;
|
||||
this->fn = fn;
|
||||
|
||||
#if defined(GEKKO)
|
||||
thrd = LWP_THREAD_NULL;
|
||||
LWP_CreateThread(&thrd, cb, static_cast<void*>(this), NULL, 12000, LWP_PRIO_HIGHEST);
|
||||
#else
|
||||
thrd_create(&thrd, cb, static_cast<void*>(this));
|
||||
#endif
|
||||
while(!this->hasInvoked);
|
||||
}
|
||||
void Thread::Detach()
|
||||
{
|
||||
#if !defined(GEKKO)
|
||||
thrd_detach(thrd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Thread::Join()
|
||||
{
|
||||
#if defined(GEKKO)
|
||||
void* res;
|
||||
LWP_JoinThread(thrd,&res);
|
||||
#else
|
||||
int res;
|
||||
thrd_join(thrd,&res);
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user