Add lock_update function for safer multithreading

&v = (v, 1).sum might fail if two threads do it at the same time or something, so you can now lock the reference to ensure no other thread messes with your data while you update its value
This commit is contained in:
Mark
2023-11-10 13:18:34 +01:00
parent 68d5b55c6f
commit 8bdd6e00e8
8 changed files with 67 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use crate::data::{self, Data, Type};
@@ -40,7 +40,7 @@ impl MersStatement for Variable {
}
fn run_custom(&self, info: &mut super::Info) -> Data {
if self.is_init {
let nothing = Arc::new(Mutex::new(Data::new(data::bool::Bool(false))));
let nothing = Arc::new(RwLock::new(Data::new(data::bool::Bool(false))));
while info.scopes[self.var.0].vars.len() <= self.var.1 {
info.scopes[self.var.0].vars.push(Arc::clone(&nothing));
}
@@ -52,7 +52,7 @@ impl MersStatement for Variable {
)))
} else {
info.scopes[self.var.0].vars[self.var.1]
.lock()
.write()
.unwrap()
.clone()
}