Skip to content

Commit b0ed6d0

Browse files
authored
Make the rustix-futex-sync dependency optional. (#106)
* Make the `rustix-futex-sync` dependency optional. Avoid depending on `rustix-futex-sync` if support for threads is not enabled. * Make "alloc" and "smallvec" optional too.
1 parent 02316e5 commit b0ed6d0

File tree

13 files changed

+97
-63
lines changed

13 files changed

+97
-63
lines changed

Cargo.toml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rustix = { version = "0.38.26", default-features = false }
1919
bitflags = { version = "2.4.0", default-features = false }
2020
memoffset = { version = "0.9.0", optional = true }
2121
log = { version = "0.4.14", default-features = false, optional = true }
22-
rustix-futex-sync = "0.2.1"
22+
rustix-futex-sync = { version = "0.2.1", optional = true }
2323

2424
# Optional logging backends for use with "origin-program". You can use any
2525
# external logger, but using these features allows origin to initialize the
@@ -40,19 +40,19 @@ errno = { version = "0.3.3", default-features = false, optional = true }
4040
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" }
4141
alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
4242
compiler_builtins = { version = "0.1.101", optional = true }
43-
smallvec = { version = "1.11.1", features = ["const_new"] }
43+
smallvec = { version = "1.11.1", optional = true, features = ["const_new"] }
4444

4545
[target.'cfg(not(target_arch = "arm"))'.dependencies.unwinding]
4646
version = "0.2.0"
4747
default-features = false
48-
features = [ "unwinder" ]
48+
features = ["unwinder"]
4949

5050
[dev-dependencies]
5151
assert_cmd = "2.0.12"
5252

5353
[features]
5454
default = ["std", "log", "libc", "errno", "thread", "init-fini-arrays"]
55-
std = ["rustix/std", "bitflags/std"]
55+
std = ["rustix/std", "bitflags/std", "alloc"]
5656
rustc-dep-of-std = [
5757
"dep:core",
5858
"dep:alloc",
@@ -72,10 +72,14 @@ set_thread_id = []
7272
origin-program = []
7373

7474
# Use origin's implementation of thread startup and shutdown.
75-
origin-thread = ["memoffset", "rustix/runtime", "origin-program", "thread"]
75+
#
76+
# To use threads, it's also necessary to enable the "thread" feature.
77+
origin-thread = ["memoffset", "rustix/runtime", "origin-program"]
7678

7779
# Use origin's implementation of signal handle registrtion.
78-
origin-signal = ["rustix/runtime", "signal"]
80+
#
81+
# To use threads, it's also necessary to enable the "signal" feature.
82+
origin-signal = ["rustix/runtime"]
7983

8084
# Use origin's `_start` definition.
8185
origin-start = ["rustix/use-explicitly-provided-auxv", "rustix/runtime", "origin-program"]
@@ -96,13 +100,10 @@ max_level_off = ["log/max_level_off"]
96100

97101
# Enable features which depend on the Rust global allocator, such as functions
98102
# that return owned strings or `Vec`s.
99-
alloc = ["rustix/alloc"]
103+
alloc = ["rustix/alloc", "smallvec"]
100104

101105
# Enable support for threads.
102-
#
103-
# Origin's threads support currently depends on dynamic allocation, so it
104-
# pulls in the "alloc" feature.
105-
thread = ["alloc", "rustix/thread", "rustix/mm", "param", "rustix/process", "rustix/runtime"]
106+
thread = ["rustix/thread", "rustix/mm", "param", "rustix/process", "rustix/runtime", "rustix-futex-sync"]
106107

107108
# Enable support for signal handlers.
108109
signal = ["rustix/runtime"]

example-crates/external-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "external-start"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-thread", "external-start", "thread", "alloc"] }
1111

1212
# Ensure that libc gets linked.
1313
libc = { version = "0.2", default-features = false }

example-crates/no-std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features. And enable "libc" to enable the libc implementations.
10-
origin = { path = "../..", default-features = false, features = ["libc", "thread"] }
10+
origin = { path = "../..", default-features = false, features = ["libc", "thread", "alloc"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start-lto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start", "thread", "alloc"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start"] }
10+
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start", "thread", "alloc"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

src/arch/aarch64.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use linux_raw_sys::general::__NR_rt_sigreturn;
66
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
77
#[cfg(relocation_model = "pic")]
88
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
9-
#[cfg(feature = "origin-thread")]
9+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
1010
use {
1111
core::ffi::c_void,
1212
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -130,15 +130,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
130130
}
131131

132132
/// The required alignment for the stack pointer.
133-
#[cfg(feature = "origin-thread")]
133+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
134134
pub(super) const STACK_ALIGNMENT: usize = 16;
135135

136136
/// A wrapper around the Linux `clone` system call.
137137
///
138138
/// This can't be implemented in `rustix` because the child starts executing at
139139
/// the same point as the parent and we need to use inline asm to have the
140140
/// child jump to our new-thread entrypoint.
141-
#[cfg(feature = "origin-thread")]
141+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
142142
#[inline]
143143
pub(super) unsafe fn clone(
144144
flags: u32,
@@ -180,7 +180,7 @@ pub(super) unsafe fn clone(
180180
}
181181

182182
/// Write a value to the platform thread-pointer register.
183-
#[cfg(feature = "origin-thread")]
183+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
184184
#[inline]
185185
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
186186
asm!("msr tpidr_el0, {}", in(reg) ptr);
@@ -200,12 +200,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
200200
}
201201

202202
/// TLS data starts at the location pointed to by the thread pointer.
203-
#[cfg(feature = "origin-thread")]
203+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
204204
pub(super) const TLS_OFFSET: usize = 0;
205205

206206
/// `munmap` the current thread, then carefully exit the thread without
207207
/// touching the deallocated stack.
208-
#[cfg(feature = "origin-thread")]
208+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
209209
#[inline]
210210
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
211211
asm!(

src/arch/arm.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::arch::asm;
66
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
77
#[cfg(feature = "origin-signal")]
88
use linux_raw_sys::general::{__NR_rt_sigreturn, __NR_sigreturn};
9-
#[cfg(feature = "origin-thread")]
9+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
1010
use {
1111
core::ffi::c_void,
1212
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -130,15 +130,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
130130
}
131131

132132
/// The required alignment for the stack pointer.
133-
#[cfg(feature = "origin-thread")]
133+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
134134
pub(super) const STACK_ALIGNMENT: usize = 4;
135135

136136
/// A wrapper around the Linux `clone` system call.
137137
///
138138
/// This can't be implemented in `rustix` because the child starts executing at
139139
/// the same point as the parent and we need to use inline asm to have the
140140
/// child jump to our new-thread entrypoint.
141-
#[cfg(feature = "origin-thread")]
141+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
142142
#[inline]
143143
pub(super) unsafe fn clone(
144144
flags: u32,
@@ -181,15 +181,15 @@ pub(super) unsafe fn clone(
181181
}
182182

183183
/// Write a value to the platform thread-pointer register.
184-
#[cfg(feature = "origin-thread")]
184+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
185185
#[inline]
186186
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
187187
rustix::runtime::arm_set_tls(ptr).expect("arm_set_tls");
188188
debug_assert_eq!(thread_pointer(), ptr);
189189
}
190190

191191
/// Read the value of the platform thread-pointer register.
192-
#[cfg(feature = "origin-thread")]
192+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
193193
#[inline]
194194
pub(super) fn thread_pointer() -> *mut c_void {
195195
let ptr;
@@ -201,12 +201,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
201201
}
202202

203203
/// TLS data starts at the location pointed to by the thread pointer.
204-
#[cfg(feature = "origin-thread")]
204+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
205205
pub(super) const TLS_OFFSET: usize = 0;
206206

207207
/// `munmap` the current thread, then carefully exit the thread without
208208
/// touching the deallocated stack.
209-
#[cfg(feature = "origin-thread")]
209+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
210210
#[inline]
211211
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
212212
asm!(

src/arch/riscv64.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Architecture-specific assembly code.
22
3-
#[cfg(any(feature = "origin-thread", feature = "origin-start"))]
3+
#[cfg(any(
4+
all(feature = "origin-thread", feature = "thread"),
5+
feature = "origin-start"
6+
))]
47
use core::arch::asm;
58
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
69
#[cfg(relocation_model = "pic")]
710
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
8-
#[cfg(feature = "origin-thread")]
11+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
912
use {
1013
core::ffi::c_void,
1114
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -130,15 +133,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
130133
}
131134

132135
/// The required alignment for the stack pointer.
133-
#[cfg(feature = "origin-thread")]
136+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
134137
pub(super) const STACK_ALIGNMENT: usize = 16;
135138

136139
/// A wrapper around the Linux `clone` system call.
137140
///
138141
/// This can't be implemented in `rustix` because the child starts executing at
139142
/// the same point as the parent and we need to use inline asm to have the
140143
/// child jump to our new-thread entrypoint.
141-
#[cfg(feature = "origin-thread")]
144+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
142145
#[inline]
143146
pub(super) unsafe fn clone(
144147
flags: u32,
@@ -180,15 +183,15 @@ pub(super) unsafe fn clone(
180183
}
181184

182185
/// Write a value to the platform thread-pointer register.
183-
#[cfg(feature = "origin-thread")]
186+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
184187
#[inline]
185188
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
186189
asm!("mv tp, {}", in(reg) ptr);
187190
debug_assert_eq!(thread_pointer(), ptr);
188191
}
189192

190193
/// Read the value of the platform thread-pointer register.
191-
#[cfg(feature = "origin-thread")]
194+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
192195
#[inline]
193196
pub(super) fn thread_pointer() -> *mut c_void {
194197
let ptr;
@@ -201,12 +204,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
201204

202205
/// TLS data starts 0x800 bytes below the location pointed to by the thread
203206
/// pointer.
204-
#[cfg(feature = "origin-thread")]
207+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
205208
pub(super) const TLS_OFFSET: usize = 0x800;
206209

207210
/// `munmap` the current thread, then carefully exit the thread without
208211
/// touching the deallocated stack.
209-
#[cfg(feature = "origin-thread")]
212+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
210213
#[inline]
211214
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
212215
asm!(

src/arch/x86.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::arch::asm;
66
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
77
#[cfg(feature = "origin-signal")]
88
use linux_raw_sys::general::{__NR_rt_sigreturn, __NR_sigreturn};
9-
#[cfg(feature = "origin-thread")]
9+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
1010
use {
1111
core::ffi::c_void,
1212
core::ptr::invalid_mut,
@@ -135,15 +135,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
135135
}
136136

137137
/// The required alignment for the stack pointer.
138-
#[cfg(feature = "origin-thread")]
138+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
139139
pub(super) const STACK_ALIGNMENT: usize = 16;
140140

141141
/// A wrapper around the Linux `clone` system call.
142142
///
143143
/// This can't be implemented in `rustix` because the child starts executing at
144144
/// the same point as the parent and we need to use inline asm to have the
145145
/// child jump to our new-thread entrypoint.
146-
#[cfg(feature = "origin-thread")]
146+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
147147
#[inline]
148148
pub(super) unsafe fn clone(
149149
flags: u32,
@@ -231,7 +231,7 @@ pub(super) unsafe fn clone(
231231
}
232232

233233
/// Write a value to the platform thread-pointer register.
234-
#[cfg(feature = "origin-thread")]
234+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
235235
#[inline]
236236
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
237237
let mut user_desc = rustix::runtime::UserDesc {
@@ -255,7 +255,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
255255
}
256256

257257
/// Read the value of the platform thread-pointer register.
258-
#[cfg(feature = "origin-thread")]
258+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
259259
#[inline]
260260
pub(super) fn thread_pointer() -> *mut c_void {
261261
let ptr;
@@ -270,12 +270,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
270270
}
271271

272272
/// TLS data ends at the location pointed to by the thread pointer.
273-
#[cfg(feature = "origin-thread")]
273+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
274274
pub(super) const TLS_OFFSET: usize = 0;
275275

276276
/// `munmap` the current thread, then carefully exit the thread without
277277
/// touching the deallocated stack.
278-
#[cfg(feature = "origin-thread")]
278+
#[cfg(all(feature = "origin-thread", feature = "thread"))]
279279
#[inline]
280280
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
281281
asm!(

0 commit comments

Comments
 (0)