Overview

The FreeWorld OS timer system provides both kernel-level timing (HPET) and user-space timer management (Windows-compatible WM_TIMER messages).

UEFI Migration: The kernel now uses HPET (High Precision Event Timer) for high-precision timing, replacing the legacy PIT (Programmable Interval Timer).

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 address
  • hpet_get_frequency() - Get HPET frequency in Hz
  • hpet_read_counter() - Read current counter value
  • hpet_wait_us() - Wait for specified microseconds
  • hpet_setup_timer0() - Configure Timer 0 for periodic interrupts
Legacy PIT: The legacy PIT (Programmable Interval Timer) driver in 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 milliseconds
  • callback - 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:

  1. Timer tick loop runs every 10ms
  2. Checks all active timers
  3. Fires timers whose interval has elapsed
  4. Sends WM_TIMER messages to windows or calls callbacks
  5. Updates timer last tick time

Integration

The Timer Manager integrates with:

  • Window Manager: Sends WM_TIMER messages to windows
  • GUI Integration: Initialized as part of the GUI system
  • Event Bus: Can emit timer events for system-wide use

Related Documentation