Skip to content

Commit

Permalink
Use hdf5 lock
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusuMET committed Aug 9, 2024
1 parent 15e3d70 commit 5acc454
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion netcdf-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netcdf-sys"
version = "0.7.0"
version = "0.8.0"
authors = [
"Michael Hiley <[email protected]>",
"Magnus Ulimoen <[email protected]>"
Expand Down
12 changes: 6 additions & 6 deletions netcdf-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -104,7 +104,7 @@ mod tests {
let mut varid: nc_type = -999_999;
let mut buf: Vec<nc_type> = 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);

Expand Down
2 changes: 1 addition & 1 deletion netcdf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netcdf"
version = "0.10.0"
version = "0.10.1"
authors = [
"Michael Hiley <[email protected]>",
"Magnus Ulimoen <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion netcdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: FnMut() -> nc_type>(mut f: F) -> nc_type {
let _l = netcdf_sys::libnetcdf_lock.lock().unwrap();
let _guard = netcdf_sys::libnetcdf_lock.lock();
f()
}

Expand Down
2 changes: 1 addition & 1 deletion netcdf/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn get(key: &str) -> Option<OwnedString> {
} 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 })
}

0 comments on commit 5acc454

Please sign in to comment.