Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACP: add Thread::into_raw and Thread::from_raw #200

Closed
h33p opened this issue Mar 30, 2023 · 4 comments
Closed

ACP: add Thread::into_raw and Thread::from_raw #200

h33p opened this issue Mar 30, 2023 · 4 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@h33p
Copy link

h33p commented Mar 30, 2023

Proposal

Problem statement

It should be possible to convert a Thread from/to a raw pointer for passing thread handles across API boundaries in an opaque manner.

Motivation, use-cases

Primary use case would be optimizing small futures executors. pollster - a minimalistic futures executor with over 1 million downloads would be a direct beneficiary of this. Currently, pollster needs to heap allocate a signal on every future being executed. Alternative would be to use a thread_local! store of static signals/thread handles, but why bother, when a Thread is already that?

As of today, Thread is merely a newtype around opaque Arc, and I was able to abuse this knowledge to remove heap allocations from pollster and speed it up by up to 2x (zesterer/pollster#19). Thus, it would be trivial to add methods to convert the thread handle into a raw pointer, and go the other way around.

Additional use cases would be passing thread handles across FFI-boundary, which would enable better interoperability with other languages and dynamically loaded Rust libraries.

Solution sketches

We would add these functions to Thread implementation:

#[must_use]
pub fn into_raw(self) -> *const () {
    let inner = unsafe { Pin::into_inner_unchecked(self.inner) };
    Arc::into_raw(inner) as *const ()
}

pub unsafe fn from_raw(raw: *const ()) -> Thread {
    Thread {
        inner: Pin::new_unchecked(Arc::from_raw(raw as *const Inner))
    }
}

Links and related work

Original PR to pollster: zesterer/pollster#14
Prior implementation of this ACP: rust-lang/rust#97524
Implementation of this ACP (I closed it as duplicate): rust-lang/rust#109794

@h33p h33p added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Mar 30, 2023
@joboet
Copy link

joboet commented Mar 30, 2023

Just for reference: this has already been proposed, with the same API: rust-lang/rust#97524

@h33p
Copy link
Author

h33p commented Mar 30, 2023

Oh, my bad! Appears that I forgot to check for the most basic of things 🙃

@joboet
Copy link

joboet commented Mar 30, 2023

Not to worry, it was quite buried. I just had a déjà vu. Anyway, it's probably best that this now gets a proper ACP...

@joshtriplett
Copy link
Member

We reviewed this (long-delayed) ACP in today's @rust-lang/libs-api meeting, and approved it. Thank you!

@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Aug 20, 2024
tgross35 added a commit to tgross35/rust that referenced this issue Aug 21, 2024
Add `Thread::{into_raw, from_raw}`

Public API:
```rust
#![unstable(feature = "thread_raw", issue = "97523")]

impl Thread {
    pub fn into_raw(self) -> *const ();
    pub unsafe fn from_raw(ptr: *const ()) -> Thread;
}
```

ACP: rust-lang/libs-team#200
tgross35 added a commit to tgross35/rust that referenced this issue Aug 21, 2024
Add `Thread::{into_raw, from_raw}`

Public API:
```rust
#![unstable(feature = "thread_raw", issue = "97523")]

impl Thread {
    pub fn into_raw(self) -> *const ();
    pub unsafe fn from_raw(ptr: *const ()) -> Thread;
}
```

ACP: rust-lang/libs-team#200
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 18, 2024
Add `Thread::{into_raw, from_raw}`

Public API:
```rust
#![unstable(feature = "thread_raw", issue = "97523")]

impl Thread {
    pub fn into_raw(self) -> *const ();
    pub unsafe fn from_raw(ptr: *const ()) -> Thread;
}
```

ACP: rust-lang/libs-team#200
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 18, 2024
Add `Thread::{into_raw, from_raw}`

Public API:
```rust
#![unstable(feature = "thread_raw", issue = "97523")]

impl Thread {
    pub fn into_raw(self) -> *const ();
    pub unsafe fn from_raw(ptr: *const ()) -> Thread;
}
```

ACP: rust-lang/libs-team#200
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 19, 2024
Rollup merge of rust-lang#97524 - ibraheemdev:thread-raw, r=ibraheemdev

Add `Thread::{into_raw, from_raw}`

Public API:
```rust
#![unstable(feature = "thread_raw", issue = "97523")]

impl Thread {
    pub fn into_raw(self) -> *const ();
    pub unsafe fn from_raw(ptr: *const ()) -> Thread;
}
```

ACP: rust-lang/libs-team#200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants