Distinguish annotation validation from content read dependencies
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
use std::fmt;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::thread::ThreadId;
|
||||
|
||||
use crate::changes::MAX_PENDING_CHANGES;
|
||||
use crate::{ChangeBatch, ChangeKey, ObjectChange};
|
||||
@@ -39,7 +40,7 @@ struct Slot {
|
||||
struct ReadOnly {
|
||||
protected: BTreeSet<ObjectRef>,
|
||||
reads: Mutex<BTreeSet<ObjectRef>>,
|
||||
regions: Mutex<Vec<BTreeSet<ObjectRef>>>,
|
||||
regions: Mutex<Vec<BTreeMap<ObjectRef, bool>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -51,6 +52,36 @@ pub(crate) struct Heap {
|
||||
changes: BTreeMap<(ObjectRef, ChangeKey), u64>,
|
||||
changes_overflow: bool,
|
||||
read_only: Vec<ReadOnly>,
|
||||
validation_reads: Mutex<HashMap<ThreadId, usize>>,
|
||||
}
|
||||
|
||||
/// Borrowed read identity; content_read distinguishes ordinary reads from
|
||||
/// annotation validation alone. Neither form retains the object.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ReadAccess {
|
||||
pub object: ObjectRef,
|
||||
pub content_read: bool,
|
||||
}
|
||||
|
||||
struct ValidationScope<'a> {
|
||||
heap: &'a Heap,
|
||||
thread: ThreadId,
|
||||
}
|
||||
impl Drop for ValidationScope<'_> {
|
||||
fn drop(&mut self) {
|
||||
let mut active = self
|
||||
.heap
|
||||
.validation_reads
|
||||
.lock()
|
||||
.expect("read tracker poisoned");
|
||||
let depth = active
|
||||
.get_mut(&self.thread)
|
||||
.expect("active validation scope");
|
||||
*depth -= 1;
|
||||
if *depth == 0 {
|
||||
active.remove(&self.thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
@@ -66,6 +97,7 @@ impl Heap {
|
||||
changes: BTreeMap::new(),
|
||||
changes_overflow: false,
|
||||
read_only: vec![],
|
||||
validation_reads: Mutex::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -120,11 +152,22 @@ impl Heap {
|
||||
"read tracking requires a read-only scope",
|
||||
))?;
|
||||
let mut regions = scope.regions.lock().expect("read tracker poisoned");
|
||||
regions.push(BTreeSet::new());
|
||||
regions.push(BTreeMap::new());
|
||||
Ok(regions.len())
|
||||
}
|
||||
|
||||
pub(crate) fn end_read_tracking(&self, token: usize) -> Result<Vec<ObjectRef>, VmError> {
|
||||
Ok(self
|
||||
.end_read_tracking_details(token)?
|
||||
.into_iter()
|
||||
.map(|read| read.object)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) fn end_read_tracking_details(
|
||||
&self,
|
||||
token: usize,
|
||||
) -> Result<Vec<ReadAccess>, VmError> {
|
||||
let scope = self.read_only.last().ok_or(VmError::InvalidArgument(
|
||||
"read tracking requires a read-only scope",
|
||||
))?;
|
||||
@@ -132,14 +175,49 @@ impl Heap {
|
||||
if token == 0 || token != regions.len() {
|
||||
return Err(VmError::InvalidArgument("read tracking nesting mismatch"));
|
||||
}
|
||||
Ok(regions.pop().unwrap().into_iter().collect())
|
||||
Ok(regions
|
||||
.pop()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|(object, content_read)| ReadAccess {
|
||||
object,
|
||||
content_read,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) fn is_read_only(&self) -> bool {
|
||||
!self.read_only.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn with_validation_reads<T>(
|
||||
&self,
|
||||
compute: impl FnOnce() -> Result<T, VmError>,
|
||||
) -> Result<T, VmError> {
|
||||
if self.read_only.is_empty() {
|
||||
return compute();
|
||||
}
|
||||
let thread = std::thread::current().id();
|
||||
{
|
||||
let mut active = self.validation_reads.lock().expect("read tracker poisoned");
|
||||
let depth = active.entry(thread).or_default();
|
||||
*depth = depth
|
||||
.checked_add(1)
|
||||
.ok_or(VmError::ExecutionLimit("validation scopes"))?;
|
||||
}
|
||||
let _scope = ValidationScope { heap: self, thread };
|
||||
compute()
|
||||
}
|
||||
|
||||
pub(crate) fn record_read(&self, reference: ObjectRef) {
|
||||
if self.read_only.is_empty() {
|
||||
return;
|
||||
}
|
||||
let content = !self
|
||||
.validation_reads
|
||||
.lock()
|
||||
.expect("read tracker poisoned")
|
||||
.contains_key(&std::thread::current().id());
|
||||
for scope in &self.read_only {
|
||||
if scope.protected.contains(&reference) {
|
||||
scope
|
||||
@@ -153,7 +231,10 @@ impl Heap {
|
||||
.expect("read tracker poisoned")
|
||||
.iter_mut()
|
||||
{
|
||||
region.insert(reference);
|
||||
region
|
||||
.entry(reference)
|
||||
.and_modify(|ordinary| *ordinary |= content)
|
||||
.or_insert(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user