std/thread/
current.rs

1use super::{Thread, ThreadId, imp};
2use crate::mem::ManuallyDrop;
3use crate::ptr;
4use crate::sys::thread_local::local_pointer;
5
6const NONE: *mut () = ptr::null_mut();
7const BUSY: *mut () = ptr::without_provenance_mut(1);
8const DESTROYED: *mut () = ptr::without_provenance_mut(2);
9
10local_pointer! {
11    static CURRENT;
12}
13
14/// Persistent storage for the thread ID.
15///
16/// We store the thread ID so that it never gets destroyed during the lifetime
17/// of a thread, either using `#[thread_local]` or multiple `local_pointer!`s.
18pub(super) mod id {
19    use super::*;
20
21    cfg_select! {
22        target_thread_local => {
23            use crate::cell::Cell;
24
25            #[thread_local]
26            static ID: Cell<Option<ThreadId>> = Cell::new(None);
27
28            pub(super) const CHEAP: bool = true;
29
30            pub(crate) fn get() -> Option<ThreadId> {
31                ID.get()
32            }
33
34            pub(super) fn set(id: ThreadId) {
35                ID.set(Some(id))
36            }
37        }
38        target_pointer_width = "16" => {
39            local_pointer! {
40                static ID0;
41                static ID16;
42                static ID32;
43                static ID48;
44            }
45
46            pub(super) const CHEAP: bool = false;
47
48            pub(crate) fn get() -> Option<ThreadId> {
49                let id0 = ID0.get().addr() as u64;
50                let id16 = ID16.get().addr() as u64;
51                let id32 = ID32.get().addr() as u64;
52                let id48 = ID48.get().addr() as u64;
53                ThreadId::from_u64((id48 << 48) + (id32 << 32) + (id16 << 16) + id0)
54            }
55
56            pub(super) fn set(id: ThreadId) {
57                let val = id.as_u64().get();
58                ID0.set(ptr::without_provenance_mut(val as usize));
59                ID16.set(ptr::without_provenance_mut((val >> 16) as usize));
60                ID32.set(ptr::without_provenance_mut((val >> 32) as usize));
61                ID48.set(ptr::without_provenance_mut((val >> 48) as usize));
62            }
63        }
64        target_pointer_width = "32" => {
65            local_pointer! {
66                static ID0;
67                static ID32;
68            }
69
70            pub(super) const CHEAP: bool = false;
71
72            pub(crate) fn get() -> Option<ThreadId> {
73                let id0 = ID0.get().addr() as u64;
74                let id32 = ID32.get().addr() as u64;
75                ThreadId::from_u64((id32 << 32) + id0)
76            }
77
78            pub(super) fn set(id: ThreadId) {
79                let val = id.as_u64().get();
80                ID0.set(ptr::without_provenance_mut(val as usize));
81                ID32.set(ptr::without_provenance_mut((val >> 32) as usize));
82            }
83        }
84        _ => {
85            local_pointer! {
86                static ID;
87            }
88
89            pub(super) const CHEAP: bool = true;
90
91            pub(crate) fn get() -> Option<ThreadId> {
92                let id = ID.get().addr() as u64;
93                ThreadId::from_u64(id)
94            }
95
96            pub(super) fn set(id: ThreadId) {
97                let val = id.as_u64().get();
98                ID.set(ptr::without_provenance_mut(val as usize));
99            }
100        }
101    }
102
103    #[inline]
104    pub(super) fn get_or_init() -> ThreadId {
105        get().unwrap_or_else(
106            #[cold]
107            || {
108                let id = ThreadId::new();
109                id::set(id);
110                id
111            },
112        )
113    }
114}
115
116/// Tries to set the thread handle for the current thread. Fails if a handle was
117/// already set or if the thread ID of `thread` would change an already-set ID.
118pub(super) fn set_current(thread: Thread) -> Result<(), Thread> {
119    if CURRENT.get() != NONE {
120        return Err(thread);
121    }
122
123    match id::get() {
124        Some(id) if id == thread.id() => {}
125        None => id::set(thread.id()),
126        _ => return Err(thread),
127    }
128
129    // Make sure that `crate::rt::thread_cleanup` will be run, which will
130    // call `drop_current`.
131    crate::sys::thread_local::guard::enable();
132    CURRENT.set(thread.into_raw().cast_mut());
133    Ok(())
134}
135
136/// Gets the id of the thread that invokes it.
137///
138/// This function will always succeed, will always return the same value for
139/// one thread and is guaranteed not to call the global allocator.
140#[inline]
141pub(crate) fn current_id() -> ThreadId {
142    // If accessing the persistent thread ID takes multiple TLS accesses, try
143    // to retrieve it from the current thread handle, which will only take one
144    // TLS access.
145    if !id::CHEAP {
146        if let Some(id) = try_with_current(|t| t.map(|t| t.id())) {
147            return id;
148        }
149    }
150
151    id::get_or_init()
152}
153
154/// Gets the OS thread ID of the thread that invokes it, if available. If not, return the Rust
155/// thread ID.
156///
157/// We use a `u64` to all possible platform IDs without excess `cfg`; most use `int`, some use a
158/// pointer, and Apple uses `uint64_t`. This is a "best effort" approach for diagnostics and is
159/// allowed to fall back to a non-OS ID (such as the Rust thread ID) or a non-unique ID (such as a
160/// PID) if the thread ID cannot be retrieved.
161pub(crate) fn current_os_id() -> u64 {
162    imp::current_os_id().unwrap_or_else(|| current_id().as_u64().get())
163}
164
165/// Gets a reference to the handle of the thread that invokes it, if the handle
166/// has been initialized.
167pub(super) fn try_with_current<F, R>(f: F) -> R
168where
169    F: FnOnce(Option<&Thread>) -> R,
170{
171    let current = CURRENT.get();
172    if current > DESTROYED {
173        // SAFETY: `Arc` does not contain interior mutability, so it does not
174        // matter that the address of the handle might be different depending
175        // on where this is called.
176        unsafe {
177            let current = ManuallyDrop::new(Thread::from_raw(current));
178            f(Some(&current))
179        }
180    } else {
181        f(None)
182    }
183}
184
185/// Gets a handle to the thread that invokes it. If the handle stored in thread-
186/// local storage was already destroyed, this creates a new unnamed temporary
187/// handle to allow thread parking in nearly all situations.
188pub(crate) fn current_or_unnamed() -> Thread {
189    let current = CURRENT.get();
190    if current > DESTROYED {
191        unsafe {
192            let current = ManuallyDrop::new(Thread::from_raw(current));
193            (*current).clone()
194        }
195    } else if current == DESTROYED {
196        Thread::new(id::get_or_init(), None)
197    } else {
198        init_current(current)
199    }
200}
201
202/// Gets a handle to the thread that invokes it.
203///
204/// # Examples
205///
206/// Getting a handle to the current thread with `thread::current()`:
207///
208/// ```
209/// use std::thread;
210///
211/// let handler = thread::Builder::new()
212///     .name("named thread".into())
213///     .spawn(|| {
214///         let handle = thread::current();
215///         assert_eq!(handle.name(), Some("named thread"));
216///     })
217///     .unwrap();
218///
219/// handler.join().unwrap();
220/// ```
221#[must_use]
222#[stable(feature = "rust1", since = "1.0.0")]
223pub fn current() -> Thread {
224    let current = CURRENT.get();
225    if current > DESTROYED {
226        unsafe {
227            let current = ManuallyDrop::new(Thread::from_raw(current));
228            (*current).clone()
229        }
230    } else {
231        init_current(current)
232    }
233}
234
235#[cold]
236fn init_current(current: *mut ()) -> Thread {
237    if current == NONE {
238        CURRENT.set(BUSY);
239        // If the thread ID was initialized already, use it.
240        let id = id::get_or_init();
241        let thread = Thread::new(id, None);
242
243        // Make sure that `crate::rt::thread_cleanup` will be run, which will
244        // call `drop_current`.
245        crate::sys::thread_local::guard::enable();
246        CURRENT.set(thread.clone().into_raw().cast_mut());
247        thread
248    } else if current == BUSY {
249        // BUSY exists solely for this check, but as it is in the slow path, the
250        // extra TLS write above shouldn't matter. The alternative is nearly always
251        // a stack overflow.
252
253        // If you came across this message, contact the author of your allocator.
254        // If you are said author: A surprising amount of functions inside the
255        // standard library (e.g. `Mutex`, `thread_local!`, `File` when using long
256        // paths, even `panic!` when using unwinding), need memory allocation, so
257        // you'll get circular dependencies all over the place when using them.
258        // I (joboet) highly recommend using only APIs from core in your allocator
259        // and implementing your own system abstractions. Still, if you feel that
260        // a particular API should be entirely allocation-free, feel free to open
261        // an issue on the Rust repository, we'll see what we can do.
262        rtabort!(
263            "\n\
264            Attempted to access thread-local data while allocating said data.\n\
265            Do not access functions that allocate in the global allocator!\n\
266            This is a bug in the global allocator.\n\
267            "
268        )
269    } else {
270        debug_assert_eq!(current, DESTROYED);
271        panic!(
272            "use of std::thread::current() is not possible after the thread's \
273            local data has been destroyed"
274        )
275    }
276}
277
278/// This should be run in [`crate::rt::thread_cleanup`] to reset the thread
279/// handle.
280pub(crate) fn drop_current() {
281    let current = CURRENT.get();
282    if current > DESTROYED {
283        unsafe {
284            CURRENT.set(DESTROYED);
285            drop(Thread::from_raw(current));
286        }
287    }
288}