Concurrent Event Simulation Engine
The Completengine simulation engine employs a multi-layered architecture designed for deterministic concurrent event processing. At its core, a priority-based scheduler orchestrates event dispatch across a managed thread pool, ensuring temporal consistency even under heavy load.
Events enter the system through typed emitters, pass through a priority queue with O(log n) insertion, and are dispatched to handler threads via a lock-free dispatcher. The scheduler maintains a global simulation clock, advancing only when all concurrent events at the current tick have resolved.
This architecture guarantees reproducible simulation results regardless of hardware thread count or system load -- a critical property for deterministic event simulation.
Deterministic simulation demands that concurrent events at the same simulation tick produce identical results regardless of execution order. Completengine achieves this through a two-phase commit protocol: events are first tentatively applied in parallel, then a conflict resolution pass detects and reorders any dependencies.
The temporal ordering model uses Lamport timestamps extended with causal dependency tracking. Each event carries a vector clock that records its causal history, enabling the scheduler to distinguish truly concurrent events (which can safely execute in parallel) from causally dependent events (which must be serialized).
Conflict resolution employs a priority-based arbitration scheme. When two events modify the same state at the same tick, the event with higher causal priority wins, and the losing event is re-queued for the next tick. This guarantees convergence within bounded ticks for any finite conflict set.
The practical result: simulation authors can write event handlers as if they execute sequentially, while the engine parallelizes execution across all available threads. The scheduler absorbs the complexity of concurrency, exposing a simple, deterministic programming model.
import { Engine, Scheduler, EventQueue } from 'completengine';
// Initialize the simulation engine
const engine = new Engine({
threads: 8,
tickRate: 1000,
deterministic: true,
});
// Configure the event scheduler
const scheduler = engine.createScheduler({
priority: 'causal',
conflictResolution: 'priority-arbitration',
maxRetries: 3,
});
// Register event handlers
scheduler.on('tick', async (events) => {
const resolved = await scheduler.resolveConflicts(events);
return resolved.dispatch();
});
// Start simulation
await engine.start();
import { EventHandler, SimEvent } from 'completengine';
// Define a typed event handler
class PhysicsHandler extends EventHandler<PhysicsEvent> {
handle(event: SimEvent<PhysicsEvent>): void {
const { position, velocity, mass } = event.payload;
this.state.update(position.add(velocity.scale(event.dt)));
this.emit('collision-check', { position, mass });
}
}