Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hdf5-sys public lock #6

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ jobs:
with: {submodules: true}
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with: {toolchain: "1.77"}
with: {toolchain: "1.80"}
- name: Build and test all crates
run:
cargo test --workspace -vv --features=hdf5-sys/static,hdf5-sys/zlib --exclude=hdf5-metno-derive
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ members = ["hdf5", "hdf5-types", "hdf5-derive", "hdf5-sys", "hdf5-src"]
default-members = ["hdf5", "hdf5-types", "hdf5-derive", "hdf5-sys"]

[workspace.package]
version = "0.9.0" # !V
rust-version = "1.77.0"
version = "0.9.1" # !V
rust-version = "1.80.0"
authors = [
"Ivan Smirnov <[email protected]>",
"Magnus Ulimoen <[email protected]>",
Expand All @@ -29,5 +29,5 @@ regex = "1.10"
hdf5 = { package = "hdf5-metno", version = "0.9.0", path = "hdf5" } # !V
hdf5-derive = { package = "hdf5-metno-derive", version = "0.9.0", path = "hdf5-derive" } # !V
hdf5-src = { package = "hdf5-metno-src", version = "0.9.0", path = "hdf5-src" } # !V
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.0", path = "hdf5-sys" } # !V
hdf5-sys = { package = "hdf5-metno-sys", version = "0.9.1", path = "hdf5-sys" } # !V
hdf5-types = { package = "hdf5-metno-types", version = "0.9.0", path = "hdf5-types" } # !V
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ toolchains; macOS Catalina).
### Rust

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

### HDF5

Expand Down
1 change: 1 addition & 0 deletions hdf5-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libc = { workspace = true }
mpi-sys = { workspace = true, optional = true }
libz-sys = { workspace = true, optional = true }
hdf5-src = { workspace = true, optional = true }
parking_lot = "0.12.3"

# Please see README for further explanation of these feature flags
[features]
Expand Down
4 changes: 4 additions & 0 deletions hdf5-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ mod internal_prelude {
};
}

use parking_lot::ReentrantMutex;
/// Lock which can be used to serialise access to the hdf5 library
pub static LOCK: ReentrantMutex<()> = ReentrantMutex::new(());

#[cfg(test)]
mod tests {
use super::h5::H5open;
Expand Down
2 changes: 1 addition & 1 deletion hdf5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ libc = { workspace = true }
lzf-sys = { version = "0.1", optional = true }
mpi-sys = { workspace = true, optional = true }
ndarray = "0.15"
parking_lot = "0.12"
paste = "1.0"
# internal
hdf5-derive = { workspace = true }
Expand All @@ -56,6 +55,7 @@ hdf5-types = { workspace = true }
[dev-dependencies]
half = { workspace = true }
num-complex = { workspace = true }
parking_lot = "0.12.3"
paste = "1.0"
pretty_assertions = "1.4"
rand = { version = "0.8", features = ["small_rng"] }
Expand Down
3 changes: 2 additions & 1 deletion hdf5/src/globals.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]

use std::mem;
use std::sync::LazyLock;

use lazy_static::lazy_static;

Expand All @@ -24,7 +25,7 @@ pub struct H5GlobalConstant(
impl std::ops::Deref for H5GlobalConstant {
type Target = hdf5_sys::h5i::hid_t;
fn deref(&self) -> &Self::Target {
lazy_static::initialize(&crate::sync::LIBRARY_INIT);
LazyLock::force(&crate::sync::LIBRARY_INIT);
cfg_if::cfg_if! {
if #[cfg(msvc_dll_indirection)] {
let dll_ptr = self.0 as *const usize;
Expand Down
40 changes: 16 additions & 24 deletions hdf5/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::LazyLock;

use lazy_static::lazy_static;
use parking_lot::ReentrantMutex;
pub(crate) use hdf5_sys::LOCK;

thread_local! {
pub static SILENCED: AtomicBool = AtomicBool::new(false);
}

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

/// Guards the execution of the provided closure with a recursive static mutex.
pub fn sync<T, F>(func: F) -> T
where
F: FnOnce() -> T,
{
lazy_static! {
static ref LOCK: ReentrantMutex<()> = {
lazy_static::initialize(&LIBRARY_INIT);
ReentrantMutex::new(())
};
}
let _ = LazyLock::force(&LIBRARY_INIT);
SILENCED.with(|silence| {
let is_silenced = silence.load(Ordering::Acquire);
if !is_silenced {
Expand Down
Loading