Skip to content

Commit

Permalink
Add blocking layer for C bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaoew1991 committed Oct 13, 2023
1 parent cd967b3 commit 1235e08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindings/c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ cbindgen = "0.25.0"
[dependencies]
bytes = "1.4.0"
opendal.workspace = true
tokio = { version = "1.27", features = ["fs", "macros", "rt-multi-thread"] }
once_cell = "1.17.1"
43 changes: 31 additions & 12 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::str::FromStr;

use ::opendal as od;
use error::opendal_error;
use once_cell::sync::Lazy;
use result::opendal_result_list;
use result::opendal_result_operator_new;
use types::opendal_blocking_lister;
Expand All @@ -47,6 +48,26 @@ use crate::types::opendal_metadata;
use crate::types::opendal_operator_options;
use crate::types::opendal_operator_ptr;

static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
});

fn build_operator(schema: od::Scheme, map: HashMap<String, String>) -> od::Result<od::Operator> {
let mut op = match od::Operator::via_map(schema, map) {
Ok(o) => o,
Err(e) => return Err(e),
};
if !op.info().full_capability().blocking {
let runtime =
tokio::runtime::Handle::try_current().unwrap_or_else(|_| RUNTIME.handle().clone());
let _guard = runtime.enter();
op = op.layer(od::layers::BlockingLayer::create().expect("blocking layer must be created"));
}
Ok(op)
}
/// \brief Construct an operator based on `scheme` and `options`
///
/// Uses an array of key-value pairs to initialize the operator based on provided `scheme`
Expand Down Expand Up @@ -121,23 +142,21 @@ pub unsafe extern "C" fn opendal_operator_new(
}
}

let op = match od::Operator::via_map(scheme, map) {
Ok(o) => o.blocking(),
match build_operator(scheme, map) {
Ok(op) => {
let op = opendal_operator_ptr::from(Box::into_raw(Box::new(op.blocking())));
opendal_result_operator_new {
operator_ptr: Box::into_raw(Box::new(op)),
error: std::ptr::null_mut(),
}
}
Err(e) => {
let e = opendal_error::from_opendal_error(e);
let result = opendal_result_operator_new {
opendal_result_operator_new {
operator_ptr: std::ptr::null_mut(),
error: Box::into_raw(Box::new(e)),
};
return result;
}
}
};

// this prevents the operator memory from being dropped by the Box
let op = opendal_operator_ptr::from(Box::into_raw(Box::new(op)));
opendal_result_operator_new {
operator_ptr: Box::into_raw(Box::new(op)),
error: std::ptr::null_mut(),
}
}

Expand Down

0 comments on commit 1235e08

Please sign in to comment.