h-gtk/utils: Refactor some mutex locks, improve formatting.

This commit is contained in:
Jordan Petridis 2018-03-30 14:39:30 +03:00
parent c338802329
commit f21398357b
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6

View File

@ -34,22 +34,25 @@ lazy_static! {
static ref IGNORESHOWS: Arc<Mutex<HashSet<i32>>> = Arc::new(Mutex::new(HashSet::new())); static ref IGNORESHOWS: Arc<Mutex<HashSet<i32>>> = Arc::new(Mutex::new(HashSet::new()));
} }
pub fn ignore_show(id: i32) -> Result<(), Error> { pub fn ignore_show(id: i32) -> Result<bool, Error> {
let mut guard = IGNORESHOWS.lock().map_err(|err| format_err!("{}", err))?; IGNORESHOWS
guard.insert(id); .lock()
Ok(()) .map(|mut guard| guard.insert(id))
.map_err(|err| format_err!("{}", err))
} }
pub fn uningore_show(id: i32) -> Result<(), Error> { pub fn uningore_show(id: i32) -> Result<bool, Error> {
let mut guard = IGNORESHOWS.lock().map_err(|err| format_err!("{}", err))?; IGNORESHOWS
guard.remove(&id); .lock()
Ok(()) .map(|mut guard| guard.remove(&id))
.map_err(|err| format_err!("{}", err))
} }
pub fn get_ignored_shows() -> Result<Vec<i32>, Error> { pub fn get_ignored_shows() -> Result<Vec<i32>, Error> {
let guard = IGNORESHOWS.lock().map_err(|err| format_err!("{}", err))?; IGNORESHOWS
let keys = guard.iter().cloned().collect::<Vec<_>>(); .lock()
Ok(keys) .map(|guard| guard.iter().cloned().collect::<Vec<_>>())
.map_err(|err| format_err!("{}", err))
} }
pub fn cleanup(cleanup_date: DateTime<Utc>) { pub fn cleanup(cleanup_date: DateTime<Utc>) {
@ -170,13 +173,18 @@ pub fn set_image_from_path(
let hashmap = CACHED_PIXBUFS let hashmap = CACHED_PIXBUFS
.read() .read()
.map_err(|err| format_err!("Pixbuf HashMap: {}", err))?; .map_err(|err| format_err!("Pixbuf HashMap: {}", err))?;
if let Some(px) = hashmap.get(&(pd.id(), size)) { if let Some(guard) = hashmap.get(&(pd.id(), size)) {
let m = px.lock() guard
.map_err(|err| format_err!("SendCell Mutex: {}", err))?; .lock()
let px = m.try_get().ok_or_else(|| { .map_err(|err| format_err!("SendCell Mutex: {}", err))
.and_then(|sendcell| {
sendcell
.try_get()
.map(|px| image.set_from_pixbuf(px))
.ok_or_else(|| {
format_err!("Pixbuf was accessed from a different thread than created") format_err!("Pixbuf was accessed from a different thread than created")
})
})?; })?;
image.set_from_pixbuf(px);
return Ok(()); return Ok(());
} }
} }
@ -184,17 +192,15 @@ pub fn set_image_from_path(
let (sender, receiver) = channel(); let (sender, receiver) = channel();
let pd_ = pd.clone(); let pd_ = pd.clone();
THREADPOOL.spawn(move || { THREADPOOL.spawn(move || {
{
if let Ok(mut guard) = COVER_DL_REGISTRY.write() { if let Ok(mut guard) = COVER_DL_REGISTRY.write() {
guard.insert(pd_.id()); guard.insert(pd_.id());
} }
}
let _ = sender.send(downloader::cache_image(&pd_)); let _ = sender.send(downloader::cache_image(&pd_));
{
if let Ok(mut guard) = COVER_DL_REGISTRY.write() { if let Ok(mut guard) = COVER_DL_REGISTRY.write() {
guard.remove(&pd_.id()); guard.remove(&pd_.id());
} }
}
}); });
let image = image.clone(); let image = image.clone();