1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-05 10:48:46 +00:00

SwitchThread: Add Initialize() for manual thread creation

This commit is contained in:
cathery 2019-12-04 21:43:29 +03:00
parent b4dfe8a548
commit 4f1247fe0c
2 changed files with 26 additions and 10 deletions

View File

@ -13,8 +13,9 @@ private:
static void ThreadLoop(void *argument);
public:
SwitchThread() = default;
SwitchThread(){};
//Constructs the class by creating and starting the thread.
//function - the function you want to be called
//argument - the argument passed to that function
//stackSize - the stack size of the created thread
@ -27,11 +28,19 @@ public:
//Closes the thread upon exiting
~SwitchThread();
//Starts the thread. This is called automatically upon class creation.
Result Start();
//Creates the thread.
//function - the function you want to be called
//argument - the argument passed to that function
//stackSize - the stack size of the created thread
//prio - thread priority, 0x00 - highest, 0x3F - lowest. Switch uses 0x2C
Result Initialize(size_t stackSize, int prio);
//Starts the thread. The given function will be called with the given argument in a loop until the thread is closed.
Result Start(ThreadFunc function, void *argument);
//Closes the thread.
//This will block the caller indefinitely until the thread is returned!
Result Close();
Result Exit();
bool inline IsRunning() { return m_isRunning; }
};

View File

@ -11,31 +11,38 @@ void SwitchThread::ThreadLoop(void *argument)
}
SwitchThread::SwitchThread(ThreadFunc function, void *argument, size_t stackSize, int prio)
: m_function(function), m_argument(argument)
{
if (R_SUCCEEDED(threadCreate(&m_thread, &SwitchThread::ThreadLoop, this, NULL, stackSize, prio, -2)))
if (R_SUCCEEDED(Initialize(stackSize, prio)))
{
Start();
Start(function, argument);
}
}
Result SwitchThread::Initialize(size_t stackSize, int prio)
{
Result rc = threadCreate(&m_thread, &SwitchThread::ThreadLoop, this, NULL, stackSize, prio, -2);
return rc;
}
SwitchThread::~SwitchThread()
{
Close();
Exit();
}
Result SwitchThread::Start()
Result SwitchThread::Start(ThreadFunc function, void *argument)
{
if (!m_isRunning)
{
m_isRunning = true;
m_function = function;
m_argument = argument;
return threadStart(&m_thread);
}
else
return 0;
}
Result SwitchThread::Close()
Result SwitchThread::Exit()
{
if (m_isRunning)
{