nx-hbmenu/common/worker.c
2018-10-17 22:18:09 -04:00

65 lines
1.1 KiB
C

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