diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 5339d5d8b8..dae40a2727 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1195,6 +1195,7 @@ dependencies = [ "anyhow", "clap", "multimap", + "parking_lot", "serde", "serde_json", "starlark", diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index ce77d8340d..7b99990b50 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -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" diff --git a/codex-rs/execpolicy2/Cargo.toml b/codex-rs/execpolicy2/Cargo.toml index 36c384225b..c6ce6ff937 100644 --- a/codex-rs/execpolicy2/Cargo.toml +++ b/codex-rs/execpolicy2/Cargo.toml @@ -21,3 +21,4 @@ starlark = { workspace = true } thiserror = { workspace = true } multimap = { workspace = true } clap = { workspace = true, features = ["derive"] } +parking_lot = { workspace = true } diff --git a/codex-rs/execpolicy2/src/parser.rs b/codex-rs/execpolicy2/src/parser.rs index 76ecbf0762..f3f6409831 100644 --- a/codex-rs/execpolicy2/src/parser.rs +++ b/codex-rs/execpolicy2/src/parser.rs @@ -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>, - next_auto_id: RefCell, + rules_by_program: Mutex>, + 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()) } }