|
1 | 1 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 2 | +use std::sync::LazyLock; |
2 | 3 |
|
3 | | -use lazy_static::lazy_static; |
4 | | -use parking_lot::ReentrantMutex; |
| 4 | +pub(crate) use hdf5_sys::LOCK; |
5 | 5 |
|
6 | 6 | thread_local! { |
7 | 7 | pub static SILENCED: AtomicBool = AtomicBool::new(false); |
8 | 8 | } |
9 | 9 |
|
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 | +}); |
26 | 23 |
|
27 | 24 | /// Guards the execution of the provided closure with a recursive static mutex. |
28 | 25 | pub fn sync<T, F>(func: F) -> T |
29 | 26 | where |
30 | 27 | F: FnOnce() -> T, |
31 | 28 | { |
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); |
38 | 30 | SILENCED.with(|silence| { |
39 | 31 | let is_silenced = silence.load(Ordering::Acquire); |
40 | 32 | if !is_silenced { |
|
0 commit comments