diff --git a/Makefile.pc b/Makefile.pc index fde3786..774175f 100644 --- a/Makefile.pc +++ b/Makefile.pc @@ -19,7 +19,7 @@ define bin2o endef test : pc_main/main.cpp pc_main/pc_launch.c pc_main/pc_power.c \ - common/menu.c common/font.c common/language.c common/launch.c \ + common/menu.c common/font.c common/language.c common/launch.c common/worker.c \ common/menu-entry.c common/menu-list.c common/message-box.c common/text.c \ common/ui.c common/math.c common/theme.c \ common/netloader.c \ diff --git a/common/common.h b/common/common.h index b4cb2b4..b27a68c 100644 --- a/common/common.h +++ b/common/common.h @@ -10,6 +10,7 @@ #include #include #include +#include #ifndef __APPLE__ #include #endif @@ -24,6 +25,7 @@ typedef uint32_t u32; typedef uint64_t u64; typedef u32 Result; +typedef void (*workerThreadFunc)(void *); #ifdef _WIN32 #define DIRECTORY_SEPARATOR_CHAR '\\' @@ -54,6 +56,7 @@ typedef union { #include "text.h" #include "ui.h" #include "launch.h" +#include "worker.h" #include #include "math.h" #include "theme.h" diff --git a/common/worker.c b/common/worker.c new file mode 100644 index 0000000..a3d5342 --- /dev/null +++ b/common/worker.c @@ -0,0 +1,64 @@ +#include "worker.h" + +static thrd_t s_workerThread; +static cnd_t s_workerCdn; +static mtx_t s_workerMtx; + +static volatile struct +{ + workerThreadFunc func; + void* data; + + bool exit; +} s_workerParam; + +static int workerThreadProc(void* unused) +{ + mtx_lock(&s_workerMtx); + + for (;;) + { + cnd_wait(&s_workerCdn, &s_workerMtx); + + if (s_workerParam.exit) + break; + + s_workerParam.func(s_workerParam.data); + } + + mtx_unlock(&s_workerMtx); + + return 0; +} + +void workerInit(void) +{ + cnd_init(&s_workerCdn); + mtx_init(&s_workerMtx, mtx_plain); + + thrd_create(&s_workerThread, workerThreadProc, 0); +} + +void workerExit(void) +{ + int res=0; + + mtx_lock(&s_workerMtx); + s_workerParam.exit = true; + cnd_signal(&s_workerCdn); + mtx_unlock(&s_workerMtx); + + thrd_join(s_workerThread, &res); + mtx_destroy(&s_workerMtx); + cnd_destroy(&s_workerCdn); +} + +void workerSchedule(workerThreadFunc func, void* data) +{ + mtx_lock(&s_workerMtx); + s_workerParam.func = func; + s_workerParam.data = data; + cnd_signal(&s_workerCdn); + mtx_unlock(&s_workerMtx); +} + diff --git a/common/worker.h b/common/worker.h new file mode 100644 index 0000000..5133b7c --- /dev/null +++ b/common/worker.h @@ -0,0 +1,6 @@ +#pragma once +#include "common.h" + +void workerInit(void); +void workerExit(void); +void workerSchedule(workerThreadFunc func, void* data); diff --git a/nx_main/main.c b/nx_main/main.c index f9bf639..3dcfb7e 100644 --- a/nx_main/main.c +++ b/nx_main/main.c @@ -46,6 +46,7 @@ int main(int argc, char **argv) themeStartup((ThemePreset)theme); textInit(); powerInit(); + workerInit(); menuStartup(); launchInit(); @@ -101,6 +102,7 @@ int main(int argc, char **argv) fontExit(); launchExit(); + workerExit(); powerExit(); plExit(); setsysExit(); diff --git a/pc_main/main.cpp b/pc_main/main.cpp index 6019d09..90e9775 100644 --- a/pc_main/main.cpp +++ b/pc_main/main.cpp @@ -19,6 +19,7 @@ int main() themeStartup(THEME_PRESET_LIGHT); textInit(); fontInitialize(); + workerInit(); menuStartup(); while (window.isOpen()) @@ -52,6 +53,7 @@ int main() window.display(); } + workerExit(); fontExit(); return 0;