libgui/include/gui/system/CThread.h

146 lines
4.4 KiB
C
Raw Normal View History

2019-08-14 23:24:55 +02: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>
#include <coreinit/thread.h>
class CThread {
public:
2020-08-13 12:38:07 +02:00
typedef void (*Callback)(CThread *thread, void *arg);
2019-08-14 23:24:55 +02:00
//! constructor
CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL)
2020-08-13 12:38:07 +02:00
: pThread(NULL), pThreadStack(NULL), pCallback(callback), pCallbackArg(callbackArg) {
2019-08-14 23:24:55 +02:00
//! save attribute assignment
iAttributes = iAttr;
//! allocate the thread
2020-08-13 12:38:07 +02:00
pThread = (OSThread *) memalign(8, sizeof(OSThread));
2019-08-14 23:24:55 +02:00
//! allocate the stack
pThreadStack = (uint8_t *) memalign(0x20, iStackSize);
//! create the thread
2020-08-13 12:38:07 +02:00
if (pThread && pThreadStack)
OSCreateThread(pThread, &CThread::threadCallback, 1, (char *) this, pThreadStack + iStackSize, iStackSize, iPriority, iAttributes);
2019-08-14 23:24:55 +02:00
}
//! destructor
virtual ~CThread() {
shutdownThread();
}
static CThread *create(CThread::Callback callback, void *callbackArg, int32_t iAttr = eAttributeNone, int32_t iPriority = 16, int32_t iStackSize = 0x8000) {
2020-08-13 12:38:07 +02:00
return (new CThread(iAttr, iPriority, iStackSize, callback, callbackArg));
2019-08-14 23:24:55 +02:00
}
//! Get thread ID
2020-08-13 12:38:07 +02:00
virtual void *getThread() const {
2019-08-14 23:24:55 +02:00
return pThread;
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Thread entry function
virtual void executeThread(void) {
2020-08-13 12:38:07 +02:00
if (pCallback)
2019-08-14 23:24:55 +02:00
pCallback(this, pCallbackArg);
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Suspend thread
virtual void suspendThread(void) {
2020-08-13 12:38:07 +02:00
if (isThreadSuspended())
return;
2020-08-13 12:38:07 +02:00
if (pThread)
OSSuspendThread(pThread);
2019-08-14 23:24:55 +02:00
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Resume thread
virtual void resumeThread(void) {
2020-08-13 12:38:07 +02:00
if (!isThreadSuspended())
return;
2020-08-13 12:38:07 +02:00
if (pThread)
OSResumeThread(pThread);
2019-08-14 23:24:55 +02:00
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Set thread priority
virtual void setThreadPriority(int32_t prio) {
2020-08-13 12:38:07 +02:00
if (pThread)
OSSetThreadPriority(pThread, prio);
2019-08-14 23:24:55 +02:00
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Check if thread is suspended
virtual bool isThreadSuspended(void) const {
2020-08-13 12:38:07 +02:00
if (pThread)
return OSIsThreadSuspended(pThread);
2019-08-14 23:24:55 +02:00
return false;
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Check if thread is terminated
virtual bool isThreadTerminated(void) const {
2020-08-13 12:38:07 +02:00
if (pThread)
return OSIsThreadTerminated(pThread);
2019-08-14 23:24:55 +02:00
return false;
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Check if thread is running
virtual bool isThreadRunning(void) const {
2019-08-14 23:24:55 +02:00
return !isThreadSuspended() && !isThreadRunning();
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Shutdown thread
virtual void shutdownThread(void) {
//! wait for thread to finish
2020-08-13 12:38:07 +02:00
if (pThread && !(iAttributes & eAttributeDetach)) {
if (isThreadSuspended())
2019-08-14 23:24:55 +02:00
resumeThread();
OSJoinThread(pThread, NULL);
}
//! free the thread stack buffer
2020-08-13 12:38:07 +02:00
if (pThreadStack)
2019-08-14 23:24:55 +02:00
free(pThreadStack);
2020-08-13 12:38:07 +02:00
if (pThread)
2019-08-14 23:24:55 +02:00
free(pThread);
pThread = NULL;
pThreadStack = NULL;
}
2020-08-13 12:38:07 +02:00
2019-08-14 23:24:55 +02:00
//! Thread attributes
enum eCThreadAttributes {
eAttributeNone = 0x07,
eAttributeAffCore0 = 0x01,
eAttributeAffCore1 = 0x02,
eAttributeAffCore2 = 0x04,
eAttributeDetach = 0x08,
eAttributePinnedAff = 0x10
};
private:
static int32_t threadCallback(int32_t argc, const char **argv) {
2019-08-14 23:24:55 +02:00
//! After call to start() continue with the internal function
((CThread *) argv)->executeThread();
return 0;
}
2020-08-13 12:38:07 +02:00
int32_t iAttributes;
2019-08-14 23:24:55 +02:00
OSThread *pThread;
uint8_t *pThreadStack;
Callback pCallback;
void *pCallbackArg;
};
#endif