gdkx11/rt.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::cell::Cell;
4use std::sync::atomic::{AtomicBool, Ordering};
5
6thread_local! {
7 static IS_MAIN_THREAD: Cell<bool> = Cell::new(false)
8}
9
10static INITIALIZED: AtomicBool = AtomicBool::new(false);
11
12macro_rules! assert_initialized_main_thread {
13 () => {
14 if !crate::rt::is_initialized_main_thread() {
15 if crate::rt::is_initialized() {
16 panic!("GDK may only be used from the main thread.");
17 } else {
18 panic!("GDK has not been initialized. Call `gdk::init` or `gtk::init` first.");
19 }
20 }
21 };
22}
23
24/// No-op.
25macro_rules! skip_assert_initialized {
26 () => {};
27}
28
29/// Returns `true` if GDK has been initialized.
30#[inline]
31pub fn is_initialized() -> bool {
32 skip_assert_initialized!();
33 INITIALIZED.load(Ordering::Acquire)
34}
35
36/// Returns `true` if GDK has been initialized and this is the main thread.
37#[inline]
38pub fn is_initialized_main_thread() -> bool {
39 skip_assert_initialized!();
40 IS_MAIN_THREAD.with(|c| c.get())
41}