diff --git a/SwitchUSB/include/SwitchThread.h b/SwitchUSB/include/SwitchThread.h index 89385cd..e4febc5 100644 --- a/SwitchUSB/include/SwitchThread.h +++ b/SwitchUSB/include/SwitchThread.h @@ -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; } }; \ No newline at end of file diff --git a/SwitchUSB/source/SwitchThread.cpp b/SwitchUSB/source/SwitchThread.cpp index 6b6c4c4..4bdda5c 100644 --- a/SwitchUSB/source/SwitchThread.cpp +++ b/SwitchUSB/source/SwitchThread.cpp @@ -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) {