Skip to content

Commit

Permalink
Bitpack time and read type
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Mar 18, 2024
1 parent 652a82e commit e8669d9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/concurrency/data_race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl MemoryCellClocks {
if !current_span.is_dummy() {
thread_clocks.clock[index].span = current_span;
}
thread_clocks.clock[index].read_type = read_type;
thread_clocks.clock[index].set_read_type(read_type);
if self.write_was_before(&thread_clocks.clock) {
let race_free = if let Some(atomic) = self.atomic() {
// We must be ordered-after all atomic accesses, reads and writes.
Expand Down
69 changes: 41 additions & 28 deletions src/concurrency/vector_clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use smallvec::SmallVec;
use std::{
cmp::Ordering,
fmt::Debug,
ops::{Index, IndexMut},
ops::{Index, IndexMut, Shr},
};

use super::data_race::NaReadType;
Expand Down Expand Up @@ -52,27 +52,43 @@ const SMALL_VECTOR: usize = 4;
/// so that diagnostics can report what code was responsible for an operation.
#[derive(Clone, Copy, Debug)]
pub struct VTimestamp {
time: u32,
// The lowest bit indicates read type, the rest is the time.
time_and_read_type: u32,
pub span: Span,
pub read_type: NaReadType,
}

impl VTimestamp {
pub const ZERO: VTimestamp =
VTimestamp { time: 0, span: DUMMY_SP, read_type: NaReadType::Read };
pub const ZERO: VTimestamp = VTimestamp { time_and_read_type: 0, span: DUMMY_SP };

fn time(&self) -> u32 {
self.time_and_read_type.shr(1)
}

fn set_time(&mut self, time: u32) {
self.time_and_read_type =
(self.time_and_read_type & 1) | time.checked_shl(1).expect("Vector clock overflow")
}

pub fn span_data(&self) -> SpanData {
self.span.data()
}

pub fn read_type(&self) -> NaReadType {
self.read_type
if self.time_and_read_type & 1 == 0 {
NaReadType::Read
} else {
NaReadType::Retag
}
}

pub fn set_read_type(&mut self, read_type: NaReadType) {
self.time_and_read_type = (self.time_and_read_type & !1) | read_type as u32
}
}

impl PartialEq for VTimestamp {
fn eq(&self, other: &Self) -> bool {
self.time == other.time
self.time() == other.time()
}
}

Expand All @@ -86,7 +102,7 @@ impl PartialOrd for VTimestamp {

impl Ord for VTimestamp {
fn cmp(&self, other: &Self) -> Ordering {
self.time.cmp(&other.time)
self.time().cmp(&other.time())
}
}

Expand Down Expand Up @@ -138,7 +154,7 @@ impl VClock {
let idx = idx.index();
let mut_slice = self.get_mut_with_min_len(idx + 1);
let idx_ref = &mut mut_slice[idx];
idx_ref.time = idx_ref.time.checked_add(1).expect("Vector clock overflow");
idx_ref.set_time(idx_ref.time().checked_add(1).expect("Vector clock overflow"));
if !current_span.is_dummy() {
idx_ref.span = current_span;
}
Expand Down Expand Up @@ -220,14 +236,16 @@ impl PartialOrd for VClock {
for (l, r) in iter {
match order {
Ordering::Equal => order = l.cmp(r),
Ordering::Less =>
Ordering::Less => {
if l > r {
return None;
},
Ordering::Greater =>
}
}
Ordering::Greater => {
if l < r {
return None;
},
}
}
}
}

Expand All @@ -242,18 +260,16 @@ impl PartialOrd for VClock {
Ordering::Equal => Some(order),
// Right has at least 1 element > than the implicit 0,
// so the only valid values are Ordering::Less or None.
Ordering::Less =>
match order {
Ordering::Less | Ordering::Equal => Some(Ordering::Less),
Ordering::Greater => None,
},
Ordering::Less => match order {
Ordering::Less | Ordering::Equal => Some(Ordering::Less),
Ordering::Greater => None,
},
// Left has at least 1 element > than the implicit 0,
// so the only valid values are Ordering::Greater or None.
Ordering::Greater =>
match order {
Ordering::Greater | Ordering::Equal => Some(Ordering::Greater),
Ordering::Less => None,
},
Ordering::Greater => match order {
Ordering::Greater | Ordering::Equal => Some(Ordering::Greater),
Ordering::Less => None,
},
}
}

Expand Down Expand Up @@ -387,12 +403,9 @@ impl IndexMut<VectorIdx> for VClock {
/// test suite
#[cfg(test)]
mod tests {

use crate::concurrency::data_race::NaReadType;

use super::{VClock, VTimestamp, VectorIdx};
use rustc_span::DUMMY_SP;
use std::cmp::Ordering;
use std::{cmp::Ordering, ops::Shl};

#[test]
fn test_equal() {
Expand Down Expand Up @@ -462,7 +475,7 @@ mod tests {
slice
.iter()
.copied()
.map(|time| VTimestamp { time, span: DUMMY_SP, read_type: NaReadType::Read })
.map(|time| VTimestamp { time_and_read_type: time.shl(1), span: DUMMY_SP })
.collect(),
)
}
Expand Down

0 comments on commit e8669d9

Please sign in to comment.