Skip to content

Commit

Permalink
3DS: Set stack size w/o starting SVM inside a thread
Browse files Browse the repository at this point in the history
This commit introduces a simpler method for setting the stack size. By setting a global variable __stacksize__,
we can override the identically-named, weak-attributed __stacksize__ defined in libctru. This is an intentional
feature of libctru, and renders it unnecessary to run ScummVM from within a thread.

Source: devkitPro/libctru@192b88b
Explanation: https://gbatemp.net/threads/homebrew-development.360646/page-245#post-6362424
Usage in the wild: https://github.com/masterfeizz/ioQuake3DS/blob/master/code/ctr/ctr_main.c#L44
  • Loading branch information
BallM4788 committed Nov 1, 2021
1 parent a1cbc42 commit 4b9903f
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions backends/platform/3ds/main.cpp
Expand Up @@ -30,28 +30,8 @@ enum {
SYSTEM_MODEL_2DS = 3
};

struct CommandLine {
int argumentCount;
char** argumentsValue;

CommandLine(int argc, char** argv): argumentCount(argc), argumentsValue(argv) {}
};

static void mainThreadFunc(void *threadParams) {
g_system = new N3DS::OSystem_3DS();
assert(g_system);

#ifdef DYNAMIC_MODULES
PluginManager::instance().addPluginProvider(new CTRPluginProvider());
#endif

CommandLine *commandLine = static_cast<CommandLine *>(threadParams);
int res = scummvm_main(commandLine->argumentCount, commandLine->argumentsValue);

g_system->destroy();

threadExit(res);
};
// Set the size of the stack.
u32 __stacksize__ = 64 * 1024;

int main(int argc, char *argv[]) {
// Initialize basic libctru stuff
Expand All @@ -75,17 +55,16 @@ int main(int argc, char *argv[]) {
socInit((u32 *)soc_sharedmem, soc_sharedmem_size);
#endif

// Start ScummVM in a separate thread to be able to set the stack size.
// The default stack is not large enough.
CommandLine commandLine(argc, argv);
g_system = new N3DS::OSystem_3DS();
assert(g_system);

#ifdef DYNAMIC_MODULES
PluginManager::instance().addPluginProvider(new CTRPluginProvider());
#endif

s32 mainThreadPriority = 0;
svcGetThreadPriority(&mainThreadPriority, CUR_THREAD_HANDLE);
int res = scummvm_main(argc, argv);

Thread mainThread = threadCreate(&mainThreadFunc, &commandLine, 64 * 1024, mainThreadPriority, -2, false);
threadJoin(mainThread, U64_MAX);
int res = threadGetExitCode(mainThread);
threadFree(mainThread);
g_system->destroy();

// Turn on both screen backlights before exiting.
if (R_SUCCEEDED(gspLcdInit())) {
Expand Down

0 comments on commit 4b9903f

Please sign in to comment.