2017-10-29 09:24:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2015 Dimok
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef CTHREAD_H_
|
|
|
|
#define CTHREAD_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <unistd.h>
|
2018-06-17 15:19:51 +02:00
|
|
|
#include <coreinit/systeminfo.h>
|
|
|
|
#include <coreinit/thread.h>
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
class CThread {
|
2017-10-29 09:24:06 +01:00
|
|
|
public:
|
2018-03-11 16:44:04 +01:00
|
|
|
typedef void (* Callback)(CThread *thread, void *arg);
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
//! constructor
|
|
|
|
CThread(s32 iAttr, s32 iPriority = 16, s32 iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL)
|
|
|
|
: pThread(NULL)
|
|
|
|
, pThreadStack(NULL)
|
|
|
|
, pCallback(callback)
|
|
|
|
, pCallbackArg(callbackArg) {
|
|
|
|
//! save attribute assignment
|
|
|
|
iAttributes = iAttr;
|
|
|
|
//! allocate the thread
|
2018-06-17 15:19:51 +02:00
|
|
|
pThread = (OSThread*)memalign(8, sizeof(OSThread));
|
2018-03-11 16:44:04 +01:00
|
|
|
//! allocate the stack
|
|
|
|
pThreadStack = (u8 *) memalign(0x20, iStackSize);
|
2017-10-29 09:24:06 +01:00
|
|
|
//! create the thread
|
2018-06-17 15:19:51 +02:00
|
|
|
if(pThread && pThreadStack)
|
|
|
|
OSCreateThread(pThread, &CThread::threadCallback, 1, (char*)this, pThreadStack+iStackSize, iStackSize, iPriority, iAttributes);
|
2018-03-11 16:44:04 +01:00
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
//! destructor
|
|
|
|
virtual ~CThread() {
|
|
|
|
shutdownThread();
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
static CThread *create(CThread::Callback callback, void *callbackArg, s32 iAttr = eAttributeNone, s32 iPriority = 16, s32 iStackSize = 0x8000) {
|
|
|
|
return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) );
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
//! Get thread ID
|
|
|
|
virtual void* getThread() const {
|
|
|
|
return pThread;
|
|
|
|
}
|
|
|
|
//! Thread entry function
|
|
|
|
virtual void executeThread(void) {
|
|
|
|
if(pCallback)
|
2017-10-29 09:24:06 +01:00
|
|
|
pCallback(this, pCallbackArg);
|
2018-03-11 16:44:04 +01:00
|
|
|
}
|
|
|
|
//! Suspend thread
|
|
|
|
virtual void suspendThread(void) {
|
|
|
|
if(isThreadSuspended()) return;
|
|
|
|
if(pThread) OSSuspendThread(pThread);
|
|
|
|
}
|
|
|
|
//! Resume thread
|
|
|
|
virtual void resumeThread(void) {
|
|
|
|
if(!isThreadSuspended()) return;
|
|
|
|
if(pThread) OSResumeThread(pThread);
|
|
|
|
}
|
|
|
|
//! Set thread priority
|
2018-06-17 15:19:51 +02:00
|
|
|
virtual void setThreadPriority(int prio) {
|
2018-03-11 16:44:04 +01:00
|
|
|
if(pThread) OSSetThreadPriority(pThread, prio);
|
|
|
|
}
|
|
|
|
//! Check if thread is suspended
|
|
|
|
virtual bool isThreadSuspended(void) const {
|
|
|
|
if(pThread) return OSIsThreadSuspended(pThread);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//! Check if thread is terminated
|
|
|
|
virtual bool isThreadTerminated(void) const {
|
|
|
|
if(pThread) return OSIsThreadTerminated(pThread);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//! Check if thread is running
|
|
|
|
virtual bool isThreadRunning(void) const {
|
|
|
|
return !isThreadSuspended() && !isThreadRunning();
|
|
|
|
}
|
|
|
|
//! Shutdown thread
|
|
|
|
virtual void shutdownThread(void) {
|
|
|
|
//! wait for thread to finish
|
|
|
|
if(pThread && !(iAttributes & eAttributeDetach)) {
|
2018-06-17 15:19:51 +02:00
|
|
|
if(isThreadSuspended())
|
2017-10-29 09:24:06 +01:00
|
|
|
resumeThread();
|
2018-06-17 15:19:51 +02:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
OSJoinThread(pThread, NULL);
|
|
|
|
}
|
|
|
|
//! free the thread stack buffer
|
2018-06-17 15:19:51 +02:00
|
|
|
if(pThreadStack)
|
2018-03-11 16:44:04 +01:00
|
|
|
free(pThreadStack);
|
2018-06-17 15:19:51 +02:00
|
|
|
if(pThread)
|
2018-03-11 16:44:04 +01:00
|
|
|
free(pThread);
|
2018-06-17 15:19:51 +02:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
pThread = NULL;
|
|
|
|
pThreadStack = NULL;
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
//! Thread attributes
|
2018-03-11 16:44:04 +01:00
|
|
|
enum eCThreadAttributes {
|
|
|
|
eAttributeNone = 0x07,
|
|
|
|
eAttributeAffCore0 = 0x01,
|
|
|
|
eAttributeAffCore1 = 0x02,
|
|
|
|
eAttributeAffCore2 = 0x04,
|
|
|
|
eAttributeDetach = 0x08,
|
|
|
|
eAttributePinnedAff = 0x10
|
|
|
|
};
|
2017-10-29 09:24:06 +01:00
|
|
|
private:
|
2018-06-17 15:19:51 +02:00
|
|
|
static int threadCallback(int argc, const char **argv) {
|
2018-03-11 16:44:04 +01:00
|
|
|
//! After call to start() continue with the internal function
|
2018-06-17 15:19:51 +02:00
|
|
|
((CThread *) argv)->executeThread();
|
2018-03-11 16:44:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-17 15:19:51 +02:00
|
|
|
int iAttributes;
|
2018-03-11 16:44:04 +01:00
|
|
|
OSThread *pThread;
|
|
|
|
u8 *pThreadStack;
|
|
|
|
Callback pCallback;
|
|
|
|
void *pCallbackArg;
|
2017-10-29 09:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|