]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #33534 - dns2utf8:atomic_docs, r=GuillaumeGomez
authorManish Goregaokar <manishsmail@gmail.com>
Sun, 15 May 2016 14:43:41 +0000 (20:13 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Sun, 15 May 2016 14:43:41 +0000 (20:13 +0530)
Simplify text

This way it should be clear: Any number of other threads have this guaranty not just one other thread.

1  2 
src/libcore/sync/atomic.rs

index 49b923286d88894c3d9ba347378675680a6886d5,e141a92c3ee3dabe5c2d3dcd0b57f6ead4cdbb5b..52ba8d9a631d89459534b8b093fadae84cfaa96f
@@@ -83,22 -83,22 +83,22 @@@ use default::Default
  use fmt;
  
  /// A boolean type which can be safely shared between threads.
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  #[stable(feature = "rust1", since = "1.0.0")]
  pub struct AtomicBool {
 -    v: UnsafeCell<usize>,
 +    v: UnsafeCell<u8>,
  }
  
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  #[stable(feature = "rust1", since = "1.0.0")]
  impl Default for AtomicBool {
      fn default() -> Self {
 -        Self::new(Default::default())
 +        Self::new(false)
      }
  }
  
  // Send is implicitly implemented for AtomicBool.
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  #[stable(feature = "rust1", since = "1.0.0")]
  unsafe impl Sync for AtomicBool {}
  
@@@ -142,13 -142,13 +142,13 @@@ pub enum Ordering 
      #[stable(feature = "rust1", since = "1.0.0")]
      Relaxed,
      /// When coupled with a store, all previous writes become visible
-     /// to another thread that performs a load with `Acquire` ordering
+     /// to the other threads that perform a load with `Acquire` ordering
      /// on the same value.
      #[stable(feature = "rust1", since = "1.0.0")]
      Release,
      /// When coupled with a load, all subsequent loads will see data
      /// written before a store with `Release` ordering on the same value
-     /// in another thread.
+     /// in other threads.
      #[stable(feature = "rust1", since = "1.0.0")]
      Acquire,
      /// When coupled with a load, uses `Acquire` ordering, and with a store
  }
  
  /// An `AtomicBool` initialized to `false`.
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  #[stable(feature = "rust1", since = "1.0.0")]
  pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
  
 -// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 -const UINT_TRUE: usize = !0;
 -
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  impl AtomicBool {
      /// Creates a new `AtomicBool`.
      ///
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub const fn new(v: bool) -> AtomicBool {
 -        AtomicBool { v: UnsafeCell::new(-(v as isize) as usize) }
 +        AtomicBool { v: UnsafeCell::new(v as u8) }
      }
  
      /// Loads a value from the bool.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn load(&self, order: Ordering) -> bool {
 -        unsafe { atomic_load(self.v.get(), order) > 0 }
 +        unsafe { atomic_load(self.v.get(), order) != 0 }
      }
  
      /// Stores a value into the bool.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn store(&self, val: bool, order: Ordering) {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_store(self.v.get(), val, order); }
 +        unsafe { atomic_store(self.v.get(), val as u8, order); }
      }
  
      /// Stores a value into the bool, returning the old value.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn swap(&self, val: bool, order: Ordering) -> bool {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_swap(self.v.get(), val, order) > 0 }
 +        unsafe { atomic_swap(self.v.get(), val as u8, order) != 0 }
      }
  
      /// Stores a value into the `bool` if the current value is the same as the `current` value.
                              new: bool,
                              success: Ordering,
                              failure: Ordering) -> Result<bool, bool> {
 -        let current = if current { UINT_TRUE } else { 0 };
 -        let new = if new { UINT_TRUE } else { 0 };
 -
 -        match unsafe { atomic_compare_exchange(self.v.get(), current, new, success, failure) } {
 -            Ok(x) => Ok(x > 0),
 -            Err(x) => Err(x > 0),
 +        match unsafe { atomic_compare_exchange(self.v.get(), current as u8, new as u8,
 +                                               success, failure) } {
 +            Ok(x) => Ok(x != 0),
 +            Err(x) => Err(x != 0),
          }
      }
  
                                   new: bool,
                                   success: Ordering,
                                   failure: Ordering) -> Result<bool, bool> {
 -        let current = if current { UINT_TRUE } else { 0 };
 -        let new = if new { UINT_TRUE } else { 0 };
 -
 -        match unsafe { atomic_compare_exchange_weak(self.v.get(), current, new,
 +        match unsafe { atomic_compare_exchange_weak(self.v.get(), current as u8, new as u8,
                                                      success, failure) } {
 -            Ok(x) => Ok(x > 0),
 -            Err(x) => Err(x > 0),
 +            Ok(x) => Ok(x != 0),
 +            Err(x) => Err(x != 0),
          }
      }
  
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_and(self.v.get(), val, order) > 0 }
 +        unsafe { atomic_and(self.v.get(), val as u8, order) != 0 }
      }
  
      /// Logical "nand" with a boolean value.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_nand(self.v.get(), val, order) > 0 }
 +        // We can't use atomic_nand here because it can result in a bool with
 +        // an invalid value. This happens because the atomic operation is done
 +        // with an 8-bit integer internally, which would set the upper 7 bits.
 +        // So we just use a compare-exchange loop instead, which is what the
 +        // intrinsic actually expands to anyways on many platforms.
 +        let mut old = self.load(Relaxed);
 +        loop {
 +            let new = !(old && val);
 +            match self.compare_exchange_weak(old, new, order, Relaxed) {
 +                Ok(_) => break,
 +                Err(x) => old = x,
 +            }
 +        }
 +        old
      }
  
      /// Logical "or" with a boolean value.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_or(self.v.get(), val, order) > 0 }
 +        unsafe { atomic_or(self.v.get(), val as u8, order) != 0 }
      }
  
      /// Logical "xor" with a boolean value.
      #[inline]
      #[stable(feature = "rust1", since = "1.0.0")]
      pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
 -        let val = if val { UINT_TRUE } else { 0 };
 -
 -        unsafe { atomic_xor(self.v.get(), val, order) > 0 }
 +        unsafe { atomic_xor(self.v.get(), val as u8, order) != 0 }
      }
  }
  
@@@ -1255,6 -1263,18 +1255,6 @@@ unsafe fn atomic_and<T>(dst: *mut T, va
      }
  }
  
 -#[inline]
 -unsafe fn atomic_nand<T>(dst: *mut T, val: T, order: Ordering) -> T {
 -    match order {
 -        Acquire => intrinsics::atomic_nand_acq(dst, val),
 -        Release => intrinsics::atomic_nand_rel(dst, val),
 -        AcqRel  => intrinsics::atomic_nand_acqrel(dst, val),
 -        Relaxed => intrinsics::atomic_nand_relaxed(dst, val),
 -        SeqCst  => intrinsics::atomic_nand(dst, val)
 -    }
 -}
 -
 -
  #[inline]
  unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
      match order {
      }
  }
  
 -
  #[inline]
  unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
      match order {
      }
  }
  
 -
  /// An atomic fence.
  ///
  /// A fence 'A' which has `Release` ordering semantics, synchronizes with a
@@@ -1312,7 -1334,7 +1312,7 @@@ pub fn fence(order: Ordering) 
  }
  
  
 -#[cfg(any(stage0, target_has_atomic = "ptr"))]
 +#[cfg(any(stage0, target_has_atomic = "8"))]
  #[stable(feature = "atomic_debug", since = "1.3.0")]
  impl fmt::Debug for AtomicBool {
      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {