threads/event/source/main.c
#include <string.h>
#include <malloc.h>
#include <inttypes.h>
#include <stdio.h>
#include <3ds.h>
Thread threadHandle;
Handle threadRequest;
#define STACKSIZE (4 * 1024)
volatile bool runThread = true;
volatile int threadcount=0;
void threadMain(void *arg) {
while(runThread) {
svcWaitSynchronization(threadRequest, U64_MAX);
svcClearEvent(threadRequest);
threadcount++;
}
}
int main(int argc, char** argv)
{
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
svcCreateEvent(&threadRequest,0);
threadHandle = threadCreate(threadMain, 0, STACKSIZE, 0x3f, -2, true);
printf("thread handle: %p\n", threadHandle);
// Main loop
while (aptMainLoop())
{
gspWaitForVBlank();
hidScanInput();
printf("\x1b[6;1H");
printf("thread counter = %d\n",threadcount);
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break; // break in order to return to hbmenu
if (kDown & KEY_A)
svcSignalEvent(threadRequest);
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
}
// tell thread to exit
runThread = false;
// signal the thread and wait for it to exit
svcSignalEvent(threadRequest);
threadJoin(threadHandle, U64_MAX);
// close event handle
svcCloseHandle(threadRequest);
gfxExit();
return 0;
}
3ds.h
Central 3DS header.
aptMainLoop
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
consoleInit
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
gfxSwapBuffers
void gfxSwapBuffers(void)
Updates the configuration of both screens.
gfxInitDefault
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
GFX_TOP
@ GFX_TOP
Top screen.
Definition: gfx.h:26
gfxExit
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
gfxFlushBuffers
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
gspWaitForVBlank
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
KEY_A
@ KEY_A
A.
Definition: hid.h:12
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
hidScanInput
void hidScanInput(void)
Scans HID for input data.
svcCloseHandle
Result svcCloseHandle(Handle handle)
Closes a handle.
svcWaitSynchronization
Result svcWaitSynchronization(Handle handle, s64 nanoseconds)
Waits for synchronization on a handle.
svcCreateEvent
Result svcCreateEvent(Handle *event, ResetType reset_type)
Creates an event handle.
svcClearEvent
Result svcClearEvent(Handle handle)
Clears an event.
svcSignalEvent
Result svcSignalEvent(Handle handle)
Signals an event.
Thread
struct Thread_tag * Thread
libctru thread handle type
Definition: thread.h:22
threadCreate
Thread threadCreate(ThreadFunc entrypoint, void *arg, size_t stack_size, int prio, int core_id, bool detached)
Creates a new libctru thread.
threadJoin
Result threadJoin(Thread thread, u64 timeout_ns)
Waits for a libctru thread to finish (or returns immediately if it is already finished).
U64_MAX
#define U64_MAX
The maximum value of a u64.
Definition: types.h:12
Handle
u32 Handle
Resource handle.
Definition: types.h:41
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23