diff --git a/src/praetor/mod.rs b/src/praetor/mod.rs index afe0ba1..eefb23f 100644 --- a/src/praetor/mod.rs +++ b/src/praetor/mod.rs @@ -953,6 +953,10 @@ mod job { Self { result } } + pub fn into_inner(self) -> std::thread::Result { + self.result + } + // unwraps the result, propagating panics pub fn into_result(self) -> T { match self.result { diff --git a/src/praetor/tests.rs b/src/praetor/tests.rs index 5f774f3..0e88370 100644 --- a/src/praetor/tests.rs +++ b/src/praetor/tests.rs @@ -1,4 +1,7 @@ -use std::{mem::MaybeUninit, pin::Pin}; +use std::{ + mem::MaybeUninit, + pin::{pin, Pin}, +}; use super::{util::TaggedAtomicPtr, *}; @@ -11,11 +14,11 @@ fn pin_ptr(pin: &Pin<&mut T>) -> NonNull { fn new_job() { let mut list = JobList::new(); - let stack = pin!(StackJob::new(|_: &Scope| 3 + 3)); + let stack = StackJob::new(|| 3 + 3, crate::latch::MutexLatch::new()); - let job = pin!(stack.as_ref().as_job()); + let job = stack.as_job(); unsafe { - list.push_front(job.as_ref()); + list.push_front(&job); } unsafe { @@ -24,7 +27,7 @@ fn new_job() { _ = stack.unwrap(); job_ref.complete(Ok(6)); - let result = job_ref.wait(); + let result = job_ref.wait().into_inner(); assert_eq!(result.ok(), Some(6)); } } @@ -37,14 +40,14 @@ fn job_list_pop_back() { let c = pin!(Job::empty()); unsafe { - list.push_front(a.as_ref()); - list.push_front(b.as_ref()); - list.push_back(c.as_ref()); + list.push_front(&a); + list.push_front(&b); + list.push_back(&c); } assert_eq!(list.pop_back(), Some(pin_ptr(&c))); unsafe { - list.push_front(c.as_ref()); + list.push_front(&c); } assert_eq!(list.pop_back(), Some(pin_ptr(&a))); assert_eq!(list.pop_back(), Some(pin_ptr(&b))); @@ -61,14 +64,14 @@ fn job_list_pop_front() { let c = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); - list.push_front(b.as_ref()); - list.push_back(c.as_ref()); + list.push_front(&a); + list.push_front(&b); + list.push_back(&c); } assert_eq!(list.pop_front(), Some(pin_ptr(&b))); unsafe { - list.push_back(b.as_ref()); + list.push_back(&b); } assert_eq!(list.pop_front(), Some(pin_ptr(&a))); assert_eq!(list.pop_front(), Some(pin_ptr(&c))); @@ -85,9 +88,9 @@ fn unlink_job_middle() { let c = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); - list.push_front(b.as_ref()); - list.push_front(c.as_ref()); + list.push_front(&a); + list.push_front(&b); + list.push_front(&c); } unsafe { @@ -108,9 +111,9 @@ fn unlink_job_front() { let c = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); - list.push_front(b.as_ref()); - list.push_front(c.as_ref()); + list.push_front(&a); + list.push_front(&b); + list.push_front(&c); } unsafe { @@ -131,9 +134,9 @@ fn unlink_job_back() { let c = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); - list.push_front(b.as_ref()); - list.push_front(c.as_ref()); + list.push_front(&a); + list.push_front(&b); + list.push_front(&c); } unsafe { @@ -152,7 +155,7 @@ fn unlink_job_single() { let a = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); + list.push_front(&a); } unsafe { @@ -342,7 +345,7 @@ fn job_list_pop_back_emptied() { let a = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); + list.push_front(&a); } assert_eq!(list.pop_back(), Some(pin_ptr(&a))); @@ -357,7 +360,7 @@ fn job_list_pop_front_emptied() { let a = pin!(Job::<()>::empty()); unsafe { - list.push_front(a.as_ref()); + list.push_front(&a); } assert_eq!(list.pop_front(), Some(pin_ptr(&a))); @@ -367,9 +370,27 @@ fn job_list_pop_front_emptied() { } #[test] +#[tracing_test::traced_test] fn spawn() { let pool = ThreadPool::new(); + let mut x = 0; + pool.scope(|s| { + tracing::info!("scope"); + s.spawn(|_| { + tracing::info!("spawn"); + x += 1; + tracing::info!("x: {x}"); + }); + tracing::info!("~scope"); + }); + + eprintln!("x: {x}"); +} + +#[test] +fn rayon_spawn() { + let pool = rayon::ThreadPoolBuilder::new().build().unwrap(); let mut x = 0; pool.scope(|s| { s.spawn(|_| {