Skip to content

Commit

Permalink
tango_api_version() added
Browse files Browse the repository at this point in the history
  • Loading branch information
bazhenov committed Jan 5, 2025
1 parent 4ddfe08 commit 6aac86e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tango-bench/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ mod paired_test {
};

let mut spi_self = Spi::for_self(mode).ok_or(Error::SpiSelfWasMoved)?;
let mut spi_lib = Spi::for_library(path, mode)?;
let mut spi_lib = Spi::for_library(&path, mode)
.with_context(|| format!("Unale to load library: {}", path.display()))?;

Check warning on line 467 in tango-bench/src/cli.rs

View workflow job for this annotation

GitHub Actions / lint

"Unale" should be "Unable".

settings.filter_outliers = filter_outliers;
settings.cache_firewall = cache_firewall;
Expand Down
14 changes: 13 additions & 1 deletion tango-bench/src/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Spi {
pub(crate) fn for_library(path: impl AsRef<Path>, mode: SpiModeKind) -> Result<Spi, Error> {
let lib = unsafe { Library::new(path.as_ref()) }
.map_err(|e| Error::UnableToLoadLibrary(path.as_ref().to_path_buf(), e))?;
Ok(spi_handle_for_vtable(ffi::VTable::new(lib).unwrap(), mode))
Ok(spi_handle_for_vtable(ffi::VTable::new(lib)?, mode))
}

pub(crate) fn for_self(mode: SpiModeKind) -> Option<Spi> {
Expand Down Expand Up @@ -346,9 +346,11 @@ pub mod ffi {
type EstimateIterationsFn = unsafe extern "C" fn(c_uint) -> c_ulonglong;
type PrepareStateFn = unsafe extern "C" fn(c_ulonglong) -> c_int;
type GetLastErrorFn = unsafe extern "C" fn(*mut *const c_char, *mut c_ulonglong) -> c_int;
type ApiVersionFn = unsafe extern "C" fn() -> c_uint;
type FreeFn = unsafe extern "C" fn();

pub(super) static SELF_VTABLE: Mutex<Option<VTable>> = Mutex::new(Some(VTable::for_self()));
pub const TANGO_API_VERSION: u32 = 3;

#[no_mangle]
unsafe extern "C" fn tango_count() -> c_ulonglong {
Expand All @@ -358,6 +360,11 @@ pub mod ffi {
.unwrap_or(0)
}

#[no_mangle]
unsafe extern "C" fn tango_api_version() -> c_uint {
TANGO_API_VERSION
}

#[no_mangle]
unsafe extern "C" fn tango_select(idx: c_ulonglong) {
if let Some(s) = STATE.as_mut() {
Expand Down Expand Up @@ -506,6 +513,11 @@ pub mod ffi {

impl VTable {
pub(super) fn new(lib: Library) -> Result<Self, Error> {
let api_version_fn = *lookup_symbol::<ApiVersionFn>(&lib, "tango_api_version")?;
let api_version = unsafe { (api_version_fn)() };
if api_version != TANGO_API_VERSION {
return Err(Error::IncorrectVersion(api_version));
}
Ok(Self {
init_fn: *lookup_symbol(&lib, "tango_init")?,
count_fn: *lookup_symbol(&lib, "tango_count")?,
Expand Down
7 changes: 7 additions & 0 deletions tango-bench/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "async")]
pub use asynchronous::async_benchmark_fn;
use core::ptr;
use dylib::ffi::TANGO_API_VERSION;
use num_traits::ToPrimitive;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use std::{
Expand Down Expand Up @@ -51,6 +52,12 @@ pub enum Error {

#[error("Unknown FFI Error")]
UnknownFFIError,

#[error(
"Non matching tango version. Expected: {}, got: {0}",
TANGO_API_VERSION
)]
IncorrectVersion(u32),
}

/// Registers benchmark in the system
Expand Down

0 comments on commit 6aac86e

Please sign in to comment.