initial commit
This commit is contained in:
69
source/threads.cpp
Normal file
69
source/threads.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <sys/stat.h>
|
||||
#include <switch.h>
|
||||
#include "threads.h"
|
||||
#include "util.h"
|
||||
|
||||
static threads::threadProcMngr *threadMngr;
|
||||
|
||||
threads::threadProcMngr::~threadProcMngr()
|
||||
{
|
||||
for(threadInfo *t : threads)
|
||||
{
|
||||
threadWaitForExit(&t->thrd);
|
||||
threadClose(&t->thrd);
|
||||
delete t->status;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
threadInfo *threads::threadProcMngr::newThread(ThreadFunc func, void *args, funcPtr _drawfunc)
|
||||
{
|
||||
threadInfo *t = new threadInfo;
|
||||
t->status = new threadStatus;
|
||||
t->running = false;
|
||||
t->finished = false;
|
||||
t->thrdFunc = func;
|
||||
t->drawFunc = _drawfunc;
|
||||
t->argPtr = args;
|
||||
|
||||
mutexLock(&threadLock);
|
||||
threads.push_back(t);
|
||||
mutexUnlock(&threadLock);
|
||||
return threads[threads.size() - 1];
|
||||
}
|
||||
|
||||
void threads::threadProcMngr::update()
|
||||
{
|
||||
if(!threads.empty())
|
||||
{
|
||||
Result res = 0;
|
||||
threadInfo *t = threads[0];
|
||||
if(!t->running && R_SUCCEEDED((res = threadCreate(&t->thrd, t->thrdFunc, t, NULL, 0x80000, 0x2B, 1))))
|
||||
{
|
||||
threadStart(&t->thrd);
|
||||
t->running = true;
|
||||
}
|
||||
else if(!t->running && R_FAILED(res))//Should kill the thread that failed.
|
||||
t->finished = true;
|
||||
else if(t->finished)
|
||||
{
|
||||
threadWaitForExit(&t->thrd);
|
||||
threadClose(&t->thrd);
|
||||
delete t->status;
|
||||
delete t;
|
||||
mutexLock(&threadLock);
|
||||
threads.erase(threads.begin());
|
||||
mutexUnlock(&threadLock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
threadInfo *threads::newThread(ThreadFunc func, void *args, funcPtr _drawFunc)
|
||||
{
|
||||
return threadMngr->newThread(func, args, _drawFunc);
|
||||
}
|
||||
Reference in New Issue
Block a user