From 9d6299579912a321073343ed25f6abee850f87ee Mon Sep 17 00:00:00 2001 From: TuxSH Date: Sun, 26 Apr 2020 21:37:18 +0100 Subject: [PATCH] rosalina: make the task runner thread actually exit --- sysmodules/pm/source/task_runner.c | 18 +++++++++++++++++- sysmodules/pm/source/task_runner.h | 4 ++++ sysmodules/rosalina/include/task_runner.h | 5 ++++- sysmodules/rosalina/source/main.c | 7 ++++--- sysmodules/rosalina/source/task_runner.c | 13 +++++++++++-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/sysmodules/pm/source/task_runner.c b/sysmodules/pm/source/task_runner.c index 79cbf02..4c76d52 100644 --- a/sysmodules/pm/source/task_runner.c +++ b/sysmodules/pm/source/task_runner.c @@ -4,6 +4,11 @@ TaskRunner g_taskRunner; +static void taskRunnerNoOpFunction(void *args) +{ + (void)args; +} + void TaskRunner_Init(void) { memset(&g_taskRunner, 0, sizeof(TaskRunner)); @@ -20,12 +25,23 @@ void TaskRunner_RunTask(void (*task)(void *argdata), void *argdata, size_t argsi LightEvent_Signal(&g_taskRunner.parametersSetEvent); } +void TaskRunner_Terminate(void) +{ + g_taskRunner.shouldTerminate = true; + TaskRunner_RunTask(taskRunnerNoOpFunction, NULL, 0); +} + void TaskRunner_HandleTasks(void *p) { (void)p; - for (;;) { + while (!g_taskRunner.shouldTerminate) { LightEvent_Signal(&g_taskRunner.readyEvent); LightEvent_Wait(&g_taskRunner.parametersSetEvent); g_taskRunner.task(g_taskRunner.argStorage); } } + +void TaskRunner_WaitReady(void) +{ + LightEvent_Wait(&g_taskRunner.readyEvent); +} diff --git a/sysmodules/pm/source/task_runner.h b/sysmodules/pm/source/task_runner.h index de277cb..ac445cf 100644 --- a/sysmodules/pm/source/task_runner.h +++ b/sysmodules/pm/source/task_runner.h @@ -8,11 +8,15 @@ typedef struct TaskRunner { LightEvent parametersSetEvent; void (*task)(void *argdata); u8 argStorage[0x40]; + bool shouldTerminate; } TaskRunner; extern TaskRunner g_taskRunner; void TaskRunner_Init(void); void TaskRunner_RunTask(void (*task)(void *argdata), void *argdata, size_t argsize); +void TaskRunner_Terminate(void); + /// Thread function void TaskRunner_HandleTasks(void *p); +void TaskRunner_WaitReady(void); diff --git a/sysmodules/rosalina/include/task_runner.h b/sysmodules/rosalina/include/task_runner.h index 9e924d8..dbab322 100644 --- a/sysmodules/rosalina/include/task_runner.h +++ b/sysmodules/rosalina/include/task_runner.h @@ -9,6 +9,7 @@ typedef struct TaskRunner { LightEvent parametersSetEvent; void (*task)(void *argdata); u8 argStorage[0x40]; + bool shouldTerminate; } TaskRunner; extern TaskRunner g_taskRunner; @@ -17,6 +18,8 @@ MyThread *taskRunnerCreateThread(void); void TaskRunner_Init(void); void TaskRunner_RunTask(void (*task)(void *argdata), void *argdata, size_t argsize); +void TaskRunner_Terminate(void); + /// Thread function void TaskRunner_HandleTasks(void); -void TaskRunner_WaitReady(void); \ No newline at end of file +void TaskRunner_WaitReady(void); diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index 77234a7..7fd0317 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -97,7 +97,7 @@ void exit(int rc) svcCloseHandle(*fsRegGetSessionHandle()); srvExit(); __sync_fini();*/ - + svcExitProcess(); } @@ -163,10 +163,9 @@ static void handleTermNotification(u32 notificationId) static void handleNextApplicationDebuggedByForce(u32 notificationId) { - int dummy; (void)notificationId; // Following call needs to be async because pm -> Loader depends on rosalina hb:ldr, handled in this very thread. - TaskRunner_RunTask(debuggerFetchAndSetNextApplicationDebugHandleTask, &dummy, 0); + TaskRunner_RunTask(debuggerFetchAndSetNextApplicationDebugHandleTask, NULL, 0); } static const ServiceManagerServiceEntry services[] = { @@ -204,6 +203,8 @@ int main(void) if (R_FAILED(ServiceManager_Run(services, notifications, NULL))) svcBreak(USERBREAK_PANIC); + TaskRunner_Terminate(); + MyThread_Join(menuThread, -1LL); MyThread_Join(taskRunnerThread, -1LL); MyThread_Join(errDispThread, -1LL); diff --git a/sysmodules/rosalina/source/task_runner.c b/sysmodules/rosalina/source/task_runner.c index 589e4b2..3e320cc 100644 --- a/sysmodules/rosalina/source/task_runner.c +++ b/sysmodules/rosalina/source/task_runner.c @@ -7,7 +7,10 @@ TaskRunner g_taskRunner; static MyThread taskRunnerThread; static u8 ALIGN(8) taskRunnerThreadStack[0x1000]; -extern bool terminationRequest; +static void taskRunnerNoOpFunction(void *args) +{ + (void)args; +} MyThread *taskRunnerCreateThread(void) { @@ -32,9 +35,15 @@ void TaskRunner_RunTask(void (*task)(void *argdata), void *argdata, size_t argsi LightEvent_Signal(&g_taskRunner.parametersSetEvent); } +void TaskRunner_Terminate(void) +{ + g_taskRunner.shouldTerminate = true; + TaskRunner_RunTask(taskRunnerNoOpFunction, NULL, 0); +} + void TaskRunner_HandleTasks(void) { - while (!terminationRequest) { + while (!g_taskRunner.shouldTerminate) { LightEvent_Signal(&g_taskRunner.readyEvent); LightEvent_Wait(&g_taskRunner.parametersSetEvent); g_taskRunner.task(g_taskRunner.argStorage);