From 353fa7841675fe70a75ccc4c650c2c15d5efbcbb Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 10 Oct 2017 19:50:03 +0200 Subject: Only spawn a new thread if needed --- src/lib.rs | 63 +++++++++++++++++++++++++++++-------------------- src/plugins/currency.rs | 1 - 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 04210a6..e1ca7e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,16 @@ use irc::error::Error as IrcError; use plugin::*; +// Lock the mutex and ignore if it is poisoned +macro_rules! lock_plugin { + ($e:expr) => { + match $e.lock() { + Ok(plugin) => plugin, + Err(poisoned) => poisoned.into_inner(), + } + } +} + /// Runs the bot /// /// # Remarks @@ -80,35 +90,36 @@ pub fn run() { } for plugin in plugins.clone() { - // Clone everything before the move - let server = server.clone(); - let message = Arc::clone(&message); - let command = command.clone(); - - // Spawn a new thread for each plugin - spawn(move || { - // Lock the mutex and ignore if it is poisoned - let mut plugin = match plugin.lock() { - Ok(plugin) => plugin, - Err(poisoned) => poisoned.into_inner(), - }; - - // Send the message to the plugin if the plugin needs it - if plugin.is_allowed(&server, &message) { - plugin.execute(&server, &message).unwrap(); - } + // Send the message to the plugin if the plugin needs it + if lock_plugin!(plugin).is_allowed(&server, &message) { + + // Clone everything before the move + // The server uses an Arc internally too + let plugin = Arc::clone(&plugin); + let message = Arc::clone(&message); + let server = server.clone(); - // Check if the command is for this plugin - if let Some(mut c) = command { - if !c.tokens.is_empty() && - plugin.name().to_lowercase() == c.tokens[0].to_lowercase() { + // Execute the plugin in another thread + spawn(move || { lock_plugin!(plugin).execute(&server, &message).unwrap(); }); + } + + // Check if the command is for this plugin + // Clone it for the move + if let Some(mut c) = command.clone() { + + // Skip empty commands + if c.tokens.is_empty() { continue; } - // The first token contains the name of the plugin - c.tokens.remove(0); - plugin.command(&server, c).unwrap(); - } + if lock_plugin!(plugin).name().to_lowercase() == c.tokens[0].to_lowercase() { + + // The first token contains the name of the plugin + c.tokens.remove(0); + + // Clone the server for the move - it uses an Arc internally + let server = server.clone(); + spawn(move || { lock_plugin!(plugin).command(&server, c).unwrap(); }); } - }); + } } }) .unwrap(); diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index d29e560..08501e1 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -62,7 +62,6 @@ impl<'a> ConvertionRequest<'a> { } impl Currency { - pub fn new() -> Currency { Currency {} } -- cgit v1.2.3-70-g09d2