diff --git a/compio-runtime/Cargo.toml b/compio-runtime/Cargo.toml index b2a64c06..61721555 100644 --- a/compio-runtime/Cargo.toml +++ b/compio-runtime/Cargo.toml @@ -56,7 +56,8 @@ libc = { workspace = true } windows-sys = { workspace = true, features = ["Win32_UI_WindowsAndMessaging"] } [target.'cfg(target_os = "macos")'.dev-dependencies] -core-foundation = "0.9" +core-foundation = "0.9.4" +block2 = "0.4.0" [features] event = ["dep:cfg-if", "compio-buf/arrayvec"] diff --git a/compio-runtime/tests/custom_loop.rs b/compio-runtime/tests/custom_loop.rs index 66fc5f8c..1b9e5ec3 100644 --- a/compio-runtime/tests/custom_loop.rs +++ b/compio-runtime/tests/custom_loop.rs @@ -3,12 +3,14 @@ fn cf_run_loop() { use std::{future::Future, os::raw::c_void, time::Duration}; + use block2::{Block, ConcreteBlock}; use compio_driver::AsRawFd; - use compio_runtime::Runtime; + use compio_runtime::{event::Event, Runtime}; use core_foundation::{ base::TCFType, filedescriptor::{kCFFileDescriptorReadCallBack, CFFileDescriptor, CFFileDescriptorRef}, - runloop::{kCFRunLoopDefaultMode, CFRunLoop}, + runloop::{kCFRunLoopDefaultMode, CFRunLoop, CFRunLoopRef}, + string::CFStringRef, }; struct CFRunLoopRuntime { @@ -73,11 +75,24 @@ fn cf_run_loop() { let runtime = CFRunLoopRuntime::new(); - let res = runtime.block_on(async { - compio_runtime::time::sleep(Duration::from_secs(1)).await; - 1 + runtime.block_on(async { + let event = Event::new(); + let block = ConcreteBlock::new(|| { + event.handle().notify(); + }); + extern "C" { + fn CFRunLoopPerformBlock(rl: CFRunLoopRef, mode: CFStringRef, block: &Block<(), ()>); + } + let run_loop = CFRunLoop::get_current(); + unsafe { + CFRunLoopPerformBlock( + run_loop.as_concrete_TypeRef(), + kCFRunLoopDefaultMode, + &block, + ); + } + event.wait().await; }); - assert_eq!(res, 1); } #[cfg(windows)]