channels and tokio::spawn

This commit is contained in:
Andy Killorin 2024-12-19 12:37:38 -05:00
commit 0acce0cd46
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 183 additions and 9 deletions

View file

@ -6,4 +6,4 @@ edition = "2021"
[dependencies]
anyhow = "1.0.94"
libloading = "0.8.6"
tokio = { version = "1.42.0", features = ["full"] }
tokio = { version = "1.42.0", features = ["full", "sync"] }

View file

@ -2,16 +2,25 @@ use std::{future::Future, pin::Pin};
use anyhow::Result;
use libloading::{Library, Symbol};
use tokio::sync::oneshot::{self, Receiver};
#[tokio::main]
async fn main() -> Result<()> {
let lib = unsafe {Library::new("../loaded/target/release/libloaded.so")?};
let func: Symbol<unsafe extern fn(u64,u64) -> Pin<Box<dyn Future<Output = u64>>>> = unsafe {lib.get(b"add_wrapper")?};
let (tx,rx) = oneshot::channel();
let fut = unsafe{func(12,54)};
let out = (fut).await;
let func: Symbol<unsafe extern fn(u64,u64, Receiver<u64>) -> Pin<Box<dyn Future<Output = u64> + Send>>> = unsafe {lib.get(b"add_wrapper")?};
let func = unsafe{func.into_raw()};
let fut = unsafe{func(12,54, rx)};
let out = tokio::spawn(async move {
fut.await
});
tx.send(8).unwrap();
let out = out.await.unwrap();
println!("returned {out}");
Ok(())