diff --git a/src/main.rs b/src/main.rs index e65c711..1a45193 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate cairo; extern crate neovim_lib; extern crate rmp; +mod ui_mutex; mod nvim; mod ui_model; mod ui; diff --git a/src/nvim.rs b/src/nvim.rs index af1ef9e..2aa733e 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -1,46 +1,8 @@ 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 { - data: UnsafeCell, - main_thread_name: Option, -} - -unsafe impl Sync for MainLoopMutex {} - -impl MainLoopMutex { - pub fn new(t: T) -> MainLoopMutex { - 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(mutex: Arc>, cb: F) - where I: 'static, - F: Fn(&MainLoopMutex) + 'static - { - gtk::idle_add(move || { - cb(&*mutex); - gtk::Continue(false) - }); - } -} pub struct Nvim { nvim: Neovim, @@ -52,7 +14,7 @@ pub trait RedrawEvents { impl Nvim { pub fn start(mut ui: Ui) -> Result { // let mut session = try!(Session::new_tcp("127.0.0.1:6666")); - let mut session = if cfg!(target_os = "windows") { + let session = if cfg!(target_os = "windows") { Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap() } else { Session::new_child().unwrap() diff --git a/src/ui_mutex.rs b/src/ui_mutex.rs new file mode 100644 index 0000000..25b0ba2 --- /dev/null +++ b/src/ui_mutex.rs @@ -0,0 +1,43 @@ + +use std::cell::{RefCell, RefMut}; +use std::thread; +use std::sync::Arc; +use gtk; + +pub struct MainLoopMutex { + data: RefCell, + main_thread_name: Option, +} + +// here sync used to mark that internal data is acessed only from main thread +// this behaviour works because of borrow_mut check thread name +unsafe impl Sync for MainLoopMutex {} + +impl MainLoopMutex { + pub fn new(t: T) -> MainLoopMutex { + MainLoopMutex { + data: RefCell::new(t), + main_thread_name: thread::current().name().map(|v| v.to_owned()), + } + } + + pub fn borrow_mut(&self) -> RefMut { + if thread::current().name().map(|v| v.to_owned()) != self.main_thread_name { + panic!("Can access value only from main thread"); + } + + self.data.borrow_mut() + } + + pub fn safe_call(mutex: Arc>, cb: F) + where I: 'static, + F: Fn(&MainLoopMutex) + 'static + { + gtk::idle_add(move || { + cb(&*mutex); + gtk::Continue(false) + }); + } +} + +