yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::app::platform::wasm::WasmWorkerPool Class Reference

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< WorkerStatsGetWorkerStats () 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< WorkerStatsworker_stats_
 
size_t active_workers_ {0}
 
size_t total_tasks_submitted_ {0}
 
size_t total_tasks_completed_ {0}
 

Detailed Description

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:

  • ROM decompression (LC-LZ2)
  • Graphics sheet decoding
  • Palette calculations
  • Asar assembly compilation

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.

Member Typedef Documentation

◆ TaskCallback

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.

◆ ProgressCallback

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.

Member Enumeration Documentation

◆ TaskType

Enumerator
kRomDecompression 
kGraphicsDecoding 
kPaletteCalculation 
kAsarCompilation 
kCustom 

Definition at line 46 of file wasm_worker_pool.h.

◆ Priority

Enumerator
kLow 
kNormal 
kHigh 
kCritical 

Definition at line 55 of file wasm_worker_pool.h.

Constructor & Destructor Documentation

◆ WasmWorkerPool()

yaze::app::platform::wasm::WasmWorkerPool::WasmWorkerPool ( size_t num_workers = 0)

Definition at line 535 of file wasm_worker_pool.cc.

◆ ~WasmWorkerPool()

yaze::app::platform::wasm::WasmWorkerPool::~WasmWorkerPool ( )

Definition at line 536 of file wasm_worker_pool.cc.

Member Function Documentation

◆ Initialize()

bool yaze::app::platform::wasm::WasmWorkerPool::Initialize ( )

Definition at line 538 of file wasm_worker_pool.cc.

◆ Shutdown()

void yaze::app::platform::wasm::WasmWorkerPool::Shutdown ( )

Definition at line 539 of file wasm_worker_pool.cc.

◆ SubmitTask()

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.

Parameters
typeThe type of task to process
input_dataThe input data for the task
callbackCallback to invoke on completion (executed on main thread)
priorityTask priority (higher priority tasks are processed first)
Returns
Task ID that can be used for cancellation

Definition at line 541 of file wasm_worker_pool.cc.

◆ SubmitCustomTask()

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.

Parameters
type_stringCustom task type identifier
input_dataThe input data for the task
callbackCallback to invoke on completion
priorityTask priority
Returns
Task ID

Definition at line 552 of file wasm_worker_pool.cc.

◆ SubmitTaskWithProgress()

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.

◆ Cancel()

bool yaze::app::platform::wasm::WasmWorkerPool::Cancel ( uint32_t task_id)

Cancel a pending task.

Parameters
task_idThe task ID to cancel
Returns
true if task was cancelled, false if already running or completed

Definition at line 573 of file wasm_worker_pool.cc.

◆ CancelAllOfType()

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.

◆ WaitAll()

bool yaze::app::platform::wasm::WasmWorkerPool::WaitAll ( uint32_t timeout_ms = 0)

Wait for all pending tasks to complete.

Parameters
timeout_msMaximum time to wait in milliseconds (0 = infinite)
Returns
true if all tasks completed, false if timeout

Definition at line 575 of file wasm_worker_pool.cc.

◆ GetPendingCount()

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.

◆ GetActiveWorkerCount()

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.

◆ GetWorkerStats()

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.

◆ IsInitialized()

bool yaze::app::platform::wasm::WasmWorkerPool::IsInitialized ( ) const
inline

Check if the worker pool is initialized.

Definition at line 177 of file wasm_worker_pool.h.

References initialized_.

◆ SetMaxWorkers()

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.

◆ ProcessCallbacks()

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.

◆ WorkerThread()

void yaze::app::platform::wasm::WasmWorkerPool::WorkerThread ( size_t worker_id)
private

Definition at line 582 of file wasm_worker_pool.cc.

◆ ProcessTask()

void yaze::app::platform::wasm::WasmWorkerPool::ProcessTask ( const Task & task,
size_t worker_id )
private

Definition at line 583 of file wasm_worker_pool.cc.

◆ ExecuteTask()

std::vector< uint8_t > yaze::app::platform::wasm::WasmWorkerPool::ExecuteTask ( const Task & task)
private

Definition at line 584 of file wasm_worker_pool.cc.

◆ ProcessRomDecompression()

std::vector< uint8_t > yaze::app::platform::wasm::WasmWorkerPool::ProcessRomDecompression ( const std::vector< uint8_t > & input)
private

Definition at line 585 of file wasm_worker_pool.cc.

◆ ProcessGraphicsDecoding()

std::vector< uint8_t > yaze::app::platform::wasm::WasmWorkerPool::ProcessGraphicsDecoding ( const std::vector< uint8_t > & input)
private

Definition at line 586 of file wasm_worker_pool.cc.

◆ ProcessPaletteCalculation()

std::vector< uint8_t > yaze::app::platform::wasm::WasmWorkerPool::ProcessPaletteCalculation ( const std::vector< uint8_t > & input)
private

Definition at line 587 of file wasm_worker_pool.cc.

◆ ProcessAsarCompilation()

std::vector< uint8_t > yaze::app::platform::wasm::WasmWorkerPool::ProcessAsarCompilation ( const std::vector< uint8_t > & input)
private

Definition at line 588 of file wasm_worker_pool.cc.

◆ ReportProgress()

void yaze::app::platform::wasm::WasmWorkerPool::ReportProgress ( uint32_t task_id,
float progress,
const std::string & message )
private

Definition at line 589 of file wasm_worker_pool.cc.

◆ QueueCallback()

void yaze::app::platform::wasm::WasmWorkerPool::QueueCallback ( std::function< void()> callback)
private

Definition at line 590 of file wasm_worker_pool.cc.

Member Data Documentation

◆ kSynchronousTaskId

constexpr uint32_t yaze::app::platform::wasm::WasmWorkerPool::kSynchronousTaskId = UINT32_MAX
staticconstexpr

Definition at line 90 of file wasm_worker_pool.h.

◆ initialized_

bool yaze::app::platform::wasm::WasmWorkerPool::initialized_ = false
private

Definition at line 218 of file wasm_worker_pool.h.

Referenced by IsInitialized().

◆ shutting_down_

bool yaze::app::platform::wasm::WasmWorkerPool::shutting_down_ = false
private

Definition at line 219 of file wasm_worker_pool.h.

◆ num_workers_

size_t yaze::app::platform::wasm::WasmWorkerPool::num_workers_
private

Definition at line 220 of file wasm_worker_pool.h.

◆ next_task_id_

uint32_t yaze::app::platform::wasm::WasmWorkerPool::next_task_id_ {1}
private

Definition at line 262 of file wasm_worker_pool.h.

◆ worker_stats_

std::vector<WorkerStats> yaze::app::platform::wasm::WasmWorkerPool::worker_stats_
private

Definition at line 263 of file wasm_worker_pool.h.

◆ active_workers_

size_t yaze::app::platform::wasm::WasmWorkerPool::active_workers_ {0}
private

Definition at line 264 of file wasm_worker_pool.h.

◆ total_tasks_submitted_

size_t yaze::app::platform::wasm::WasmWorkerPool::total_tasks_submitted_ {0}
private

Definition at line 265 of file wasm_worker_pool.h.

◆ total_tasks_completed_

size_t yaze::app::platform::wasm::WasmWorkerPool::total_tasks_completed_ {0}
private

Definition at line 266 of file wasm_worker_pool.h.


The documentation for this class was generated from the following files: