From 8fda4f79ed5eda95ab8892c6385a8abb6ad2623c Mon Sep 17 00:00:00 2001 From: ddidderr Date: Wed, 25 Jun 2025 21:16:39 +0200 Subject: [PATCH] [code] Mutex -> RwLock --- crates/lanspread-client/src/lib.rs | 12 +++---- .../src-tauri/src/lib.rs | 31 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/crates/lanspread-client/src/lib.rs b/crates/lanspread-client/src/lib.rs index 7c3e186..f09cf13 100644 --- a/crates/lanspread-client/src/lib.rs +++ b/crates/lanspread-client/src/lib.rs @@ -10,7 +10,7 @@ use s2n_quic::{Client as QuicClient, Connection, client::Connect, provider::limi use tokio::{ io::AsyncWriteExt, sync::{ - Mutex, + RwLock, mpsc::{UnboundedReceiver, UnboundedSender}, }, }; @@ -162,7 +162,7 @@ async fn download_game_files( } struct Ctx { - game_dir: Arc>>, + game_dir: Arc>>, } #[allow(clippy::too_many_lines)] @@ -181,7 +181,7 @@ pub async fn run( // client context let ctx = Ctx { - game_dir: Arc::new(Mutex::new(None)), + game_dir: Arc::new(RwLock::new(None)), }; loop { @@ -230,7 +230,7 @@ pub async fn run( continue; } ClientCommand::SetGameDir(game_dir) => { - *ctx.game_dir.lock().await = Some(game_dir.clone()); + *ctx.game_dir.write().await = Some(game_dir.clone()); continue; } ClientCommand::DownloadGameFiles { @@ -239,7 +239,7 @@ pub async fn run( } => { log::info!("got ClientCommand::DownloadGameFiles"); - let games_folder = { ctx.game_dir.lock().await.clone() }; + let games_folder = { ctx.game_dir.read().await.clone() }; if let Some(games_folder) = games_folder { let tx_notify_ui = tx_notify_ui.clone(); tokio::task::spawn(async move { @@ -344,7 +344,7 @@ pub async fn run( file_descriptions.len() ); - let games_folder = { ctx.game_dir.lock().await.clone() }; + let games_folder = { ctx.game_dir.read().await.clone() }; match games_folder { Some(games_folder) => { diff --git a/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs b/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs index 5c21ca4..2582055 100644 --- a/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs +++ b/crates/lanspread-tauri-deno-ts/src-tauri/src/lib.rs @@ -11,16 +11,16 @@ use lanspread_db::db::{Game, GameDB}; use lanspread_mdns::{LANSPREAD_INSTANCE_NAME, LANSPREAD_SERVICE_TYPE, discover_service}; use tauri::{AppHandle, Emitter as _, Manager}; use tauri_plugin_shell::{ShellExt, process::Command}; -use tokio::sync::{Mutex, mpsc::UnboundedSender}; +use tokio::sync::{Mutex, RwLock, mpsc::UnboundedSender}; // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ struct LanSpreadState { - server_addr: Mutex>, + server_addr: RwLock>, client_ctrl: UnboundedSender, - games: Arc>, + games: Arc>, games_in_download: Arc>>, - games_folder: Arc>, + games_folder: Arc>, } #[tauri::command] @@ -85,7 +85,7 @@ fn run_game_windows(id: String, state: tauri::State) { const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done"; let games_folder = - tauri::async_runtime::block_on(async { state.inner().games_folder.lock().await.clone() }); + tauri::async_runtime::block_on(async { state.inner().games_folder.read().await.clone() }); let games_folder = PathBuf::from(games_folder); if !games_folder.exists() { @@ -180,7 +180,7 @@ fn update_game_directory(app_handle: tauri::AppHandle, path: String) { .state::() .inner() .games_folder - .lock() + .write() .await; *games_folder = path.clone(); @@ -205,7 +205,7 @@ fn update_game_directory(app_handle: tauri::AppHandle, path: String) { .state::() .inner() .games - .lock() + .write() .await; // Reset all games to uninstalled @@ -239,11 +239,7 @@ async fn find_server(app: AppHandle) { Ok(server_addr) => { log::info!("Found server at {server_addr}"); let state: tauri::State = app.state(); - { - // mutex scope - let mut addr = state.server_addr.lock().await; - *addr = Some(server_addr); - } + *state.server_addr.write().await = Some(server_addr); state .client_ctrl .send(ClientCommand::ServerAddr(server_addr)) @@ -266,8 +262,7 @@ async fn update_game_db(games: Vec, app: AppHandle) { let state = app.state::(); // Store games list - let mut state_games = state.games.lock().await; - *state_games = GameDB::from(games.clone()); + *state.games.write().await = GameDB::from(games.clone()); // Tell Frontend about new games list if let Err(e) = app.emit("games-list-updated", Some(games)) { @@ -361,11 +356,11 @@ pub fn run() { tokio::sync::mpsc::unbounded_channel::(); let lanspread_state = LanSpreadState { - server_addr: Mutex::new(None), + server_addr: RwLock::new(None), client_ctrl: tx_client_control, - games: Arc::new(Mutex::new(GameDB::empty())), + games: Arc::new(RwLock::new(GameDB::empty())), games_in_download: Arc::new(Mutex::new(HashSet::new())), - games_folder: Arc::new(Mutex::new("".to_string())), + games_folder: Arc::new(RwLock::new("".to_string())), }; tauri::Builder::default() @@ -451,7 +446,7 @@ pub fn run() { .state::() .inner() .games_folder - .lock() + .read() .await .clone();