channels and tokio::spawn
This commit is contained in:
parent
bf8a1ad8ed
commit
0acce0cd46
2 changed files with 183 additions and 9 deletions
|
|
@ -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"] }
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
Loading…
Reference in a new issue