diff --git a/ylong_runtime/src/builder/common_builder.rs b/ylong_runtime/src/builder/common_builder.rs index 9336e663fbda97dd94df3b8c5514e5d79bb0ac21..5ab7b7478eede4a39a89c740620e1f9dc164b18d 100644 --- a/ylong_runtime/src/builder/common_builder.rs +++ b/ylong_runtime/src/builder/common_builder.rs @@ -25,6 +25,9 @@ pub(crate) struct CommonBuilder { /// Name prefix of worker threads pub(crate) worker_name: Option, + /// Name prefix of blocking worker threads + pub(crate) blocking_worker_name: Option, + /// Core affinity, default set to true #[cfg(not(feature = "ffrt"))] pub(crate) is_affinity: bool, @@ -56,6 +59,7 @@ impl CommonBuilder { pub(crate) fn new() -> Self { CommonBuilder { worker_name: None, + blocking_worker_name: None, #[cfg(not(feature = "ffrt"))] is_affinity: true, blocking_permanent_thread_num: BLOCKING_PERMANENT_THREAD_NUM, @@ -112,12 +116,18 @@ macro_rules! impl_common { } impl $self { - /// Sets the name prefix for all worker threads. + /// Sets the name prefix for async worker threads. pub fn worker_name(mut self, name: String) -> Self { self.common.worker_name = Some(name); self } + /// Sets the name prefix for blocking worker threads. + pub fn blocking_worker_name(mut self, name: String) -> Self { + self.common.blocking_worker_name = Some(name); + self + } + /// Sets the maximum number of permanent threads in blocking thread pool pub fn blocking_permanent_thread_num( mut self, @@ -169,3 +179,28 @@ macro_rules! impl_common { } pub(crate) use impl_common; + +#[cfg(test)] +mod test { + use crate::builder::MultiThreadBuilder; + + /// UT test cases for builder + /// + /// # Brief + /// 1. Creates a new MultiThreadBuilder, check it's work_name is None + /// 2. Uses worker_name() and blocking_worker_name(), and then check it's + /// work_name + #[test] + fn ut_builder_basic() { + let builder = MultiThreadBuilder::new(); + assert_eq!(builder.common.worker_name, None); + assert_eq!(builder.common.blocking_worker_name, None); + let builder = builder.worker_name("work_name".to_string()); + let builder = builder.blocking_worker_name("blocking_worker_name".to_string()); + assert_eq!(builder.common.worker_name, Some("work_name".to_string())); + assert_eq!( + builder.common.blocking_worker_name, + Some("blocking_worker_name".to_string()) + ); + } +} diff --git a/ylong_runtime/src/builder/mod.rs b/ylong_runtime/src/builder/mod.rs index a213879a518ac434f85310231bfd49064700c47b..0be3e95fd543346fdd1202b0e9b90b3db18447e6 100644 --- a/ylong_runtime/src/builder/mod.rs +++ b/ylong_runtime/src/builder/mod.rs @@ -280,17 +280,6 @@ mod current_thread_test { let result = runtime.block_on(handle).unwrap(); assert_eq!(result, index); } - - let runtime = RuntimeBuilder::new_current_thread().build().unwrap(); - let handle = runtime.spawn(async move { - let runtime = RuntimeBuilder::new_current_thread().build().unwrap(); - let handle = runtime.spawn(async move { 1_usize }); - let result = runtime.block_on(handle).unwrap(); - assert_eq!(result, 1); - result - }); - let result = runtime.block_on(handle).unwrap(); - assert_eq!(result, 1); } } diff --git a/ylong_runtime/src/executor/async_pool.rs b/ylong_runtime/src/executor/async_pool.rs index e16eb159d7d06b1d1295dca20411d9410ade62b0..7149542e46375256383d63472bc41002810aed8d 100644 --- a/ylong_runtime/src/executor/async_pool.rs +++ b/ylong_runtime/src/executor/async_pool.rs @@ -389,7 +389,7 @@ impl AsyncPoolSpawner { let mut builder = thread::Builder::new(); if let Some(worker_name) = self.inner.worker_name.clone() { - builder = builder.name(format!("async-{worker_id}-{worker_name}")); + builder = builder.name(format!("{worker_name}-{worker_id}")); } else { builder = builder.name(format!("async-{worker_id}")); } diff --git a/ylong_runtime/src/executor/blocking_pool.rs b/ylong_runtime/src/executor/blocking_pool.rs index 43a38f10a0a2f1ff7f6c9f33948786ecfc842480..734413aa4563e9f9644857875fc3991c20afd449 100644 --- a/ylong_runtime/src/executor/blocking_pool.rs +++ b/ylong_runtime/src/executor/blocking_pool.rs @@ -64,6 +64,7 @@ impl BlockPoolSpawner { shutdown_condvar: Condvar::new(), stack_size: builder.stack_size, after_start: builder.after_start.clone(), + blocking_worker_name: builder.blocking_worker_name.clone(), before_stop: builder.before_stop.clone(), max_thread_num, keep_alive_time, @@ -126,6 +127,9 @@ struct Inner { /// A callback func to be called before thread stops before_stop: Option, + /// Name prefix of blocking worker threads + blocking_worker_name: Option, + /// Maximum thread number for the blocking pool max_thread_num: u8, @@ -172,7 +176,14 @@ impl BlockPoolSpawner { let mut shared = self.inner.shared.lock().unwrap(); shared.total_thread_num += 1; let worker_id = shared.worker_id; - let mut builder = thread::Builder::new().name(format!("block-r-{worker_id}")); + let mut builder = thread::Builder::new(); + + if let Some(blocking_worker_name) = self.inner.blocking_worker_name.clone() { + builder = builder.name(format!("{blocking_worker_name}-r-{worker_id}")); + } else { + builder = builder.name(format!("block-r-{worker_id}")); + } + if let Some(stack_size) = self.inner.stack_size { builder = builder.stack_size(stack_size); } @@ -223,7 +234,14 @@ impl BlockPoolSpawner { shared.total_thread_num += 1; // sets all required attributes for the thread let worker_id = shared.worker_id; - let mut builder = thread::Builder::new().name(format!("block-{worker_id}")); + let mut builder = thread::Builder::new(); + + if let Some(blocking_worker_name) = self.inner.blocking_worker_name.clone() { + builder = builder.name(format!("{blocking_worker_name}-{worker_id}")); + } else { + builder = builder.name(format!("block-{worker_id}")); + } + if let Some(stack_size) = self.inner.stack_size { builder = builder.stack_size(stack_size); } diff --git a/ylong_runtime/src/executor/mod.rs b/ylong_runtime/src/executor/mod.rs index e0f30ba220173119c54af345a7eed9edf870b3eb..c1d680598ebb25551dd33778144795b4f49bf674 100644 --- a/ylong_runtime/src/executor/mod.rs +++ b/ylong_runtime/src/executor/mod.rs @@ -281,7 +281,15 @@ impl Runtime { }; #[cfg(not(feature = "ffrt"))] worker::CURRENT_HANDLE.with(|ctx| { - ctx.set(&cur_context as *const _ as *const ()); + if ctx.get().is_null() { + ctx.set(&cur_context as *const _ as *const ()); + } else { + panic!( + "Cannot block_on() a task in a runtime asynchronous context. \ + This happens because a block_on() tries to block the current \ + thread which is being used to drive an asynchronous task." + ); + } }); let ret = match &self.async_spawner { diff --git a/ylong_runtime/src/io/stdio.rs b/ylong_runtime/src/io/stdio.rs index 95723421dab1f35aaa63c084215e38cd918e77a5..1a3221ff7354afda54d9b5e96ad3df9308f04408 100644 --- a/ylong_runtime/src/io/stdio.rs +++ b/ylong_runtime/src/io/stdio.rs @@ -191,3 +191,49 @@ macro_rules! std_async_write { }; } pub(crate) use std_async_write; + +#[cfg(test)] +mod stdio_test { + use std::io::Cursor; + + use crate::io::stdio::BufInner; + use crate::io::ReadBuf; + + /// UT test cases for BufInner + /// + /// # Brief + /// 1. Creates a BufInner and calls basic method + /// 2. Checks if the result is Ok + #[test] + fn ut_buf_basic_test() { + let mut buf = BufInner::new(); + assert!(buf.is_empty()); + + let input = "Hello".as_bytes(); + buf.clone_from(input); + assert_eq!(buf.inner, input); + + let mut tmp = [0; 3]; + let mut read_buf = ReadBuf::new(&mut tmp); + buf.set_len(&mut read_buf); + assert_eq!(buf.len(), 3); + + let mut tmp = [0; 5]; + let mut read_buf = ReadBuf::new(&mut tmp); + buf.clone_into(&mut read_buf); + assert_eq!(read_buf.filled(), input[0..3].to_owned()); + + let mut buf = BufInner::new(); + buf.clone_from(input); + + let mut cursor = Cursor::new(vec![0; 5]); + buf.write_into(&mut cursor).unwrap(); + assert_eq!(cursor.get_ref(), input); + assert!(buf.is_empty()); + + buf.clone_from(input); + let mut cursor = Cursor::new(vec![2; 5]); + buf.read_from(&mut cursor).unwrap(); + assert_eq!(&buf.inner, cursor.get_ref()); + } +} diff --git a/ylong_runtime/tests/block_on.rs b/ylong_runtime/tests/block_on.rs index 910027ebea8a48e5b7cd823e8c26ae99c2a9321e..e18286afe0ec59fa82a567f74074875026b029b2 100644 --- a/ylong_runtime/tests/block_on.rs +++ b/ylong_runtime/tests/block_on.rs @@ -56,15 +56,6 @@ fn sdv_single1_block_on() { assert_eq!(res, 3); } -#[test] -fn sdv_nest_block_on() { - let runtime = RuntimeBuilder::new_multi_thread().build().unwrap(); - let res = runtime.block_on(async { runtime.block_on(async { 1 + 2 }) }); - assert_eq!(res, 3); - let res = ylong_runtime::block_on(async { ylong_runtime::block_on(async { 1 + 2 }) }); - assert_eq!(res, 3); -} - #[test] fn sdv_block_on_nest_spawn() { let runtime = RuntimeBuilder::new_multi_thread().build().unwrap();