Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add blocking layer for C bindings #3278

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading