Use rayon to manage all the threads.

This commit is contained in:
Jordan Petridis 2018-03-30 09:31:25 +03:00
parent 0623592f75
commit 1595256c86
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 6 additions and 7 deletions

View File

@ -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);

View File

@ -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<RwLock<HashMap<i32, Arc<Mutex<Progress>>>>> =
{ 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<Action>) -> Result<(), Error> {
@ -89,7 +90,7 @@ pub fn add(id: i32, directory: &str, sender: Sender<Action>) -> 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();

View File

@ -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<Utc> {
fn refresh_feed(source: Option<Vec<Source>>, sender: Sender<Action>) -> 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<HashSet<i32>> = RwLock::new(HashSet::new());
static ref THREADPOOL: rayon::ThreadPool = rayon::ThreadPoolBuilder::new()
.breadth_first()
.build()
.unwrap();
}