Remove the old CPU threading remains, start redesigning interfaces.

This commit is contained in:
Henrik Rydgård
2017-12-15 12:40:38 +01:00
parent 49c3cb83fe
commit 42f2312030
10 changed files with 56 additions and 179 deletions
+4
View File
@@ -24,11 +24,15 @@ class GraphicsContext;
// called from emu thread
void UpdateRunLoop();
void Core_Run(GraphicsContext *ctx);
void Core_Stop();
void Core_ErrorPause();
// For platforms that don't call Core_Run
void Core_SetGraphicsContext(GraphicsContext *ctx);
void Core_RunRenderThreadFrame();
// called from gui
void Core_EnableStepping(bool step);
void Core_DoSingleStep();
+4 -168
View File
@@ -81,12 +81,7 @@ ParamSFOData g_paramSFO;
static GlobalUIState globalUIState;
static CoreParameter coreParameter;
static FileLoader *loadedFile;
static std::thread *cpuThread = nullptr;
static std::thread::id cpuThreadID;
static std::mutex cpuThreadLock;
static std::condition_variable cpuThreadCond;
static std::condition_variable cpuThreadReplyCond;
static u64 cpuThreadUntil;
bool audioInitialized;
bool coreCollectDebugStats = false;
@@ -142,45 +137,6 @@ void Audio_Shutdown() {
}
}
bool IsOnSeparateCPUThread() {
if (cpuThread != nullptr) {
return cpuThreadID == std::this_thread::get_id();
} else {
return false;
}
}
void CPU_SetStateNoLock(CPUThreadState to) {
cpuThreadState = to;
cpuThreadCond.notify_one();
cpuThreadReplyCond.notify_one();
}
void CPU_SetState(CPUThreadState to) {
std::lock_guard<std::mutex> guard(cpuThreadLock);
CPU_SetStateNoLock(to);
}
bool CPU_NextState(CPUThreadState from, CPUThreadState to) {
std::lock_guard<std::mutex> guard(cpuThreadLock);
if (cpuThreadState == from) {
CPU_SetStateNoLock(to);
return true;
} else {
return false;
}
}
bool CPU_NextStateNot(CPUThreadState from, CPUThreadState to) {
std::lock_guard<std::mutex> guard(cpuThreadLock);
if (cpuThreadState != from) {
CPU_SetStateNoLock(to);
return true;
} else {
return false;
}
}
bool CPU_IsReady() {
if (coreState == CORE_POWERUP)
return false;
@@ -195,13 +151,6 @@ bool CPU_HasPendingAction() {
return cpuThreadState != CPU_THREAD_RUNNING;
}
void CPU_WaitStatus(std::condition_variable &cond, bool (*pred)()) {
std::unique_lock<std::mutex> guard(cpuThreadLock);
while (!pred()) {
cond.wait(guard);
}
}
void CPU_Shutdown();
void CPU_Init() {
@@ -278,7 +227,6 @@ void CPU_Init() {
if (!LoadFile(&loadedFile, &coreParameter.errorString)) {
CPU_Shutdown();
coreParameter.fileToStart = "";
CPU_SetState(CPU_THREAD_NOT_RUNNING);
return;
}
@@ -321,52 +269,6 @@ void UpdateLoadedFile(FileLoader *fileLoader) {
loadedFile = fileLoader;
}
void CPU_RunLoop() {
setCurrentThreadName("CPU");
if (CPU_NextState(CPU_THREAD_PENDING, CPU_THREAD_STARTING)) {
CPU_Init();
CPU_NextState(CPU_THREAD_STARTING, CPU_THREAD_RUNNING);
} else if (!CPU_NextState(CPU_THREAD_RESUME, CPU_THREAD_RUNNING)) {
ERROR_LOG(CPU, "CPU thread in unexpected state: %d", cpuThreadState);
return;
}
while (cpuThreadState != CPU_THREAD_SHUTDOWN)
{
CPU_WaitStatus(cpuThreadCond, &CPU_HasPendingAction);
switch (cpuThreadState) {
case CPU_THREAD_EXECUTE:
mipsr4k.RunLoopUntil(cpuThreadUntil);
CPU_NextState(CPU_THREAD_EXECUTE, CPU_THREAD_RUNNING);
break;
// These are fine, just keep looping.
case CPU_THREAD_RUNNING:
case CPU_THREAD_SHUTDOWN:
break;
case CPU_THREAD_QUIT:
// Just leave the thread, CPU is switching off thread.
CPU_SetState(CPU_THREAD_NOT_RUNNING);
return;
default:
ERROR_LOG(CPU, "CPU thread in unexpected state: %d", cpuThreadState);
// Begin shutdown, otherwise we'd just spin on this bad state.
CPU_SetState(CPU_THREAD_SHUTDOWN);
break;
}
}
if (coreState != CORE_ERROR) {
coreState = CORE_POWERDOWN;
}
CPU_Shutdown();
CPU_SetState(CPU_THREAD_NOT_RUNNING);
}
void Core_UpdateState(CoreState newState) {
if ((coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME) && newState != CORE_RUNNING)
coreStatePending = true;
@@ -384,11 +286,6 @@ void Core_UpdateDebugStats(bool collectStats) {
gpuStats.ResetFrame();
}
void System_Wake() {
// Ping the threads so they check coreState.
CPU_NextStateNot(CPU_THREAD_NOT_RUNNING, CPU_THREAD_SHUTDOWN);
}
// Ugly!
static bool pspIsInited = false;
static bool pspIsIniting = false;
@@ -415,17 +312,7 @@ bool PSP_InitStart(const CoreParameter &coreParam, std::string *error_string) {
coreParameter.errorString = "";
pspIsIniting = true;
// Keeping this around because we might need it in the future.
const bool separateCPUThread = false;
if (separateCPUThread) {
Core_ListenShutdown(System_Wake);
CPU_SetState(CPU_THREAD_PENDING);
cpuThread = new std::thread(&CPU_RunLoop);
cpuThreadID = cpuThread->get_id();
cpuThread->detach();
} else {
CPU_Init();
}
CPU_Init();
*error_string = coreParameter.errorString;
bool success = coreParameter.fileToStart != "";
@@ -461,11 +348,6 @@ bool PSP_InitUpdate(std::string *error_string) {
bool PSP_Init(const CoreParameter &coreParam, std::string *error_string) {
PSP_InitStart(coreParam, error_string);
// For a potential resurrection of separate CPU thread later.
if (false) {
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsReady);
}
while (!PSP_InitUpdate(error_string))
sleep_ms(10);
return pspIsInited;
@@ -496,15 +378,7 @@ void PSP_Shutdown() {
if (coreState == CORE_RUNNING)
Core_UpdateState(CORE_ERROR);
Core_NotifyShutdown();
if (cpuThread != nullptr) {
CPU_NextStateNot(CPU_THREAD_NOT_RUNNING, CPU_THREAD_SHUTDOWN);
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsShutdown);
delete cpuThread;
cpuThread = 0;
cpuThreadID = std::thread::id();
} else {
CPU_Shutdown();
}
CPU_Shutdown();
GPU_Shutdown();
g_paramSFO.Clear();
host->SetWindowTitle(0);
@@ -534,45 +408,7 @@ void PSP_RunLoopUntil(u64 globalticks) {
return;
}
// We no longer allow a separate CPU thread but if we add a render queue
// to GL we're gonna need it.
bool useCPUThread = false;
if (useCPUThread && cpuThread == nullptr) {
// Need to start the cpu thread.
Core_ListenShutdown(System_Wake);
CPU_SetState(CPU_THREAD_RESUME);
cpuThread = new std::thread(&CPU_RunLoop);
cpuThreadID = cpuThread->get_id();
cpuThread->detach();
// Probably needs to tell the gpu that it will need to queue up its output
// on another thread.
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsReady);
} else if (!useCPUThread && cpuThread != nullptr) {
CPU_SetState(CPU_THREAD_QUIT);
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsShutdown);
delete cpuThread;
cpuThread = nullptr;
cpuThreadID = std::thread::id();
}
if (cpuThread != nullptr) {
cpuThreadUntil = globalticks;
if (CPU_NextState(CPU_THREAD_RUNNING, CPU_THREAD_EXECUTE)) {
// The CPU doesn't actually respect cpuThreadUntil well, especially when skipping frames.
// TODO: Something smarter? Or force CPU to bail periodically?
while (!CPU_IsReady()) {
// Have the GPU do stuff here.
if (coreState != CORE_RUNNING) {
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsReady);
}
}
} else {
ERROR_LOG(CPU, "Unable to execute CPU run loop, unexpected state: %d", cpuThreadState);
}
} else {
mipsr4k.RunLoopUntil(globalticks);
}
mipsr4k.RunLoopUntil(globalticks);
gpu->CleanupBeforeUI();
}
-2
View File
@@ -78,8 +78,6 @@ void Core_UpdateDebugStats(bool collectStats);
void Audio_Init();
void Audio_Shutdown();
bool IsOnSeparateCPUThread();
bool IsAudioInitialised();
void UpdateLoadedFile(FileLoader *fileLoader);