From 58abf5872023aca7ee6459fa3b2e067d57923ba5 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 9 Mar 2025 20:49:56 -0400 Subject: Finish testing and fixing --- src/thread/scope.rst | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/thread/scope.rst (limited to 'src/thread') diff --git a/src/thread/scope.rst b/src/thread/scope.rst new file mode 100644 index 0000000..09319cb --- /dev/null +++ b/src/thread/scope.rst @@ -0,0 +1,47 @@ +use std::marker::PhantomData; + +use crate::{Keyable, ThreadKey}; + +use super::{Scope, ScopedJoinHandle}; + +pub fn scope<'env, F, T>(key: impl Keyable, f: F) -> T +where + F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T, +{ + let scope = Scope(PhantomData); + let t = f(&scope); + drop(key); + t +} + +impl<'scope> Scope<'scope, '_> { + #[allow(clippy::unused_self)] + pub fn spawn( + &self, + f: impl FnOnce(ThreadKey) -> T + Send + 'scope, + ) -> std::io::Result> { + unsafe { + // safety: the lifetimes ensure that the data lives long enough + let handle = std::thread::Builder::new().spawn_unchecked(|| { + // safety: the thread just started, so the key cannot be acquired yet + let key = ThreadKey::get().unwrap_unchecked(); + f(key) + })?; + + Ok(ScopedJoinHandle { + handle, + _phantom: PhantomData, + }) + } + } +} + +impl ScopedJoinHandle<'_, T> { + pub fn is_finished(&self) -> bool { + self.handle.is_finished() + } + + pub fn join(self) -> std::thread::Result { + self.handle.join() + } +} -- cgit v1.2.3