diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-10-10 19:50:03 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-10-11 15:41:32 +0200 |
| commit | 353fa7841675fe70a75ccc4c650c2c15d5efbcbb (patch) | |
| tree | 51d5a57f24d990e67869c6e51deb4f5648abbfee | |
| parent | 0195219d5c0b0ff1486b3e6bdcd62a807d3d2932 (diff) | |
| download | frippy-353fa7841675fe70a75ccc4c650c2c15d5efbcbb.tar.gz frippy-353fa7841675fe70a75ccc4c650c2c15d5efbcbb.zip | |
Only spawn a new thread if needed
| -rw-r--r-- | src/lib.rs | 63 | ||||
| -rw-r--r-- | src/plugins/currency.rs | 1 |
2 files changed, 37 insertions, 27 deletions
@@ -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 {} } |
