Web Worker pool for offloading CPU-intensive operations. More...
#include <wasm_worker_pool.h>
Classes | |
| struct | Task |
| struct | WorkerStats |
Public Types | |
| enum class | TaskType { kRomDecompression , kGraphicsDecoding , kPaletteCalculation , kAsarCompilation , kCustom } |
| enum class | Priority { kLow = 0 , kNormal = 1 , kHigh = 2 , kCritical = 3 } |
| using | TaskCallback = std::function<void(bool success, const std::vector<uint8_t>& result)> |
| using | ProgressCallback = std::function<void(float progress, const std::string& message)> |
Public Member Functions | |
| WasmWorkerPool (size_t num_workers=0) | |
| ~WasmWorkerPool () | |
| bool | Initialize () |
| void | Shutdown () |
| uint32_t | SubmitTask (TaskType type, const std::vector< uint8_t > &input_data, TaskCallback callback, Priority priority=Priority::kNormal) |
| Submit a task to the worker pool. | |
| uint32_t | SubmitCustomTask (const std::string &type_string, const std::vector< uint8_t > &input_data, TaskCallback callback, Priority priority=Priority::kNormal) |
| Submit a custom task type. | |
| uint32_t | SubmitTaskWithProgress (TaskType type, const std::vector< uint8_t > &input_data, TaskCallback completion_callback, ProgressCallback progress_callback, Priority priority=Priority::kNormal) |
| Submit a task with progress reporting. | |
| bool | Cancel (uint32_t task_id) |
| Cancel a pending task. | |
| void | CancelAllOfType (TaskType type) |
| Cancel all pending tasks of a specific type. | |
| bool | WaitAll (uint32_t timeout_ms=0) |
| Wait for all pending tasks to complete. | |
| size_t | GetPendingCount () const |
| Get the number of pending tasks. | |
| size_t | GetActiveWorkerCount () const |
| Get the number of active workers. | |
| std::vector< WorkerStats > | GetWorkerStats () const |
| Get statistics for all workers. | |
| bool | IsInitialized () const |
| Check if the worker pool is initialized. | |
| void | SetMaxWorkers (size_t count) |
| Set the maximum number of concurrent workers. | |
| void | ProcessCallbacks () |
| Process any pending callbacks on the main thread. Should be called periodically from the main loop. | |
Static Public Attributes | |
| static constexpr uint32_t | kSynchronousTaskId = UINT32_MAX |
Private Member Functions | |
| void | WorkerThread (size_t worker_id) |
| void | ProcessTask (const Task &task, size_t worker_id) |
| std::vector< uint8_t > | ExecuteTask (const Task &task) |
| std::vector< uint8_t > | ProcessRomDecompression (const std::vector< uint8_t > &input) |
| std::vector< uint8_t > | ProcessGraphicsDecoding (const std::vector< uint8_t > &input) |
| std::vector< uint8_t > | ProcessPaletteCalculation (const std::vector< uint8_t > &input) |
| std::vector< uint8_t > | ProcessAsarCompilation (const std::vector< uint8_t > &input) |
| void | ReportProgress (uint32_t task_id, float progress, const std::string &message) |
| void | QueueCallback (std::function< void()> callback) |
Private Attributes | |
| bool | initialized_ = false |
| bool | shutting_down_ = false |
| size_t | num_workers_ |
| uint32_t | next_task_id_ {1} |
| std::vector< WorkerStats > | worker_stats_ |
| size_t | active_workers_ {0} |
| size_t | total_tasks_submitted_ {0} |
| size_t | total_tasks_completed_ {0} |
Web Worker pool for offloading CPU-intensive operations.
This class manages a pool of background workers (using pthreads in Emscripten) to handle heavy processing tasks without blocking the UI thread. Supported task types include:
The implementation uses Emscripten's pthread support which maps to Web Workers in the browser. Callbacks are executed on the main thread to ensure safe UI updates.
Definition at line 43 of file wasm_worker_pool.h.
| using yaze::app::platform::wasm::WasmWorkerPool::TaskCallback = std::function<void(bool success, const std::vector<uint8_t>& result)> |
Definition at line 63 of file wasm_worker_pool.h.
| using yaze::app::platform::wasm::WasmWorkerPool::ProgressCallback = std::function<void(float progress, const std::string& message)> |
Definition at line 66 of file wasm_worker_pool.h.
|
strong |
| Enumerator | |
|---|---|
| kRomDecompression | |
| kGraphicsDecoding | |
| kPaletteCalculation | |
| kAsarCompilation | |
| kCustom | |
Definition at line 46 of file wasm_worker_pool.h.
|
strong |
| Enumerator | |
|---|---|
| kLow | |
| kNormal | |
| kHigh | |
| kCritical | |
Definition at line 55 of file wasm_worker_pool.h.
| yaze::app::platform::wasm::WasmWorkerPool::WasmWorkerPool | ( | size_t | num_workers = 0 | ) |
Definition at line 535 of file wasm_worker_pool.cc.
| yaze::app::platform::wasm::WasmWorkerPool::~WasmWorkerPool | ( | ) |
Definition at line 536 of file wasm_worker_pool.cc.
| bool yaze::app::platform::wasm::WasmWorkerPool::Initialize | ( | ) |
Definition at line 538 of file wasm_worker_pool.cc.
| void yaze::app::platform::wasm::WasmWorkerPool::Shutdown | ( | ) |
Definition at line 539 of file wasm_worker_pool.cc.
| uint32_t yaze::app::platform::wasm::WasmWorkerPool::SubmitTask | ( | TaskType | type, |
| const std::vector< uint8_t > & | input_data, | ||
| TaskCallback | callback, | ||
| Priority | priority = Priority::kNormal ) |
Submit a task to the worker pool.
| type | The type of task to process |
| input_data | The input data for the task |
| callback | Callback to invoke on completion (executed on main thread) |
| priority | Task priority (higher priority tasks are processed first) |
Definition at line 541 of file wasm_worker_pool.cc.
| uint32_t yaze::app::platform::wasm::WasmWorkerPool::SubmitCustomTask | ( | const std::string & | type_string, |
| const std::vector< uint8_t > & | input_data, | ||
| TaskCallback | callback, | ||
| Priority | priority = Priority::kNormal ) |
Submit a custom task type.
| type_string | Custom task type identifier |
| input_data | The input data for the task |
| callback | Callback to invoke on completion |
| priority | Task priority |
Definition at line 552 of file wasm_worker_pool.cc.
| uint32_t yaze::app::platform::wasm::WasmWorkerPool::SubmitTaskWithProgress | ( | TaskType | type, |
| const std::vector< uint8_t > & | input_data, | ||
| TaskCallback | completion_callback, | ||
| ProgressCallback | progress_callback, | ||
| Priority | priority = Priority::kNormal ) |
Submit a task with progress reporting.
Definition at line 562 of file wasm_worker_pool.cc.
| bool yaze::app::platform::wasm::WasmWorkerPool::Cancel | ( | uint32_t | task_id | ) |
Cancel a pending task.
| task_id | The task ID to cancel |
Definition at line 573 of file wasm_worker_pool.cc.
| void yaze::app::platform::wasm::WasmWorkerPool::CancelAllOfType | ( | TaskType | type | ) |
Cancel all pending tasks of a specific type.
Definition at line 574 of file wasm_worker_pool.cc.
| bool yaze::app::platform::wasm::WasmWorkerPool::WaitAll | ( | uint32_t | timeout_ms = 0 | ) |
Wait for all pending tasks to complete.
| timeout_ms | Maximum time to wait in milliseconds (0 = infinite) |
Definition at line 575 of file wasm_worker_pool.cc.
| size_t yaze::app::platform::wasm::WasmWorkerPool::GetPendingCount | ( | ) | const |
Get the number of pending tasks.
Definition at line 576 of file wasm_worker_pool.cc.
| size_t yaze::app::platform::wasm::WasmWorkerPool::GetActiveWorkerCount | ( | ) | const |
Get the number of active workers.
Definition at line 577 of file wasm_worker_pool.cc.
| std::vector< WasmWorkerPool::WorkerStats > yaze::app::platform::wasm::WasmWorkerPool::GetWorkerStats | ( | ) | const |
Get statistics for all workers.
Definition at line 578 of file wasm_worker_pool.cc.
|
inline |
Check if the worker pool is initialized.
Definition at line 177 of file wasm_worker_pool.h.
References initialized_.
| void yaze::app::platform::wasm::WasmWorkerPool::SetMaxWorkers | ( | size_t | count | ) |
Set the maximum number of concurrent workers.
Definition at line 579 of file wasm_worker_pool.cc.
| void yaze::app::platform::wasm::WasmWorkerPool::ProcessCallbacks | ( | ) |
Process any pending callbacks on the main thread. Should be called periodically from the main loop.
Definition at line 580 of file wasm_worker_pool.cc.
|
private |
Definition at line 582 of file wasm_worker_pool.cc.
|
private |
Definition at line 583 of file wasm_worker_pool.cc.
|
private |
Definition at line 584 of file wasm_worker_pool.cc.
|
private |
Definition at line 585 of file wasm_worker_pool.cc.
|
private |
Definition at line 586 of file wasm_worker_pool.cc.
|
private |
Definition at line 587 of file wasm_worker_pool.cc.
|
private |
Definition at line 588 of file wasm_worker_pool.cc.
|
private |
Definition at line 589 of file wasm_worker_pool.cc.
|
private |
Definition at line 590 of file wasm_worker_pool.cc.
|
staticconstexpr |
Definition at line 90 of file wasm_worker_pool.h.
|
private |
Definition at line 218 of file wasm_worker_pool.h.
Referenced by IsInitialized().
|
private |
Definition at line 219 of file wasm_worker_pool.h.
|
private |
Definition at line 220 of file wasm_worker_pool.h.
|
private |
Definition at line 262 of file wasm_worker_pool.h.
|
private |
Definition at line 263 of file wasm_worker_pool.h.
|
private |
Definition at line 264 of file wasm_worker_pool.h.
|
private |
Definition at line 265 of file wasm_worker_pool.h.
|
private |
Definition at line 266 of file wasm_worker_pool.h.