Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Use volatile read in steal #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ impl<T> Buffer<T> {
(*slot).stamp.store(index, ord);
}

/// Reads a value from the specified `index`.
unsafe fn read(&self, index: isize, ord: Ordering) -> Option<T> {
/// Volatile read from the specified `index`.
unsafe fn read_volatile(&self, index: isize, ord: Ordering) -> Option<T> {
let slot = self.at(index);
if (*slot).stamp.load(ord) == index {
Some(ptr::read((*slot).value.get()))
Some(ptr::read_volatile((*slot).value.get()))
} else {
None
}
}

/// Reads a value from the specified `index`.
unsafe fn read_unchecked(&self, index: isize) -> T {
/// Volatile read from the specified `index` without stamp checking.
unsafe fn read_volatile_unchecked(&self, index: isize) -> T {
let slot = self.at(index);
ptr::read((*slot).value.get())
}
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<T> Worker<T> {
unsafe {
// Read the value to be popped.
let buffer = self.cached_buffer.get();
let data = buffer.read_unchecked(f);
let data = buffer.read_volatile_unchecked(f);

// Shrink the buffer if `len - 1` is less than one fourth of the capacity.
if buffer.cap > MIN_CAP && len <= buffer.cap as isize / 4 {
Expand Down Expand Up @@ -367,7 +367,7 @@ impl<T> Stealer<T> {
let buffer = self.inner.buffer.load(Ordering::Acquire, guard);

// Read the value at the front.
let value = unsafe { buffer.deref().read(f, Ordering::Acquire)? };
let value = unsafe { buffer.deref().read_volatile(f, Ordering::Acquire)? };

// Try incrementing the front index to steal the value.
if self.inner
Expand Down
7 changes: 6 additions & 1 deletion src/lifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ impl<T> Buffer<T> {
unsafe fn read(&self, index: isize) -> T {
ptr::read(self.at(index))
}

/// Volatile read from the specified `index`.
unsafe fn read_volatile(&self, index: isize) -> T {
ptr::read_volatile(self.at(index))
}
}

impl<T> Clone for Buffer<T> {
Expand Down Expand Up @@ -261,7 +266,7 @@ impl<T> Worker<T> {
} else {
// Read the value to be popped.
let buffer = self.cached_buffer.get();
let mut value = unsafe { Some(buffer.read(b)) };
let mut value = unsafe { Some(buffer.read_volatile(b)) };

// Are we popping the last element from the deque?
if len == 0 {
Expand Down