From fda47fc32287d34099d7a48160672a3899dccf83 Mon Sep 17 00:00:00 2001 From: Acrimon Date: Fri, 16 Aug 2019 00:07:09 +0200 Subject: [PATCH] [Voxygen] Switch mutexes to parking_lot and channels to crossbeam. --- Cargo.lock | 36 ++++++++++++++++++++++++++++ voxygen/Cargo.toml | 1 + voxygen/src/audio/base.rs | 9 +++---- voxygen/src/discord.rs | 12 ++++------ voxygen/src/main.rs | 2 +- voxygen/src/menu/main/client_init.rs | 10 +++----- voxygen/src/scene/terrain.rs | 9 +++---- voxygen/src/singleplayer.rs | 4 ++-- 8 files changed, 58 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 068ce3a238..655f58237b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1484,6 +1484,14 @@ dependencies = [ "scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lock_api" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "log" version = "0.3.9" @@ -1925,6 +1933,16 @@ dependencies = [ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "parking_lot_core" version = "0.3.1" @@ -1964,6 +1982,20 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "parking_lot_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -2991,6 +3023,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "msgbox 0.1.1 (git+https://github.com/bekker/msgbox-rs.git)", "num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "portpicker 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rodio 0.8.1 (git+https://github.com/desttinghim/rodio.git?rev=dd93f905c1afefaac03c496a666ecab27d3e391b)", @@ -3366,6 +3399,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum lock_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed946d4529956a20f2d63ebe1b69996d5a2137c91913fe3ebbeff957f5bca7ff" +"checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum lz4-compress 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f966533a922a9bba9e95e594c1fdb3b9bf5fdcdb11e37e51ad84cd76e468b91" @@ -3415,9 +3449,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa7767817701cce701d5585b9c4db3cdd02086398322c1d7e8bf5094a96a2ce7" +"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum parking_lot_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cb88cb1cb3790baa6776844f968fea3be44956cf184fa1be5a03341f5491278c" +"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" "checksum percent-encoding 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba4f28a6faf4ffea762ba8f4baef48c61a6db348647c73095034041fc79dd954" "checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f" diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index bf52ab9d35..43c477e8dc 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -58,3 +58,4 @@ rodio = { git = "https://github.com/desttinghim/rodio.git", rev = "dd93f905c1afe crossbeam = "0.7.2" heaptrack = "0.3.0" hashbrown = { version = "0.5.0", features = ["serde", "nightly"] } +parking_lot = "0.9.0" diff --git a/voxygen/src/audio/base.rs b/voxygen/src/audio/base.rs index a12afe6486..72365e6063 100644 --- a/voxygen/src/audio/base.rs +++ b/voxygen/src/audio/base.rs @@ -6,10 +6,11 @@ use crossbeam::{ queue::SegQueue, sync::ShardedLock, }; +use parking_lot::{Condvar, Mutex}; use rodio::{Decoder, Device, Sink}; use std::fs::File; use std::io::BufReader; -use std::sync::{Arc, Condvar, Mutex}; +use std::sync::Arc; use std::thread; trait AudioConfig { @@ -136,7 +137,7 @@ impl AudioPlayer { fn set_playing(&self, playing: bool) { *self.event_loop.playing.write().unwrap() = playing; let &(ref lock, ref condvar) = &*self.event_loop.condvar; - let mut started = lock.lock().unwrap(); + let mut started = lock.lock(); *started = playing; if playing { condvar.notify_one(); @@ -157,10 +158,10 @@ impl MonoMode for AudioPlayer { thread::spawn(move || { let block = || { let (ref lock, ref condvar) = *condition; - let mut started = lock.lock().unwrap(); + let mut started = lock.lock(); *started = false; while !*started { - started = condvar.wait(started).unwrap(); + condvar.wait(&mut started); } }; let mut playback = MonoEmitter::new(&Settings::load().audio); diff --git a/voxygen/src/discord.rs b/voxygen/src/discord.rs index c4c1ad0da0..7ccdc8d837 100644 --- a/voxygen/src/discord.rs +++ b/voxygen/src/discord.rs @@ -1,12 +1,10 @@ -use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC}; -use std::time::SystemTime; - use crate::DISCORD_INSTANCE; - -use std::sync::mpsc::Sender; -use std::sync::{mpsc, Mutex}; +use crossbeam::channel::{unbounded, Receiver, Sender}; +use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC}; +use parking_lot::Mutex; use std::thread; use std::thread::JoinHandle; +use std::time::SystemTime; /// Connects to the discord application where Images and more resides /// can be viewed at https://discordapp.com/developers/applications/583662036194689035/rich-presence/assets @@ -30,7 +28,7 @@ pub struct DiscordState { } pub fn run() -> Mutex { - let (tx, rx) = mpsc::channel(); + let (tx, rx) = unbounded(); Mutex::new(DiscordState { tx, diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 7911712ca3..e7cf65560c 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -9,7 +9,7 @@ extern crate lazy_static; pub mod discord; #[cfg(feature = "discord")] -use std::sync::Mutex; +use parking_lot::Mutex; #[macro_use] pub mod ui; diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index a6095e9eb2..02937fd78d 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -1,12 +1,8 @@ use client::{error::Error as ClientError, Client}; use common::comp; +use crossbeam::channel::{unbounded, Receiver, TryRecvError}; use log::info; -use std::{ - net::ToSocketAddrs, - sync::mpsc::{channel, Receiver, TryRecvError}, - thread, - time::Duration, -}; +use std::{net::ToSocketAddrs, thread, time::Duration}; #[cfg(feature = "discord")] use crate::{discord, discord::DiscordUpdate}; @@ -38,7 +34,7 @@ impl ClientInit { ) -> Self { let (server_address, default_port, prefer_ipv6) = connection_args; - let (tx, rx) = channel(); + let (tx, rx) = unbounded(); thread::spawn(move || { // Sleep the thread to wait for the single-player server to start up. diff --git a/voxygen/src/scene/terrain.rs b/voxygen/src/scene/terrain.rs index 848c02d859..637f76ea24 100644 --- a/voxygen/src/scene/terrain.rs +++ b/voxygen/src/scene/terrain.rs @@ -8,9 +8,10 @@ use common::{ vol::{SampleVol, VolSize}, volumes::vol_map_2d::VolMap2dErr, }; +use crossbeam::channel; use frustum_query::frustum::Frustum; use hashbrown::HashMap; -use std::{i32, ops::Mul, sync::mpsc, time::Duration}; +use std::{i32, ops::Mul, time::Duration}; use vek::*; struct TerrainChunk { @@ -56,8 +57,8 @@ pub struct Terrain { // The mpsc sender and receiver used for talking to meshing worker threads. // We keep the sender component for no reason other than to clone it and send it to new workers. - mesh_send_tmp: mpsc::Sender, - mesh_recv: mpsc::Receiver, + mesh_send_tmp: channel::Sender, + mesh_recv: channel::Receiver, mesh_todo: HashMap, ChunkMeshState>, } @@ -65,7 +66,7 @@ impl Terrain { pub fn new() -> Self { // Create a new mpsc (Multiple Produced, Single Consumer) pair for communicating with // worker threads that are meshing chunks. - let (send, recv) = mpsc::channel(); + let (send, recv) = channel::unbounded(); Self { chunks: HashMap::default(), diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index 66b6b5aaf1..2252924f44 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -1,11 +1,11 @@ use client::Client; use common::clock::Clock; +use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError}; use log::info; use portpicker::pick_unused_port; use server::{Event, Input, Server, ServerSettings}; use std::{ net::SocketAddr, - sync::mpsc::{channel, Receiver, Sender, TryRecvError}, thread::{self, JoinHandle}, time::Duration, }; @@ -28,7 +28,7 @@ pub struct Singleplayer { impl Singleplayer { pub fn new(client: Option<&Client>) -> (Self, SocketAddr) { - let (sender, receiver) = channel(); + let (sender, receiver) = unbounded(); let sock = SocketAddr::from(( [127, 0, 0, 1],