Added worker, based on 3ds new-hbmenu.

This commit is contained in:
yellows8 2018-10-17 22:18:09 -04:00
parent 55efa03f15
commit 11dccb4fd0
6 changed files with 78 additions and 1 deletions

View File

@ -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 \

View File

@ -10,6 +10,7 @@
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <threads.h>
#ifndef __APPLE__
#include <malloc.h>
#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 <turbojpeg.h>
#include "math.h"
#include "theme.h"

64
common/worker.c Normal file
View File

@ -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);
}

6
common/worker.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "common.h"
void workerInit(void);
void workerExit(void);
void workerSchedule(workerThreadFunc func, void* data);

View File

@ -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();

View File

@ -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;