This commit is contained in:
2026-02-03 03:08:13 +01:00
parent 5c805c60d7
commit a07a87a35f
6 changed files with 129 additions and 95 deletions

View File

@@ -3,11 +3,11 @@ use crossbeam_channel::{Receiver, RecvTimeoutError, Sender};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use super::link::LinkState;
use super::sequencer::{set_realtime_priority, AudioCommand, MidiCommand};
use super::realtime::{precise_sleep_us, set_realtime_priority};
use super::sequencer::{AudioCommand, MidiCommand};
use super::timing::SyncTime;
/// A command scheduled for dispatch at a specific time.
@@ -103,23 +103,6 @@ pub fn dispatcher_loop(
}
}
/// High-precision sleep using clock_nanosleep on Linux
#[cfg(target_os = "linux")]
fn precise_sleep(micros: u64) {
let duration_ns = micros * 1000;
let ts = libc::timespec {
tv_sec: (duration_ns / 1_000_000_000) as i64,
tv_nsec: (duration_ns % 1_000_000_000) as i64,
};
unsafe {
libc::clock_nanosleep(libc::CLOCK_MONOTONIC, 0, &ts, std::ptr::null_mut());
}
}
#[cfg(not(target_os = "linux"))]
fn precise_sleep(micros: u64) {
thread::sleep(Duration::from_micros(micros));
}
/// Wait until the target time for dispatch.
/// With RT priority: spin-wait for precision
@@ -136,7 +119,7 @@ fn wait_until_dispatch(target_us: SyncTime, link: &LinkState, has_rt: bool) {
} else {
// Without RT priority: sleep (spin-waiting is counterproductive)
if remaining > 0 {
precise_sleep(remaining);
precise_sleep_us(remaining);
}
}
}