Timer Manager
Overview
The FreeWorld OS timer system provides both kernel-level timing (HPET) and user-space timer management (Windows-compatible WM_TIMER messages).
Kernel Timer (HPET - UEFI-Aware)
Location: kernel/drivers/hpet_uefi.asm
High Precision Event Timer driver using ACPI-discovered address:
- HPET Base Address: Retrieved from ACPI HPET table
- Frequency Detection: Calculates HPET frequency from period
- Main Counter: High-precision 64-bit counter
- Timer 0: Configurable for periodic interrupts
- Microsecond Timing:
hpet_wait_us()for precise delays
Key Functions
hpet_init_uefi()- Initialize HPET using ACPI addresshpet_get_frequency()- Get HPET frequency in Hzhpet_read_counter()- Read current counter valuehpet_wait_us()- Wait for specified microsecondshpet_setup_timer0()- Configure Timer 0 for periodic interrupts
kernel/drivers/timer.asm is deprecated and no longer used in UEFI boot.
User-Space Timer Manager
Location: system/timer.js
The Timer Manager provides Windows-compatible timer functionality, allowing windows to receive periodic WM_TIMER messages. Windows don't just react to users; they react to time, enabling animations, periodic updates, and scheduled tasks.
Features
- SetTimer: Create timers associated with windows
- KillTimer: Remove and stop timers
- WM_TIMER Messages: Send timer messages to windows
- Priority Queue: Efficient timer processing
- 10ms Tick: Timer tick loop for accurate timing
API
setTimer(hwnd, timerId, interval, callback)
Creates a timer associated with a window:
hwnd- Window handle (or null for system timer)timerId- Timer ID (or null for auto-generated)interval- Interval in millisecondscallback- Callback function or message handler
Returns the timer ID.
killTimer(timerId)
Removes and stops a timer:
timerId- Timer ID to remove
start()
Starts the timer tick loop (10ms intervals).
stop()
Stops the timer tick loop.
Usage Example
const TimerManager = require('./system/timer');
const timerManager = new TimerManager();
timerManager.start();
// Create a timer that fires every 1000ms
const timerId = timerManager.setTimer(hwnd, null, 1000, (timer) => {
console.log('Timer fired!');
// Or send WM_TIMER message to window
window.handleMessage({
type: 'WM_TIMER',
timerId: timer.id
});
});
// Later, remove the timer
timerManager.killTimer(timerId);
Timer Processing
The Timer Manager uses a priority queue to efficiently process timers:
- Timer tick loop runs every 10ms
- Checks all active timers
- Fires timers whose interval has elapsed
- Sends
WM_TIMERmessages to windows or calls callbacks - Updates timer last tick time
Integration
The Timer Manager integrates with:
- Window Manager: Sends
WM_TIMERmessages to windows - GUI Integration: Initialized as part of the GUI system
- Event Bus: Can emit timer events for system-wide use
Related Documentation
- Window Management - Window message handling
- GUI Integration - System integration
- Event System - Event communication