Skip to content

Add native SCHED_FIFO promotion for the Linux build without dbus#45

Open
HEnquist wants to merge 5 commits into
mozilla:masterfrom
HEnquist:native-rt-without-dbus
Open

Add native SCHED_FIFO promotion for the Linux build without dbus#45
HEnquist wants to merge 5 commits into
mozilla:masterfrom
HEnquist:native-rt-without-dbus

Conversation

@HEnquist

@HEnquist HEnquist commented Jul 8, 2026

Copy link
Copy Markdown

Give the Linux no-dbus build a real implementation instead of a no-op:
promote the current thread directly with pthread_setschedparam(SCHED_FIFO).
The rtkit path and other platforms are untouched. See #44 for the rationale.

  • New rt_linux_native.rs, wired in via a dedicated cfg branch after the rtkit branch
  • Priority defaults to 10 (matching rtkit), overridable via the optional set_rt_priority() (1-99)
  • Restores the saved policy on demotion (keeps SCHED_RESET_ON_FORK set, which an
    unprivileged thread cannot clear)
  • Tests cover the RLIMIT_RTPRIO boundary and the priority override; the fallback CI
    job raises RLIMIT_RTPRIO so promotion runs for real
  • Docs: add a Platforms section and correct API docs that assumed D-Bus was the only
    Linux backend

Edit: the priority override was originally an AUDIO_RT_PRIORITY environment variable; it is now
the optional set_rt_priority() method instead (a library should not read env vars on its own).

HEnquist added 4 commits July 8, 2026 22:42
The Linux build without the `dbus` feature previously fell through to the
blanket fallback, where promotion was a no-op that returned Ok, so callers
believed they had real-time priority when they did not.

Give that build a real implementation: promote the current thread directly
with pthread_setschedparam(SCHED_FIFO | SCHED_RESET_ON_FORK) at priority 10,
restoring the saved policy on demotion. This needs no D-Bus daemon and works
whenever the process may request real-time scheduling (root, CAP_SYS_NICE, or
an RLIMIT_RTPRIO budget). The rtkit path and other platforms are untouched.

- New rt_linux_native.rs with the native backend, wired into a dedicated cfg
  branch after the rtkit branch.
- The requested priority defaults to 10 and can be overridden with the
  AUDIO_RT_PRIORITY environment variable (1-99).
- Tests exercise the RLIMIT_RTPRIO boundary and the priority override; the
  fallback CI job raises RLIMIT_RTPRIO so promotion runs for real.
- Docs: add a Platforms section and correct API docs that assumed D-Bus was
  the only Linux backend.
A library reading an environment variable it was never told about is a hidden
global input that can surprise the embedding application. Replace it with an
explicit, optional setter that the caller drives: set_rt_priority(Some(n))
overrides the default of 10, None restores it. Linux no-dbus only; if never
called, promotion uses priority 10 as before.
- Drop the `param` field from RtPriorityThreadInfoInternal so the struct has no
  padding, matching the rtkit path's struct. `serialize`'s transmute no longer
  reads uninitialized padding bytes (which was UB). Demotion now restores the
  saved policy with a zeroed param, as the rtkit path already does.
- rt_scheduling_available: treat a demotion failure after a successful promotion
  as fatal rather than ignoring it.
- Fix a stale test comment that referred to the removed environment variable.

@padenot padenot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, one small question before we can merge. I'll tag a release when it's done.

Comment thread src/rt_linux_native.rs
}

/// Promote a thread identified by its tid to real-time priority. Promoting a thread other than the
/// caller (in particular in another process) requires the caller to be privileged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this works in light of the comment above that says that RtPriorityThreadInfoInternal's underlying values are only meaningful from the same process?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe copy paste error?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. I'm traveling right now without a computer, so I will fix the comment and push once I'm back home.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the fix in d4689e1. You're right, that comment was wrong. The struct is meant to be usable across processes: the thread is identified by its system-wide tid (from gettid), which is what promote_thread_to_real_time passes to sched_setscheduler, so a suitably privileged process can promote it. Corrected the comment to match.

It claimed the thread info was only meaningful within the same process, which
contradicts promote_thread_to_real_time_internal promoting a thread in another
process. The thread is identified by its system-wide tid, so cross-process
promotion works; correct the comment to say so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants