Skip to content

Commit

Permalink
Merge pull request #151 from magnusuMET/feature/hdf5-lock
Browse files Browse the repository at this point in the history
Use hdf5 lock
  • Loading branch information
magnusuMET authored Aug 9, 2024
2 parents 15e3d70 + 8f9c9e8 commit 35a673a
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 762 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
3 changes: 2 additions & 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 All @@ -25,6 +25,7 @@ curl-sys = { version = "0.4.51", optional = true }
hdf5-sys = { workspace = true }
netcdf-src = { workspace = true, optional = true }
mpi-sys = { workspace = true, optional = true }
parking_lot = "0.12.3"

[dev-dependencies]

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
3 changes: 2 additions & 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 Expand Up @@ -30,6 +30,7 @@ netcdf-derive = { workspace = true, optional = true }
bitflags = "2.4.2"
libc = "0.2.155"
mpi-sys = { workspace = true, optional = true }
parking_lot = "0.12.3"

[dev-dependencies]
clap = { version = "4.5.1", features = ["derive"] }
Expand Down
Loading

0 comments on commit 35a673a

Please sign in to comment.