Skip to content

Commit a2a7086

Browse files
committed
use RwLock instead of Mutex
1 parent a99534e commit a2a7086

File tree

4 files changed

+59
-86
lines changed

4 files changed

+59
-86
lines changed

Cargo.lock

+50-78
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "draw-together"
3-
version = "2.2.5"
3+
version = "2.2.6"
44
edition = "2024"
55

66
[[bin]]

src/data.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{path::Path, sync::Arc};
22
use tokio::{
33
fs::File,
44
io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
5-
sync::Mutex,
5+
sync::RwLock,
66
};
77

88
#[derive(Debug)]
@@ -121,7 +121,7 @@ impl ClientMessage {
121121
}
122122

123123
pub struct Data {
124-
pub data: Arc<Mutex<Vec<u8>>>,
124+
pub data: Arc<RwLock<Vec<u8>>>,
125125
pub listeners: Vec<tokio::sync::mpsc::Sender<Vec<u8>>>,
126126
}
127127

@@ -143,10 +143,11 @@ impl Data {
143143

144144
drop(file);
145145

146-
let data = Arc::new(Mutex::new(data));
147-
let task_data = Arc::clone(&data);
146+
let data = Arc::new(RwLock::new(data));
148147
if let Some(path) = path {
149148
if save {
149+
let task_data = Arc::clone(&data);
150+
150151
tokio::spawn(async move {
151152
let mut file = File::options()
152153
.write(true)
@@ -163,7 +164,7 @@ impl Data {
163164

164165
file.seek(tokio::io::SeekFrom::Start(0)).await.unwrap();
165166

166-
let data = task_data.lock().await;
167+
let data = task_data.read().await;
167168
file.write_all(&data).await.unwrap();
168169

169170
drop(data);
@@ -191,7 +192,7 @@ impl Data {
191192

192193
pub async fn write(&mut self, data: &[ClientMessage]) {
193194
let self_data = Arc::clone(&self.data);
194-
let mut self_data = self_data.lock().await;
195+
let mut self_data = self_data.write().await;
195196

196197
for message in data {
197198
match message.action {

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async fn main() {
5050

5151
headers.insert("Content-Type", "robert/history-2".parse().unwrap());
5252

53-
let data = data.data.as_ref().lock().await;
53+
let data = data.data.read().await;
5454
let body = Body::from(data.clone());
5555

5656
(headers, body)

0 commit comments

Comments
 (0)