use std::cell::Cell;
use std::ptr;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use ffi;
thread_local! {
static IS_MAIN_THREAD: Cell<bool> = Cell::new(false)
}
static INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
macro_rules! assert_initialized_main_thread {
() => (
if !::rt::is_initialized_main_thread() {
if ::rt::is_initialized() {
panic!("GDK may only be used from the main thread.");
}
else {
panic!("GDK has not been initialized. Call `gdk::init` or `gtk::init` first.");
}
}
)
}
macro_rules! skip_assert_initialized {
() => ()
}
macro_rules! assert_not_initialized {
() => (
if ::rt::is_initialized() {
panic!("This function has to be called before `gdk::init` or `gtk::init`.");
}
)
}
macro_rules! callback_guard {
() => (
let _guard = ::glib::CallbackGuard::new();
if cfg!(debug_assertions) {
assert_initialized_main_thread!();
}
)
}
#[inline]
pub fn is_initialized() -> bool {
skip_assert_initialized!();
INITIALIZED.load(Ordering::Acquire)
}
#[inline]
pub fn is_initialized_main_thread() -> bool {
skip_assert_initialized!();
IS_MAIN_THREAD.with(|c| c.get())
}
pub unsafe fn set_initialized() {
skip_assert_initialized!();
if is_initialized_main_thread() {
return;
}
else if is_initialized() {
panic!("Attempted to initialize GDK from two different threads.");
}
INITIALIZED.store(true, Ordering::Release);
IS_MAIN_THREAD.with(|c| c.set(true));
}
pub fn init() {
assert_not_initialized!();
unsafe {
ffi::gdk_init(ptr::null_mut(), ptr::null_mut());
set_initialized();
}
}