add mutex guard for arboard funcs

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-02-23 21:57:51 +08:00
parent bb26ba3384
commit 135e0c8a99

View File

@ -52,6 +52,11 @@ lazy_static::lazy_static! {
pub static ref DEVICE_NAME: Arc<Mutex<String>> = Default::default();
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
lazy_static::lazy_static! {
static ref ARBOARD_MTX: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
}
pub fn global_init() -> bool {
#[cfg(target_os = "linux")]
{
@ -96,7 +101,11 @@ pub fn check_clipboard(
) -> Option<Message> {
let side = if old.is_none() { "host" } else { "client" };
let old = if let Some(old) = old { old } else { &CONTENT };
if let Ok(content) = ctx.get_text() {
let content = {
let _lock = ARBOARD_MTX.lock().unwrap();
ctx.get_text()
};
if let Ok(content) = content {
if content.len() < 2_000_000 && !content.is_empty() {
let changed = content != *old.lock().unwrap();
if changed {
@ -174,6 +183,7 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc<Mutex<String>>>)
let side = if old.is_none() { "host" } else { "client" };
let old = if let Some(old) = old { old } else { &CONTENT };
*old.lock().unwrap() = content.clone();
let _lock = ARBOARD_MTX.lock().unwrap();
allow_err!(ctx.set_text(content));
log::debug!("{} updated on {}", CLIPBOARD_NAME, side);
}