You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for Thread::{into_raw, from_raw} #97523
This is a tracking issue for Thread::into_raw and Thread::from_raw.
These functions allow conversions between a Thread and a raw pointer, which is useful in concurrent code as it allows storing a Thread in an AtomicPtr.
Not sure If I am missing something, but into_raw requires the thread by value, and that is not always available. E.g. JoinHandle::thread only gives out a shared reference.
How can I unpark a thread for which I just have a JoinHandle?
Yeah, thanks! I didn't notice Thread was Clone. I am perfectly happy with that now.
Thread::as_raw: sure, if it can be designed such that the data inside the Arc inside Thread doesn't get dropped meanwhile. Thread::into_inner leaks the Arc, so that's taken care of.
As a point of interest: is it legal to use Thread::from_raw if the pointer is shared in a multi-threaded context? A lot of code assumes this to be true for Arc::from_raw, and of course the implementation uses an Arc, but it is non-obvious.
Use of Thread::from_raw in a multi-threaded context might result in two Threads existing that both point to the same block of memory, but with the strong count still being 1 (even if momentarily).
An API like fn Thread::from_raw_ref(ptr: *const ()) -> &Thread would make it much easier to use this API correctly (and avoid a lot of error-prone core::mem::forget in referential contexts to boot). With Arc, this is already trivially possible since the pointer you get back is a *const T, but here we only get a *const () so you can't even temporarily turn it into a reference to the original Thread.
@zesterer It's fairly obvious to me that it's unsound to call Thread::from_raw twice in an unsynchronized manner if one of them can drop the value in the meantime. I personally don't think the from_raw_ref API is necessary, and it would still be dangerous if someone calls from_raw_ref at the same time as someone else calls from_raw.
Regarding the unresolved question,
Do we want to commit to guaranteeing that Thread can always be represented as a pointer/integer?
This API inherently guarantees that the Thread can be represented as a pointer.
I fail to see any reason why we would guarantee that pointer could be converted to/from an integer outside of the usual expose_provenance / with_exposed_provenance rules which are already valid today. If that would really be desired it could also be added later.
I personally don't see anything that's blocking this for a FCP / stabilization. Could we move along with that?
Activity
yanchith commentedon Feb 7, 2025
Not sure If I am missing something, but
into_rawrequires the thread by value, and that is not always available. E.g.JoinHandle::threadonly gives out a shared reference.How can I unpark a thread for which I just have a JoinHandle?
ibraheemdev commentedon Feb 7, 2025
@yanchith
Threadis cloneable, but it might make sense to have aas_rawmethod as well.yanchith commentedon Feb 9, 2025
Yeah, thanks! I didn't notice Thread was Clone. I am perfectly happy with that now.
Thread::as_raw: sure, if it can be designed such that the data inside the Arc inside Thread doesn't get dropped meanwhile.Thread::into_innerleaks the Arc, so that's taken care of.zesterer commentedon May 25, 2025
As a point of interest: is it legal to use
Thread::from_rawif the pointer is shared in a multi-threaded context? A lot of code assumes this to be true forArc::from_raw, and of course the implementation uses anArc, but it is non-obvious.Use of
Thread::from_rawin a multi-threaded context might result in twoThreads existing that both point to the same block of memory, but with the strong count still being 1 (even if momentarily).An API like
fn Thread::from_raw_ref(ptr: *const ()) -> &Threadwould make it much easier to use this API correctly (and avoid a lot of error-pronecore::mem::forgetin referential contexts to boot). WithArc, this is already trivially possible since the pointer you get back is a*const T, but here we only get a*const ()so you can't even temporarily turn it into a reference to the originalThread.orlp commentedon Dec 20, 2025
@zesterer It's fairly obvious to me that it's unsound to call
Thread::from_rawtwice in an unsynchronized manner if one of them can drop the value in the meantime. I personally don't think thefrom_raw_refAPI is necessary, and it would still be dangerous if someone callsfrom_raw_refat the same time as someone else callsfrom_raw.Regarding the unresolved question,
This API inherently guarantees that the
Threadcan be represented as a pointer.I fail to see any reason why we would guarantee that pointer could be converted to/from an integer outside of the usual
expose_provenance/with_exposed_provenancerules which are already valid today. If that would really be desired it could also be added later.I personally don't see anything that's blocking this for a FCP / stabilization. Could we move along with that?