Remodel threading to use a global thread pool so it never block on the main thread. Bump emscripten to 3.1.65

This commit is contained in:
msqr1
2024-08-26 22:37:35 -07:00
parent 53262fe7f3
commit c4ff62fa31
14 changed files with 111 additions and 81 deletions

View File

@@ -2,9 +2,13 @@
CommonModel::CommonModel(int index, bool normalMdl, std::string storepath, std::string id, int tarStart, int tarSize) :
normalMdl{normalMdl}, index{index},
t{&CommonModel::extractAndLoad, this, reinterpret_cast<unsigned char*>(tarStart), tarSize},
storepath{std::move(storepath)},
id{std::move(id)} {}
id{std::move(id)}
{
globalPool.exec([this, tarStart, tarSize]{
extractAndLoad(reinterpret_cast<unsigned char*>(tarStart), tarSize);
});
}
void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) {
int res{untar(tar, tarSize, storepath)};
free(tar);
@@ -30,10 +34,6 @@ void CommonModel::extractAndLoad(unsigned char* tar, int tarSize) {
if(normalMdl ? std::get<VoskModel*>(mdl) != nullptr : std::get<VoskSpkModel*>(mdl) != nullptr) fireEv(index, "0");
else fireEv(index, "Unable to load model for recognition");
fs::remove_all(storepath);
// Wait for potential recognizer usage
thrdUsed.wait(false, std::memory_order_relaxed);
if(recognizerMain) recognizerMain();
}
int CommonModel::findWord(std::string word) {
return vosk_model_find_word(std::get<VoskModel*>(mdl), word.c_str());
@@ -41,9 +41,4 @@ int CommonModel::findWord(std::string word) {
CommonModel::~CommonModel() {
if(normalMdl) vosk_model_free(std::get<VoskModel*>(mdl));
else vosk_spk_model_free(std::get<VoskSpkModel*>(mdl));
if(t.joinable()) {
thrdUsed.store(true, std::memory_order_relaxed);
thrdUsed.notify_one();
t.join();
}
}