From 1595256c86bcba450167f4cd53c22cbab0d3ec4a Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 30 Mar 2018 09:31:25 +0300 Subject: [PATCH] Use rayon to manage all the threads. --- hammond-gtk/src/app.rs | 4 ++-- hammond-gtk/src/manager.rs | 5 +++-- hammond-gtk/src/utils.rs | 4 +--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/hammond-gtk/src/app.rs b/hammond-gtk/src/app.rs index 06a5602..338bbdf 100644 --- a/hammond-gtk/src/app.rs +++ b/hammond-gtk/src/app.rs @@ -7,6 +7,7 @@ use gtk::SettingsExt as GtkSettingsExt; use gtk::prelude::*; use failure::Error; +use rayon; use hammond_data::{Podcast, Source}; use hammond_data::utils::delete_show; @@ -20,7 +21,6 @@ use widgets::mark_all_watched; use std::sync::Arc; use std::sync::mpsc::{channel, Receiver, Sender}; -use std::thread; use std::time::Duration; #[derive(Clone, Debug)] @@ -222,7 +222,7 @@ impl App { } // Spawn a thread so it won't block the ui. - thread::spawn(clone!(pd => move || { + rayon::spawn(clone!(pd => move || { if let Err(err) = delete_show(&pd) { error!("Something went wrong trying to remove {}", pd.title()); error!("Error: {}", err); diff --git a/hammond-gtk/src/manager.rs b/hammond-gtk/src/manager.rs index c4ead04..b003494 100644 --- a/hammond-gtk/src/manager.rs +++ b/hammond-gtk/src/manager.rs @@ -1,4 +1,5 @@ use failure::Error; +use rayon; // use hammond_data::Episode; use hammond_data::dbqueries; @@ -11,7 +12,6 @@ use std::sync::{Arc, Mutex, RwLock}; use std::sync::mpsc::Sender; // use std::sync::atomic::AtomicUsize; // use std::path::PathBuf; -use std::thread; // This is messy, undocumented and hacky af. // I am terrible at writting downloaders and download managers. @@ -75,6 +75,7 @@ impl DownloadProgress for Progress { lazy_static! { pub static ref ACTIVE_DOWNLOADS: Arc>>>> = { Arc::new(RwLock::new(HashMap::new())) }; + static ref DLPOOL: rayon::ThreadPool = rayon::ThreadPoolBuilder::new().build().unwrap(); } pub fn add(id: i32, directory: &str, sender: Sender) -> Result<(), Error> { @@ -89,7 +90,7 @@ pub fn add(id: i32, directory: &str, sender: Sender) -> Result<(), Error } let dir = directory.to_owned(); - thread::spawn(move || { + DLPOOL.spawn(move || { if let Ok(episode) = dbqueries::get_episode_from_rowid(id) { let pid = episode.podcast_id(); let id = episode.rowid(); diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 247dd26..07bebb0 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -24,7 +24,6 @@ use std::collections::{HashMap, HashSet}; use std::sync::{Mutex, RwLock}; use std::sync::Arc; use std::sync::mpsc::*; -use std::thread; use app::Action; @@ -87,7 +86,7 @@ pub fn get_cleanup_date(settings: &Settings) -> DateTime { fn refresh_feed(source: Option>, sender: Sender) -> Result<(), Error> { sender.send(Action::HeaderBarShowUpdateIndicator)?; - thread::spawn(move || { + rayon::spawn(move || { let mut sources = source.unwrap_or_else(|| { dbqueries::get_sources().expect("Failed to retrieve Sources from the database.") }); @@ -135,7 +134,6 @@ lazy_static! { { RwLock::new(HashMap::new()) }; static ref COVER_DL_REGISTRY: RwLock> = RwLock::new(HashSet::new()); static ref THREADPOOL: rayon::ThreadPool = rayon::ThreadPoolBuilder::new() - .breadth_first() .build() .unwrap(); }