Skip to content

Commit

Permalink
Reorganize FFI code so less parts are exported public
Browse files Browse the repository at this point in the history
  • Loading branch information
bazhenov committed Dec 19, 2023
1 parent 4b44f2e commit e84b9e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
19 changes: 16 additions & 3 deletions tango-bench/src/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ impl State {
}
}

/// Global state of the benchmarking library
static mut STATE: Option<State> = None;

/// `tango_init()` implementation
///
/// This function is not exported from the library, but is used by the `tango_init()` functions
/// generated by the `tango_benchmark!()` macro.
pub unsafe fn __tango_init(benchmarks: Vec<Box<dyn MeasureTarget>>) {
if STATE.is_none() {
STATE = Some(State {
benchmarks,
selected_function: 0,
});
}
}

/// Defines all the foundation types and exported symbols for the FFI communication API between two
/// executables.
///
Expand Down Expand Up @@ -137,9 +153,6 @@ pub mod ffi {
const TANGO_FREE: FreeFn = tango_free;
}

/// Global state of the benchmarking library
pub static mut STATE: Option<State> = None;

#[no_mangle]
unsafe extern "C" fn tango_count() -> usize {
STATE.as_ref().map(|s| s.benchmarks.len()).unwrap_or(0)
Expand Down
14 changes: 4 additions & 10 deletions tango-bench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,17 @@ pub enum Error {
#[macro_export]
macro_rules! tango_benchmarks {
($($func_expr:expr),+) => {

/// Type checking tango_init() function
const TANGO_INIT: $crate::dylib::ffi::InitFn = tango_init;

/// Exported function for initializing the benchmark harness
#[no_mangle]
unsafe extern "C" fn tango_init() {
use $crate::dylib::{ffi::STATE, State};
if STATE.is_none() {
let mut benchmarks = vec![];
$(benchmarks.extend($crate::IntoBenchmarks::into_benchmarks($func_expr));)*
STATE = Some(State {
benchmarks,
selected_function: 0,
});
}
let mut benchmarks = vec![];
$(benchmarks.extend($crate::IntoBenchmarks::into_benchmarks($func_expr));)*
$crate::dylib::__tango_init(benchmarks)
}

};
}

Expand Down

0 comments on commit e84b9e9

Please sign in to comment.