2019-12-04 17:24:56 +00:00
|
|
|
#include "SwitchThread.h"
|
|
|
|
#include "switch.h"
|
|
|
|
|
|
|
|
void SwitchThread::ThreadLoop(void *argument)
|
|
|
|
{
|
|
|
|
svcSleepThread(1e+7L);
|
|
|
|
while (static_cast<SwitchThread *>(argument)->m_isRunning)
|
|
|
|
{
|
|
|
|
static_cast<SwitchThread *>(argument)->m_function(static_cast<SwitchThread *>(argument)->m_argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchThread::SwitchThread(ThreadFunc function, void *argument, size_t stackSize, int prio)
|
|
|
|
{
|
2019-12-04 18:43:29 +00:00
|
|
|
if (R_SUCCEEDED(Initialize(stackSize, prio)))
|
2019-12-04 17:24:56 +00:00
|
|
|
{
|
2019-12-04 18:43:29 +00:00
|
|
|
Start(function, argument);
|
2019-12-04 17:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:43:29 +00:00
|
|
|
Result SwitchThread::Initialize(size_t stackSize, int prio)
|
|
|
|
{
|
|
|
|
Result rc = threadCreate(&m_thread, &SwitchThread::ThreadLoop, this, NULL, stackSize, prio, -2);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:24:56 +00:00
|
|
|
SwitchThread::~SwitchThread()
|
|
|
|
{
|
2019-12-04 18:43:29 +00:00
|
|
|
Exit();
|
2019-12-04 17:24:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 18:43:29 +00:00
|
|
|
Result SwitchThread::Start(ThreadFunc function, void *argument)
|
2019-12-04 17:24:56 +00:00
|
|
|
{
|
|
|
|
if (!m_isRunning)
|
|
|
|
{
|
|
|
|
m_isRunning = true;
|
2019-12-04 18:43:29 +00:00
|
|
|
m_function = function;
|
|
|
|
m_argument = argument;
|
2019-12-04 17:24:56 +00:00
|
|
|
return threadStart(&m_thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-04 18:43:29 +00:00
|
|
|
Result SwitchThread::Exit()
|
2019-12-04 17:24:56 +00:00
|
|
|
{
|
|
|
|
if (m_isRunning)
|
|
|
|
{
|
|
|
|
m_isRunning = false;
|
|
|
|
svcCancelSynchronization(m_thread.handle);
|
|
|
|
threadWaitForExit(&m_thread);
|
|
|
|
return threadClose(&m_thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|