using parking_lot mutex + AtomicU64 for auto id

This commit is contained in:
kevin zhao
2025-11-13 22:07:38 -05:00
parent 73cbf5a83e
commit bc375db0ab
4 changed files with 13 additions and 11 deletions
+1
View File
@@ -1195,6 +1195,7 @@ dependencies = [
"anyhow",
"clap",
"multimap",
"parking_lot",
"serde",
"serde_json",
"starlark",
+1
View File
@@ -153,6 +153,7 @@ owo-colors = "4.2.0"
paste = "1.0.15"
path-absolutize = "3.1.1"
pathdiff = "0.2"
parking_lot = "0.12.4"
portable-pty = "0.9.0"
predicates = "3"
pretty_assertions = "1.4.1"
+1
View File
@@ -21,3 +21,4 @@ starlark = { workspace = true }
thiserror = { workspace = true }
multimap = { workspace = true }
clap = { workspace = true, features = ["derive"] }
parking_lot = { workspace = true }
+10 -11
View File
@@ -1,6 +1,5 @@
use std::cell::RefCell;
use multimap::MultiMap;
use parking_lot::Mutex;
use starlark::any::ProvidesStaticType;
use starlark::environment::GlobalsBuilder;
use starlark::environment::Module;
@@ -12,6 +11,8 @@ use starlark::values::Value;
use starlark::values::list::ListRef;
use starlark::values::list::UnpackList;
use starlark::values::none::NoneType;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use crate::decision::Decision;
use crate::error::Error;
@@ -54,33 +55,31 @@ impl PolicyParser {
#[derive(Debug, ProvidesStaticType)]
struct PolicyBuilder {
rules_by_program: RefCell<MultiMap<String, Rule>>,
next_auto_id: RefCell<i64>,
rules_by_program: Mutex<MultiMap<String, Rule>>,
next_auto_id: AtomicU64,
}
impl PolicyBuilder {
fn new() -> Self {
Self {
rules_by_program: RefCell::new(MultiMap::new()),
next_auto_id: RefCell::new(0),
rules_by_program: Mutex::new(MultiMap::new()),
next_auto_id: AtomicU64::new(0),
}
}
fn alloc_id(&self) -> String {
let mut next = self.next_auto_id.borrow_mut();
let id = *next;
*next += 1;
let id = self.next_auto_id.fetch_add(1, Ordering::Relaxed);
format!("rule_{id}")
}
fn add_rule(&self, rule: Rule) {
self.rules_by_program
.borrow_mut()
.lock()
.insert(rule.pattern.first.clone(), rule);
}
fn build(&self) -> crate::policy::Policy {
crate::policy::Policy::new(self.rules_by_program.borrow().clone())
crate::policy::Policy::new(self.rules_by_program.lock().clone())
}
}