diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-10 20:08:11 -0700 |
| commit | 93e0a4215c915b98cf433ac6d0bcfbc60f0168ec (patch) | |
| tree | 727111506d1f89cd8a511b8b79c102131222421f /src/client/bridge/gateway | |
| parent | Resume on resumable session invalidations (diff) | |
| download | serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.tar.xz serenity-93e0a4215c915b98cf433ac6d0bcfbc60f0168ec.zip | |
Switch to parking_lot::{Mutex, RwLock}
Switch to the `parking_lot` crate's implementations of
`std::sync::Mutex` and `std::sync::RwLock`, which are more efficient.
A writeup on why `parking_lot` is more efficient can be read here:
<https://github.com/Amanieu/parking_lot>
Upgrade path:
Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to
unwrap or handle a result), and
`rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to
`rwlock.read()` and `rwlock.write()`.
For example, modify:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().unwrap().user.id);
```
to:
```rust
use serenity::CACHE;
println!("{}", CACHE.read().user.id);
```
Diffstat (limited to 'src/client/bridge/gateway')
| -rw-r--r-- | src/client/bridge/gateway/shard_manager.rs | 14 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_queuer.rs | 10 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_runner.rs | 10 |
3 files changed, 16 insertions, 18 deletions
diff --git a/src/client/bridge/gateway/shard_manager.rs b/src/client/bridge/gateway/shard_manager.rs index 2068bef..4a50b56 100644 --- a/src/client/bridge/gateway/shard_manager.rs +++ b/src/client/bridge/gateway/shard_manager.rs @@ -1,8 +1,8 @@ use internal::prelude::*; -use parking_lot::Mutex as ParkingLotMutex; +use parking_lot::Mutex; use std::collections::HashMap; use std::sync::mpsc::{self, Receiver, Sender}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::thread; use super::super::super::EventHandler; use super::{ @@ -19,7 +19,7 @@ use typemap::ShareMap; use framework::Framework; pub struct ShardManager { - pub runners: Arc<ParkingLotMutex<HashMap<ShardId, ShardRunnerInfo>>>, + pub runners: Arc<Mutex<HashMap<ShardId, ShardRunnerInfo>>>, /// The index of the first shard to initialize, 0-indexed. shard_index: u64, /// The number of shards to initialize. @@ -38,7 +38,7 @@ impl ShardManager { shard_total: u64, ws_url: Arc<Mutex<String>>, token: Arc<Mutex<String>>, - data: Arc<ParkingLotMutex<ShareMap>>, + data: Arc<Mutex<ShareMap>>, event_handler: Arc<H>, framework: Arc<Mutex<Option<Box<Framework + Send>>>>, threadpool: ThreadPool, @@ -46,7 +46,7 @@ impl ShardManager { let (thread_tx, thread_rx) = mpsc::channel(); let (shard_queue_tx, shard_queue_rx) = mpsc::channel(); - let runners = Arc::new(ParkingLotMutex::new(HashMap::new())); + let runners = Arc::new(Mutex::new(HashMap::new())); let mut shard_queuer = ShardQueuer { data: data.clone(), @@ -82,14 +82,14 @@ impl ShardManager { shard_total: u64, ws_url: Arc<Mutex<String>>, token: Arc<Mutex<String>>, - data: Arc<ParkingLotMutex<ShareMap>>, + data: Arc<Mutex<ShareMap>>, event_handler: Arc<H>, threadpool: ThreadPool, ) -> Self where H: EventHandler + Send + Sync + 'static { let (thread_tx, thread_rx) = mpsc::channel(); let (shard_queue_tx, shard_queue_rx) = mpsc::channel(); - let runners = Arc::new(ParkingLotMutex::new(HashMap::new())); + let runners = Arc::new(Mutex::new(HashMap::new())); let mut shard_queuer = ShardQueuer { data: data.clone(), diff --git a/src/client/bridge/gateway/shard_queuer.rs b/src/client/bridge/gateway/shard_queuer.rs index ea49de4..f75e35d 100644 --- a/src/client/bridge/gateway/shard_queuer.rs +++ b/src/client/bridge/gateway/shard_queuer.rs @@ -1,9 +1,9 @@ use gateway::Shard; use internal::prelude::*; -use parking_lot::Mutex as ParkingLotMutex; +use parking_lot::Mutex; use std::collections::HashMap; use std::sync::mpsc::{Receiver, Sender}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::thread; use std::time::{Duration, Instant}; use super::super::super::EventHandler; @@ -27,13 +27,13 @@ use framework::Framework; /// blocking nature of the loop itself as well as a 5 second thread sleep /// between shard starts. pub struct ShardQueuer<H: EventHandler + Send + Sync + 'static> { - pub data: Arc<ParkingLotMutex<ShareMap>>, + pub data: Arc<Mutex<ShareMap>>, pub event_handler: Arc<H>, #[cfg(feature = "framework")] pub framework: Arc<Mutex<Option<Box<Framework + Send>>>>, pub last_start: Option<Instant>, pub manager_tx: Sender<ShardManagerMessage>, - pub runners: Arc<ParkingLotMutex<HashMap<ShardId, ShardRunnerInfo>>>, + pub runners: Arc<Mutex<HashMap<ShardId, ShardRunnerInfo>>>, pub rx: Receiver<ShardQueuerMessage>, pub threadpool: ThreadPool, pub token: Arc<Mutex<String>>, @@ -81,7 +81,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardQueuer<H> { fn start(&mut self, shard_id: ShardId, shard_total: ShardId) -> Result<()> { let shard_info = [shard_id.0, shard_total.0]; let shard = Shard::new(self.ws_url.clone(), self.token.clone(), shard_info)?; - let locked = Arc::new(ParkingLotMutex::new(shard)); + let locked = Arc::new(Mutex::new(shard)); let mut runner = feature_framework! {{ ShardRunner::new( diff --git a/src/client/bridge/gateway/shard_runner.rs b/src/client/bridge/gateway/shard_runner.rs index 1b1de0e..53d4b80 100644 --- a/src/client/bridge/gateway/shard_runner.rs +++ b/src/client/bridge/gateway/shard_runner.rs @@ -1,7 +1,7 @@ use internal::prelude::*; use internal::ws_impl::ReceiverExt; use model::event::{Event, GatewayEvent}; -use parking_lot::Mutex as ParkingLotMutex; +use parking_lot::Mutex; use std::sync::mpsc::{self, Receiver, Sender}; use std::sync::Arc; use super::super::super::{EventHandler, dispatch}; @@ -12,11 +12,9 @@ use websocket::WebSocketError; #[cfg(feature = "framework")] use framework::Framework; -#[cfg(feature = "framework")] -use std::sync::Mutex; pub struct ShardRunner<H: EventHandler + Send + Sync + 'static> { - data: Arc<ParkingLotMutex<ShareMap>>, + data: Arc<Mutex<ShareMap>>, event_handler: Arc<H>, #[cfg(feature = "framework")] framework: Arc<Mutex<Option<Box<Framework + Send>>>>, @@ -34,7 +32,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { shard: LockedShard, manager_tx: Sender<ShardManagerMessage>, framework: Arc<Mutex<Option<Box<Framework + Send>>>>, - data: Arc<ParkingLotMutex<ShareMap>>, + data: Arc<Mutex<ShareMap>>, event_handler: Arc<H>, threadpool: ThreadPool, ) -> Self { @@ -58,7 +56,7 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { pub fn new( shard: LockedShard, manager_tx: Sender<ShardManagerMessage>, - data: Arc<ParkingLotMutex<ShareMap>>, + data: Arc<Mutex<ShareMap>>, event_handler: Arc<H>, threadpool: ThreadPool, ) -> Self { |