Skip to content

Commit b8cc39b

Browse files
authored
wasm32-wasi: Use __errno_location instead of feature(thread_local). (#66)
Recent versions of wasi-libc provide an `__errno_location` function which returns the address of errno, similar to other platforms. Change the errno crate to use that, instead of using the unstable `feature(thread_local)`. At the moment, only Rust nightly has a new enough wasi-libc to support this, however it's not a regression because the `feature(thread_local)` already dependend on nightly. In the future, the newer wasi-libc will be in stable Rust too, making this work on stable.
1 parent dbd02cf commit b8cc39b

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! println!("Error {}: {}", code, e);
1818
//! ```
1919
20-
#![cfg_attr(target_os = "wasi", feature(thread_local))]
2120
#![cfg_attr(not(feature = "std"), no_std)]
2221

2322
#[cfg_attr(unix, path = "unix.rs")]

src/wasi.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ where
4545
pub const STRERROR_NAME: &str = "strerror_r";
4646

4747
pub fn errno() -> Errno {
48-
// libc_errno is thread-local, so simply read its value.
49-
unsafe { Errno(libc_errno) }
48+
unsafe { Errno(*__errno_location()) }
5049
}
5150

5251
pub fn set_errno(Errno(new_errno): Errno) {
53-
// libc_errno is thread-local, so simply assign to it.
5452
unsafe {
55-
libc_errno = new_errno;
53+
*__errno_location() = new_errno;
5654
}
5755
}
5856

5957
extern "C" {
60-
#[thread_local]
61-
#[link_name = "errno"]
62-
static mut libc_errno: c_int;
63-
58+
fn __errno_location() -> *mut c_int;
6459
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int;
6560
}

0 commit comments

Comments
 (0)