libui/ffi_tools.rs
1//! Utilities to manage the state of the interface to the libUI bindings.
2use std::sync::atomic::{AtomicBool, Ordering};
3
4static INITIALIZED: AtomicBool = AtomicBool::new(false);
5
6/// Set the global flag stating that libUI is initialized.
7///
8/// # Unsafety
9/// If this is called when the library is not, in fact, initialized,
10/// the program will be placed in an undefined state.
11///
12/// # Panics
13/// Panics if called when libUI is already marked as initialized.
14pub unsafe fn set_initialized() {
15 assert!(!INITIALIZED.swap(true, Ordering::SeqCst),
16 "Tried to initialize libUI when it was already initialized. Aborting because this is an unsafe situation.");
17}
18
19/// Set the global flag stating that libUI is no longer initialized.
20///
21/// # Unsafety
22/// If this is called when the library is actually still initialized,
23/// the program could try to create a new instance, violating the library's
24/// invariants and likely causing a segfault.
25pub unsafe fn unset_initialized() {
26 INITIALIZED.store(false, Ordering::SeqCst);
27}
28
29/// Retrieve the global flag indicating whether libUI is initialized.
30pub fn is_initialized() -> bool {
31 INITIALIZED.load(Ordering::SeqCst)
32}