Merge branch 'master' into master

This commit is contained in:
Jorge Aparicio 2019-12-17 11:47:30 +00:00 committed by GitHub
commit 6658509412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 14 deletions

View File

@ -182,7 +182,7 @@ where
Ok(values)
}
}
deserializer.deserialize_seq(ValueVisitor(PhantomData))
deserializer.deserialize_map(ValueVisitor(PhantomData))
}
}
@ -225,7 +225,7 @@ where
Ok(values)
}
}
deserializer.deserialize_seq(ValueVisitor(PhantomData))
deserializer.deserialize_map(ValueVisitor(PhantomData))
}
}

View File

@ -494,7 +494,11 @@ unsafe fn dequeue<T>(buffer: *mut Cell<T>, dequeue_pos: &AtomicU8, mask: u8) ->
} else if dif < 0 {
return None;
} else {
pos = dequeue_pos.load(Ordering::Relaxed);
if pos == 255 && dif == 255{
return None;
} else {
pos = dequeue_pos.load(Ordering::Relaxed);
}
}
}
@ -560,4 +564,15 @@ mod tests {
assert_eq!(q.dequeue(), Some(1));
assert_eq!(q.dequeue(), None);
}
#[test]
fn blocking(){
let q = Q2::new();
for _ in 0..255 {
q.enqueue(0).unwrap();
q.dequeue();
}
// this should not block forever
q.dequeue();
}
}

View File

@ -21,6 +21,9 @@ unsafe impl XCore for MultiCore {
}
pub unsafe trait Uxx: Into<usize> + Send {
#[doc(hidden)]
fn saturate(x: usize) -> Self;
#[doc(hidden)]
fn truncate(x: usize) -> Self;
@ -39,15 +42,19 @@ pub unsafe trait Uxx: Into<usize> + Send {
}
unsafe impl Uxx for u8 {
fn truncate(x: usize) -> Self {
let max = ::core::u8::MAX;
fn saturate(x: usize) -> Self {
let max = Self::max_value() as usize;
if x >= usize::from(max) {
max
max as Self
} else {
x as u8
x as Self
}
}
fn truncate(x: usize) -> Self {
x as Self
}
unsafe fn load_acquire<C>(x: *const Self) -> Self
where
C: XCore,
@ -79,15 +86,19 @@ unsafe impl Uxx for u8 {
}
unsafe impl Uxx for u16 {
fn truncate(x: usize) -> Self {
let max = ::core::u16::MAX;
fn saturate(x: usize) -> Self {
let max = Self::max_value() as usize;
if x >= usize::from(max) {
max
max as Self
} else {
x as u16
x as Self
}
}
fn truncate(x: usize) -> Self {
x as Self
}
unsafe fn load_acquire<C>(x: *const Self) -> Self
where
C: XCore,
@ -119,6 +130,10 @@ unsafe impl Uxx for u16 {
}
unsafe impl Uxx for usize {
fn saturate(x: usize) -> Self {
x
}
fn truncate(x: usize) -> Self {
x
}

View File

@ -174,7 +174,7 @@ where
{
/// Returns the maximum number of elements the queue can hold
pub fn capacity(&self) -> U {
U::truncate(N::to_usize())
U::saturate(N::to_usize())
}
/// Returns `true` if the queue has a length of 0
@ -205,7 +205,7 @@ where
let head = self.0.head.load_relaxed().into();
let tail = self.0.tail.load_relaxed().into();
tail.wrapping_sub(head)
U::truncate(tail.wrapping_sub(head)).into()
}
}
@ -732,6 +732,20 @@ mod tests {
assert_eq!(items.next_back(), None);
}
#[test]
fn iter_overflow() {
let mut rb: Queue<i32, U4, u8> = Queue::u8();
rb.enqueue(0).unwrap();
for _ in 0..300 {
let mut items = rb.iter_mut();
assert_eq!(items.next(), Some(&mut 0));
assert_eq!(items.next(), None);
rb.dequeue().unwrap();
rb.enqueue(0).unwrap();
}
}
#[test]
fn iter_mut() {
let mut rb: Queue<i32, U4> = Queue::new();
@ -764,6 +778,7 @@ mod tests {
assert_eq!(items.next(), None);
assert_eq!(items.next_back(), None);
}
#[test]
fn sanity() {
let mut rb: Queue<i32, U4> = Queue::new();

View File

@ -96,7 +96,7 @@ where
/// assert!(String::from_utf8(v).is_err());
/// ```
#[inline]
pub fn from_utf8(vec: Vec<u8, N>) -> Result<(String<N>), Utf8Error> {
pub fn from_utf8(vec: Vec<u8, N>) -> Result<String<N>, Utf8Error> {
// validate input
str::from_utf8(&*vec)?;