From 48f547826edd9db9b94376f240a785d8a19a993d Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 21 Mar 2018 16:03:15 +0100 Subject: Give names to any threads which are spawned --- src/error.rs | 4 ++++ src/lib.rs | 33 ++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/error.rs b/src/error.rs index 36d5724..ce56a04 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,6 +17,10 @@ pub enum ErrorKind { #[fail(display = "A connection error occured")] Connection, + /// Thread spawn error + #[fail(display = "Failed to spawn thread")] + ThreadSpawn, + /// A Url error #[fail(display = "A Url error has occured")] Url, diff --git a/src/lib.rs b/src/lib.rs index 165651b..f7bcdaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ pub mod error; use std::collections::HashMap; use std::fmt; -use std::thread::spawn; +use std::thread; use std::sync::Arc; pub use irc::client::prelude::*; @@ -229,6 +229,8 @@ impl ThreadedPlugins { self.plugins.remove(&name.to_lowercase()).map(|_| ()) } + /// Runs the execute functions on all plugins. + /// Any errors that occur are printed right away. pub fn execute_plugins(&mut self, client: &IrcClient, message: Message) { let message = Arc::new(message); @@ -250,11 +252,17 @@ impl ThreadedPlugins { let client = client.clone(); // Execute the plugin in another thread - spawn(move || { - if let Err(e) = plugin.execute_threaded(&client, &message) { - log_error(e); - }; - }); + if let Err(e) = thread::Builder::new() + .name(name) + .spawn(move || { + if let Err(e) = plugin.execute_threaded(&client, &message) { + log_error(e); + }; + }) + .context(ErrorKind::ThreadSpawn) + { + log_error(e.into()); + } } } } @@ -275,11 +283,14 @@ impl ThreadedPlugins { // Clone for the move - the client uses an Arc internally let client = client.clone(); let plugin = Arc::clone(plugin); - spawn(move || { - if let Err(e) = plugin.command(&client, command) { - log_error(e); - }; - }); + thread::Builder::new() + .name(name) + .spawn(move || { + if let Err(e) = plugin.command(&client, command) { + log_error(e); + }; + }) + .context(ErrorKind::ThreadSpawn)?; } Ok(()) -- cgit v1.2.3-70-g09d2