Skip to content

Commit d6a5893

Browse files
gurchetansinghgsingh408
authored andcommitted
rustix: enable rustix::thread::futex without linux-raw-sys
It's desirable to enable futex for some cases that will be run on Android. The strategy is just to define the constants needed by the libc ourselves, if libc doesn't provide the necessary constant. Long-term, that could make the libc backend only depend on libc.
1 parent f9fc21d commit d6a5893

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

src/backend/libc/c.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,16 @@ pub(crate) use statx_flags::*;
515515
#[cfg(target_os = "android")]
516516
pub(crate) use __fsid_t as fsid_t;
517517

518+
// Android's libc crate is missing some constants.
519+
// TODO(https://github.com/bytecodealliance/rustix/issues/1589): try to upstream these
520+
// constants to libc.
521+
#[cfg(target_os = "android")]
522+
pub(crate) const CLONE_NEWTIME: c_int = 0x00000080;
523+
#[cfg(target_os = "android")]
524+
pub(crate) const FUTEX_WAITERS: u32 = 0x80000000;
525+
#[cfg(target_os = "android")]
526+
pub(crate) const FUTEX_OWNER_DIED: u32 = 0x40000000;
527+
518528
// FreeBSD added `timerfd_*` in FreeBSD 14. NetBSD added then in NetBSD 10.
519529
#[cfg(all(feature = "time", any(target_os = "freebsd", target_os = "netbsd")))]
520530
syscall!(pub(crate) fn timerfd_create(

src/backend/libc/thread/futex.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ bitflags::bitflags! {
1717
}
1818
}
1919

20+
// TODO(https://github.com/bytecodealliance/rustix/issues/1589): try to upstream these
21+
// constants to libc.
22+
const FUTEX2_SIZE_U8: u32 = 0;
23+
const FUTEX2_SIZE_U16: u32 = 1;
24+
const FUTEX2_SIZE_U32: u32 = 2;
25+
const FUTEX2_SIZE_U64: u32 = 3;
26+
const FUTEX2_SIZE_MASK: u32 = 3;
27+
const FUTEX2_NUMA: u32 = 4;
28+
const FUTEX2_PRIVATE: u32 = 128;
29+
2030
bitflags::bitflags! {
2131
/// `FUTEX2_*` flags for use with the functions in [`Waitv`].
2232
///
@@ -29,22 +39,19 @@ bitflags::bitflags! {
2939
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
3040
pub struct WaitFlags: u32 {
3141
/// `FUTEX_U8`
32-
const SIZE_U8 = linux_raw_sys::general::FUTEX2_SIZE_U8;
42+
const SIZE_U8 = FUTEX2_SIZE_U8;
3343
/// `FUTEX_U16`
34-
const SIZE_U16 = linux_raw_sys::general::FUTEX2_SIZE_U16;
44+
const SIZE_U16 = FUTEX2_SIZE_U16;
3545
/// `FUTEX_U32`
36-
const SIZE_U32 = linux_raw_sys::general::FUTEX2_SIZE_U32;
46+
const SIZE_U32 = FUTEX2_SIZE_U32;
3747
/// `FUTEX_U64`
38-
const SIZE_U64 = linux_raw_sys::general::FUTEX2_SIZE_U64;
48+
const SIZE_U64 = FUTEX2_SIZE_U64;
3949
/// `FUTEX_SIZE_MASK`
40-
const SIZE_MASK = linux_raw_sys::general::FUTEX2_SIZE_MASK;
41-
50+
const SIZE_MASK = FUTEX2_SIZE_MASK;
4251
/// `FUTEX2_NUMA`
43-
const NUMA = linux_raw_sys::general::FUTEX2_NUMA;
44-
52+
const NUMA = FUTEX2_NUMA;
4553
/// `FUTEX2_PRIVATE`
46-
const PRIVATE = linux_raw_sys::general::FUTEX2_PRIVATE;
47-
54+
const PRIVATE = FUTEX2_PRIVATE;
4855
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
4956
const _ = !0;
5057
}
@@ -85,7 +92,7 @@ pub(crate) enum Operation {
8592
}
8693

8794
/// `FUTEX_WAITERS`
88-
pub const WAITERS: u32 = linux_raw_sys::general::FUTEX_WAITERS;
95+
pub const WAITERS: u32 = c::FUTEX_WAITERS;
8996

9097
/// `FUTEX_OWNER_DIED`
91-
pub const OWNER_DIED: u32 = linux_raw_sys::general::FUTEX_OWNER_DIED;
98+
pub const OWNER_DIED: u32 = c::FUTEX_OWNER_DIED;

src/backend/libc/thread/syscalls.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ pub(crate) fn setns(fd: BorrowedFd<'_>, nstype: c::c_int) -> io::Result<c::c_int
341341
unsafe { ret_c_int(setns(borrowed_fd(fd), nstype)) }
342342
}
343343

344-
#[cfg(linux_kernel)]
344+
#[cfg(all(linux_kernel, linux_raw_dep))]
345345
#[inline]
346346
pub(crate) unsafe fn unshare(flags: crate::thread::UnshareFlags) -> io::Result<()> {
347347
ret(c::unshare(flags.bits() as i32))
348348
}
349349

350-
#[cfg(linux_kernel)]
350+
#[cfg(all(linux_kernel, linux_raw_dep))]
351351
#[inline]
352352
pub(crate) fn capget(
353353
header: &mut linux_raw_sys::general::__user_cap_header_struct,
@@ -369,7 +369,7 @@ pub(crate) fn capget(
369369
}
370370
}
371371

372-
#[cfg(linux_kernel)]
372+
#[cfg(all(linux_kernel, linux_raw_dep))]
373373
#[inline]
374374
pub(crate) fn capset(
375375
header: &mut linux_raw_sys::general::__user_cap_header_struct,
@@ -666,8 +666,9 @@ pub(crate) fn futex_waitv(
666666
timeout: Option<&Timespec>,
667667
clockid: ClockId,
668668
) -> io::Result<usize> {
669+
use crate::backend::c::clockid_t;
669670
use futex::Wait as FutexWait;
670-
use linux_raw_sys::general::__kernel_clockid_t as clockid_t;
671+
671672
syscall! {
672673
fn futex_waitv(
673674
waiters: *const FutexWait,

src/test_macro.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use crate::ioctl_readwrite;
2+
3+
fn main() {}

src/thread/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ mod clock;
66
pub mod futex;
77
#[cfg(linux_kernel)]
88
mod id;
9-
#[cfg(linux_kernel)]
9+
#[cfg(all(linux_kernel, linux_raw_dep))]
1010
mod libcap;
1111
#[cfg(linux_kernel)]
1212
mod membarrier;
13-
#[cfg(linux_kernel)]
13+
#[cfg(all(linux_kernel, linux_raw_dep))]
1414
mod prctl;
1515
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
1616
mod sched;
1717
mod sched_yield;
18-
#[cfg(linux_kernel)]
18+
#[cfg(all(linux_kernel, linux_raw_dep))]
1919
mod setns;
2020

2121
#[cfg(not(target_os = "redox"))]
@@ -25,13 +25,14 @@ pub use id::*;
2525
#[cfg(linux_kernel)]
2626
// #[expect(deprecated, reason = "CapabilityFlags is deprecated")]
2727
#[allow(deprecated)]
28+
#[cfg(all(linux_kernel, linux_raw_dep))]
2829
pub use libcap::{capabilities, set_capabilities, CapabilityFlags, CapabilitySet, CapabilitySets};
2930
#[cfg(linux_kernel)]
3031
pub use membarrier::*;
31-
#[cfg(linux_kernel)]
32+
#[cfg(all(linux_kernel, linux_raw_dep))]
3233
pub use prctl::*;
3334
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
3435
pub use sched::*;
3536
pub use sched_yield::sched_yield;
36-
#[cfg(linux_kernel)]
37+
#[cfg(all(linux_kernel, linux_raw_dep))]
3738
pub use setns::*;

0 commit comments

Comments
 (0)