Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Thread scheduling test. The low priority thread join is not successful, but the low priority thread has a chance to run during one of the printf calls (due to svcSendSyncRequest).
#include <3ds.h>
#include <stdio.h>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
static LightEvent test_event;
static s64 base_tick;
static std::vector<std::pair<std::string_view, s64>> thread_order;
int main(int argc, char **argv)
{
gfxInitDefault();
//Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
consoleInit(GFX_TOP, NULL);
LightEvent_Init(&test_event, ResetType::RESET_ONESHOT);
base_tick = svcGetSystemTick();
auto highPrio = threadCreate([](void *arg) {
thread_order.emplace_back("High priority thread start!", svcGetSystemTick() - base_tick);
LightEvent_Wait(&test_event);
thread_order.emplace_back("High priority thread done!", svcGetSystemTick() - base_tick);
}, nullptr, 0x1000, 0x20, 0, false);
thread_order.emplace_back("Created high priority thread", svcGetSystemTick() - base_tick);
auto lowPrio = threadCreate([](void *arg) {
thread_order.emplace_back("Low priority thread start!", svcGetSystemTick() - base_tick);
LightEvent_Wait(&test_event);
thread_order.emplace_back("Low priority thread done!", svcGetSystemTick() - base_tick);
}, nullptr, 0x1000, 0x30, 0, false);
thread_order.emplace_back("Created low priority thread", svcGetSystemTick() - base_tick);
thread_order.emplace_back("Send signal 1", svcGetSystemTick() - base_tick);
LightEvent_Signal(&test_event);
svcSleepThread(500000ULL);
thread_order.emplace_back("Send signal 2", svcGetSystemTick() - base_tick);
LightEvent_Signal(&test_event);
thread_order.emplace_back("Join high priority thread", svcGetSystemTick() - base_tick);
threadJoin(highPrio, 0);
thread_order.emplace_back("Join low priority thread", svcGetSystemTick() - base_tick);
threadJoin(lowPrio, 0);
thread_order.emplace_back("Test done", svcGetSystemTick() - base_tick);
printf("Hello World!\n");
std::string str;
for (auto &sv : thread_order) {
str = sv.first;
printf("%s %lld\n", str.c_str(), sv.second);
}
printf("Press Start to exit.\n");
// Main loop
while (aptMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown();
if (kDown & KEY_START) break; // break in order to return to hbmenu
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
//Wait for VBlank
gspWaitForVBlank();
}
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment