//! pool use std::ffi::c_void; use std::os::windows::prelude::*; use std::sync::mpsc::{channel, Sender}; use std::time::Duration; use lol_io::const_uuid_wide; use lol_io::kernel::threading::event::{InitialState, OwnedEventHandle, Reset}; use lol_io::kernel::threading::pool::wait::WaitTimeoutError; use lol_io::prelude::*; use lol_io::{kernel::co, kernel::threading::pool::*}; unsafe extern "system" fn test_wait_callback( _instance: isize, context: *mut c_void, _wait: isize, waitresult: u32, ) { let result = WaitTimeoutError::parse_raw(waitresult); let sender = unsafe { &*(context as *const Sender>) }; sender.send(result.map_err(CompletionError::from)).unwrap(); } #[test] fn lol_test_pool_wait() { const EVENT_ID: *const u16 = const_uuid_wide!(); let event = OwnedEventHandle::new_raw(EVENT_ID, Reset::Manual, InitialState::Unset).unwrap(); let (sender, receiver) = channel::>(); let (_pool, env) = make_pool().unwrap(); let wait = ThreadpoolWait::new(Some(&env), test_wait_callback, &sender as *const _ as _).unwrap(); wait.set(event.as_raw_handle() as _, None); event.set().unwrap(); let msg = receiver.recv(); assert!(msg.unwrap().is_ok()); wait.wait(Pending::Wait); } #[test] fn lol_test_pool_wait_cancel() { const EVENT_ID: *const u16 = const_uuid_wide!(); let event = OwnedEventHandle::new_raw(EVENT_ID, Reset::Manual, InitialState::Unset).unwrap(); let (_pool, env) = make_pool().unwrap(); let wait = ThreadpoolWait::new(Some(&env), test_wait_callback, std::ptr::null_mut()).unwrap(); wait.set(event.as_raw_handle() as _, None); wait.cancel(); event.set().unwrap(); wait.wait(Pending::Wait); } #[test] fn lol_test_pool_wait_timeout() { const EVENT_ID: *const u16 = const_uuid_wide!(); let event = OwnedEventHandle::new_raw(EVENT_ID, Reset::Manual, InitialState::Unset).unwrap(); let (sender, receiver) = channel::>(); let (_pool, env) = make_pool().unwrap(); let wait = ThreadpoolWait::new(Some(&env), test_wait_callback, &sender as *const _ as _).unwrap(); wait.set(event.as_raw_handle() as _, Some(Duration::from_millis(5))); std::thread::sleep(Duration::from_millis(40)); event.set().unwrap(); let msg = receiver.recv(); assert!(msg.unwrap().is_err()); wait.wait(Pending::Wait); } pub fn make_pool() -> std::io::Result<(Threadpool, ThreadpoolCallbackEnvironment)> { let pool = Threadpool::new()?; pool.min_threads(1)? .max_threads(1) .set_stack_size(128, 128)?; let env = pool .new_environment() .with_priority(co::ThreadpoolPriority::HIGH); Ok((pool, env)) }