Start safe event call implementation

This commit is contained in:
daa84 2016-03-25 12:51:28 +03:00
parent 8e57752e97
commit e15727ff74

View File

@ -1,7 +1,46 @@
use neovim_lib::{Neovim, NeovimApi, Session};
use std::io::{Result, Error, ErrorKind};
use std::cell::UnsafeCell;
use std::thread;
use std::sync::Arc;
use rmp::Value;
use ui::Ui;
use gtk;
pub struct MainLoopMutex<T: Sized> {
data: UnsafeCell<T>,
main_thread_name: Option<String>,
}
unsafe impl<T: Sized + Send> Sync for MainLoopMutex<T> {}
impl<T> MainLoopMutex<T> {
pub fn new(t: T) -> MainLoopMutex<T> {
MainLoopMutex {
data: UnsafeCell::new(t),
main_thread_name: thread::current().name().map(|v| v.to_owned()),
}
}
// TODO: return some sort of ref guard here
pub fn get(&self) -> &mut T {
if thread::current().name().map(|v| v.to_owned()) != self.main_thread_name {
panic!("Can access value only from main thread");
}
unsafe { &mut *self.data.get() }
}
pub fn safe_call<F, I>(mutex: Arc<MainLoopMutex<I>>, cb: F)
where I: 'static,
F: Fn(&MainLoopMutex<I>) + 'static
{
gtk::idle_add(move || {
cb(&*mutex);
gtk::Continue(false)
});
}
}
pub struct Nvim {
nvim: Neovim,