From 5acc454de402b78647c88fec4c71d468e60fcc99 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Fri, 9 Aug 2024 10:16:38 +0200 Subject: [PATCH] Use hdf5 lock --- Cargo.toml | 6 +++--- README.md | 2 +- netcdf-sys/Cargo.toml | 2 +- netcdf-sys/src/lib.rs | 12 ++++++------ netcdf/Cargo.toml | 2 +- netcdf/src/lib.rs | 2 +- netcdf/src/rc.rs | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bf417c2..2f57afc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,9 +10,9 @@ default-members = ["netcdf", "netcdf-sys"] resolver = "2" [workspace.dependencies] -netcdf = { path = "netcdf", version = "0.10.0" } -netcdf-sys = { path = "netcdf-sys", version = "0.7.0" } +netcdf = { path = "netcdf", version = "0.10.1" } +netcdf-sys = { path = "netcdf-sys", version = "0.8.0" } netcdf-src = { path = "netcdf-src", version = "0.4.0" } netcdf-derive = { path = "netcdf-derive", version = "0.1.0" } -hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.0" } +hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.1" } mpi-sys = { version = "0.2.1" } diff --git a/README.md b/README.md index 19a77a0..92ec473 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Some examples of usage can be found in the [tests/lib.rs](netcdf/tests/lib.rs) f The `netcdf` crate is thread-safe, although the `netcdf-c` library is not itself threadsafe. To render a safe interface, a global mutex is used to serialize access to the underlying library. Consider using a non threadsafe version of `hdf5` to avoid double locking (performance consideration). -Use of `netcdf-sys` is not thread-safe. Users of this library must take care that calls do not interfere with simultaneous use of e.g. `netcdf`. Using the `hdf5-sys` library could also pose a problem, as this library is used throughout `netCDF-c` and internal state may be disrupted. +Use of `netcdf-sys` is not thread-safe. Users of this library must take care that calls do not interfere with simultaneous use of e.g. `netcdf` or `hdf5-sys`. Use the lock provided by `netcdf-sys` to serialise access to the `hdf5` and `netCDF` libraries. ## License diff --git a/netcdf-sys/Cargo.toml b/netcdf-sys/Cargo.toml index b7a8358..90a78ac 100644 --- a/netcdf-sys/Cargo.toml +++ b/netcdf-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "netcdf-sys" -version = "0.7.0" +version = "0.8.0" authors = [ "Michael Hiley ", "Magnus Ulimoen " diff --git a/netcdf-sys/src/lib.rs b/netcdf-sys/src/lib.rs index b6d383f..59e8c54 100644 --- a/netcdf-sys/src/lib.rs +++ b/netcdf-sys/src/lib.rs @@ -33,12 +33,12 @@ pub use filter::*; #[cfg(feature = "mpi")] pub mod par; -use std::sync::Mutex; - /// Global netCDF lock for using all functions in the netCDF library /// /// Per the NetCDF FAQ: "THE C-BASED LIBRARIES ARE NOT THREAD-SAFE" -pub static libnetcdf_lock: Mutex<()> = Mutex::new(()); +/// This lock is the same as the one in `hdf5`, so the two libraries +/// can be used at the same time +pub use hdf5_sys::LOCK as libnetcdf_lock; #[cfg(test)] mod tests { @@ -57,7 +57,7 @@ mod tests { let mut ncid: nc_type = -999_999; unsafe { - let _g = libnetcdf_lock.lock().unwrap(); + let _g = libnetcdf_lock.lock(); let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid); assert_eq!(err, NC_NOERR); let err = nc_close(ncid); @@ -78,7 +78,7 @@ mod tests { let mut varid: nc_type = -999_999; let mut nvars: nc_type = -999_999; unsafe { - let _g = libnetcdf_lock.lock().unwrap(); + let _g = libnetcdf_lock.lock(); let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid); assert_eq!(err, NC_NOERR); let err = nc_inq_nvars(ncid, &mut nvars); @@ -104,7 +104,7 @@ mod tests { let mut varid: nc_type = -999_999; let mut buf: Vec = vec![0; 6 * 12]; unsafe { - let _g = libnetcdf_lock.lock().unwrap(); + let _g = libnetcdf_lock.lock(); let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid); assert_eq!(err, NC_NOERR); diff --git a/netcdf/Cargo.toml b/netcdf/Cargo.toml index 40025d1..249e272 100644 --- a/netcdf/Cargo.toml +++ b/netcdf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "netcdf" -version = "0.10.0" +version = "0.10.1" authors = [ "Michael Hiley ", "Magnus Ulimoen " diff --git a/netcdf/src/lib.rs b/netcdf/src/lib.rs index 639f44b..e1aaf24 100644 --- a/netcdf/src/lib.rs +++ b/netcdf/src/lib.rs @@ -257,7 +257,7 @@ pub(crate) mod utils { /// All functions should be wrapped in this locker. Disregarding this, expect /// segfaults, especially on non-threadsafe hdf5 builds pub(crate) fn with_lock nc_type>(mut f: F) -> nc_type { - let _l = netcdf_sys::libnetcdf_lock.lock().unwrap(); + let _guard = netcdf_sys::libnetcdf_lock.lock(); f() } diff --git a/netcdf/src/rc.rs b/netcdf/src/rc.rs index 26a90ef..afb8049 100644 --- a/netcdf/src/rc.rs +++ b/netcdf/src/rc.rs @@ -36,7 +36,7 @@ pub fn get(key: &str) -> Option { } else { return None; }; - let _lock = netcdf_sys::libnetcdf_lock.lock().unwrap(); + let _lock = netcdf_sys::libnetcdf_lock.lock(); let value = unsafe { netcdf_sys::nc_rc_get(key.as_ptr()) }; NonNull::new(value).map(|inner| OwnedString { inner }) }