dirkwhoffmann / vAmiga

vAmiga is a user-friendly Amiga 500, 1000, 2000 emulator for macOS

Home Page:https://dirkwhoffmann.github.io/vAmiga

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improved thread synchronization

dirkwhoffmann opened this issue · comments

  • Problem
    In SYNC mode, the emulator thread utilizes sleep() to proceed at the right pace and the GUI threads grabs a texture whenever it needs one. This works pretty well in the Desktop version, but causes problems in the new WASM port (which is currently under development). To reduce jitter, a better thread synchronization is needed.

  • Solution
    Implement a more sophisticated PULSE mode. Instead of computing a new texture on every pulse, provide a parameter that specifies the target frame. The emulator thread will then compute as many frames as needed. So, instead of waking up the emulator thread by a timer, it will always be woken up by the GPU drawing loop. If VSYNC is enabled, a new frame is requested on every pulse. If VSYNC is disabled, the GUI requests as many frames as needed (0, 1, or 2) to run at the right pace.

vAmiga got a new thread synchronization mode called Adaptive.

From Thread.h:

 * The Thread class is also responsible for timing synchronization. I.e., it
 * has to ensure that the proper amount of frames are executed per second.
 * Three different synchronization modes are supported:
 *
 * - Periodic:
 *
 *   In periodic mode the thread puts itself to sleep and utilizes a timer to
 *   schedule a wakeup call. In this mode, no further action has to be taken
 *   by the GUI. This method was the default mode used by vAmiga up to version
 *   2.3.
 *
 * - Pulsed:
 *
 *   In pulsed mode, the thread waits for an external wake-up signal that has
 *   to be sent by the GUI. When the wake-up signal is received, a single frame
 *   is computed. vAmiga uses this mode to implement VSYNC.
 *
 * - Adaptive:
 *
 *  In adaptive mode, the thread waits for an external wake-up signal just as
 *  it does in pulsed mode. When the wake-up signal comes in, the thread
 *  computes the number of missing frames based on the current time and the time
 *  the thread had been lauchen. Then it executes all missing frames or
 *  resynchronizes if the number of missing frames is way off.

Adaptive mode should reduce jitter, because it establishes some synchronization between the emulator thread and the drawing loop. To achieve best results, the GUI should issue a wakeUp() call right after it has grabbed the most recent texture from the emulator.

Next step: Port code over to vAmiga.net.

Part of v2.4b1.