Skip to content

Commit 35a673a

Browse files
authored
Merge pull request #151 from magnusuMET/feature/hdf5-lock
Use hdf5 lock
2 parents 15e3d70 + 8f9c9e8 commit 35a673a

14 files changed

+585
-762
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ default-members = ["netcdf", "netcdf-sys"]
1010
resolver = "2"
1111

1212
[workspace.dependencies]
13-
netcdf = { path = "netcdf", version = "0.10.0" }
14-
netcdf-sys = { path = "netcdf-sys", version = "0.7.0" }
13+
netcdf = { path = "netcdf", version = "0.10.1" }
14+
netcdf-sys = { path = "netcdf-sys", version = "0.8.0" }
1515
netcdf-src = { path = "netcdf-src", version = "0.4.0" }
1616
netcdf-derive = { path = "netcdf-derive", version = "0.1.0" }
17-
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.0" }
17+
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.1" }
1818
mpi-sys = { version = "0.2.1" }

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Some examples of usage can be found in the [tests/lib.rs](netcdf/tests/lib.rs) f
5252

5353
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).
5454

55-
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.
55+
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.
5656

5757
## License
5858

netcdf-sys/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netcdf-sys"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors = [
55
"Michael Hiley <[email protected]>",
66
"Magnus Ulimoen <[email protected]>"
@@ -25,6 +25,7 @@ curl-sys = { version = "0.4.51", optional = true }
2525
hdf5-sys = { workspace = true }
2626
netcdf-src = { workspace = true, optional = true }
2727
mpi-sys = { workspace = true, optional = true }
28+
parking_lot = "0.12.3"
2829

2930
[dev-dependencies]
3031

netcdf-sys/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ pub use filter::*;
3333
#[cfg(feature = "mpi")]
3434
pub mod par;
3535

36-
use std::sync::Mutex;
37-
3836
/// Global netCDF lock for using all functions in the netCDF library
3937
///
4038
/// Per the NetCDF FAQ: "THE C-BASED LIBRARIES ARE NOT THREAD-SAFE"
41-
pub static libnetcdf_lock: Mutex<()> = Mutex::new(());
39+
/// This lock is the same as the one in `hdf5`, so the two libraries
40+
/// can be used at the same time
41+
pub use hdf5_sys::LOCK as libnetcdf_lock;
4242

4343
#[cfg(test)]
4444
mod tests {
@@ -57,7 +57,7 @@ mod tests {
5757

5858
let mut ncid: nc_type = -999_999;
5959
unsafe {
60-
let _g = libnetcdf_lock.lock().unwrap();
60+
let _g = libnetcdf_lock.lock();
6161
let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid);
6262
assert_eq!(err, NC_NOERR);
6363
let err = nc_close(ncid);
@@ -78,7 +78,7 @@ mod tests {
7878
let mut varid: nc_type = -999_999;
7979
let mut nvars: nc_type = -999_999;
8080
unsafe {
81-
let _g = libnetcdf_lock.lock().unwrap();
81+
let _g = libnetcdf_lock.lock();
8282
let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid);
8383
assert_eq!(err, NC_NOERR);
8484
let err = nc_inq_nvars(ncid, &mut nvars);
@@ -104,7 +104,7 @@ mod tests {
104104
let mut varid: nc_type = -999_999;
105105
let mut buf: Vec<nc_type> = vec![0; 6 * 12];
106106
unsafe {
107-
let _g = libnetcdf_lock.lock().unwrap();
107+
let _g = libnetcdf_lock.lock();
108108
let err = nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid);
109109
assert_eq!(err, NC_NOERR);
110110

netcdf/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netcdf"
3-
version = "0.10.0"
3+
version = "0.10.1"
44
authors = [
55
"Michael Hiley <[email protected]>",
66
"Magnus Ulimoen <[email protected]>"
@@ -30,6 +30,7 @@ netcdf-derive = { workspace = true, optional = true }
3030
bitflags = "2.4.2"
3131
libc = "0.2.155"
3232
mpi-sys = { workspace = true, optional = true }
33+
parking_lot = "0.12.3"
3334

3435
[dev-dependencies]
3536
clap = { version = "4.5.1", features = ["derive"] }

0 commit comments

Comments
 (0)