Skip to content

Commit

Permalink
feat: weakly provide the sys_errno_location function for all errno …
Browse files Browse the repository at this point in the history
…access
  • Loading branch information
mkroening committed Jan 17, 2025
1 parent 81a4a92 commit 2ad444f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,39 @@ pub const ERFKILL: i32 = 132;
/// Robust mutexes: Memory page has hardware error
pub const EHWPOISON: i32 = 133;

/// Returns the pointer to `errno`.
#[cfg(not(any(feature = "common-os", feature = "nostd")))]
#[thread_local]
pub(crate) static ERRNO: core::cell::UnsafeCell<i32> = core::cell::UnsafeCell::new(0);
#[unsafe(no_mangle)]
#[linkage = "weak"]
pub extern "C" fn sys_errno_location() -> *mut i32 {
use core::cell::UnsafeCell;

#[thread_local]
static ERRNO: UnsafeCell<i32> = UnsafeCell::new(0);

ERRNO.get()
}

/// Get the error number from the thread local storage
///
/// Soft-deprecated in favor of using [`sys_errno_location`].
#[cfg(not(feature = "nostd"))]
#[unsafe(no_mangle)]
pub extern "C" fn sys_get_errno() -> i32 {
sys_errno()
}

/// Get the error number from the thread local storage
///
/// Soft-deprecated in favor of using [`sys_errno_location`].
#[cfg(not(feature = "nostd"))]
#[unsafe(no_mangle)]
pub extern "C" fn sys_errno() -> i32 {
cfg_if::cfg_if! {
if #[cfg(feature = "common-os")] {
0
} else {
unsafe { ERRNO.get().read() }
unsafe { *sys_errno_location() }
}
}
}
Expand All @@ -435,7 +448,7 @@ pub(crate) trait ToErrno {
let _ = errno;
} else {
unsafe {
ERRNO.get().write(errno);
*sys_errno_location() = errno;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)]
#![cfg_attr(target_arch = "x86_64", feature(abi_x86_interrupt))]
#![feature(allocator_api)]
#![feature(linkage)]
#![feature(linked_list_cursors)]
#![feature(map_try_insert)]
#![feature(maybe_uninit_as_bytes)]
Expand Down

0 comments on commit 2ad444f

Please sign in to comment.