Startup wasm worker all at once

This commit is contained in:
msqr1
2024-09-27 17:34:30 -07:00
parent 4f78f596ae
commit e3f19bc6cf
9 changed files with 25 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
#include "Util.h"
#include <emscripten/wasm_worker.h>
#include <emscripten/em_js.h>
#include "emscripten/wasm_worker.h"
#include "emscripten/em_js.h"
EM_JS(void, mtFireEv, (int index, int typeIdx, int content), {
objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) }));
@@ -48,9 +49,9 @@ int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
}
return Successful;
}
void Worker::startup(int _fn, int _pool) {
static void workerStartup(int _pool) {
WorkerPool& pool{*reinterpret_cast<WorkerPool*>(_pool)};
std::function<void()>& fn{*reinterpret_cast<std::function<void()>*>(_fn)};
std::function<void()> fn;
while(!pool.done) {
// Wait until unlocked
emscripten_atomic_wait_u32(&pool.qLock, true, -1);
@@ -70,15 +71,24 @@ void Worker::startup(int _fn, int _pool) {
fn();
}
}
using _startupFn = void(*)(int);
EM_JS(void, startupWorkers, (_startupFn startupFn, WorkerPool* pool), {
Object.values(_wasmWorkers).forEach(worker => {
worker.postMessage({
"_wsc": startupFn,
"x": [ pool ]
});
})
})
static constexpr int workerStack{32768};
static std::array<std::byte, MAX_WORKERS * workerStack> stacks;
#undef MAX_WORKERS
WorkerPool::WorkerPool() {
for(unsigned int i = 0; i < workers.size(); i++) {
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].fn),reinterpret_cast<int>(this));
for(int i{}; i < MAX_WORKERS; ++i) {
emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
}
startupWorkers(workerStartup, this);
}
#undef MAX_WORKERS
WorkerPool::~WorkerPool() {
done = true;
emscripten_atomic_store_u32(&qLock, false);