Skip to content

Commit

Permalink
Merge pull request #121 from magnusuMET/feature/del_lazy_static
Browse files Browse the repository at this point in the history
Move lock to `netcdf-sys` and remove `lazy_static`
  • Loading branch information
magnusuMET authored Dec 2, 2023
2 parents 3d4ae03 + 5b37b3c commit 7afd544
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
3 changes: 1 addition & 2 deletions netcdf-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ categories = ["ffi", "filesystem", "science"]
exclude = [
"testdata/**",
]
rust-version = "1.60"
rust-version = "1.70"

[dependencies]
libz-sys = { version = "1.0.25" }
Expand All @@ -26,7 +26,6 @@ hdf5-sys = { version = "0.8.0" }
netcdf-src = { path = "../netcdf-src", version = "0.3.0", optional = true }

[dev-dependencies]
lazy_static = "1.4.0"

[features]
default = []
Expand Down
18 changes: 7 additions & 11 deletions netcdf-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,20 @@ mod filter;
#[cfg(feature = "4.8.0")]
pub use filter::*;

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(());

#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::ffi;
use std::path;

use lazy_static::lazy_static;
use std::sync::Mutex;

// Per the NetCDF FAQ, "THE C-BASED LIBRARIES ARE NOT THREAD-SAFE"
// So, here is our global mutex.
// Use lazy-static dependency to avoid use of static_mutex feature which
// breaks compatibility with stable channel.
lazy_static! {
pub static ref libnetcdf_lock: Mutex<()> = Mutex::new(());
}

#[test]
fn test_nc_open_close() {
let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down
1 change: 0 additions & 1 deletion netcdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ default = ["ndarray"]
static = ["netcdf-sys/static"]

[dependencies]
lazy_static = "1.4.0"
ndarray = { version = "0.15", optional = true }
netcdf-sys = { path = "../netcdf-sys", version = "0.5.0" }
bitflags = "1.2.1"
Expand Down
9 changes: 1 addition & 8 deletions netcdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@
#![allow(clippy::wildcard_imports)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

use lazy_static::lazy_static;
use netcdf_sys::nc_type;
use std::sync::Mutex;

pub mod attribute;
pub mod dimension;
Expand Down Expand Up @@ -162,15 +160,10 @@ pub fn open_mem<'a>(name: Option<&str>, mem: &'a [u8]) -> error::Result<MemFile<
RawFile::open_from_memory(name, mem)
}

lazy_static! {
/// Use this when accessing `netCDF` functions
pub(crate) static ref LOCK: Mutex<()> = Mutex::new(());
}

/// 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 = LOCK.lock().unwrap();
let _l = netcdf_sys::libnetcdf_lock.lock().unwrap();
f()
}

Expand Down

0 comments on commit 7afd544

Please sign in to comment.