1use gdk_sys;
8use std::cell::Cell;
9use std::ptr;
10use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
11
12thread_local! {
13 static IS_MAIN_THREAD: Cell<bool> = Cell::new(false)
14}
15
16static INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
17
18macro_rules! assert_initialized_main_thread {
20 () => {
21 if !::rt::is_initialized_main_thread() {
22 if ::rt::is_initialized() {
23 panic!("GDK may only be used from the main thread.");
24 } else {
25 panic!("GDK has not been initialized. Call `gdk::init` or `gtk::init` first.");
26 }
27 }
28 };
29}
30
31macro_rules! skip_assert_initialized {
33 () => {};
34}
35
36macro_rules! assert_not_initialized {
38 () => {
39 if ::rt::is_initialized() {
40 panic!("This function has to be called before `gdk::init` or `gtk::init`.");
41 }
42 };
43}
44
45#[inline]
47pub fn is_initialized() -> bool {
48 skip_assert_initialized!();
49 INITIALIZED.load(Ordering::Acquire)
50}
51
52#[inline]
54pub fn is_initialized_main_thread() -> bool {
55 skip_assert_initialized!();
56 IS_MAIN_THREAD.with(|c| c.get())
57}
58
59pub unsafe fn set_initialized() {
61 skip_assert_initialized!();
62 if is_initialized_main_thread() {
63 return;
64 } else if is_initialized() {
65 panic!("Attempted to initialize GDK from two different threads.");
66 }
67 INITIALIZED.store(true, Ordering::Release);
68 IS_MAIN_THREAD.with(|c| c.set(true));
69}
70
71pub fn init() {
72 assert_not_initialized!();
73 unsafe {
74 gdk_sys::gdk_init(ptr::null_mut(), ptr::null_mut());
75 set_initialized();
76 }
77}