MemoryMappingModule/source/CThread.h

157 lines
5.2 KiB
C
Raw Normal View History

2020-05-29 19:25:05 +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/>.
****************************************************************************/
#pragma once
#include "globals.h"
2022-02-02 18:34:27 +01:00
#include <coreinit/thread.h>
#include <cstdint>
#include <cstring>
2020-05-29 19:25:05 +02:00
#include <malloc.h>
#include <unistd.h>
class CThread {
public:
2020-06-03 18:36:02 +02:00
typedef void (*Callback)(CThread *thread, void *arg);
2020-05-29 19:25:05 +02:00
//! constructor
explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t stacksize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr)
2022-02-02 18:34:27 +01:00
: pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) {
2020-05-29 19:25:05 +02:00
//! save attribute assignment
iAttributes = iAttr;
iStackSize = stacksize;
//! allocate the thread on the default Cafe OS heap
pThread = (OSThread *) gMEMAllocFromDefaultHeapExForThreads(sizeof(OSThread), 0x10);
//! allocate the stack on the default Cafe OS heap
pThreadStack = (uint8_t *) gMEMAllocFromDefaultHeapExForThreads(iStackSize, 0x20);
2020-05-29 19:25:05 +02:00
//! create the thread
2020-06-03 18:36:02 +02:00
if (pThread && pThreadStack) {
2022-02-02 18:34:27 +01:00
// clang-format off
OSCreateThread(pThread, (int(*)(int, const char **)) & CThread::threadCallback, 1, (char *) this, (void *) (pThreadStack + iStackSize), iStackSize, iPriority, iAttributes);
// clang-format on
2020-05-29 19:25:05 +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-06-03 18:36:02 +02:00
return (new CThread(iAttr, iPriority, iStackSize, callback, callbackArg));
2020-05-29 19:25:05 +02:00
}
//! Get thread ID
2021-09-24 16:51:11 +02:00
[[nodiscard]] virtual void *getThread() const {
2020-05-29 19:25:05 +02:00
return pThread;
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Thread entry function
2021-09-24 16:51:11 +02:00
virtual void executeThread() {
2020-06-03 18:36:02 +02:00
if (pCallback)
2020-05-29 19:25:05 +02:00
pCallback(this, pCallbackArg);
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Suspend thread
2021-09-24 16:51:11 +02:00
virtual void suspendThread() {
2020-06-03 18:36:02 +02:00
if (isThreadSuspended()) return;
if (pThread) OSSuspendThread(pThread);
2020-05-29 19:25:05 +02:00
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Resume thread
2021-09-24 16:51:11 +02:00
virtual void resumeThread() {
2020-06-03 18:36:02 +02:00
if (!isThreadSuspended()) return;
if (pThread) {
OSResumeThread(pThread);
}
2020-05-29 19:25:05 +02:00
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Set thread priority
virtual void setThreadPriority(int32_t prio) {
2020-06-03 18:36:02 +02:00
if (pThread) OSSetThreadPriority(pThread, prio);
2020-05-29 19:25:05 +02:00
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Check if thread is suspended
2021-09-24 16:51:11 +02:00
[[nodiscard]] virtual bool isThreadSuspended() const {
2020-06-03 18:36:02 +02:00
if (pThread) return OSIsThreadSuspended(pThread);
2020-05-29 19:25:05 +02:00
return false;
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Check if thread is terminated
2021-09-24 16:51:11 +02:00
[[nodiscard]] virtual bool isThreadTerminated() const {
2020-06-03 18:36:02 +02:00
if (pThread) return OSIsThreadTerminated(pThread);
2020-05-29 19:25:05 +02:00
return false;
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Check if thread is running
2021-09-24 16:51:11 +02:00
[[nodiscard]] virtual bool isThreadRunning() const {
2020-05-29 19:25:05 +02:00
return !isThreadSuspended() && !isThreadRunning();
}
//! Gets the thread affinity.
2021-09-24 16:51:11 +02:00
[[nodiscard]] virtual uint16_t getThreadAffinity() const {
2020-06-03 18:36:02 +02:00
if (pThread) return OSGetThreadAffinity(pThread);
2020-05-29 19:25:05 +02:00
return 0;
}
//! Shutdown thread
void shutdownThread() {
2020-05-29 19:25:05 +02:00
//! wait for thread to finish
2020-06-03 18:36:02 +02:00
if (pThread && !(iAttributes & eAttributeDetach)) {
while (isThreadSuspended()) {
2020-05-29 19:25:05 +02:00
resumeThread();
}
2021-09-24 16:51:11 +02:00
OSJoinThread(pThread, nullptr);
2020-05-29 19:25:05 +02:00
}
// Some games (e.g. Minecraft) expect the default heap to be empty.
// Make sure to clean up the memory after using it
2020-05-29 19:25:05 +02:00
//! free the thread stack buffer
2020-06-03 18:36:02 +02:00
if (pThreadStack) {
memset(pThreadStack, 0, iStackSize);
gMEMFreeToDefaultHeapForThreads(pThreadStack);
2020-05-29 19:25:05 +02:00
}
2020-06-03 18:36:02 +02:00
if (pThread) {
memset(pThread, 0, sizeof(OSThread));
gMEMFreeToDefaultHeapForThreads(pThread);
2020-05-29 19:25:05 +02:00
}
pThread = nullptr;
2021-09-24 16:51:11 +02:00
pThreadStack = nullptr;
2020-05-29 19:25:05 +02:00
}
2020-06-03 18:36:02 +02:00
2020-05-29 19:25:05 +02:00
//! Thread attributes
enum eCThreadAttributes {
eAttributeNone = 0x07,
eAttributeAffCore0 = 0x01,
eAttributeAffCore1 = 0x02,
eAttributeAffCore2 = 0x04,
eAttributeDetach = 0x08,
2020-06-03 18:36:02 +02:00
eAttributePinnedAff = 0x10
2020-05-29 19:25:05 +02:00
};
2022-02-02 18:34:27 +01:00
2020-05-29 19:25:05 +02:00
private:
static int32_t threadCallback(int32_t argc, void *arg) {
//! After call to start() continue with the internal function
((CThread *) arg)->executeThread();
return 0;
}
2020-06-03 18:36:02 +02:00
uint32_t iStackSize;
2020-05-29 19:25:05 +02:00
int32_t iAttributes;
OSThread *pThread;
uint8_t *pThreadStack;
Callback pCallback;
void *pCallbackArg;
};