Skip to content

Commit 2802d9d

Browse files
authored
Merge pull request #6 from magnusuMET/feature/hdf5-sys-lock
hdf5-sys public lock
2 parents a07b868 + 6e07f83 commit 2802d9d

File tree

8 files changed

+29
-31
lines changed

8 files changed

+29
-31
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ jobs:
279279
with: {submodules: true}
280280
- name: Install Rust
281281
uses: dtolnay/rust-toolchain@stable
282-
with: {toolchain: "1.77"}
282+
with: {toolchain: "1.80"}
283283
- name: Build and test all crates
284284
run:
285285
cargo test --workspace -vv --features=hdf5-sys/static,hdf5-sys/zlib --exclude=hdf5-metno-derive

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ members = ["hdf5", "hdf5-types", "hdf5-derive", "hdf5-sys", "hdf5-src"]
44
default-members = ["hdf5", "hdf5-types", "hdf5-derive", "hdf5-sys"]
55

66
[workspace.package]
7-
version = "0.9.0" # !V
8-
rust-version = "1.77.0"
7+
version = "0.9.1" # !V
8+
rust-version = "1.80.0"
99
authors = [
1010
"Ivan Smirnov <[email protected]>",
1111
"Magnus Ulimoen <[email protected]>",
@@ -29,5 +29,5 @@ regex = "1.10"
2929
hdf5 = { package = "hdf5-metno", version = "0.9.0", path = "hdf5" } # !V
3030
hdf5-derive = { package = "hdf5-metno-derive", version = "0.9.0", path = "hdf5-derive" } # !V
3131
hdf5-src = { package = "hdf5-metno-src", version = "0.9.0", path = "hdf5-src" } # !V
32-
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.0", path = "hdf5-sys" } # !V
32+
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.1", path = "hdf5-sys" } # !V
3333
hdf5-types = { package = "hdf5-metno-types", version = "0.9.0", path = "hdf5-types" } # !V

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ toolchains; macOS Catalina).
118118
### Rust
119119

120120
`hdf5` crate is tested continuously for all three official release channels, and
121-
requires a reasonably recent Rust compiler (e.g. of version 1.77 or newer).
121+
requires a reasonably recent Rust compiler (e.g. of version 1.80 or newer).
122122

123123
### HDF5
124124

hdf5-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ libc = { workspace = true }
1919
mpi-sys = { workspace = true, optional = true }
2020
libz-sys = { workspace = true, optional = true }
2121
hdf5-src = { workspace = true, optional = true }
22+
parking_lot = "0.12.3"
2223

2324
# Please see README for further explanation of these feature flags
2425
[features]

hdf5-sys/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ mod internal_prelude {
6565
};
6666
}
6767

68+
use parking_lot::ReentrantMutex;
69+
/// Lock which can be used to serialise access to the hdf5 library
70+
pub static LOCK: ReentrantMutex<()> = ReentrantMutex::new(());
71+
6872
#[cfg(test)]
6973
mod tests {
7074
use super::h5::H5open;

hdf5/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ libc = { workspace = true }
4646
lzf-sys = { version = "0.1", optional = true }
4747
mpi-sys = { workspace = true, optional = true }
4848
ndarray = "0.15"
49-
parking_lot = "0.12"
5049
paste = "1.0"
5150
# internal
5251
hdf5-derive = { workspace = true }
@@ -56,6 +55,7 @@ hdf5-types = { workspace = true }
5655
[dev-dependencies]
5756
half = { workspace = true }
5857
num-complex = { workspace = true }
58+
parking_lot = "0.12.3"
5959
paste = "1.0"
6060
pretty_assertions = "1.4"
6161
rand = { version = "0.8", features = ["small_rng"] }

hdf5/src/globals.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22

33
use std::mem;
4+
use std::sync::LazyLock;
45

56
use lazy_static::lazy_static;
67

@@ -24,7 +25,7 @@ pub struct H5GlobalConstant(
2425
impl std::ops::Deref for H5GlobalConstant {
2526
type Target = hdf5_sys::h5i::hid_t;
2627
fn deref(&self) -> &Self::Target {
27-
lazy_static::initialize(&crate::sync::LIBRARY_INIT);
28+
LazyLock::force(&crate::sync::LIBRARY_INIT);
2829
cfg_if::cfg_if! {
2930
if #[cfg(msvc_dll_indirection)] {
3031
let dll_ptr = self.0 as *const usize;

hdf5/src/sync.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
use std::sync::atomic::{AtomicBool, Ordering};
2+
use std::sync::LazyLock;
23

3-
use lazy_static::lazy_static;
4-
use parking_lot::ReentrantMutex;
4+
pub(crate) use hdf5_sys::LOCK;
55

66
thread_local! {
77
pub static SILENCED: AtomicBool = AtomicBool::new(false);
88
}
99

10-
lazy_static! {
11-
pub(crate) static ref LIBRARY_INIT: () = {
12-
// No functions called here must try to create the LOCK,
13-
// as this could cause a deadlock in initialisation
14-
unsafe {
15-
// Ensure hdf5 does not invalidate handles which might
16-
// still be live on other threads on program exit
17-
::hdf5_sys::h5::H5dont_atexit();
18-
::hdf5_sys::h5::H5open();
19-
// Ignore errors on stdout
20-
crate::error::silence_errors_no_sync(true);
21-
// Register filters lzf/blosc if available
22-
crate::hl::filters::register_filters();
23-
}
24-
};
25-
}
10+
pub(crate) static LIBRARY_INIT: LazyLock<()> = LazyLock::new(|| {
11+
let _guard = hdf5_sys::LOCK.lock();
12+
unsafe {
13+
// Ensure hdf5 does not invalidate handles which might
14+
// still be live on other threads on program exit
15+
::hdf5_sys::h5::H5dont_atexit();
16+
::hdf5_sys::h5::H5open();
17+
// Ignore errors on stdout
18+
crate::error::silence_errors_no_sync(true);
19+
// Register filters lzf/blosc if available
20+
crate::hl::filters::register_filters();
21+
}
22+
});
2623

2724
/// Guards the execution of the provided closure with a recursive static mutex.
2825
pub fn sync<T, F>(func: F) -> T
2926
where
3027
F: FnOnce() -> T,
3128
{
32-
lazy_static! {
33-
static ref LOCK: ReentrantMutex<()> = {
34-
lazy_static::initialize(&LIBRARY_INIT);
35-
ReentrantMutex::new(())
36-
};
37-
}
29+
let _ = LazyLock::force(&LIBRARY_INIT);
3830
SILENCED.with(|silence| {
3931
let is_silenced = silence.load(Ordering::Acquire);
4032
if !is_silenced {

0 commit comments

Comments
 (0)