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

Add std::thread::add_spawn_hook. #125405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented May 22, 2024

Implementation of rust-lang/rfcs#3642

@m-ou-se m-ou-se added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-thread-locals Area: Thread local storage (TLS) A-thread Area: `std::thread` labels May 22, 2024
@rustbot
Copy link
Collaborator

rustbot commented May 22, 2024

r? @Amanieu

rustbot has assigned @Amanieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 22, 2024
Comment on lines +5 to +7
static SPAWN_HOOKS: RwLock<
Vec<&'static (dyn Fn(&Thread) -> io::Result<Box<dyn FnOnce() + Send>> + Sync)>,
> = RwLock::new(Vec::new());
Copy link
Member Author

@m-ou-se m-ou-se May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to use a lock free linked list:

static SPAWN_HOOK: AtomicPtr<SpawnHook> = AtomicPtr::new(ptr::null_mut());

struct SpawnHook {
    next: Option<&'static SpawnHook>,
    hook: &'static (dyn Fn(&Thread) -> io::Result<Box<dyn FnOnce() + Send>> + Sync),
}

pub fn add_spawn_hook<F, G>(hook: F)
where
    F: 'static + Sync + Fn(&Thread) -> io::Result<G>,
    G: 'static + Send + FnOnce(),
{
    let mut next = SPAWN_HOOK.load(Ordering::Relaxed);
    let mut hook = Box::leak(Box::new(SpawnHook {
        next: unsafe { next.as_ref() },
        hook: Box::leak(Box::new(move |thread: &Thread| {
            let f: Box<dyn FnOnce() + Send> = Box::new(hook(thread)?);
            Ok(f)
        })),
    }));
    while let Err(n) =
        SPAWN_HOOK.compare_exchange_weak(next, hook, Ordering::Release, Ordering::Relaxed)
    {
        next = n;
        hook.next = unsafe { next.as_ref() };
    }
}

pub(super) fn run_spawn_hooks(thread: &Thread) -> io::Result<Vec<Box<dyn FnOnce() + Send>>> {
    let mut result = Vec::new();
    let mut next = unsafe { SPAWN_HOOK.load(Ordering::Acquire).as_ref() };
    while let Some(hook) = next {
        result.push((hook.hook)(thread)?);
        next = hook.next;
    }
    Ok(result)
}

But that uses two Boxes per hook (one for the dyn Fn and one for the linked list node (SpawnHook). We could merge them into a single Box by storing the Fn by value in a SpawnHook<F>, but that results in a lot more complexity and much more subtle unsafe code, which is probably not worth it.

@bors
Copy link
Contributor

bors commented Jul 20, 2024

☔ The latest upstream changes (presumably #127998) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-thread Area: `std::thread` A-thread-locals Area: Thread local storage (TLS) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants