如何在 Rust FFI 库中重用 Tokio 运行时

How to reuse Tokio runtime in Rust FFI library

我想为 sn_api 库编写一个 FFI 包装器,其中包含 async 个函数。它将用于用 Red 编写的单线程非异步代码。

,最简单的方法是在每个导出的函数中使用Runtime::new().unwrap().block_on(...),尽管它涉及很多创建新的Tokio 运行次并且看起来太重了每次通话 运行:

use std::os::raw::c_char;
use std::ffi::{CString, CStr};
use sn_api::{BootstrapConfig, Safe};
use tokio::runtime::Runtime;

#[no_mangle]
pub extern "C" _safe_connect(ptr: *const Safe, bootstrap_contact: *const c_char) {
    assert!(!ptr.is_null());
    let _safe = unsafe {
        &*ptr
    };

    let bootstrap_contact = unsafe {
        CStr::from_ptr(bootstrap_contact)
    }

    let mut bootstrap_contacts = BootstrapConfig::default();
    bootstrap_contacts.insert(bootstrap_contact.parse().expect("Invalid bootstrap address"));

    // how to reuse the Runtime in other functions?
    Runtime::new().unwrap().block_on(_safe.connect(None, None, Some(bootstrap_contacts)));
}

是否可以 运行 一个公共 Runtime 上的所有异步函数?我想这需要创建一些单例/全局,但我的库是用 crate-type = ["cdylib"] 编译的,这似乎不是全局的好地方。什么是最好的方法?

试试这个。

来自这里:

#[tokio::main]
async fn main() {
    println!("hello");
}

转化为:

fn main() {
    let mut rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        println!("hello");
    })
}

参考:https://tokio.rs/tokio/tutorial/hello-tokio#async-main-function

我决定采用一种方法,我创建了一个 Tokio Runtime,然后将其传递给每个包含异步代码的 FFI 函数调用:

use std::os::raw::c_char;
use std::ffi::{CString, CStr};
use sn_api::{BootstrapConfig, Safe};
use tokio::runtime::Runtime;

#[no_mangle]
pub extern "C" fn init_runtime() -> *mut Runtime {
    Box::into_raw(Box::new(Runtime::new().unwrap()))
}

#[no_mangle]
pub extern "C" _safe_connect(rt_ptr: *mut Runtime, safe_ptr: *mut Safe, bootstrap_contact: *const c_char) {
    assert!(!safe_ptr.is_null());
    assert!(!rt_ptr.is_null());

    let bootstrap_contact = unsafe {
        CStr::from_ptr(bootstrap_contact)
    }
    let mut bootstrap_contacts = BootstrapConfig::default();
    bootstrap_contacts.insert(bootstrap_contact.parse().expect("Invalid bootstrap address"));

    unsafe {
        let _safe = &mut *safe_ptr;
        let rt = &mut *rt_ptr;
        rt.block_on(_safe.connect(None, None, Some(bootstrap_contacts))).unwrap();
    }
}

我遇到了同样的问题。这是我的剪辑:export-tokio-to-lib.

plugin.rs:

use async_ffi::{FfiFuture, FutureExt};
use tokio::runtime::Handle;

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn test(arg: f32, handle: *const Handle) -> FfiFuture<safer_ffi::String> {
    let handle = &*handle;

    async move {
        let _enter = handle.enter();
        tokio::time::sleep(std::time::Duration::from_secs_f32(arg)).await;

        format!("slept {arg} secs").into()
    }
    .into_ffi()
}