Skip to content

Commit afb43b5

Browse files
authored
Fix two issues on musl (#50)
* Fix the build with musl, where pthread_t is a pointer musl defines pthread_t as *mut c_void instead of an integer, so the to_ne_bytes/from_ne_bytes serialization didn't compile. Go through usize, which is as wide as pthread_t on every Linux target, keeping the serialized layout unchanged. Also check the musl targets in CI. Fixes #49 * Promote threads by tid with the syscall, not musl's stub musl's sched_setscheduler always fails with ENOSYS: POSIX specifies it as process-scoped, which Linux doesn't provide. The syscall is per-thread, which is what promoting a thread by tid needs.
1 parent 0aa1ecd commit afb43b5

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎.github/workflows/rust.yml‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,39 @@ jobs:
9191
sudo prlimit --pid $$ --rtprio=10:10
9292
echo "RLIMIT_RTPRIO soft=$(ulimit -Sr) hard=$(ulimit -Hr)"
9393
rustup run ${{ matrix.rust }} cargo test --no-default-features
94+
95+
# musl defines pthread_t as a pointer instead of an integer, unlike glibc.
96+
build-musl:
97+
runs-on: ubuntu-24.04
98+
strategy:
99+
matrix:
100+
rust: [stable, nightly]
101+
102+
steps:
103+
- uses: actions/checkout@v2
104+
105+
- name: Install Rust (musl target)
106+
run: |
107+
rustup toolchain install ${{ matrix.rust }} --profile minimal
108+
rustup target add x86_64-unknown-linux-musl --toolchain ${{ matrix.rust }}
109+
110+
- name: Install Dependencies
111+
run: sudo apt-get update && sudo apt-get install libdbus-1-dev
112+
113+
- name: Build (musl, with dbus)
114+
shell: bash
115+
# The crate is a staticlib/rlib, so this doesn't link, and hence needs no musl libdbus: only
116+
# the headers, which pkg-config finds for the host.
117+
run: PKG_CONFIG_ALLOW_CROSS=1 rustup run ${{ matrix.rust }} cargo build --target x86_64-unknown-linux-musl --features dbus
118+
119+
- name: Build (musl, without dbus)
120+
shell: bash
121+
run: rustup run ${{ matrix.rust }} cargo build --target x86_64-unknown-linux-musl --no-default-features
122+
123+
- name: Test (musl, without dbus)
124+
shell: bash
125+
# Statically linked musl test binaries run fine on the glibc runner. As in the fallback job,
126+
# the native promotion path needs permission to request real-time scheduling.
127+
run: |
128+
sudo prlimit --pid $$ --rtprio=10:10
129+
rustup run ${{ matrix.rust }} cargo test --target x86_64-unknown-linux-musl --no-default-features -- --nocapture

‎src/rt_linux.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ impl RtPriorityThreadInfoInternal {
6262
/// struct, so no uninitialized padding bytes are ever read. Any trailing padding stays zero.
6363
pub fn serialize(&self) -> [u8; std::mem::size_of::<Self>()] {
6464
let thread_id = self.thread_id.to_ne_bytes();
65-
let pthread_id = self.pthread_id.to_ne_bytes();
65+
// `pthread_t` is an integer with glibc but a pointer with musl: go through `usize`, which
66+
// is as wide as both.
67+
let pthread_id = (self.pthread_id as usize).to_ne_bytes();
6668
let pid = self.pid.to_ne_bytes();
6769
let policy = self.policy.to_ne_bytes();
6870
let priority = self.priority.to_ne_bytes();
@@ -91,7 +93,7 @@ impl RtPriorityThreadInfoInternal {
9193
let mut src = bytes.iter().copied();
9294
RtPriorityThreadInfoInternal {
9395
thread_id: kernel_pid_t::from_ne_bytes(take(&mut src)),
94-
pthread_id: libc::pthread_t::from_ne_bytes(take(&mut src)),
96+
pthread_id: usize::from_ne_bytes(take(&mut src)) as libc::pthread_t,
9597
pid: libc::pid_t::from_ne_bytes(take(&mut src)),
9698
policy: libc::c_int::from_ne_bytes(take(&mut src)),
9799
priority: libc::c_int::from_ne_bytes(take(&mut src)),

‎src/rt_linux_native.rs‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ impl RtPriorityThreadInfoInternal {
7878
/// struct, so no uninitialized padding bytes are ever read. Any trailing padding stays zero.
7979
pub fn serialize(&self) -> [u8; std::mem::size_of::<Self>()] {
8080
let thread_id = self.thread_id.to_ne_bytes();
81-
let pthread_id = self.pthread_id.to_ne_bytes();
81+
// `pthread_t` is an integer with glibc but a pointer with musl: go through `usize`, which
82+
// is as wide as both.
83+
let pthread_id = (self.pthread_id as usize).to_ne_bytes();
8284
let pid = self.pid.to_ne_bytes();
8385
let policy = self.policy.to_ne_bytes();
8486
let priority = self.priority.to_ne_bytes();
@@ -107,7 +109,7 @@ impl RtPriorityThreadInfoInternal {
107109
let mut src = bytes.iter().copied();
108110
RtPriorityThreadInfoInternal {
109111
thread_id: kernel_pid_t::from_ne_bytes(take(&mut src)),
110-
pthread_id: libc::pthread_t::from_ne_bytes(take(&mut src)),
112+
pthread_id: usize::from_ne_bytes(take(&mut src)) as libc::pthread_t,
111113
pid: libc::pid_t::from_ne_bytes(take(&mut src)),
112114
policy: libc::c_int::from_ne_bytes(take(&mut src)),
113115
priority: libc::c_int::from_ne_bytes(take(&mut src)),
@@ -140,6 +142,26 @@ fn sched_error(context: &str) -> AudioThreadPriorityError {
140142
AudioThreadPriorityError::new(&format!("{}: {}", context, OSError::last_os_error()))
141143
}
142144

145+
/// Set the scheduling policy and priority of the thread with tid `tid`.
146+
///
147+
/// This goes through the syscall directly because musl's `sched_setscheduler` is a stub that always
148+
/// fails with `ENOSYS`: POSIX specifies it as process-scoped, which Linux does not provide. The
149+
/// syscall is per-thread, which is what is needed here.
150+
fn sched_setscheduler(
151+
tid: libc::pid_t,
152+
policy: libc::c_int,
153+
param: &libc::sched_param,
154+
) -> libc::c_long {
155+
unsafe {
156+
libc::syscall(
157+
libc::SYS_sched_setscheduler,
158+
tid,
159+
policy,
160+
param as *const libc::sched_param,
161+
)
162+
}
163+
}
164+
143165
/// A thread's system-wide tid narrowed to `pid_t` for the scheduler syscalls. A tid always fits in
144166
/// `pid_t` (it is a pid), but convert defensively rather than truncating.
145167
fn scheduler_tid(thread_id: kernel_pid_t) -> Result<libc::pid_t, AudioThreadPriorityError> {
@@ -236,8 +258,7 @@ pub fn promote_thread_to_real_time_internal(
236258
let mut param = unsafe { std::mem::zeroed::<libc::sched_param>() };
237259
param.sched_priority = requested_priority();
238260

239-
let rc =
240-
unsafe { libc::sched_setscheduler(tid, libc::SCHED_FIFO | SCHED_RESET_ON_FORK, &param) };
261+
let rc = sched_setscheduler(tid, libc::SCHED_FIFO | SCHED_RESET_ON_FORK, &param);
241262
if rc < 0 {
242263
return Err(sched_error("could not promote thread"));
243264
}
@@ -254,8 +275,7 @@ pub fn demote_thread_from_real_time_internal(
254275
let tid = scheduler_tid(thread_info.thread_id)?;
255276
let mut param = unsafe { std::mem::zeroed::<libc::sched_param>() };
256277
param.sched_priority = thread_info.priority;
257-
let rc =
258-
unsafe { libc::sched_setscheduler(tid, thread_info.policy | SCHED_RESET_ON_FORK, &param) };
278+
let rc = sched_setscheduler(tid, thread_info.policy | SCHED_RESET_ON_FORK, &param);
259279
if rc < 0 {
260280
return Err(sched_error("could not demote thread"));
261281
}

0 commit comments

Comments
 (0)