@@ -12,6 +12,8 @@ use crate::arch::{
1212} ;
1313#[ cfg( feature = "alloc" ) ]
1414use alloc:: boxed:: Box ;
15+ #[ cfg( feature = "unstable-errno" ) ]
16+ use core:: cell:: Cell ;
1517use core:: cmp:: max;
1618use core:: ffi:: c_void;
1719use core:: mem:: { align_of, size_of} ;
@@ -76,6 +78,8 @@ impl Thread {
7678/// This is not `repr(C)` and not ABI-exposed.
7779struct ThreadData {
7880 thread_id : AtomicI32 ,
81+ #[ cfg( feature = "unstable-errno" ) ]
82+ errno_val : Cell < i32 > ,
7983 detached : AtomicU8 ,
8084 stack_addr : * mut c_void ,
8185 stack_size : usize ,
@@ -104,6 +108,8 @@ impl ThreadData {
104108 ) -> Self {
105109 Self {
106110 thread_id : AtomicI32 :: new ( ThreadId :: as_raw ( tid) ) ,
111+ #[ cfg( feature = "unstable-errno" ) ]
112+ errno_val : Cell :: new ( 0 ) ,
107113 detached : AtomicU8 :: new ( INITIAL ) ,
108114 stack_addr,
109115 stack_size,
@@ -1006,6 +1012,15 @@ pub unsafe fn set_current_id_after_a_fork(tid: ThreadId) {
10061012 . store ( tid. as_raw_nonzero ( ) . get ( ) , SeqCst ) ;
10071013}
10081014
1015+ /// Return the address of the thread-local `errno` state.
1016+ ///
1017+ /// This is equivalent to `__errno_location()` in glibc and musl.
1018+ #[ cfg( feature = "unstable-errno" ) ]
1019+ #[ inline]
1020+ pub fn errno_location ( ) -> * mut i32 {
1021+ unsafe { core:: ptr:: addr_of_mut!( ( * current_metadata( ) ) . thread. errno_val) . cast :: < i32 > ( ) }
1022+ }
1023+
10091024/// Return the TLS address for the given `offset` for the current thread.
10101025#[ inline]
10111026#[ must_use]
0 commit comments