aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorErk- <[email protected]>2018-12-22 14:04:31 +0100
committerAlex M. M <[email protected]>2018-12-22 14:04:31 +0100
commita2baca08bfd558ed7981cfe8ddb2659f3b6e9fb9 (patch)
tree498124365df67b3e2c3e18b6bc1383c3bfa2d70c /src/client
parentFix `Emoji::url` to use .gif for animated emoji (#449) (diff)
downloadserenity-a2baca08bfd558ed7981cfe8ddb2659f3b6e9fb9.tar.xz
serenity-a2baca08bfd558ed7981cfe8ddb2659f3b6e9fb9.zip
Implement the proposed change to the sharemap (`Mutex` => `RwLock`) (#453)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/bridge/gateway/shard_manager.rs5
-rw-r--r--src/client/bridge/gateway/shard_queuer.rs3
-rw-r--r--src/client/bridge/gateway/shard_runner.rs5
-rw-r--r--src/client/context.rs8
-rw-r--r--src/client/dispatch.rs10
-rw-r--r--src/client/mod.rs13
6 files changed, 22 insertions, 22 deletions
diff --git a/src/client/bridge/gateway/shard_manager.rs b/src/client/bridge/gateway/shard_manager.rs
index 579b003..74817ef 100644
--- a/src/client/bridge/gateway/shard_manager.rs
+++ b/src/client/bridge/gateway/shard_manager.rs
@@ -2,6 +2,7 @@ use crate::gateway::InterMessage;
use crate::internal::prelude::*;
use crate::CacheAndHttp;
use parking_lot::Mutex;
+use parking_lot::RwLock;
use std::{
collections::{HashMap, VecDeque},
sync::{
@@ -76,7 +77,7 @@ use crate::client::bridge::voice::ClientVoiceManager;
/// let token = Arc::new(Mutex::new(token));
///
/// let gateway_url = Arc::new(Mutex::new(http::get_gateway()?.url));
-/// let data = Arc::new(Mutex::new(ShareMap::custom()));
+/// let data = Arc::new(RwLock::new(ShareMap::custom()));
/// let event_handler = Arc::new(Handler);
/// let framework = Arc::new(Mutex::new(None));
/// let threadpool = ThreadPool::with_name("my threadpool".to_owned(), 5);
@@ -357,7 +358,7 @@ impl Drop for ShardManager {
}
pub struct ShardManagerOptions<'a, H: EventHandler + Send + Sync + 'static> {
- pub data: &'a Arc<Mutex<ShareMap>>,
+ pub data: &'a Arc<RwLock<ShareMap>>,
pub event_handler: &'a Arc<H>,
#[cfg(feature = "framework")]
pub framework: &'a Arc<Mutex<Option<Box<Framework + Send>>>>,
diff --git a/src/client/bridge/gateway/shard_queuer.rs b/src/client/bridge/gateway/shard_queuer.rs
index 12df4c8..54e4478 100644
--- a/src/client/bridge/gateway/shard_queuer.rs
+++ b/src/client/bridge/gateway/shard_queuer.rs
@@ -2,6 +2,7 @@ use crate::gateway::Shard;
use crate::internal::prelude::*;
use crate::CacheAndHttp;
use parking_lot::Mutex;
+use parking_lot::RwLock;
use std::{
collections::{HashMap, VecDeque},
sync::{
@@ -45,7 +46,7 @@ pub struct ShardQueuer<H: EventHandler + Send + Sync + 'static> {
/// dispatching.
///
/// [`Client::data`]: ../../struct.Client.html#structfield.data
- pub data: Arc<Mutex<ShareMap>>,
+ pub data: Arc<RwLock<ShareMap>>,
/// A reference to an `EventHandler`, such as the one given to the
/// [`Client`].
///
diff --git a/src/client/bridge/gateway/shard_runner.rs b/src/client/bridge/gateway/shard_runner.rs
index fe71462..f052e9f 100644
--- a/src/client/bridge/gateway/shard_runner.rs
+++ b/src/client/bridge/gateway/shard_runner.rs
@@ -4,6 +4,7 @@ use crate::internal::ws_impl::{ReceiverExt, SenderExt};
use crate::model::event::{Event, GatewayEvent};
use crate::CacheAndHttp;
use parking_lot::Mutex;
+use parking_lot::RwLock;
use serde::Deserialize;
use std::{
borrow::Cow,
@@ -38,7 +39,7 @@ use super::super::voice::ClientVoiceManager;
///
/// [`Shard`]: ../../../gateway/struct.Shard.html
pub struct ShardRunner<H: EventHandler + Send + Sync + 'static> {
- data: Arc<Mutex<ShareMap>>,
+ data: Arc<RwLock<ShareMap>>,
event_handler: Arc<H>,
#[cfg(feature = "framework")]
framework: Arc<Mutex<Option<Box<Framework + Send>>>>,
@@ -489,7 +490,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> {
///
/// [`ShardRunner::new`]: struct.ShardRunner.html#method.new
pub struct ShardRunnerOptions<H: EventHandler + Send + Sync + 'static> {
- pub data: Arc<Mutex<ShareMap>>,
+ pub data: Arc<RwLock<ShareMap>>,
pub event_handler: Arc<H>,
#[cfg(feature = "framework")]
pub framework: Arc<Mutex<Option<Box<Framework + Send>>>>,
diff --git a/src/client/context.rs b/src/client/context.rs
index ff92f5e..b3e14dc 100644
--- a/src/client/context.rs
+++ b/src/client/context.rs
@@ -4,7 +4,7 @@ use crate::error::Result;
use crate::gateway::InterMessage;
use crate::http;
use crate::model::prelude::*;
-use parking_lot::Mutex;
+use parking_lot::RwLock;
use serde_json::Value;
use std::sync::{
Arc,
@@ -15,8 +15,6 @@ use crate::utils::VecMap;
use crate::utils::vecmap_to_json_map;
#[cfg(feature = "cache")]
pub use crate::cache::Cache;
-#[cfg(feature = "cache")]
-use parking_lot::RwLock;
/// The context is a general utility struct provided on event dispatches, which
/// helps with dealing with the current "context" of the event dispatch.
@@ -39,7 +37,7 @@ pub struct Context {
/// information.
///
/// [`Client::data`]: struct.Client.html#structfield.data
- pub data: Arc<Mutex<ShareMap>>,
+ pub data: Arc<RwLock<ShareMap>>,
/// The messenger to communicate with the shard runner.
pub shard: ShardMessenger,
/// The ID of the shard this context is related to.
@@ -51,7 +49,7 @@ pub struct Context {
impl Context {
/// Create a new Context to be passed to an event handler.
pub(crate) fn new(
- data: Arc<Mutex<ShareMap>>,
+ data: Arc<RwLock<ShareMap>>,
runner_tx: Sender<InterMessage>,
shard_id: u64,
cache: Arc<RwLock<Cache>>,
diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs
index 0425f29..57160d4 100644
--- a/src/client/dispatch.rs
+++ b/src/client/dispatch.rs
@@ -49,7 +49,7 @@ macro_rules! update {
#[cfg(feature = "cache")]
fn context(
- data: &Arc<Mutex<ShareMap>>,
+ data: &Arc<RwLock<ShareMap>>,
runner_tx: &Sender<InterMessage>,
shard_id: u64,
cache: &Arc<RwLock<Cache>>,
@@ -59,7 +59,7 @@ fn context(
#[cfg(not(feature = "cache"))]
fn context(
- data: &Arc<Mutex<ShareMap>>,
+ data: &Arc<RwLock<ShareMap>>,
runner_tx: &Sender<InterMessage>,
shard_id: u64,
) -> Context {
@@ -76,7 +76,7 @@ pub(crate) enum DispatchEvent {
pub(crate) fn dispatch<H: EventHandler + Send + Sync + 'static>(
event: DispatchEvent,
framework: &Arc<Mutex<Option<Box<Framework + Send>>>>,
- data: &Arc<Mutex<ShareMap>>,
+ data: &Arc<RwLock<ShareMap>>,
event_handler: &Arc<H>,
runner_tx: &Sender<InterMessage>,
threadpool: &ThreadPool,
@@ -119,7 +119,7 @@ pub(crate) fn dispatch<H: EventHandler + Send + Sync + 'static>(
#[allow(clippy::unused_mut)]
pub(crate) fn dispatch<H: EventHandler + Send + Sync + 'static>(
event: DispatchEvent,
- data: &Arc<Mutex<ShareMap>>,
+ data: &Arc<RwLock<ShareMap>>,
event_handler: &Arc<H>,
runner_tx: &Sender<InterMessage>,
threadpool: &ThreadPool,
@@ -166,7 +166,7 @@ fn dispatch_message<H>(
#[allow(clippy::cyclomatic_complexity, unused_assignments, unused_mut)]
fn handle_event<H: EventHandler + Send + Sync + 'static>(
event: DispatchEvent,
- data: &Arc<Mutex<ShareMap>>,
+ data: &Arc<RwLock<ShareMap>>,
event_handler: &Arc<H>,
runner_tx: &Sender<InterMessage>,
threadpool: &ThreadPool,
diff --git a/src/client/mod.rs b/src/client/mod.rs
index bbedf89..164fea7 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -40,12 +40,11 @@ pub use crate::http as rest;
#[cfg(feature = "cache")]
pub use crate::cache::Cache;
-#[cfg(feature = "cache")]
-use parking_lot::RwLock;
use crate::http;
use crate::internal::prelude::*;
use parking_lot::Mutex;
+use parking_lot::RwLock;
use self::bridge::gateway::{ShardManager, ShardManagerMonitor, ShardManagerOptions};
use std::{sync::Arc, time::Duration};
use threadpool::ThreadPool;
@@ -141,7 +140,7 @@ pub struct Client {
/// macro_rules! reg {
/// ($ctx:ident $name:expr) => {
/// {
- /// let mut data = $ctx.data.lock();
+ /// let mut data = $ctx.data.write();
/// let counter = data.get_mut::<MessageEventCounter>().unwrap();
/// let entry = counter.entry($name).or_insert(0);
/// *entry += 1;
@@ -164,7 +163,7 @@ pub struct Client {
/// let mut client = Client::new(&env::var("DISCORD_TOKEN").unwrap(), Handler);
///
/// {
- /// let mut data = client.data.lock();
+ /// let mut data = client.data.write();
/// data.insert::<MessageEventCounter>(HashMap::default());
/// }
///
@@ -179,7 +178,7 @@ pub struct Client {
/// [`Event::MessageDeleteBulk`]: ../model/event/enum.Event.html#variant.MessageDeleteBulk
/// [`Event::MessageUpdate`]: ../model/event/enum.Event.html#variant.MessageUpdate
/// [example 05]: https://github.com/serenity-rs/serenity/tree/current/examples/05_command_framework
- pub data: Arc<Mutex<ShareMap>>,
+ pub data: Arc<RwLock<ShareMap>>,
/// A vector of all active shards that have received their [`Event::Ready`]
/// payload, and have dispatched to [`on_ready`] if an event handler was
/// configured.
@@ -348,7 +347,7 @@ impl Client {
let name = "serenity client".to_owned();
let threadpool = ThreadPool::with_name(name, 5);
let url = Arc::new(Mutex::new(http::get_gateway()?.url));
- let data = Arc::new(Mutex::new(ShareMap::custom()));
+ let data = Arc::new(RwLock::new(ShareMap::custom()));
let event_handler = Arc::new(handler);
#[cfg(feature = "framework")]
@@ -451,7 +450,7 @@ impl Client {
let name = "serenity client".to_owned();
let threadpool = ThreadPool::with_name(name, 5);
let url = Arc::new(Mutex::new(http::get_gateway()?.url));
- let data = Arc::new(Mutex::new(ShareMap::custom()));
+ let data = Arc::new(RwLock::new(ShareMap::custom()));
let event_handler = Arc::new(handler);
#[cfg(feature = "framework")]