]> Chaos Git - corbenik/ctrulib.git/commitdiff
Revise APT/GSP/NDSP to use the new thread API
authorfincs <fincs.alt1@gmail.com>
Sat, 21 Nov 2015 17:55:51 +0000 (18:55 +0100)
committerfincs <fincs.alt1@gmail.com>
Mon, 7 Dec 2015 10:44:36 +0000 (11:44 +0100)
examples/threads/event/source/main.c
libctru/source/ndsp/ndsp.c
libctru/source/services/apt.c
libctru/source/services/gspgpu.c

index cb2ba463dacea9d0c4b796078a99b4b6b134d384..7d75ceece3d13601d00447467fe287128bc3ef5f 100644 (file)
@@ -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;
index 4c5a50392c367e495eee5a01825b69d816e3ab17..c9e8a93353ac56f8363a403d22503da1677bdec7 100644 (file)
@@ -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)
index 5bbbbe2709e18d53ad106f0231007f50c6357638..ab3a17befd8ca5754986d341d8e3d80ea0741306 100644 (file)
@@ -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);
index 130b42aebc9d6e5a84616f47af77350a7e2f675d..01ac1caee33912f94bd07d21b3bfe27a8c8373c4 100644 (file)
@@ -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