Overview
The renderer module provides functions for visualizing thread states, synchronization primitives, and simulation progress. Each synchronization scenario has its own specialized renderer that knows how to display its specific state.Core Renderer: renderClients()
The primary rendering function for the mutex-based bank scenario.
Function Signature
renderer.js
Parameters
container- DOM element to render intothreads- Array ofThreadobjects from the enginemutexOwner- The thread currently holding the mutex (ornull)account- Object withbalancepropertyinitialBalance- Initial account balance for calculating percentage
Example Usage
ui.js
Rendered Output
The function renders:- Balance Status Bar - Visual progress bar showing remaining balance percentage
- Thread Cards - Individual cards for each thread showing their state
Balance Bar
renderer.js
- Green - More than 50% remaining
- Yellow - 20-50% remaining
- Red - Less than 20% remaining
Thread State Visualization
renderer.js
- Running (with mutex) - Yellow border, glowing effect, 💰 icon
- Blocked - Red border, pulsing glow, 🔒 icon
- Finished - Faded opacity, ✅ icon
- Ready - Default gray, 👤 icon
Semaphore Renderer: renderSemaphoreView()
Renders the printer scenario with semaphore-controlled resources.
Function Signature
semaphoreRenderer.js
Parameters (Object Destructuring)
printersContainer- DOM element for printer cardsthreadsContainer- DOM element for job/thread cardsqueueContainer- DOM element for semaphore queuesemaphoreCountNode- DOM element showing available tokenscontext- Scenario context with semaphore and printersthreads- Array of thread objects
Example Usage
semaphoreUI.js
Printer Card Rendering
semaphoreRenderer.js
Printer Phase Detection
semaphoreRenderer.js
Condition Renderer: renderConditionView()
Renders the restaurant scenario with condition variables.
Function Signature
conditionRenderer.js
Rendered Components
-
Global Stats
- Available dishes
- Total cooked
- Total eaten
- Waiting Queue - Customers blocked on condition variable
- Chef Panel - Dedicated visualization for the chef thread
- Customer Cards - Individual cards for each customer thread
Chef Phase Detection
conditionRenderer.js
Customer Phase Detection
conditionRenderer.js
Barrier Renderer: renderBarrierView()
Renders the race scenario with barrier synchronization.
Function Signature
barrierRenderer.js
Racer State Tracking
barrierRenderer.js
Racer Phase Detection
barrierRenderer.js
Rendering Patterns
All renderer functions follow these common patterns:1. Container Clearing
2. State-Based Styling
Thread state determines visual appearance:3. Safe Value Handling
null, undefined, and invalid values.
4. Dynamic Content
Thread State Reference
Threads can be in one of these states:"ready"- Ready to run"running"- Currently executing"blocked"- Waiting for a resource"finished"- Completed execution
- Border colors
- Background colors
- Icons/emojis
- Opacity
- Glow effects
Update Cycle
Renderers are called from the UI module’supdate() method:
Performance Considerations
- Clear Before Render - Always clear containers to prevent memory leaks
- Batch DOM Updates - Build HTML strings then set
innerHTMLonce - Minimal Reflows - Use CSS classes for styling instead of inline styles where possible
- Safe Calculations - Protect against division by zero and null values
Styling with Tailwind CSS
All renderers use Tailwind CSS utility classes:- Responsive design
- Consistent spacing
- Smooth transitions
- Dark mode support
Next Steps
- UI Overview - Learn about the UI architecture
- Timeline API - Understand event logging
- Engine API - See how the simulation engine works