aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-03-21 16:40:15 +0100
committerJokler <jokler.contact@gmail.com>2018-03-21 16:40:15 +0100
commit516ee046784acb4a6dc97b844ff3af9a54308e30 (patch)
tree672f422c6817734c7404ec26569a366319c1a183 /src
parent48f547826edd9db9b94376f240a785d8a19a993d (diff)
downloadfrippy-516ee046784acb4a6dc97b844ff3af9a54308e30.tar.gz
frippy-516ee046784acb4a6dc97b844ff3af9a54308e30.zip
Replace every Mutex with RwLock
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs1
-rw-r--r--src/plugins/factoids/database.rs2
-rw-r--r--src/plugins/factoids/mod.rs35
-rw-r--r--src/plugins/sed.rs20
-rw-r--r--src/plugins/tell/database.rs2
-rw-r--r--src/plugins/tell/mod.rs29
6 files changed, 31 insertions, 58 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f7bcdaa..37f1225 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -53,6 +53,7 @@ extern crate irc;
extern crate regex;
extern crate reqwest;
extern crate time;
+extern crate antidote;
pub mod plugin;
pub mod plugins;
diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs
index b1fe8dd..7788d7c 100644
--- a/src/plugins/factoids/database.rs
+++ b/src/plugins/factoids/database.rs
@@ -40,7 +40,7 @@ pub struct NewFactoid<'a> {
pub created: NaiveDateTime,
}
-pub trait Database: Send {
+pub trait Database: Send + Sync {
fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError>;
fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidsError>;
fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidsError>;
diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs
index d09313c..10e512a 100644
--- a/src/plugins/factoids/mod.rs
+++ b/src/plugins/factoids/mod.rs
@@ -2,9 +2,9 @@ extern crate rlua;
use std::fmt;
use std::str::FromStr;
-use std::sync::Mutex;
use self::rlua::prelude::*;
use irc::client::prelude::*;
+use antidote::RwLock;
use time;
use chrono::NaiveDateTime;
@@ -25,22 +25,13 @@ static LUA_SANDBOX: &'static str = include_str!("sandbox.lua");
#[derive(PluginName)]
pub struct Factoids<T: Database> {
- factoids: Mutex<T>,
-}
-
-macro_rules! try_lock {
- ( $m:expr ) => {
- match $m.lock() {
- Ok(guard) => guard,
- Err(poisoned) => poisoned.into_inner(),
- }
- }
+ factoids: RwLock<T>,
}
impl<T: Database> Factoids<T> {
pub fn new(db: T) -> Factoids<T> {
Factoids {
- factoids: Mutex::new(db),
+ factoids: RwLock::new(db),
}
}
@@ -50,7 +41,7 @@ impl<T: Database> Factoids<T> {
content: &str,
author: &str,
) -> Result<&str, FactoidsError> {
- let count = try_lock!(self.factoids).count_factoids(name)?;
+ let count = self.factoids.read().count_factoids(name)?;
let tm = time::now().to_timespec();
let factoid = database::NewFactoid {
@@ -61,7 +52,7 @@ impl<T: Database> Factoids<T> {
created: NaiveDateTime::from_timestamp(tm.sec, 0u32),
};
- Ok(try_lock!(self.factoids)
+ Ok(self.factoids.write()
.insert_factoid(&factoid)
.map(|()| "Successfully added!")?)
}
@@ -95,9 +86,9 @@ impl<T: Database> Factoids<T> {
}
let name = command.tokens.remove(0);
- let count = try_lock!(self.factoids).count_factoids(&name)?;
+ let count = self.factoids.read().count_factoids(&name)?;
- match try_lock!(self.factoids).delete_factoid(&name, count - 1) {
+ match self.factoids.write().delete_factoid(&name, count - 1) {
Ok(()) => Ok("Successfully removed"),
Err(e) => Err(e)?,
}
@@ -108,7 +99,7 @@ impl<T: Database> Factoids<T> {
0 => Err(ErrorKind::InvalidCommand)?,
1 => {
let name = &command.tokens[0];
- let count = try_lock!(self.factoids).count_factoids(name)?;
+ let count = self.factoids.read().count_factoids(name)?;
if count < 1 {
Err(ErrorKind::NotFound)?;
@@ -127,7 +118,7 @@ impl<T: Database> Factoids<T> {
}
};
- let factoid = try_lock!(self.factoids)
+ let factoid = self.factoids.read()
.get_factoid(name, idx)
.context(ErrorKind::NotFound)?;
@@ -141,7 +132,7 @@ impl<T: Database> Factoids<T> {
0 => Err(ErrorKind::InvalidCommand)?,
1 => {
let name = &command.tokens[0];
- let count = try_lock!(self.factoids).count_factoids(name)?;
+ let count = self.factoids.read().count_factoids(name)?;
Ok(match count {
0 => Err(ErrorKind::NotFound)?,
@@ -152,7 +143,7 @@ impl<T: Database> Factoids<T> {
_ => {
let name = &command.tokens[0];
let idx = i32::from_str(&command.tokens[1]).context(ErrorKind::InvalidIndex)?;
- let factoid = try_lock!(self.factoids).get_factoid(name, idx)?;
+ let factoid = self.factoids.read().get_factoid(name, idx)?;
Ok(format!(
"{}: Added by {} at {} UTC",
@@ -167,8 +158,8 @@ impl<T: Database> Factoids<T> {
Err(ErrorKind::InvalidIndex)?
} else {
let name = command.tokens.remove(0);
- let count = try_lock!(self.factoids).count_factoids(&name)?;
- let factoid = try_lock!(self.factoids).get_factoid(&name, count - 1)?;
+ let count = self.factoids.read().count_factoids(&name)?;
+ let factoid = self.factoids.read().get_factoid(&name, count - 1)?;
let content = factoid.content;
let value = if content.starts_with('>') {
diff --git a/src/plugins/sed.rs b/src/plugins/sed.rs
index 4c7e215..8d8cb0d 100644
--- a/src/plugins/sed.rs
+++ b/src/plugins/sed.rs
@@ -1,7 +1,7 @@
-use std::sync::Mutex;
use std::collections::HashMap;
use circular_queue::CircularQueue;
use regex::{Regex, RegexBuilder};
+use antidote::RwLock;
use irc::client::prelude::*;
@@ -20,28 +20,19 @@ lazy_static! {
#[derive(PluginName, Debug)]
pub struct Sed {
per_channel: usize,
- channel_messages: Mutex<HashMap<String, CircularQueue<String>>>,
-}
-
-macro_rules! try_lock {
- ( $m:expr ) => {
- match $m.lock() {
- Ok(guard) => guard,
- Err(poisoned) => poisoned.into_inner(),
- }
- }
+ channel_messages: RwLock<HashMap<String, CircularQueue<String>>>,
}
impl Sed {
pub fn new(per_channel: usize) -> Sed {
Sed {
per_channel: per_channel,
- channel_messages: Mutex::new(HashMap::new()),
+ channel_messages: RwLock::new(HashMap::new()),
}
}
fn add_message(&self, channel: String, message: String) {
- let mut channel_messages = try_lock!(self.channel_messages);
+ let mut channel_messages = self.channel_messages.write();
let messages = channel_messages
.entry(channel)
.or_insert(CircularQueue::with_capacity(self.per_channel));
@@ -55,7 +46,6 @@ impl Sed {
for c in input.chars() {
if escape && !r"/\".contains(c) {
output.push('\\');
-
} else if !escape && c == '\\' {
escape = true;
continue;
@@ -99,7 +89,7 @@ impl Sed {
.build()
.context(ErrorKind::InvalidRegex)?;
- let channel_messages = try_lock!(self.channel_messages);
+ let channel_messages = self.channel_messages.read();
let messages = channel_messages.get(channel).ok_or(ErrorKind::NoMessages)?;
for message in messages.iter() {
diff --git a/src/plugins/tell/database.rs b/src/plugins/tell/database.rs
index 98e9fb3..42c0d88 100644
--- a/src/plugins/tell/database.rs
+++ b/src/plugins/tell/database.rs
@@ -40,7 +40,7 @@ pub struct NewTellMessage<'a> {
pub message: &'a str,
}
-pub trait Database: Send {
+pub trait Database: Send + Sync {
fn insert_tell(&mut self, tell: &NewTellMessage) -> Result<(), TellError>;
fn get_tells(&self, receiver: &str) -> Result<Vec<TellMessage>, TellError>;
fn get_receivers(&self) -> Result<Vec<String>, TellError>;
diff --git a/src/plugins/tell/mod.rs b/src/plugins/tell/mod.rs
index 851fa94..be2e17b 100644
--- a/src/plugins/tell/mod.rs
+++ b/src/plugins/tell/mod.rs
@@ -1,9 +1,8 @@
use irc::client::prelude::*;
-
-use std::time::Duration;
-use std::sync::Mutex;
+use antidote::RwLock;
use time;
+use std::time::Duration;
use chrono::NaiveDateTime;
use humantime::format_duration;
@@ -18,24 +17,15 @@ use self::error::*;
pub mod database;
use self::database::Database;
-macro_rules! try_lock {
- ( $m:expr ) => {
- match $m.lock() {
- Ok(guard) => guard,
- Err(poisoned) => poisoned.into_inner(),
- }
- }
-}
-
-#[derive(PluginName, Default)]
+#[derive(PluginName)]
pub struct Tell<T: Database> {
- tells: Mutex<T>,
+ tells: RwLock<T>,
}
impl<T: Database> Tell<T> {
pub fn new(db: T) -> Tell<T> {
Tell {
- tells: Mutex::new(db),
+ tells: RwLock::new(db),
}
}
@@ -71,7 +61,8 @@ impl<T: Database> Tell<T> {
.map(|channel| client.list_users(&channel))
.map(|option| {
option.and_then(|users| {
- users.into_iter()
+ users
+ .into_iter()
.find(|user| user.get_nickname().eq_ignore_ascii_case(&receiver))
})
})
@@ -91,7 +82,7 @@ impl<T: Database> Tell<T> {
};
debug!("Saving tell for {:?}", receiver);
- try_lock!(self.tells).insert_tell(&tell)?;
+ self.tells.write().insert_tell(&tell)?;
no_receiver = false;
}
@@ -107,7 +98,7 @@ impl<T: Database> Tell<T> {
}
fn on_namelist(&self, client: &IrcClient, channel: &str) -> Result<(), FrippyError> {
- let receivers = try_lock!(self.tells)
+ let receivers = self.tells.read()
.get_receivers()
.context(FrippyErrorKind::Tell)?;
@@ -133,7 +124,7 @@ impl<T: Database> Tell<T> {
return Ok(());
}
- let mut tells = try_lock!(self.tells);
+ let mut tells = self.tells.write();
let tell_messages = match tells.get_tells(&receiver.to_lowercase()) {
Ok(t) => t,