From 12555d77a47c31eeb0560a6b167d9bddd6859abd Mon Sep 17 00:00:00 2001 From: fincs Date: Sat, 21 Nov 2015 18:55:51 +0100 Subject: [PATCH] Revise APT/GSP/NDSP to use the new thread API --- examples/threads/event/source/main.c | 36 ++++++++++------------------ libctru/source/ndsp/ndsp.c | 13 ++++------ libctru/source/services/apt.c | 12 ++++------ libctru/source/services/gspgpu.c | 12 +++++----- 4 files changed, 28 insertions(+), 45 deletions(-) diff --git a/examples/threads/event/source/main.c b/examples/threads/event/source/main.c index cb2ba46..7d75cee 100644 --- a/examples/threads/event/source/main.c +++ b/examples/threads/event/source/main.c @@ -6,39 +6,34 @@ #include <3ds.h> -Handle threadHandle, threadRequest; +Thread threadHandle; +Handle threadRequest; #define STACKSIZE (4 * 1024) -volatile bool threadExit = false; +volatile bool runThread = true; volatile int threadcount=0; void threadMain(void *arg) { - while(1) { + while(runThread) { svcWaitSynchronization(threadRequest, U64_MAX); - svcClearEvent(threadRequest); - - if(threadExit) svcExitThread(); + svcClearEvent(threadRequest); threadcount++; } } -int main(int argc, char** argv) { - - +int main(int argc, char** argv) +{ gfxInitDefault(); - consoleInit(GFX_TOP, NULL); - svcCreateEvent(&threadRequest,0); - u32 *threadStack = memalign(32, STACKSIZE); - Result ret = svcCreateThread(&threadHandle, threadMain, 0, &threadStack[STACKSIZE/4], 0x3f, 0); + threadHandle = threadCreate(threadMain, 0, STACKSIZE, 0x3f, -2, true); - printf("thread create returned %lx\n", ret); + printf("thread handle: %p\n", threadHandle); // Main loop while (aptMainLoop()) @@ -62,19 +57,14 @@ int main(int argc, char** argv) { } // tell thread to exit - threadExit = true; + runThread = false; - // signal the thread + // signal the thread and wait for it to exit svcSignalEvent(threadRequest); + threadJoin(threadHandle, U64_MAX); - // give it time to exit - svcSleepThread(10000000ULL); - - // close handles and free allocated stack + // close event handle svcCloseHandle(threadRequest); - svcCloseHandle(threadHandle); - free(threadStack); - gfxExit(); return 0; diff --git a/libctru/source/ndsp/ndsp.c b/libctru/source/ndsp/ndsp.c index 4c5a503..c9e8a93 100644 --- a/libctru/source/ndsp/ndsp.c +++ b/libctru/source/ndsp/ndsp.c @@ -2,6 +2,7 @@ #include <3ds/services/cfgu.h> #include <3ds/services/fs.h> #include <3ds/env.h> +#include <3ds/thread.h> #define NDSP_THREAD_STACK_SIZE 0x1000 @@ -24,8 +25,7 @@ static LightLock ndspMutex; static u8 dspVar5Backup[0x1080]; static volatile bool ndspThreadRun; -static Handle ndspThread; -static u64 ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]; // u64 so that it's 8-byte aligned +static Thread ndspThread; static Result ndspLoadComponent(void) { @@ -374,8 +374,6 @@ static void ndspThreadMain(void* arg) frameCount++; bNeedsSync = true; } - - svcExitThread(); } void ndspUseComponent(const void* binary, u32 size, u16 progMask, u16 dataMask) @@ -484,8 +482,8 @@ Result ndspInit(void) rc = svcCreateEvent(&sleepEvent, 0); if (R_FAILED(rc)) goto _fail2; - rc = svcCreateThread(&ndspThread, ndspThreadMain, 0x0, (u32*)(&ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]), 0x31, -2); - if (R_FAILED(rc)) goto _fail3; + ndspThread = threadCreate(ndspThreadMain, 0x0, NDSP_THREAD_STACK_SIZE, 0x31, -2, true); + if (!ndspThread) goto _fail3; aptHook(&aptCookie, ndspAptHook, NULL); return 0; @@ -513,8 +511,7 @@ void ndspExit(void) ndspThreadRun = false; if (bSleeping) svcSignalEvent(sleepEvent); - svcWaitSynchronization(ndspThread, U64_MAX); - svcCloseHandle(ndspThread); + threadJoin(ndspThread, U64_MAX); svcCloseHandle(sleepEvent); aptUnhook(&aptCookie); if (!bSleeping) diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 5bbbbe2..ab3a17b 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -13,6 +13,7 @@ #include <3ds/services/gspgpu.h> #include <3ds/ipc.h> #include <3ds/env.h> +#include <3ds/thread.h> #define APT_HANDLER_STACKSIZE (0x1000) @@ -28,8 +29,7 @@ Handle aptLockHandle; Handle aptuHandle; Handle aptEvents[3]; -Handle aptEventHandlerThread; -u64 aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]; // u64 so that it's 8-byte aligned +Thread aptEventHandlerThread; LightLock aptStatusMutex; Handle aptStatusEvent; @@ -442,8 +442,6 @@ void aptEventHandler(void *arg) break; } } - - svcExitThread(); } static int aptRefCount = 0; @@ -498,8 +496,7 @@ Result aptInit(void) aptCloseSession(); // create APT event handler thread - svcCreateThread(&aptEventHandlerThread, aptEventHandler, 0x0, - (u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe); + aptEventHandlerThread = threadCreate(aptEventHandler, 0x0, APT_HANDLER_STACKSIZE, 0x31, -2, true); // Wait for the state to become APT_RUNNING aptWaitStatusEvent(); @@ -554,8 +551,7 @@ void aptExit(void) } svcSignalEvent(aptEvents[2]); - svcWaitSynchronization(aptEventHandlerThread, U64_MAX); - svcCloseHandle(aptEventHandlerThread); + threadJoin(aptEventHandlerThread, U64_MAX); svcCloseHandle(aptEvents[2]); svcCloseHandle(aptSleepSync); diff --git a/libctru/source/services/gspgpu.c b/libctru/source/services/gspgpu.c index 130b42a..01ac1ca 100644 --- a/libctru/source/services/gspgpu.c +++ b/libctru/source/services/gspgpu.c @@ -7,6 +7,7 @@ #include <3ds/synchronization.h> #include <3ds/services/gspgpu.h> #include <3ds/ipc.h> +#include <3ds/thread.h> #define GSP_EVENT_STACK_SIZE 0x1000 @@ -15,9 +16,8 @@ static int gspRefCount; Handle gspEvents[GSPGPU_EVENT_MAX]; vu32 gspEventCounts[GSPGPU_EVENT_MAX]; -u64 gspEventStack[GSP_EVENT_STACK_SIZE/sizeof(u64)]; //u64 so that it's 8-byte aligned volatile bool gspRunEvents; -Handle gspEventThread; +Thread gspEventThread; static Handle gspEvent; static vu8* gspEventData; @@ -60,15 +60,16 @@ Result gspInitEventHandler(Handle _gspEvent, vu8* _gspSharedMem, u8 gspThreadId) gspEvent = _gspEvent; gspEventData = _gspSharedMem + gspThreadId*0x40; gspRunEvents = true; - return svcCreateThread(&gspEventThread, gspEventThreadMain, 0x0, (u32*)((char*)gspEventStack + sizeof(gspEventStack)), 0x31, 0xfffffffe); + gspEventThread = threadCreate(gspEventThreadMain, 0x0, GSP_EVENT_STACK_SIZE, 0x31, -2, true); + return 0; } void gspExitEventHandler(void) { // Stop event thread gspRunEvents = false; - svcWaitSynchronization(gspEventThread, 1000000000); - svcCloseHandle(gspEventThread); + svcSignalEvent(gspEvent); + threadJoin(gspEventThread, U64_MAX); // Free events int i; @@ -143,7 +144,6 @@ void gspEventThreadMain(void *arg) } } } - svcExitThread(); } //essentially : get commandIndex and totalCommands, calculate offset of new command, copy command and update totalCommands -- 2.39.5