Skip to content

Commit f5e658a

Browse files
committed
feat: Improving locking mechanism
1 parent a550a09 commit f5e658a

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::env;
44
use std::sync::Arc;
55
use tokio::signal::unix::{signal, SignalKind};
66
use tokio::sync::mpsc;
7-
use tokio::sync::Mutex;
7+
use tokio::sync::RwLock;
88
use tracing::{error, info};
99

1010
mod api;
@@ -32,7 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
3232
};
3333

3434
let state = State {
35-
vms: Arc::new(Mutex::new(Vec::new())),
35+
vms: Arc::new(RwLock::new(Vec::new())),
3636
tmp_dir: cli_options.tmp_dir,
3737
log_dir: cli_options.log_dir,
3838
assets_dir: cli_options.assets_dir,
@@ -73,7 +73,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7373

7474
graceful.await?;
7575

76-
let vms = state_ptr.vms.lock().await;
76+
let vms = state_ptr.vms.read().await;
7777

7878
for v in vms.iter() {
7979
info!("Terminating [{}]", v.name);

src/state.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use std::sync::Arc;
3-
use tokio::sync::Mutex;
3+
use tokio::sync::RwLock;
44

55
#[derive(Serialize, Deserialize, Debug, Clone)]
66
pub struct Vm {
@@ -9,7 +9,7 @@ pub struct Vm {
99
}
1010

1111
pub struct State {
12-
pub vms: Arc<Mutex<Vec<Vm>>>,
12+
pub vms: Arc<RwLock<Vec<Vm>>>,
1313
pub tmp_dir: String,
1414
pub log_dir: String,
1515
pub assets_dir: String,
@@ -20,7 +20,7 @@ pub struct State {
2020
pub type StatePtr = Arc<State>;
2121

2222
pub async fn get_vms(state_ptr: StatePtr) -> Vec<Vm> {
23-
let vms = state_ptr.vms.lock().await;
23+
let vms = state_ptr.vms.read().await;
2424

2525
let mut new_vms: Vec<Vm> = Vec::new();
2626
for (_, item) in vms.iter().enumerate() {
@@ -31,7 +31,7 @@ pub async fn get_vms(state_ptr: StatePtr) -> Vec<Vm> {
3131
}
3232

3333
pub async fn add_vm(state_ptr: StatePtr, name: &str, pid: u32) {
34-
let mut vms = state_ptr.vms.lock().await;
34+
let mut vms = state_ptr.vms.write().await;
3535

3636
vms.push(Vm {
3737
name: String::from(name),
@@ -40,15 +40,15 @@ pub async fn add_vm(state_ptr: StatePtr, name: &str, pid: u32) {
4040
}
4141

4242
pub async fn remove_vm(state_ptr: StatePtr, name: &str) {
43-
let mut vms = state_ptr.vms.lock().await;
43+
let mut vms = state_ptr.vms.write().await;
4444

4545
if let Some(index) = vms.iter().position(|vm| vm.name == name) {
4646
vms.remove(index);
4747
}
4848
}
4949

5050
pub async fn get_vm_pid(state_ptr: StatePtr, name: &str) -> Option<u32> {
51-
let vms = state_ptr.vms.lock().await;
51+
let vms = state_ptr.vms.read().await;
5252

5353
if let Some(index) = vms.iter().position(|vm| vm.name == name) {
5454
return Option::Some(vms[index].pid);

0 commit comments

Comments
 (0)