Skip to content

Commit

Permalink
fix sending images in quick succession
Browse files Browse the repository at this point in the history
  • Loading branch information
LGFae committed Jul 12, 2023
1 parent 481fefc commit 48ef2ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
29 changes: 18 additions & 11 deletions daemon/src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
num::NonZeroI32,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex, MutexGuard,
Arc, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
},
};

Expand Down Expand Up @@ -55,7 +55,7 @@ struct WallpaperInner {

pub struct Wallpaper {
output_id: u32,
inner: Mutex<WallpaperInner>,
inner: RwLock<WallpaperInner>,
layer_surface: LayerSurface,

animation_state: AnimationState,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Wallpaper {
output_id: output_info.id,
layer_surface,
pool,
inner: Mutex::new(WallpaperInner {
inner: RwLock::new(WallpaperInner {
width,
height,
scale_factor,
Expand Down Expand Up @@ -149,19 +149,26 @@ impl Wallpaper {
}

#[inline]
fn lock(&self) -> (MutexGuard<WallpaperInner>, MutexGuard<SlotPool>) {
(self.inner.lock().unwrap(), self.pool.lock().unwrap())
fn lock(&self) -> (RwLockReadGuard<WallpaperInner>, MutexGuard<SlotPool>) {
(self.inner.read().unwrap(), self.pool.lock().unwrap())
}

#[inline]
fn lock_inner_mut(&self) -> (RwLockWriteGuard<WallpaperInner>, MutexGuard<SlotPool>) {
(self.inner.write().unwrap(), self.pool.lock().unwrap())
}

pub fn canvas_change<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut [u8]) -> T,
{
loop {
let (inner, mut pool) = self.lock();
if let Some(canvas) = inner.slot.canvas(&mut pool) {
log::debug!("got canvas! - output {}", self.output_id);
return f(canvas);
{
let (inner, mut pool) = self.lock();
if let Some(canvas) = inner.slot.canvas(&mut pool) {
log::debug!("got canvas! - output {}", self.output_id);
return f(canvas);
}
}
log::debug!("failed to get canvas - output {}", self.output_id);
// sleep to mitigate busy waiting
Expand Down Expand Up @@ -204,7 +211,7 @@ impl Wallpaper {

pub fn set_img_info(&self, img_info: BgImg) {
log::debug!("output {} - drawing: {}", self.output_id, img_info);
self.lock().0.img = img_info;
self.lock_inner_mut().0.img = img_info;
}

pub fn draw(&self) {
Expand Down Expand Up @@ -233,7 +240,7 @@ impl Wallpaper {
if let Some(s) = scale_factor {
self.layer_surface.set_buffer_scale(s.get() as u32).unwrap();
}
let (mut inner, mut pool) = self.lock();
let (mut inner, mut pool) = self.lock_inner_mut();
let width = width.unwrap_or(inner.width);
let height = height.unwrap_or(inner.height);
let scale_factor = scale_factor.unwrap_or(inner.scale_factor);
Expand Down
16 changes: 14 additions & 2 deletions utils/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,20 @@ impl Answer {
pub fn read_socket(stream: &UnixStream) -> Result<Vec<u8>, String> {
let mut reader = BufReader::new(stream);
let mut buf = vec![0; 8];
if let Err(e) = reader.read_exact(&mut buf[0..std::mem::size_of::<usize>()]) {
return Err(format!("failed to read serialized length: {e}"));

let mut tries = 0;
loop {
match reader.read_exact(&mut buf[0..std::mem::size_of::<usize>()]) {
Ok(()) => break,
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock && tries < 5 {
std::thread::sleep(Duration::from_millis(1));
} else {
return Err(format!("failed to read serialized length: {e}"));
}
}
}
tries += 1;
}
let len = usize::from_ne_bytes(buf[0..std::mem::size_of::<usize>()].try_into().unwrap());
buf.clear();
Expand Down

0 comments on commit 48ef2ef

Please sign in to comment.