Skip to content

Commit 3fa021d

Browse files
committed
Add libc_thread_local cfg and unstable feature
1 parent 956ba42 commit 3fa021d

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ rustc-std-workspace-core = { version = "1.0.0", optional = true }
2626
default = ["std"]
2727
std = []
2828
align = []
29-
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
29+
rustc-dep-of-std = ['align', 'rustc-std-workspace-core', 'unstable']
3030
extra_traits = []
31+
unstable = []
3132
# use_std is deprecated, use `std` instead
3233
use_std = [ 'std' ]
3334

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ libc = "0.2"
3535
* `extra_traits`: all `struct`s implemented in `libc` are `Copy` and `Clone`.
3636
This feature derives `Debug`, `Eq`, `Hash`, and `PartialEq`.
3737

38+
* `unstable`: enable currently unstable bindings. Right now, this just allows
39+
bindings to `#[thread_local]` statics on certain platforms. Requires nightly.
40+
3841
* **deprecated**: `use_std` is deprecated, and is equivalent to `std`.
3942

4043
## Rust version support

build.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use std::str;
55
fn main() {
66
let rustc_minor_ver =
77
rustc_minor_version().expect("Failed to get rustc version");
8-
let rustc_dep_of_std =
9-
std::env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
10-
let align_cargo_feature = std::env::var("CARGO_FEATURE_ALIGN").is_ok();
8+
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
9+
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
10+
let unstable_cargo_feature = env::var("CARGO_FEATURE_UNSTABLE").is_ok();
1111

12-
if std::env::var("CARGO_FEATURE_USE_STD").is_ok() {
12+
if env::var("CARGO_FEATURE_USE_STD").is_ok() {
1313
println!(
1414
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
1515
please consider using the `std` cargo feature instead\""
1616
);
1717
}
1818

19-
if std::env::var("LIBC_CI").is_ok() {
19+
if env::var("LIBC_CI").is_ok() {
2020
if let Some(12) = which_freebsd() {
2121
println!("cargo:rustc-cfg=freebsd12");
2222
}
@@ -53,6 +53,11 @@ fn main() {
5353
if rustc_minor_ver >= 33 || rustc_dep_of_std {
5454
println!("cargo:rustc-cfg=libc_packedN");
5555
}
56+
57+
// #[thread_local] is currently unstable
58+
if unstable_cargo_feature || rustc_dep_of_std {
59+
println!("cargo:rustc-cfg=libc_thread_local");
60+
}
5661
}
5762

5863
fn rustc_minor_version() -> Option<u32> {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
feature = "rustc-dep-of-std",
2222
feature(cfg_target_vendor, link_cfg, no_core)
2323
)]
24+
#![cfg_attr(libc_thread_local, feature(thread_local))]
2425
// Enable extra lints:
2526
#![cfg_attr(feature = "extra_traits", deny(missing_debug_implementations))]
2627
#![deny(missing_copy_implementations, safe_packed_borrows)]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// DragonFlyBSD's __error function is declared with "static inline", so it must
2+
// be implemented in the libc crate, as a pointer to a static thread_local.
3+
f! {
4+
pub fn __error() -> *mut ::c_int {
5+
&mut errno
6+
}
7+
}
8+
9+
extern {
10+
#[thread_local]
11+
pub static mut errno: ::c_int;
12+
}

src/unix/bsd/freebsdlike/dragonfly/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,18 +1036,9 @@ f! {
10361036
(_CMSG_ALIGN(::mem::size_of::<::cmsghdr>()) +
10371037
_CMSG_ALIGN(length as usize)) as ::c_uint
10381038
}
1039-
1040-
#[cfg(libc_thread_local)]
1041-
pub fn __error() -> *mut ::c_int {
1042-
&mut errno
1043-
}
10441039
}
10451040

10461041
extern {
1047-
#[cfg(libc_thread_local)]
1048-
#[thread_local]
1049-
static mut errno: ::c_int;
1050-
10511042
pub fn setgrent();
10521043
pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int)
10531044
-> ::c_int;
@@ -1069,3 +1060,10 @@ extern {
10691060
pub fn fstatfs(fd: ::c_int, buf: *mut statfs) -> ::c_int;
10701061
pub fn uname(buf: *mut ::utsname) -> ::c_int;
10711062
}
1063+
1064+
cfg_if! {
1065+
if #[cfg(libc_thread_local)] {
1066+
mod errno;
1067+
pub use self::errno::*;
1068+
}
1069+
}

0 commit comments

Comments
 (0)