Test and fix removal of wasm worker id check
This commit is contained in:
@@ -1,19 +1,20 @@
|
|||||||
#include "Recognizer.h"
|
#include "Recognizer.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
|
static const char* recognizerInitErr{"Unable to initialize recognizer"};
|
||||||
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model) :
|
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model) :
|
||||||
rec{vosk_recognizer_new(std::get<VoskModel*>(model->mdl), sampleRate)} {
|
rec{vosk_recognizer_new(std::get<VoskModel*>(model->mdl), sampleRate)} {
|
||||||
if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer");
|
if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast<int>(recognizerInitErr));
|
||||||
else globalPool.exec([this, index]{main(index);});
|
else globalPool.exec([this, index]{main(index);});
|
||||||
}
|
}
|
||||||
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, CommonModel* spkModel) :
|
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, CommonModel* spkModel) :
|
||||||
rec{vosk_recognizer_new_spk(std::get<VoskModel*>(model->mdl), sampleRate, std::get<VoskSpkModel*>(spkModel->mdl))} {
|
rec{vosk_recognizer_new_spk(std::get<VoskModel*>(model->mdl), sampleRate, std::get<VoskSpkModel*>(spkModel->mdl))} {
|
||||||
if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer");
|
if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast<int>(recognizerInitErr));
|
||||||
else globalPool.exec([this, index]{main(index);});
|
else globalPool.exec([this, index]{main(index);});
|
||||||
}
|
}
|
||||||
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, const std::string& grm, int) :
|
Recognizer::Recognizer(int index, float sampleRate, CommonModel* model, const std::string& grm, int) :
|
||||||
rec{vosk_recognizer_new_grm(std::get<VoskModel*>(model->mdl), sampleRate, grm.c_str())} {
|
rec{vosk_recognizer_new_grm(std::get<VoskModel*>(model->mdl), sampleRate, grm.c_str())} {
|
||||||
if(rec == nullptr) fireEv(index, Event::status, "Unable to initialize recognizer");
|
if(rec == nullptr) mtFireEv(index, Event::status, reinterpret_cast<int>(recognizerInitErr));
|
||||||
else globalPool.exec([this, index]{main(index);});
|
else globalPool.exec([this, index]{main(index);});
|
||||||
}
|
}
|
||||||
void Recognizer::safeDelete(bool _processCurrent) {
|
void Recognizer::safeDelete(bool _processCurrent) {
|
||||||
|
|||||||
33
src/Util.cc
33
src/Util.cc
@@ -1,25 +1,12 @@
|
|||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include <emscripten/em_js.h>
|
|
||||||
#include <emscripten/wasm_worker.h>
|
#include <emscripten/wasm_worker.h>
|
||||||
|
#include <emscripten/em_js.h>
|
||||||
|
|
||||||
bool _isWorker() {
|
EM_JS(void, mtFireEv, (int index, int typeIdx, int content), {
|
||||||
// First thread to flip this bool is the main thread
|
|
||||||
static bool test{true};
|
|
||||||
if(test) {
|
|
||||||
test = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
thread_local bool isWorker{_isWorker()};
|
|
||||||
|
|
||||||
EM_JS(void, _fireEv, (int index, int typeIdx, int content), {
|
|
||||||
objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) }));
|
objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) }));
|
||||||
})
|
});
|
||||||
void fireEv(int index, int typeIdx, const char* content) {
|
void fireEv(int index, int typeIdx, const char* content) {
|
||||||
int contentAddr{reinterpret_cast<int>(content)};
|
emscripten_wasm_worker_post_function_viii(0, mtFireEv, index, typeIdx, reinterpret_cast<int>(content));
|
||||||
if(isWorker) emscripten_wasm_worker_post_function_viii(0, _fireEv, index, typeIdx, contentAddr);
|
|
||||||
else _fireEv(index, typeIdx, contentAddr);
|
|
||||||
}
|
}
|
||||||
int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
|
int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
|
||||||
if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat;
|
if(std::memcmp(tar + 257, "ustar", 5)) return IncorrectFormat;
|
||||||
@@ -61,9 +48,9 @@ int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
|
|||||||
}
|
}
|
||||||
return Successful;
|
return Successful;
|
||||||
}
|
}
|
||||||
void Worker::startup(int _self, int _pool) {
|
void Worker::startup(int _fn, int _pool) {
|
||||||
Worker& self = *reinterpret_cast<Worker*>(_self);
|
WorkerPool& pool{*reinterpret_cast<WorkerPool*>(_pool)};
|
||||||
WorkerPool& pool = *reinterpret_cast<WorkerPool*>(_pool);
|
std::function<void()>& fn{*reinterpret_cast<std::function<void()>*>(_fn)};
|
||||||
while(!pool.done) {
|
while(!pool.done) {
|
||||||
// Wait until unlocked
|
// Wait until unlocked
|
||||||
emscripten_atomic_wait_u32(&pool.qLock, true, -1);
|
emscripten_atomic_wait_u32(&pool.qLock, true, -1);
|
||||||
@@ -75,12 +62,12 @@ void Worker::startup(int _self, int _pool) {
|
|||||||
}
|
}
|
||||||
// If this locks, the returned (loaded) value will be false, and we move on
|
// If this locks, the returned (loaded) value will be false, and we move on
|
||||||
if(emscripten_atomic_cas_u32(&pool.qLock, false, true)) continue;
|
if(emscripten_atomic_cas_u32(&pool.qLock, false, true)) continue;
|
||||||
self.fn = pool.taskQ.front();
|
fn = pool.taskQ.front();
|
||||||
pool.taskQ.pop();
|
pool.taskQ.pop();
|
||||||
// Unlock
|
// Unlock
|
||||||
emscripten_atomic_store_u32(&pool.qLock, false);
|
emscripten_atomic_store_u32(&pool.qLock, false);
|
||||||
emscripten_atomic_notify(&pool.qLock, 1);
|
emscripten_atomic_notify(&pool.qLock, 1);
|
||||||
self.fn();
|
fn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static constexpr int workerStack{65536};
|
static constexpr int workerStack{65536};
|
||||||
@@ -89,7 +76,7 @@ static std::array<std::byte, MAX_WORKERS * workerStack> stacks;
|
|||||||
WorkerPool::WorkerPool() {
|
WorkerPool::WorkerPool() {
|
||||||
for(int i = 0; i < workers.size(); i++) {
|
for(int i = 0; i < workers.size(); i++) {
|
||||||
workers[i].handle = emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
|
workers[i].handle = emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
|
||||||
emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast<int>(&workers[i]), reinterpret_cast<int>(this));
|
emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast<int>(&workers[i].fn),reinterpret_cast<int>(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WorkerPool::~WorkerPool() {
|
WorkerPool::~WorkerPool() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <emscripten/atomic.h>
|
#include <emscripten/atomic.h>
|
||||||
#include <emscripten/console.h>
|
#include <emscripten/console.h>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
struct AudioData {
|
struct AudioData {
|
||||||
@@ -33,7 +34,7 @@ struct WorkerPool;
|
|||||||
struct Worker {
|
struct Worker {
|
||||||
int handle;
|
int handle;
|
||||||
std::function<void()> fn;
|
std::function<void()> fn;
|
||||||
static void startup(int _self, int _pool);
|
static void startup(int _fn, int _pool);
|
||||||
};
|
};
|
||||||
#ifndef MAX_WORKERS
|
#ifndef MAX_WORKERS
|
||||||
#define MAX_WORKERS 1
|
#define MAX_WORKERS 1
|
||||||
@@ -47,7 +48,12 @@ struct WorkerPool {
|
|||||||
~WorkerPool();
|
~WorkerPool();
|
||||||
void exec(std::function<void()> fn);
|
void exec(std::function<void()> fn);
|
||||||
};
|
};
|
||||||
|
// Must be called from the main thread
|
||||||
|
extern "C" void mtFireEv(int index, int typeIdx, int content);
|
||||||
|
|
||||||
|
// Must be called from a wasm worker
|
||||||
void fireEv(int index, int typeIdx, const char* content = nullptr);
|
void fireEv(int index, int typeIdx, const char* content = nullptr);
|
||||||
|
|
||||||
int untar(unsigned char* tar, int tarSize, const std::string& storepath);
|
int untar(unsigned char* tar, int tarSize, const std::string& storepath);
|
||||||
|
|
||||||
extern WorkerPool globalPool;
|
extern WorkerPool globalPool;
|
||||||
Reference in New Issue
Block a user