2015-01-02 07:53:35 +00:00
|
|
|
//! Atomic types
|
|
|
|
//!
|
|
|
|
//! Atomic types provide primitive shared-memory communication between
|
|
|
|
//! threads, and are the building blocks of other concurrent
|
|
|
|
//! types.
|
|
|
|
//!
|
|
|
|
//! This module defines atomic versions of a select number of primitive
|
2019-01-07 20:41:55 +00:00
|
|
|
//! types, including [`AtomicBool`], [`AtomicIsize`], [`AtomicUsize`],
|
|
|
|
//! [`AtomicI8`], [`AtomicU16`], etc.
|
2015-01-02 07:53:35 +00:00
|
|
|
//! Atomic types present operations that, when used correctly, synchronize
|
|
|
|
//! updates between threads.
|
|
|
|
//!
|
2017-03-28 03:55:03 +00:00
|
|
|
//! [`AtomicBool`]: struct.AtomicBool.html
|
|
|
|
//! [`AtomicIsize`]: struct.AtomicIsize.html
|
|
|
|
//! [`AtomicUsize`]: struct.AtomicUsize.html
|
2019-01-07 20:41:55 +00:00
|
|
|
//! [`AtomicI8`]: struct.AtomicI8.html
|
|
|
|
//! [`AtomicU16`]: struct.AtomicU16.html
|
2017-03-28 03:55:03 +00:00
|
|
|
//!
|
|
|
|
//! Each method takes an [`Ordering`] which represents the strength of
|
2015-01-02 07:53:35 +00:00
|
|
|
//! the memory barrier for that operation. These orderings are the
|
2017-01-20 02:39:54 +00:00
|
|
|
//! same as [LLVM atomic orderings][1]. For more information see the [nomicon][2].
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
2017-03-28 03:55:03 +00:00
|
|
|
//! [`Ordering`]: enum.Ordering.html
|
|
|
|
//!
|
2018-08-07 09:22:45 +00:00
|
|
|
//! [1]: https://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations
|
2017-01-27 18:08:51 +00:00
|
|
|
//! [2]: ../../../nomicon/atomics.html
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
2017-03-28 03:55:03 +00:00
|
|
|
//! Atomic variables are safe to share between threads (they implement [`Sync`])
|
2016-05-16 16:54:12 +00:00
|
|
|
//! but they do not themselves provide the mechanism for sharing and follow the
|
|
|
|
//! [threading model](../../../std/thread/index.html#the-threading-model) of rust.
|
2017-03-28 03:55:03 +00:00
|
|
|
//! The most common way to share an atomic variable is to put it into an [`Arc`][arc] (an
|
2015-01-02 07:53:35 +00:00
|
|
|
//! atomically-reference-counted shared pointer).
|
|
|
|
//!
|
2017-03-28 03:55:03 +00:00
|
|
|
//! [`Sync`]: ../../marker/trait.Sync.html
|
2017-03-29 04:52:16 +00:00
|
|
|
//! [arc]: ../../../std/sync/struct.Arc.html
|
2017-03-28 03:55:03 +00:00
|
|
|
//!
|
2019-01-07 20:41:55 +00:00
|
|
|
//! Atomic types may be stored in static variables, initialized using
|
|
|
|
//! the constant initializers like [`AtomicBool::new`]. Atomic statics
|
2015-01-02 07:53:35 +00:00
|
|
|
//! are often used for lazy global initialization.
|
|
|
|
//!
|
2019-01-07 20:41:55 +00:00
|
|
|
//! [`AtomicBool::new`]: struct.AtomicBool.html#method.new
|
|
|
|
//!
|
|
|
|
//! # Portability
|
|
|
|
//!
|
|
|
|
//! All atomic types in this module are guaranteed to be [lock-free] if they're
|
|
|
|
//! available. This means they don't internally acquire a global mutex. Atomic
|
|
|
|
//! types and operations are not guaranteed to be wait-free. This means that
|
|
|
|
//! operations like `fetch_or` may be implemented with a compare-and-swap loop.
|
|
|
|
//!
|
|
|
|
//! Atomic operations may be implemented at the instruction layer with
|
|
|
|
//! larger-size atomics. For example some platforms use 4-byte atomic
|
|
|
|
//! instructions to implement `AtomicI8`. Note that this emulation should not
|
|
|
|
//! have an impact on correctness of code, it's just something to be aware of.
|
|
|
|
//!
|
|
|
|
//! The atomic types in this module may not be available on all platforms. The
|
|
|
|
//! atomic types here are all widely available, however, and can generally be
|
|
|
|
//! relied upon existing. Some notable exceptions are:
|
|
|
|
//!
|
|
|
|
//! * PowerPC and MIPS platforms with 32-bit pointers do not have `AtomicU64` or
|
|
|
|
//! `AtomicI64` types.
|
|
|
|
//! * ARM platforms like `armv5te` that aren't for Linux do not have any atomics
|
|
|
|
//! at all.
|
|
|
|
//! * ARM targets with `thumbv6m` do not have atomic operations at all.
|
|
|
|
//!
|
|
|
|
//! Note that future platforms may be added that also do not have support for
|
|
|
|
//! some atomic operations. Maximally portable code will want to be careful
|
|
|
|
//! about which atomic types are used. `AtomicUsize` and `AtomicIsize` are
|
|
|
|
//! generally the most portable, but even then they're not available everywhere.
|
|
|
|
//! For reference, the `std` library requires pointer-sized atomics, although
|
|
|
|
//! `core` does not.
|
|
|
|
//!
|
|
|
|
//! Currently you'll need to use `#[cfg(target_arch)]` primarily to
|
|
|
|
//! conditionally compile in code with atomics. There is an unstable
|
|
|
|
//! `#[cfg(target_has_atomic)]` as well which may be stabilized in the future.
|
|
|
|
//!
|
|
|
|
//! [lock-free]: https://en.wikipedia.org/wiki/Non-blocking_algorithm
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! A simple spinlock:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::sync::Arc;
|
2015-01-10 21:42:48 +00:00
|
|
|
//! use std::sync::atomic::{AtomicUsize, Ordering};
|
2015-02-17 23:10:25 +00:00
|
|
|
//! use std::thread;
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
2015-01-10 21:42:48 +00:00
|
|
|
//! let spinlock = Arc::new(AtomicUsize::new(1));
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
|
|
|
//! let spinlock_clone = spinlock.clone();
|
2016-05-16 16:54:12 +00:00
|
|
|
//! let thread = thread::spawn(move|| {
|
2015-01-02 07:53:35 +00:00
|
|
|
//! spinlock_clone.store(0, Ordering::SeqCst);
|
2015-01-06 05:59:45 +00:00
|
|
|
//! });
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
2015-05-08 15:12:29 +00:00
|
|
|
//! // Wait for the other thread to release the lock
|
2015-01-02 07:53:35 +00:00
|
|
|
//! while spinlock.load(Ordering::SeqCst) != 0 {}
|
2016-05-16 16:54:12 +00:00
|
|
|
//!
|
|
|
|
//! if let Err(panic) = thread.join() {
|
|
|
|
//! println!("Thread had an error: {:?}", panic);
|
|
|
|
//! }
|
2015-01-02 07:53:35 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2015-05-08 15:12:29 +00:00
|
|
|
//! Keep a global count of live threads:
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
|
|
|
//! ```
|
2019-01-07 20:41:55 +00:00
|
|
|
//! use std::sync::atomic::{AtomicUsize, Ordering};
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
2019-01-07 20:41:55 +00:00
|
|
|
//! static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0);
|
2015-01-02 07:53:35 +00:00
|
|
|
//!
|
2015-05-08 15:12:29 +00:00
|
|
|
//! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
|
|
|
|
//! println!("live threads: {}", old_thread_count + 1);
|
2015-01-02 07:53:35 +00:00
|
|
|
//! ```
|
2014-05-13 04:30:48 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2016-08-02 06:40:18 +00:00
|
|
|
#![cfg_attr(not(target_has_atomic = "8"), allow(dead_code))]
|
|
|
|
#![cfg_attr(not(target_has_atomic = "8"), allow(unused_imports))]
|
2014-08-04 22:42:36 +00:00
|
|
|
|
2014-12-30 18:19:20 +00:00
|
|
|
use self::Ordering::*;
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
use intrinsics;
|
2014-12-22 11:29:46 +00:00
|
|
|
use cell::UnsafeCell;
|
2015-07-19 16:11:30 +00:00
|
|
|
use fmt;
|
2015-04-10 17:23:22 +00:00
|
|
|
|
2018-12-19 21:43:29 +00:00
|
|
|
use hint::spin_loop;
|
|
|
|
|
2017-03-15 04:28:31 +00:00
|
|
|
/// Save power or switch hyperthreads in a busy-wait spin-loop.
|
|
|
|
///
|
|
|
|
/// This function is deliberately more primitive than
|
2018-02-16 11:20:54 +00:00
|
|
|
/// [`std::thread::yield_now`](../../../std/thread/fn.yield_now.html) and
|
2018-02-16 09:30:31 +00:00
|
|
|
/// does not directly yield to the system's scheduler.
|
|
|
|
/// In some cases it might be useful to use a combination of both functions.
|
|
|
|
/// Careful benchmarking is advised.
|
2017-03-15 04:28:31 +00:00
|
|
|
///
|
|
|
|
/// On some platforms this function may not do anything at all.
|
|
|
|
#[inline]
|
2017-11-27 18:24:13 +00:00
|
|
|
#[stable(feature = "spin_loop_hint", since = "1.24.0")]
|
2017-11-21 23:00:01 +00:00
|
|
|
pub fn spin_loop_hint() {
|
2018-12-19 21:43:29 +00:00
|
|
|
spin_loop()
|
2017-03-15 04:28:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// A boolean type which can be safely shared between threads.
|
2016-08-16 15:58:20 +00:00
|
|
|
///
|
2017-10-05 21:20:58 +00:00
|
|
|
/// This type has the same in-memory representation as a [`bool`].
|
|
|
|
///
|
|
|
|
/// [`bool`]: ../../../std/primitive.bool.html
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-10-27 10:46:33 +00:00
|
|
|
#[repr(C, align(1))]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub struct AtomicBool {
|
2016-05-12 15:12:05 +00:00
|
|
|
v: UnsafeCell<u8>,
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-10 17:23:22 +00:00
|
|
|
impl Default for AtomicBool {
|
2016-10-02 12:45:49 +00:00
|
|
|
/// Creates an `AtomicBool` initialized to `false`.
|
2015-05-27 08:18:36 +00:00
|
|
|
fn default() -> Self {
|
2016-05-12 15:12:05 +00:00
|
|
|
Self::new(false)
|
2015-04-10 17:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 11:12:25 +00:00
|
|
|
// Send is implicitly implemented for AtomicBool.
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-22 11:29:46 +00:00
|
|
|
unsafe impl Sync for AtomicBool {}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// A raw pointer type which can be safely shared between threads.
|
2016-08-16 15:58:20 +00:00
|
|
|
///
|
|
|
|
/// This type has the same in-memory representation as a `*mut T`.
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-10-27 10:46:33 +00:00
|
|
|
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
|
|
|
|
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
|
|
|
|
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub struct AtomicPtr<T> {
|
2015-05-27 08:18:36 +00:00
|
|
|
p: UnsafeCell<*mut T>,
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-04-26 02:55:15 +00:00
|
|
|
impl<T> Default for AtomicPtr<T> {
|
2016-09-11 17:28:01 +00:00
|
|
|
/// Creates a null `AtomicPtr<T>`.
|
2015-04-26 02:55:15 +00:00
|
|
|
fn default() -> AtomicPtr<T> {
|
|
|
|
AtomicPtr::new(::ptr::null_mut())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-07-15 12:09:04 +00:00
|
|
|
unsafe impl<T> Send for AtomicPtr<T> {}
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2015-11-16 16:54:28 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-22 11:29:46 +00:00
|
|
|
unsafe impl<T> Sync for AtomicPtr<T> {}
|
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
/// Atomic memory orderings
|
|
|
|
///
|
2018-11-17 13:48:18 +00:00
|
|
|
/// Memory orderings specify the way atomic operations synchronize memory.
|
|
|
|
/// In its weakest [`Relaxed`][Ordering::Relaxed], only the memory directly touched by the
|
|
|
|
/// operation is synchronized. On the other hand, a store-load pair of [`SeqCst`][Ordering::SeqCst]
|
|
|
|
/// operations synchronize other memory while additionally preserving a total order of such
|
|
|
|
/// operations across all threads.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Rust's memory orderings are [the same as
|
2018-08-07 09:22:45 +00:00
|
|
|
/// LLVM's](https://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations).
|
2017-01-20 02:43:43 +00:00
|
|
|
///
|
2017-04-06 11:57:40 +00:00
|
|
|
/// For more information see the [nomicon].
|
|
|
|
///
|
|
|
|
/// [nomicon]: ../../../nomicon/atomics.html
|
2018-11-17 13:48:18 +00:00
|
|
|
/// [Ordering::Relaxed]: #variant.Relaxed
|
|
|
|
/// [Ordering::SeqCst]: #variant.SeqCst
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-12-16 16:37:18 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
2018-08-15 15:56:29 +00:00
|
|
|
#[non_exhaustive]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub enum Ordering {
|
2017-03-29 04:52:16 +00:00
|
|
|
/// No ordering constraints, only atomic operations.
|
|
|
|
///
|
|
|
|
/// Corresponds to LLVM's [`Monotonic`] ordering.
|
|
|
|
///
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`Monotonic`]: https://llvm.org/docs/Atomics.html#monotonic
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed,
|
2018-08-06 10:05:59 +00:00
|
|
|
/// When coupled with a store, all previous operations become ordered
|
|
|
|
/// before any load of this value with [`Acquire`] (or stronger) ordering.
|
|
|
|
/// In particular, all previous writes become visible to all threads
|
|
|
|
/// that perform an [`Acquire`] (or stronger) load of this value.
|
2017-03-29 04:52:16 +00:00
|
|
|
///
|
2018-08-06 10:05:59 +00:00
|
|
|
/// Notice that using this ordering for an operation that combines loads
|
|
|
|
/// and stores leads to a [`Relaxed`] load operation!
|
|
|
|
///
|
|
|
|
/// This ordering is only applicable for operations that can perform a store.
|
|
|
|
///
|
|
|
|
/// Corresponds to LLVM's [`Release`] ordering.
|
|
|
|
///
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`Release`]: https://llvm.org/docs/Atomics.html#release
|
|
|
|
/// [`Acquire`]: https://llvm.org/docs/Atomics.html#acquire
|
2018-08-06 10:05:59 +00:00
|
|
|
/// [`Relaxed`]: https://llvm.org/docs/Atomics.html#monotonic
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
Release,
|
2018-08-06 10:05:59 +00:00
|
|
|
/// When coupled with a load, if the loaded value was written by a store operation with
|
|
|
|
/// [`Release`] (or stronger) ordering, then all subsequent operations
|
|
|
|
/// become ordered after that store. In particular, all subsequent loads will see data
|
|
|
|
/// written before the store.
|
2017-03-29 04:52:16 +00:00
|
|
|
///
|
2018-08-06 10:05:59 +00:00
|
|
|
/// Notice that using this ordering for an operation that combines loads
|
|
|
|
/// and stores leads to a [`Relaxed`] store operation!
|
|
|
|
///
|
|
|
|
/// This ordering is only applicable for operations that can perform a load.
|
|
|
|
///
|
|
|
|
/// Corresponds to LLVM's [`Acquire`] ordering.
|
|
|
|
///
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`Acquire`]: https://llvm.org/docs/Atomics.html#acquire
|
|
|
|
/// [`Release`]: https://llvm.org/docs/Atomics.html#release
|
2018-08-06 10:05:59 +00:00
|
|
|
/// [`Relaxed`]: https://llvm.org/docs/Atomics.html#monotonic
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
Acquire,
|
2018-08-06 10:05:59 +00:00
|
|
|
/// Has the effects of both [`Acquire`] and [`Release`] together:
|
|
|
|
/// For loads it uses [`Acquire`] ordering. For stores it uses the [`Release`] ordering.
|
|
|
|
///
|
|
|
|
/// Notice that in the case of `compare_and_swap`, it is possible that the operation ends up
|
2018-11-17 13:48:18 +00:00
|
|
|
/// not performing any store and hence it has just [`Acquire`] ordering. However,
|
|
|
|
/// [`AcqRel`][`AcquireRelease`] will never perform [`Relaxed`] accesses.
|
2018-03-19 13:33:39 +00:00
|
|
|
///
|
2018-03-25 12:19:27 +00:00
|
|
|
/// This ordering is only applicable for operations that combine both loads and stores.
|
|
|
|
///
|
2018-08-06 10:05:59 +00:00
|
|
|
/// Corresponds to LLVM's [`AcquireRelease`] ordering.
|
2017-03-29 04:52:16 +00:00
|
|
|
///
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`AcquireRelease`]: https://llvm.org/docs/Atomics.html#acquirerelease
|
|
|
|
/// [`Acquire`]: https://llvm.org/docs/Atomics.html#acquire
|
|
|
|
/// [`Release`]: https://llvm.org/docs/Atomics.html#release
|
2018-08-06 10:05:59 +00:00
|
|
|
/// [`Relaxed`]: https://llvm.org/docs/Atomics.html#monotonic
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
AcqRel,
|
2018-08-06 10:05:59 +00:00
|
|
|
/// Like [`Acquire`]/[`Release`]/[`AcqRel`] (for load, store, and load-with-store
|
|
|
|
/// operations, respectively) with the additional guarantee that all threads see all
|
2014-05-13 04:30:48 +00:00
|
|
|
/// sequentially consistent operations in the same order.
|
2018-08-06 10:05:59 +00:00
|
|
|
///
|
|
|
|
/// Corresponds to LLVM's [`SequentiallyConsistent`] ordering.
|
|
|
|
///
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`SequentiallyConsistent`]: https://llvm.org/docs/Atomics.html#sequentiallyconsistent
|
|
|
|
/// [`Acquire`]: https://llvm.org/docs/Atomics.html#acquire
|
|
|
|
/// [`Release`]: https://llvm.org/docs/Atomics.html#release
|
2018-08-06 10:05:59 +00:00
|
|
|
/// [`AcqRel`]: https://llvm.org/docs/Atomics.html#acquirerelease
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-19 15:35:47 +00:00
|
|
|
SeqCst,
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 03:55:03 +00:00
|
|
|
/// An [`AtomicBool`] initialized to `false`.
|
2017-03-29 04:52:16 +00:00
|
|
|
///
|
2017-03-28 03:55:03 +00:00
|
|
|
/// [`AtomicBool`]: struct.AtomicBool.html
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-01-30 16:47:36 +00:00
|
|
|
#[cfg_attr(not(stage0), rustc_deprecated(
|
|
|
|
since = "1.34.0",
|
|
|
|
reason = "the `new` function is now preferred",
|
|
|
|
suggestion = "AtomicBool::new(false)",
|
|
|
|
))]
|
|
|
|
#[cfg_attr(stage0, rustc_deprecated(
|
|
|
|
since = "1.34.0",
|
|
|
|
reason = "the `new` function is now preferred",
|
|
|
|
))]
|
2015-05-27 08:18:36 +00:00
|
|
|
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
|
2014-05-13 04:30:48 +00:00
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2014-05-13 04:30:48 +00:00
|
|
|
impl AtomicBool {
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Creates a new `AtomicBool`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicBool;
|
|
|
|
///
|
|
|
|
/// let atomic_true = AtomicBool::new(true);
|
|
|
|
/// let atomic_false = AtomicBool::new(false);
|
|
|
|
/// ```
|
2014-10-27 16:14:57 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-27 08:18:36 +00:00
|
|
|
pub const fn new(v: bool) -> AtomicBool {
|
2016-05-12 15:12:05 +00:00
|
|
|
AtomicBool { v: UnsafeCell::new(v as u8) }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:20:58 +00:00
|
|
|
/// Returns a mutable reference to the underlying [`bool`].
|
2016-08-16 15:58:20 +00:00
|
|
|
///
|
|
|
|
/// This is safe because the mutable reference guarantees that no other threads are
|
|
|
|
/// concurrently accessing the atomic data.
|
|
|
|
///
|
2017-10-05 21:20:58 +00:00
|
|
|
/// [`bool`]: ../../../std/primitive.bool.html
|
|
|
|
///
|
2016-08-16 15:58:20 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let mut some_bool = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(*some_bool.get_mut(), true);
|
|
|
|
/// *some_bool.get_mut() = false;
|
|
|
|
/// assert_eq!(some_bool.load(Ordering::SeqCst), false);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2016-12-14 20:13:12 +00:00
|
|
|
#[stable(feature = "atomic_access", since = "1.15.0")]
|
2016-08-16 15:58:20 +00:00
|
|
|
pub fn get_mut(&mut self) -> &mut bool {
|
|
|
|
unsafe { &mut *(self.v.get() as *mut bool) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes the atomic and returns the contained value.
|
|
|
|
///
|
|
|
|
/// This is safe because passing `self` by value guarantees that no other threads are
|
|
|
|
/// concurrently accessing the atomic data.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicBool;
|
|
|
|
///
|
|
|
|
/// let some_bool = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(some_bool.into_inner(), true);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2016-12-14 20:13:12 +00:00
|
|
|
#[stable(feature = "atomic_access", since = "1.15.0")]
|
2016-08-16 15:58:20 +00:00
|
|
|
pub fn into_inner(self) -> bool {
|
2018-01-05 01:11:20 +00:00
|
|
|
self.v.into_inner() != 0
|
2016-08-16 15:58:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Loads a value from the bool.
|
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `load` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:33:20 +00:00
|
|
|
/// of this operation. Possible values are [`SeqCst`], [`Acquire`] and [`Relaxed`].
|
2014-08-04 22:42:36 +00:00
|
|
|
///
|
2014-11-11 18:36:09 +00:00
|
|
|
/// # Panics
|
2014-08-04 22:42:36 +00:00
|
|
|
///
|
2016-12-26 09:51:25 +00:00
|
|
|
/// Panics if `order` is [`Release`] or [`AcqRel`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-26 09:51:25 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
2017-03-28 03:55:03 +00:00
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let some_bool = AtomicBool::new(true);
|
|
|
|
///
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), true);
|
2014-11-19 15:35:47 +00:00
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn load(&self, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
unsafe { atomic_load(self.v.get(), order) != 0 }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Stores a value into the bool.
|
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `store` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:33:20 +00:00
|
|
|
/// of this operation. Possible values are [`SeqCst`], [`Release`] and [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `order` is [`Acquire`] or [`AcqRel`].
|
2016-12-26 09:51:25 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let some_bool = AtomicBool::new(true);
|
|
|
|
///
|
|
|
|
/// some_bool.store(false, Ordering::Relaxed);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
2014-11-19 15:35:47 +00:00
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn store(&self, val: bool, order: Ordering) {
|
2016-10-16 16:41:01 +00:00
|
|
|
unsafe {
|
|
|
|
atomic_store(self.v.get(), val as u8, order);
|
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 14:23:46 +00:00
|
|
|
/// Stores a value into the bool, returning the previous value.
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `swap` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:57:43 +00:00
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
2016-12-26 09:51:25 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let some_bool = AtomicBool::new(true);
|
|
|
|
///
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(some_bool.swap(false, Ordering::Relaxed), true);
|
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
2014-11-19 15:35:47 +00:00
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn swap(&self, val: bool, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
unsafe { atomic_swap(self.v.get(), val as u8, order) != 0 }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:20:58 +00:00
|
|
|
/// Stores a value into the [`bool`] if the current value is the same as the `current` value.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2015-06-29 18:44:40 +00:00
|
|
|
/// The return value is always the previous value. If it is equal to `current`, then the value
|
|
|
|
/// was updated.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_and_swap` also takes an [`Ordering`] argument which describes the memory
|
2018-08-06 10:05:59 +00:00
|
|
|
/// ordering of this operation. Notice that even when using [`AcqRel`], the operation
|
|
|
|
/// might fail and hence just perform an `Acquire` load, but not have `Release` semantics.
|
2018-08-07 09:57:43 +00:00
|
|
|
/// Using [`Acquire`] makes the store part of this operation [`Relaxed`] if it
|
|
|
|
/// happens, and using [`Release`] makes the load part [`Relaxed`].
|
2016-12-26 09:51:25 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
2018-08-07 09:22:45 +00:00
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2017-10-05 21:20:58 +00:00
|
|
|
/// [`bool`]: ../../../std/primitive.bool.html
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2014-11-19 15:35:47 +00:00
|
|
|
/// # Examples
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2014-11-19 15:35:47 +00:00
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let some_bool = AtomicBool::new(true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(some_bool.compare_and_swap(true, false, Ordering::Relaxed), true);
|
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
|
|
|
///
|
|
|
|
/// assert_eq!(some_bool.compare_and_swap(true, true, Ordering::Relaxed), false);
|
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2015-06-29 18:44:40 +00:00
|
|
|
pub fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool {
|
2016-03-14 10:57:50 +00:00
|
|
|
match self.compare_exchange(current, new, order, strongest_failure_ordering(order)) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(x) => x,
|
|
|
|
}
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:20:58 +00:00
|
|
|
/// Stores a value into the [`bool`] if the current value is the same as the `current` value.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2016-03-14 10:57:50 +00:00
|
|
|
/// The return value is a result indicating whether the new value was written and containing
|
2016-06-02 17:36:10 +00:00
|
|
|
/// the previous value. On success this value is guaranteed to be equal to `current`.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_exchange` takes two [`Ordering`] arguments to describe the memory
|
2016-12-26 09:51:25 +00:00
|
|
|
/// ordering of this operation. The first describes the required ordering if the
|
|
|
|
/// operation succeeds while the second describes the required ordering when the
|
2018-08-07 09:57:43 +00:00
|
|
|
/// operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
/// and must be equivalent to or weaker than the success ordering.
|
|
|
|
///
|
2016-12-26 09:51:25 +00:00
|
|
|
///
|
2017-10-05 21:20:58 +00:00
|
|
|
/// [`bool`]: ../../../std/primitive.bool.html
|
2016-12-26 09:51:25 +00:00
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-26 09:51:25 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let some_bool = AtomicBool::new(true);
|
|
|
|
///
|
|
|
|
/// assert_eq!(some_bool.compare_exchange(true,
|
|
|
|
/// false,
|
|
|
|
/// Ordering::Acquire,
|
|
|
|
/// Ordering::Relaxed),
|
2016-03-14 10:57:50 +00:00
|
|
|
/// Ok(true));
|
2016-01-17 05:11:31 +00:00
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
|
|
|
///
|
|
|
|
/// assert_eq!(some_bool.compare_exchange(true, true,
|
|
|
|
/// Ordering::SeqCst,
|
|
|
|
/// Ordering::Acquire),
|
2016-03-14 10:57:50 +00:00
|
|
|
/// Err(false));
|
2016-01-17 05:11:31 +00:00
|
|
|
/// assert_eq!(some_bool.load(Ordering::Relaxed), false);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
#[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
pub fn compare_exchange(&self,
|
|
|
|
current: bool,
|
|
|
|
new: bool,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<bool, bool> {
|
|
|
|
match unsafe {
|
|
|
|
atomic_compare_exchange(self.v.get(), current as u8, new as u8, success, failure)
|
|
|
|
} {
|
2016-05-12 15:12:05 +00:00
|
|
|
Ok(x) => Ok(x != 0),
|
|
|
|
Err(x) => Err(x != 0),
|
2016-03-14 10:57:50 +00:00
|
|
|
}
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:20:58 +00:00
|
|
|
/// Stores a value into the [`bool`] if the current value is the same as the `current` value.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// Unlike [`compare_exchange`], this function is allowed to spuriously fail even when the
|
2016-01-17 05:11:31 +00:00
|
|
|
/// comparison succeeds, which can result in more efficient code on some platforms. The
|
2016-03-14 10:57:50 +00:00
|
|
|
/// return value is a result indicating whether the new value was written and containing the
|
|
|
|
/// previous value.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
|
2018-08-07 09:57:43 +00:00
|
|
|
/// ordering of this operation. The first describes the required ordering if the
|
|
|
|
/// operation succeeds while the second describes the required ordering when the
|
|
|
|
/// operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
/// and must be equivalent to or weaker than the success ordering.
|
2016-12-26 09:51:25 +00:00
|
|
|
///
|
2017-10-05 21:20:58 +00:00
|
|
|
/// [`bool`]: ../../../std/primitive.bool.html
|
2017-03-29 04:52:16 +00:00
|
|
|
/// [`compare_exchange`]: #method.compare_exchange
|
2016-12-26 09:51:25 +00:00
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-26 09:51:25 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
///
|
|
|
|
/// let val = AtomicBool::new(false);
|
|
|
|
///
|
|
|
|
/// let new = true;
|
|
|
|
/// let mut old = val.load(Ordering::Relaxed);
|
|
|
|
/// loop {
|
2016-03-14 10:57:50 +00:00
|
|
|
/// match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
|
|
|
|
/// Ok(_) => break,
|
|
|
|
/// Err(x) => old = x,
|
2016-01-17 05:11:31 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
#[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
pub fn compare_exchange_weak(&self,
|
|
|
|
current: bool,
|
|
|
|
new: bool,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<bool, bool> {
|
|
|
|
match unsafe {
|
|
|
|
atomic_compare_exchange_weak(self.v.get(), current as u8, new as u8, success, failure)
|
|
|
|
} {
|
2016-05-12 15:12:05 +00:00
|
|
|
Ok(x) => Ok(x != 0),
|
|
|
|
Err(x) => Err(x != 0),
|
2016-03-14 10:57:50 +00:00
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Logical "and" with a boolean value.
|
|
|
|
///
|
|
|
|
/// Performs a logical "and" operation on the current value and the argument `val`, and sets
|
|
|
|
/// the new value to the result.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
2018-08-07 09:57:43 +00:00
|
|
|
/// `fetch_and` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
///
|
2014-05-13 04:30:48 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-02 07:53:35 +00:00
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_and(false, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_and(true, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(false);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_and(false, Ordering::SeqCst), false);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
unsafe { atomic_and(self.v.get(), val as u8, order) != 0 }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Logical "nand" with a boolean value.
|
|
|
|
///
|
|
|
|
/// Performs a logical "nand" operation on the current value and the argument `val`, and sets
|
|
|
|
/// the new value to the result.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
2018-08-07 09:57:43 +00:00
|
|
|
/// `fetch_nand` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
///
|
2014-05-13 04:30:48 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-02 07:53:35 +00:00
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_nand(true, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst) as usize, 0);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(false);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), false);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
// 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.
|
2017-04-07 16:04:15 +00:00
|
|
|
// So we just use fetch_xor or swap instead.
|
2017-04-07 15:28:55 +00:00
|
|
|
if val {
|
|
|
|
// !(x & true) == !x
|
|
|
|
// We must invert the bool.
|
|
|
|
self.fetch_xor(true, order)
|
|
|
|
} else {
|
|
|
|
// !(x & false) == true
|
2017-04-07 16:04:15 +00:00
|
|
|
// We must set the bool to true.
|
|
|
|
self.swap(true, order)
|
2016-05-12 15:12:05 +00:00
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Logical "or" with a boolean value.
|
|
|
|
///
|
|
|
|
/// Performs a logical "or" operation on the current value and the argument `val`, and sets the
|
|
|
|
/// new value to the result.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
2018-08-07 09:57:43 +00:00
|
|
|
/// `fetch_or` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
///
|
2014-05-13 04:30:48 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-02 07:53:35 +00:00
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_or(false, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_or(true, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(false);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_or(false, Ordering::SeqCst), false);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
unsafe { atomic_or(self.v.get(), val as u8, order) != 0 }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Logical "xor" with a boolean value.
|
|
|
|
///
|
|
|
|
/// Performs a logical "xor" operation on the current value and the argument `val`, and sets
|
|
|
|
/// the new value to the result.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
2018-08-07 09:57:43 +00:00
|
|
|
/// `fetch_xor` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
///
|
2014-05-13 04:30:48 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-02 07:53:35 +00:00
|
|
|
/// use std::sync::atomic::{AtomicBool, Ordering};
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), true);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(true);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_xor(true, Ordering::SeqCst), true);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
|
|
|
/// let foo = AtomicBool::new(false);
|
2015-06-10 14:53:09 +00:00
|
|
|
/// assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), false);
|
|
|
|
/// assert_eq!(foo.load(Ordering::SeqCst), false);
|
2014-05-13 04:30:48 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
|
2016-05-12 15:12:05 +00:00
|
|
|
unsafe { atomic_xor(self.v.get(), val as u8, order) != 0 }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2014-05-13 04:30:48 +00:00
|
|
|
impl<T> AtomicPtr<T> {
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Creates a new `AtomicPtr`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicPtr;
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let ptr = &mut 5;
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let atomic_ptr = AtomicPtr::new(ptr);
|
|
|
|
/// ```
|
2014-10-27 16:14:57 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-27 08:18:36 +00:00
|
|
|
pub const fn new(p: *mut T) -> AtomicPtr<T> {
|
|
|
|
AtomicPtr { p: UnsafeCell::new(p) }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:58:20 +00:00
|
|
|
/// Returns a mutable reference to the underlying pointer.
|
|
|
|
///
|
|
|
|
/// This is safe because the mutable reference guarantees that no other threads are
|
|
|
|
/// concurrently accessing the atomic data.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
|
|
|
/// let mut atomic_ptr = AtomicPtr::new(&mut 10);
|
|
|
|
/// *atomic_ptr.get_mut() = &mut 5;
|
|
|
|
/// assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2016-12-14 20:13:12 +00:00
|
|
|
#[stable(feature = "atomic_access", since = "1.15.0")]
|
2016-08-16 15:58:20 +00:00
|
|
|
pub fn get_mut(&mut self) -> &mut *mut T {
|
|
|
|
unsafe { &mut *self.p.get() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes the atomic and returns the contained value.
|
|
|
|
///
|
|
|
|
/// This is safe because passing `self` by value guarantees that no other threads are
|
|
|
|
/// concurrently accessing the atomic data.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicPtr;
|
|
|
|
///
|
|
|
|
/// let atomic_ptr = AtomicPtr::new(&mut 5);
|
|
|
|
/// assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2016-12-14 20:13:12 +00:00
|
|
|
#[stable(feature = "atomic_access", since = "1.15.0")]
|
2016-08-16 15:58:20 +00:00
|
|
|
pub fn into_inner(self) -> *mut T {
|
2018-01-05 01:11:20 +00:00
|
|
|
self.p.into_inner()
|
2016-08-16 15:58:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Loads a value from the pointer.
|
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `load` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:33:20 +00:00
|
|
|
/// of this operation. Possible values are [`SeqCst`], [`Acquire`] and [`Relaxed`].
|
2014-08-04 22:42:36 +00:00
|
|
|
///
|
2014-11-11 18:36:09 +00:00
|
|
|
/// # Panics
|
2014-08-04 22:42:36 +00:00
|
|
|
///
|
2016-12-27 12:52:30 +00:00
|
|
|
/// Panics if `order` is [`Release`] or [`AcqRel`].
|
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-27 12:52:30 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
2016-12-27 12:52:30 +00:00
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let ptr = &mut 5;
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let some_ptr = AtomicPtr::new(ptr);
|
|
|
|
///
|
|
|
|
/// let value = some_ptr.load(Ordering::Relaxed);
|
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn load(&self, order: Ordering) -> *mut T {
|
2016-10-16 16:41:01 +00:00
|
|
|
unsafe { atomic_load(self.p.get() as *mut usize, order) as *mut T }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 15:35:47 +00:00
|
|
|
/// Stores a value into the pointer.
|
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `store` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:33:20 +00:00
|
|
|
/// of this operation. Possible values are [`SeqCst`], [`Release`] and [`Relaxed`].
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `order` is [`Acquire`] or [`AcqRel`].
|
2016-12-27 12:52:30 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let ptr = &mut 5;
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let some_ptr = AtomicPtr::new(ptr);
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let other_ptr = &mut 10;
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// some_ptr.store(other_ptr, Ordering::Relaxed);
|
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn store(&self, ptr: *mut T, order: Ordering) {
|
2016-10-16 16:41:01 +00:00
|
|
|
unsafe {
|
|
|
|
atomic_store(self.p.get() as *mut usize, ptr as usize, order);
|
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 14:23:46 +00:00
|
|
|
/// Stores a value into the pointer, returning the previous value.
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `swap` takes an [`Ordering`] argument which describes the memory ordering
|
2018-08-07 09:57:43 +00:00
|
|
|
/// of this operation. All ordering modes are possible. Note that using
|
|
|
|
/// [`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
/// using [`Release`] makes the load part [`Relaxed`].
|
2016-12-27 12:52:30 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let ptr = &mut 5;
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let some_ptr = AtomicPtr::new(ptr);
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let other_ptr = &mut 10;
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
|
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
|
2015-05-27 08:18:36 +00:00
|
|
|
unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T }
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 18:44:40 +00:00
|
|
|
/// Stores a value into the pointer if the current value is the same as the `current` value.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2015-06-29 18:44:40 +00:00
|
|
|
/// The return value is always the previous value. If it is equal to `current`, then the value
|
|
|
|
/// was updated.
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_and_swap` also takes an [`Ordering`] argument which describes the memory
|
2018-08-07 09:26:05 +00:00
|
|
|
/// ordering of this operation. Notice that even when using [`AcqRel`], the operation
|
|
|
|
/// might fail and hence just perform an `Acquire` load, but not have `Release` semantics.
|
2018-08-07 09:57:43 +00:00
|
|
|
/// Using [`Acquire`] makes the store part of this operation [`Relaxed`] if it
|
|
|
|
/// happens, and using [`Release`] makes the load part [`Relaxed`].
|
2016-12-27 12:52:30 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let ptr = &mut 5;
|
2014-11-19 15:35:47 +00:00
|
|
|
/// let some_ptr = AtomicPtr::new(ptr);
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let other_ptr = &mut 10;
|
|
|
|
/// let another_ptr = &mut 10;
|
2014-11-19 15:35:47 +00:00
|
|
|
///
|
|
|
|
/// let value = some_ptr.compare_and_swap(other_ptr, another_ptr, Ordering::Relaxed);
|
|
|
|
/// ```
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2015-06-29 18:44:40 +00:00
|
|
|
pub fn compare_and_swap(&self, current: *mut T, new: *mut T, order: Ordering) -> *mut T {
|
2016-03-14 10:57:50 +00:00
|
|
|
match self.compare_exchange(current, new, order, strongest_failure_ordering(order)) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(x) => x,
|
|
|
|
}
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores a value into the pointer if the current value is the same as the `current` value.
|
|
|
|
///
|
2016-03-14 10:57:50 +00:00
|
|
|
/// The return value is a result indicating whether the new value was written and containing
|
2016-06-02 17:36:10 +00:00
|
|
|
/// the previous value. On success this value is guaranteed to be equal to `current`.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_exchange` takes two [`Ordering`] arguments to describe the memory
|
2018-08-07 09:57:43 +00:00
|
|
|
/// ordering of this operation. The first describes the required ordering if the
|
|
|
|
/// operation succeeds while the second describes the required ordering when the
|
|
|
|
/// operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
/// and must be equivalent to or weaker than the success ordering.
|
2016-12-27 12:52:30 +00:00
|
|
|
///
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-27 12:52:30 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
|
|
|
/// let ptr = &mut 5;
|
|
|
|
/// let some_ptr = AtomicPtr::new(ptr);
|
|
|
|
///
|
|
|
|
/// let other_ptr = &mut 10;
|
|
|
|
/// let another_ptr = &mut 10;
|
|
|
|
///
|
|
|
|
/// let value = some_ptr.compare_exchange(other_ptr, another_ptr,
|
|
|
|
/// Ordering::SeqCst, Ordering::Relaxed);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
#[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
pub fn compare_exchange(&self,
|
|
|
|
current: *mut T,
|
|
|
|
new: *mut T,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<*mut T, *mut T> {
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe {
|
2016-03-14 10:57:50 +00:00
|
|
|
let res = atomic_compare_exchange(self.p.get() as *mut usize,
|
|
|
|
current as usize,
|
|
|
|
new as usize,
|
|
|
|
success,
|
|
|
|
failure);
|
|
|
|
match res {
|
|
|
|
Ok(x) => Ok(x as *mut T),
|
|
|
|
Err(x) => Err(x as *mut T),
|
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 05:11:31 +00:00
|
|
|
|
|
|
|
/// Stores a value into the pointer if the current value is the same as the `current` value.
|
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// Unlike [`compare_exchange`], this function is allowed to spuriously fail even when the
|
2016-01-17 05:11:31 +00:00
|
|
|
/// comparison succeeds, which can result in more efficient code on some platforms. The
|
2016-03-14 10:57:50 +00:00
|
|
|
/// return value is a result indicating whether the new value was written and containing the
|
|
|
|
/// previous value.
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// `compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
|
2018-08-07 09:57:43 +00:00
|
|
|
/// ordering of this operation. The first describes the required ordering if the
|
|
|
|
/// operation succeeds while the second describes the required ordering when the
|
|
|
|
/// operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
/// of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
/// [`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
/// and must be equivalent to or weaker than the success ordering.
|
2016-12-27 12:52:30 +00:00
|
|
|
///
|
2017-03-29 04:52:16 +00:00
|
|
|
/// [`compare_exchange`]: #method.compare_exchange
|
2016-12-27 12:52:30 +00:00
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2016-12-27 12:52:30 +00:00
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2016-01-17 05:11:31 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicPtr, Ordering};
|
|
|
|
///
|
|
|
|
/// let some_ptr = AtomicPtr::new(&mut 5);
|
|
|
|
///
|
|
|
|
/// let new = &mut 10;
|
|
|
|
/// let mut old = some_ptr.load(Ordering::Relaxed);
|
|
|
|
/// loop {
|
2016-03-14 10:57:50 +00:00
|
|
|
/// match some_ptr.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
|
|
|
|
/// Ok(_) => break,
|
|
|
|
/// Err(x) => old = x,
|
2016-01-17 05:11:31 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
#[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
pub fn compare_exchange_weak(&self,
|
|
|
|
current: *mut T,
|
|
|
|
new: *mut T,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<*mut T, *mut T> {
|
2016-03-14 10:57:50 +00:00
|
|
|
unsafe {
|
|
|
|
let res = atomic_compare_exchange_weak(self.p.get() as *mut usize,
|
|
|
|
current as usize,
|
|
|
|
new as usize,
|
|
|
|
success,
|
|
|
|
failure);
|
|
|
|
match res {
|
|
|
|
Ok(x) => Ok(x as *mut T),
|
|
|
|
Err(x) => Err(x as *mut T),
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 01:23:04 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2017-11-27 21:26:19 +00:00
|
|
|
#[stable(feature = "atomic_bool_from", since = "1.24.0")]
|
2017-11-27 01:23:04 +00:00
|
|
|
impl From<bool> for AtomicBool {
|
2018-12-11 22:41:12 +00:00
|
|
|
/// Converts a `bool` into an `AtomicBool`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicBool;
|
|
|
|
/// let atomic_bool = AtomicBool::from(true);
|
|
|
|
/// assert_eq!(format!("{:?}", atomic_bool), "true")
|
|
|
|
/// ```
|
2017-11-27 01:23:04 +00:00
|
|
|
#[inline]
|
|
|
|
fn from(b: bool) -> Self { Self::new(b) }
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:20:55 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2017-10-31 19:15:10 +00:00
|
|
|
#[stable(feature = "atomic_from", since = "1.23.0")]
|
2017-10-29 09:28:54 +00:00
|
|
|
impl<T> From<*mut T> for AtomicPtr<T> {
|
|
|
|
#[inline]
|
|
|
|
fn from(p: *mut T) -> Self { Self::new(p) }
|
|
|
|
}
|
|
|
|
|
2017-05-19 04:33:08 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2016-04-16 04:21:53 +00:00
|
|
|
macro_rules! atomic_int {
|
2017-11-26 22:19:18 +00:00
|
|
|
($stable:meta,
|
2016-04-16 04:21:53 +00:00
|
|
|
$stable_cxchg:meta,
|
|
|
|
$stable_debug:meta,
|
2016-08-16 15:58:20 +00:00
|
|
|
$stable_access:meta,
|
2017-12-27 14:11:05 +00:00
|
|
|
$stable_from:meta,
|
2018-02-09 18:19:52 +00:00
|
|
|
$stable_nand:meta,
|
2017-09-25 23:39:52 +00:00
|
|
|
$s_int_type:expr, $int_ref:expr,
|
2018-03-14 18:41:22 +00:00
|
|
|
$extra_feature:expr,
|
2018-03-02 05:52:27 +00:00
|
|
|
$min_fn:ident, $max_fn:ident,
|
2018-09-08 11:50:19 +00:00
|
|
|
$align:expr,
|
2019-01-30 16:47:36 +00:00
|
|
|
$atomic_new:expr,
|
2016-04-16 04:21:53 +00:00
|
|
|
$int_type:ident $atomic_type:ident $atomic_init:ident) => {
|
|
|
|
/// An integer type which can be safely shared between threads.
|
2016-08-16 15:58:20 +00:00
|
|
|
///
|
2017-09-25 23:39:52 +00:00
|
|
|
/// This type has the same in-memory representation as the underlying
|
|
|
|
/// integer type, [`
|
|
|
|
#[doc = $s_int_type]
|
|
|
|
/// `](
|
|
|
|
#[doc = $int_ref]
|
|
|
|
/// ). For more about the differences between atomic types and
|
2019-01-07 20:41:55 +00:00
|
|
|
/// non-atomic types as well as information about the portability of
|
|
|
|
/// this type, please see the [module-level documentation].
|
2017-09-25 23:39:52 +00:00
|
|
|
///
|
|
|
|
/// [module-level documentation]: index.html
|
2016-04-16 04:21:53 +00:00
|
|
|
#[$stable]
|
2018-10-27 10:46:33 +00:00
|
|
|
#[repr(C, align($align))]
|
2016-04-16 04:21:53 +00:00
|
|
|
pub struct $atomic_type {
|
|
|
|
v: UnsafeCell<$int_type>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An atomic integer initialized to `0`.
|
|
|
|
#[$stable]
|
2019-01-30 16:47:36 +00:00
|
|
|
#[cfg_attr(stage0, rustc_deprecated(
|
|
|
|
since = "1.34.0",
|
|
|
|
reason = "the `new` function is now preferred",
|
|
|
|
))]
|
|
|
|
#[cfg_attr(not(stage0), rustc_deprecated(
|
|
|
|
since = "1.34.0",
|
|
|
|
reason = "the `new` function is now preferred",
|
|
|
|
suggestion = $atomic_new,
|
|
|
|
))]
|
2016-04-16 04:21:53 +00:00
|
|
|
pub const $atomic_init: $atomic_type = $atomic_type::new(0);
|
|
|
|
|
|
|
|
#[$stable]
|
|
|
|
impl Default for $atomic_type {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 14:11:05 +00:00
|
|
|
#[$stable_from]
|
2017-10-29 09:28:54 +00:00
|
|
|
impl From<$int_type> for $atomic_type {
|
2018-12-11 22:41:12 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!(
|
|
|
|
"Converts an `", stringify!($int_type), "` into an `", stringify!($atomic_type), "`."),
|
|
|
|
#[inline]
|
|
|
|
fn from(v: $int_type) -> Self { Self::new(v) }
|
|
|
|
}
|
2017-10-29 09:28:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 04:21:53 +00:00
|
|
|
#[$stable_debug]
|
|
|
|
impl fmt::Debug for $atomic_type {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-02-26 19:35:36 +00:00
|
|
|
fmt::Debug::fmt(&self.load(Ordering::SeqCst), f)
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send is implicitly implemented.
|
|
|
|
#[$stable]
|
|
|
|
unsafe impl Sync for $atomic_type {}
|
|
|
|
|
|
|
|
impl $atomic_type {
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Creates a new atomic integer.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::", stringify!($atomic_type), ";
|
|
|
|
|
|
|
|
let atomic_forty_two = ", stringify!($atomic_type), "::new(42);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
|
|
|
pub const fn new(v: $int_type) -> Self {
|
|
|
|
$atomic_type {v: UnsafeCell::new(v)}
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Returns a mutable reference to the underlying integer.
|
|
|
|
|
|
|
|
This is safe because the mutable reference guarantees that no other threads are
|
|
|
|
concurrently accessing the atomic data.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let mut some_var = ", stringify!($atomic_type), "::new(10);
|
|
|
|
assert_eq!(*some_var.get_mut(), 10);
|
|
|
|
*some_var.get_mut() = 5;
|
|
|
|
assert_eq!(some_var.load(Ordering::SeqCst), 5);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable_access]
|
|
|
|
pub fn get_mut(&mut self) -> &mut $int_type {
|
|
|
|
unsafe { &mut *self.v.get() }
|
|
|
|
}
|
2016-08-16 15:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Consumes the atomic and returns the contained value.
|
|
|
|
|
|
|
|
This is safe because passing `self` by value guarantees that no other threads are
|
|
|
|
concurrently accessing the atomic data.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::", stringify!($atomic_type), ";
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
assert_eq!(some_var.into_inner(), 5);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable_access]
|
|
|
|
pub fn into_inner(self) -> $int_type {
|
|
|
|
self.v.into_inner()
|
|
|
|
}
|
2016-08-16 15:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Loads a value from the atomic integer.
|
|
|
|
|
|
|
|
`load` takes an [`Ordering`] argument which describes the memory ordering of this operation.
|
2018-08-07 09:33:20 +00:00
|
|
|
Possible values are [`SeqCst`], [`Acquire`] and [`Relaxed`].
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Panics
|
|
|
|
|
|
|
|
Panics if `order` is [`Release`] or [`AcqRel`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2018-03-14 18:41:22 +00:00
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:33:20 +00:00
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
2018-03-14 18:41:22 +00:00
|
|
|
[`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2018-08-07 09:33:20 +00:00
|
|
|
[`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 5);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
|
|
|
pub fn load(&self, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_load(self.v.get(), order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Stores a value into the atomic integer.
|
|
|
|
|
|
|
|
`store` takes an [`Ordering`] argument which describes the memory ordering of this operation.
|
2018-08-07 09:33:20 +00:00
|
|
|
Possible values are [`SeqCst`], [`Release`] and [`Relaxed`].
|
|
|
|
|
|
|
|
# Panics
|
|
|
|
|
|
|
|
Panics if `order` is [`Acquire`] or [`AcqRel`].
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:33:20 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
[`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
|
|
|
[`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
|
|
|
|
some_var.store(10, Ordering::Relaxed);
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 10);
|
2018-08-07 09:33:20 +00:00
|
|
|
```"),
|
2018-03-14 18:41:22 +00:00
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
|
|
|
pub fn store(&self, val: $int_type, order: Ordering) {
|
|
|
|
unsafe { atomic_store(self.v.get(), val, order); }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Stores a value into the atomic integer, returning the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`swap` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
|
|
|
|
assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn swap(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_swap(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Stores a value into the atomic integer if the current value is the same as
|
|
|
|
the `current` value.
|
|
|
|
|
|
|
|
The return value is always the previous value. If it is equal to `current`, then the
|
|
|
|
value was updated.
|
|
|
|
|
|
|
|
`compare_and_swap` also takes an [`Ordering`] argument which describes the memory
|
2018-08-07 09:26:05 +00:00
|
|
|
ordering of this operation. Notice that even when using [`AcqRel`], the operation
|
|
|
|
might fail and hence just perform an `Acquire` load, but not have `Release` semantics.
|
2018-08-07 09:57:43 +00:00
|
|
|
Using [`Acquire`] makes the store part of this operation [`Relaxed`] if it
|
|
|
|
happens, and using [`Release`] makes the load part [`Relaxed`].
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
[`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
|
|
|
|
assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 10);
|
|
|
|
|
|
|
|
assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 10);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn compare_and_swap(&self,
|
|
|
|
current: $int_type,
|
|
|
|
new: $int_type,
|
|
|
|
order: Ordering) -> $int_type {
|
|
|
|
match self.compare_exchange(current,
|
|
|
|
new,
|
|
|
|
order,
|
|
|
|
strongest_failure_ordering(order)) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(x) => x,
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Stores a value into the atomic integer if the current value is the same as
|
|
|
|
the `current` value.
|
|
|
|
|
|
|
|
The return value is a result indicating whether the new value was written and
|
|
|
|
containing the previous value. On success this value is guaranteed to be equal to
|
|
|
|
`current`.
|
|
|
|
|
|
|
|
`compare_exchange` takes two [`Ordering`] arguments to describe the memory
|
2018-08-07 09:57:43 +00:00
|
|
|
ordering of this operation. The first describes the required ordering if the
|
|
|
|
operation succeeds while the second describes the required ordering when the
|
|
|
|
operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
[`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
and must be equivalent to or weaker than the success ordering.
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2018-03-14 18:41:22 +00:00
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
[`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let some_var = ", stringify!($atomic_type), "::new(5);
|
|
|
|
|
|
|
|
assert_eq!(some_var.compare_exchange(5, 10,
|
|
|
|
Ordering::Acquire,
|
|
|
|
Ordering::Relaxed),
|
|
|
|
Ok(5));
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 10);
|
|
|
|
|
|
|
|
assert_eq!(some_var.compare_exchange(6, 12,
|
|
|
|
Ordering::SeqCst,
|
|
|
|
Ordering::Acquire),
|
|
|
|
Err(10));
|
|
|
|
assert_eq!(some_var.load(Ordering::Relaxed), 10);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable_cxchg]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn compare_exchange(&self,
|
|
|
|
current: $int_type,
|
|
|
|
new: $int_type,
|
|
|
|
success: Ordering,
|
|
|
|
failure: Ordering) -> Result<$int_type, $int_type> {
|
|
|
|
unsafe { atomic_compare_exchange(self.v.get(), current, new, success, failure) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Stores a value into the atomic integer if the current value is the same as
|
|
|
|
the `current` value.
|
|
|
|
|
|
|
|
Unlike [`compare_exchange`], this function is allowed to spuriously fail even
|
|
|
|
when the comparison succeeds, which can result in more efficient code on some
|
|
|
|
platforms. The return value is a result indicating whether the new value was
|
|
|
|
written and containing the previous value.
|
|
|
|
|
|
|
|
`compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
|
|
|
|
ordering of this operation. The first describes the required ordering if the
|
|
|
|
operation succeeds while the second describes the required ordering when the
|
2018-08-07 09:57:43 +00:00
|
|
|
operation fails. Using [`Acquire`] as success ordering makes the store part
|
|
|
|
of this operation [`Relaxed`], and using [`Release`] makes the successful load
|
|
|
|
[`Relaxed`]. The failure ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
and must be equivalent to or weaker than the success ordering.
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
[`compare_exchange`]: #method.compare_exchange
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2018-03-14 18:41:22 +00:00
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
2018-08-07 09:57:43 +00:00
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
[`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
2018-03-14 18:41:22 +00:00
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let val = ", stringify!($atomic_type), "::new(4);
|
|
|
|
|
|
|
|
let mut old = val.load(Ordering::Relaxed);
|
|
|
|
loop {
|
|
|
|
let new = old * 2;
|
|
|
|
match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
|
|
|
|
Ok(_) => break,
|
|
|
|
Err(x) => old = x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable_cxchg]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn compare_exchange_weak(&self,
|
|
|
|
current: $int_type,
|
|
|
|
new: $int_type,
|
|
|
|
success: Ordering,
|
|
|
|
failure: Ordering) -> Result<$int_type, $int_type> {
|
|
|
|
unsafe {
|
|
|
|
atomic_compare_exchange_weak(self.v.get(), current, new, success, failure)
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Adds to the current value, returning the previous value.
|
|
|
|
|
|
|
|
This operation wraps around on overflow.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_add` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(0);
|
|
|
|
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 10);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_add(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Subtracts from the current value, returning the previous value.
|
|
|
|
|
|
|
|
This operation wraps around on overflow.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_sub` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(20);
|
|
|
|
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 10);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_sub(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Bitwise \"and\" with the current value.
|
|
|
|
|
|
|
|
Performs a bitwise \"and\" operation on the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_and` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(0b101101);
|
|
|
|
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_and(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Bitwise \"nand\" with the current value.
|
|
|
|
|
|
|
|
Performs a bitwise \"nand\" operation on the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_nand` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
2018-04-14 13:20:25 +00:00
|
|
|
", $extra_feature, "
|
2018-03-14 18:41:22 +00:00
|
|
|
use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(0x13);
|
|
|
|
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable_nand]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_nand(self.v.get(), val, order) }
|
|
|
|
}
|
2018-02-09 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Bitwise \"or\" with the current value.
|
|
|
|
|
|
|
|
Performs a bitwise \"or\" operation on the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_or` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(0b101101);
|
|
|
|
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_or(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
doc_comment! {
|
|
|
|
concat!("Bitwise \"xor\" with the current value.
|
|
|
|
|
|
|
|
Performs a bitwise \"xor\" operation on the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_xor` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-14 18:41:22 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(0b101101);
|
|
|
|
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[$stable]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-14 18:41:22 +00:00
|
|
|
pub fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { atomic_xor(self.v.get(), val, order) }
|
|
|
|
}
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
2018-03-02 05:52:27 +00:00
|
|
|
|
|
|
|
doc_comment! {
|
|
|
|
concat!("Fetches the value, and applies a function to it that returns an optional
|
2018-04-12 20:48:48 +00:00
|
|
|
new value. Returns a `Result` of `Ok(previous_value)` if the function returned `Some(_)`, else
|
|
|
|
`Err(previous_value)`.
|
2018-03-02 05:52:27 +00:00
|
|
|
|
|
|
|
Note: This may call the function multiple times if the value has been changed from other threads in
|
|
|
|
the meantime, as long as the function returns `Some(_)`, but the function will have been applied
|
|
|
|
but once to the stored value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_update` takes two [`Ordering`] arguments to describe the memory
|
|
|
|
ordering of this operation. The first describes the required ordering for loads
|
|
|
|
and failed updates while the second describes the required ordering when the
|
|
|
|
operation finally succeeds. Beware that this is different from the two
|
|
|
|
modes in [`compare_exchange`]!
|
|
|
|
|
|
|
|
Using [`Acquire`] as success ordering makes the store part
|
|
|
|
of this operation [`Relaxed`], and using [`Release`] makes the final successful load
|
|
|
|
[`Relaxed`]. The (failed) load ordering can only be [`SeqCst`], [`Acquire`] or [`Relaxed`]
|
|
|
|
and must be equivalent to or weaker than the success ordering.
|
|
|
|
|
|
|
|
[`bool`]: ../../../std/primitive.bool.html
|
|
|
|
[`compare_exchange`]: #method.compare_exchange
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
[`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
|
|
|
|
2018-03-02 05:52:27 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```rust
|
|
|
|
#![feature(no_more_cas)]
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let x = ", stringify!($atomic_type), "::new(7);
|
|
|
|
assert_eq!(x.fetch_update(|_| None, Ordering::SeqCst, Ordering::SeqCst), Err(7));
|
|
|
|
assert_eq!(x.fetch_update(|x| Some(x + 1), Ordering::SeqCst, Ordering::SeqCst), Ok(7));
|
|
|
|
assert_eq!(x.fetch_update(|x| Some(x + 1), Ordering::SeqCst, Ordering::SeqCst), Ok(8));
|
|
|
|
assert_eq!(x.load(Ordering::SeqCst), 9);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "no_more_cas",
|
|
|
|
reason = "no more CAS loops in user code",
|
|
|
|
issue = "48655")]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
pub fn fetch_update<F>(&self,
|
|
|
|
mut f: F,
|
|
|
|
fetch_order: Ordering,
|
|
|
|
set_order: Ordering) -> Result<$int_type, $int_type>
|
|
|
|
where F: FnMut($int_type) -> Option<$int_type> {
|
|
|
|
let mut prev = self.load(fetch_order);
|
|
|
|
while let Some(next) = f(prev) {
|
|
|
|
match self.compare_exchange_weak(prev, next, set_order, fetch_order) {
|
|
|
|
x @ Ok(_) => return x,
|
|
|
|
Err(next_prev) => prev = next_prev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(prev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doc_comment! {
|
|
|
|
concat!("Maximum with the current value.
|
|
|
|
|
|
|
|
Finds the maximum of the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_max` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-02 05:52:27 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
#![feature(atomic_min_max)]
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(23);
|
|
|
|
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
|
|
|
|
assert_eq!(foo.load(Ordering::SeqCst), 42);
|
|
|
|
```
|
|
|
|
|
|
|
|
If you want to obtain the maximum value in one step, you can use the following:
|
|
|
|
|
|
|
|
```
|
|
|
|
#![feature(atomic_min_max)]
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(23);
|
|
|
|
let bar = 42;
|
|
|
|
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
|
|
|
|
assert!(max_foo == 42);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "atomic_min_max",
|
|
|
|
reason = "easier and faster min/max than writing manual CAS loop",
|
|
|
|
issue = "48655")]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
pub fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { $max_fn(self.v.get(), val, order) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doc_comment! {
|
|
|
|
concat!("Minimum with the current value.
|
|
|
|
|
|
|
|
Finds the minimum of the current value and the argument `val`, and
|
|
|
|
sets the new value to the result.
|
|
|
|
|
|
|
|
Returns the previous value.
|
|
|
|
|
2018-08-07 09:57:43 +00:00
|
|
|
`fetch_min` takes an [`Ordering`] argument which describes the memory ordering
|
|
|
|
of this operation. All ordering modes are possible. Note that using
|
|
|
|
[`Acquire`] makes the store part of this operation [`Relaxed`], and
|
|
|
|
using [`Release`] makes the load part [`Relaxed`].
|
|
|
|
|
|
|
|
[`Ordering`]: enum.Ordering.html
|
|
|
|
[`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
|
|
|
[`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
[`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
|
2018-03-02 05:52:27 +00:00
|
|
|
# Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
#![feature(atomic_min_max)]
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(23);
|
|
|
|
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
|
|
|
|
assert_eq!(foo.load(Ordering::Relaxed), 23);
|
|
|
|
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
|
|
|
|
assert_eq!(foo.load(Ordering::Relaxed), 22);
|
|
|
|
```
|
|
|
|
|
|
|
|
If you want to obtain the minimum value in one step, you can use the following:
|
|
|
|
|
|
|
|
```
|
|
|
|
#![feature(atomic_min_max)]
|
|
|
|
", $extra_feature, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
|
|
|
|
|
|
|
|
let foo = ", stringify!($atomic_type), "::new(23);
|
|
|
|
let bar = 12;
|
|
|
|
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
|
|
|
|
assert_eq!(min_foo, 12);
|
|
|
|
```"),
|
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "atomic_min_max",
|
|
|
|
reason = "easier and faster min/max than writing manual CAS loop",
|
|
|
|
issue = "48655")]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
pub fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
|
|
|
|
unsafe { $min_fn(self.v.get(), val, order) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_has_atomic = "8")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"i8", "../../../std/primitive.i8.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_min, atomic_max,
|
2018-09-08 11:50:19 +00:00
|
|
|
1,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicI8::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
i8 AtomicI8 ATOMIC_I8_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "8")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"u8", "../../../std/primitive.u8.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_umin, atomic_umax,
|
2018-09-08 11:50:19 +00:00
|
|
|
1,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicU8::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
u8 AtomicU8 ATOMIC_U8_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "16")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"i16", "../../../std/primitive.i16.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_min, atomic_max,
|
2018-09-08 11:50:19 +00:00
|
|
|
2,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicI16::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
i16 AtomicI16 ATOMIC_I16_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "16")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"u16", "../../../std/primitive.u16.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_umin, atomic_umax,
|
2018-09-08 11:50:19 +00:00
|
|
|
2,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicU16::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
u16 AtomicU16 ATOMIC_U16_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "32")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"i32", "../../../std/primitive.i32.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_min, atomic_max,
|
2018-09-08 11:50:19 +00:00
|
|
|
4,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicI32::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
i32 AtomicI32 ATOMIC_I32_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "32")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"u32", "../../../std/primitive.u32.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_umin, atomic_umax,
|
2018-09-08 11:50:19 +00:00
|
|
|
4,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicU32::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
u32 AtomicU32 ATOMIC_U32_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "64")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"i64", "../../../std/primitive.i64.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_min, atomic_max,
|
2018-09-08 11:50:19 +00:00
|
|
|
8,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicI64::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
i64 AtomicI64 ATOMIC_I64_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_has_atomic = "64")]
|
|
|
|
atomic_int! {
|
2019-01-07 20:41:55 +00:00
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
|
|
|
stable(feature = "integer_atomics_stable", since = "1.34.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"u64", "../../../std/primitive.u64.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"#![feature(integer_atomics)]\n\n",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_umin, atomic_umax,
|
2018-09-08 11:50:19 +00:00
|
|
|
8,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicU64::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
u64 AtomicU64 ATOMIC_U64_INIT
|
|
|
|
}
|
2018-12-05 14:42:56 +00:00
|
|
|
#[cfg(target_has_atomic = "128")]
|
2018-09-08 11:50:19 +00:00
|
|
|
atomic_int! {
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
"i128", "../../../std/primitive.i128.html",
|
|
|
|
"#![feature(integer_atomics)]\n\n",
|
|
|
|
atomic_min, atomic_max,
|
|
|
|
16,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicI128::new(0)",
|
2018-09-08 11:50:19 +00:00
|
|
|
i128 AtomicI128 ATOMIC_I128_INIT
|
|
|
|
}
|
2018-12-05 14:42:56 +00:00
|
|
|
#[cfg(target_has_atomic = "128")]
|
2018-09-08 11:50:19 +00:00
|
|
|
atomic_int! {
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
unstable(feature = "integer_atomics", issue = "32976"),
|
|
|
|
"u128", "../../../std/primitive.u128.html",
|
|
|
|
"#![feature(integer_atomics)]\n\n",
|
|
|
|
atomic_umin, atomic_umax,
|
|
|
|
16,
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicU128::new(0)",
|
2018-09-08 11:50:19 +00:00
|
|
|
u128 AtomicU128 ATOMIC_U128_INIT
|
|
|
|
}
|
|
|
|
#[cfg(target_pointer_width = "16")]
|
|
|
|
macro_rules! ptr_width {
|
|
|
|
() => { 2 }
|
|
|
|
}
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
macro_rules! ptr_width {
|
|
|
|
() => { 4 }
|
|
|
|
}
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
macro_rules! ptr_width {
|
|
|
|
() => { 8 }
|
|
|
|
}
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2016-04-16 04:21:53 +00:00
|
|
|
atomic_int!{
|
|
|
|
stable(feature = "rust1", since = "1.0.0"),
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
stable(feature = "extended_compare_and_swap", since = "1.10.0"),
|
2016-04-16 04:21:53 +00:00
|
|
|
stable(feature = "atomic_debug", since = "1.3.0"),
|
2016-12-14 20:13:12 +00:00
|
|
|
stable(feature = "atomic_access", since = "1.15.0"),
|
2017-12-27 14:11:05 +00:00
|
|
|
stable(feature = "atomic_from", since = "1.23.0"),
|
2018-04-14 13:20:25 +00:00
|
|
|
stable(feature = "atomic_nand", since = "1.27.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"isize", "../../../std/primitive.isize.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_min, atomic_max,
|
2018-09-08 11:50:19 +00:00
|
|
|
ptr_width!(),
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicIsize::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
isize AtomicIsize ATOMIC_ISIZE_INIT
|
|
|
|
}
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2016-04-16 04:21:53 +00:00
|
|
|
atomic_int!{
|
|
|
|
stable(feature = "rust1", since = "1.0.0"),
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 18:57:07 +00:00
|
|
|
stable(feature = "extended_compare_and_swap", since = "1.10.0"),
|
2016-04-16 04:21:53 +00:00
|
|
|
stable(feature = "atomic_debug", since = "1.3.0"),
|
2016-12-14 20:13:12 +00:00
|
|
|
stable(feature = "atomic_access", since = "1.15.0"),
|
2017-12-27 14:11:05 +00:00
|
|
|
stable(feature = "atomic_from", since = "1.23.0"),
|
2018-04-14 13:20:25 +00:00
|
|
|
stable(feature = "atomic_nand", since = "1.27.0"),
|
2017-09-25 23:39:52 +00:00
|
|
|
"usize", "../../../std/primitive.usize.html",
|
2018-03-14 18:41:22 +00:00
|
|
|
"",
|
2018-03-02 05:52:27 +00:00
|
|
|
atomic_umin, atomic_umax,
|
2018-09-08 11:50:19 +00:00
|
|
|
ptr_width!(),
|
2019-01-30 16:47:36 +00:00
|
|
|
"AtomicUsize::new(0)",
|
2016-04-16 04:21:53 +00:00
|
|
|
usize AtomicUsize ATOMIC_USIZE_INIT
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
fn strongest_failure_ordering(order: Ordering) -> Ordering {
|
|
|
|
match order {
|
|
|
|
Release => Relaxed,
|
|
|
|
Relaxed => Relaxed,
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => SeqCst,
|
2016-01-17 05:11:31 +00:00
|
|
|
Acquire => Acquire,
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => Acquire,
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn atomic_store<T>(dst: *mut T, val: T, order: Ordering) {
|
2014-05-13 04:30:48 +00:00
|
|
|
match order {
|
|
|
|
Release => intrinsics::atomic_store_rel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_store_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_store(dst, val),
|
2014-10-09 19:17:22 +00:00
|
|
|
Acquire => panic!("there is no such thing as an acquire store"),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => panic!("there is no such thing as an acquire/release store"),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-01-17 05:11:31 +00:00
|
|
|
unsafe fn atomic_load<T>(dst: *const T, order: Ordering) -> T {
|
2014-05-13 04:30:48 +00:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_load_acq(dst),
|
|
|
|
Relaxed => intrinsics::atomic_load_relaxed(dst),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_load(dst),
|
2014-10-09 19:17:22 +00:00
|
|
|
Release => panic!("there is no such thing as a release load"),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => panic!("there is no such thing as an acquire/release load"),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_swap<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xchg_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xchg_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_xchg_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_xchg_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_xchg(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:23:46 +00:00
|
|
|
/// Returns the previous value (like __sync_fetch_and_add).
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_add<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xadd_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xadd_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_xadd_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_xadd_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_xadd(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:23:46 +00:00
|
|
|
/// Returns the previous value (like __sync_fetch_and_sub).
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_sub<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xsub_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xsub_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_xsub_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_xsub_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_xsub(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-08-01 13:37:38 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
unsafe fn atomic_compare_exchange<T>(dst: *mut T,
|
|
|
|
old: T,
|
|
|
|
new: T,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<T, T> {
|
2016-03-14 10:57:50 +00:00
|
|
|
let (val, ok) = match (success, failure) {
|
2016-01-17 05:11:31 +00:00
|
|
|
(Acquire, Acquire) => intrinsics::atomic_cxchg_acq(dst, old, new),
|
|
|
|
(Release, Relaxed) => intrinsics::atomic_cxchg_rel(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(AcqRel, Acquire) => intrinsics::atomic_cxchg_acqrel(dst, old, new),
|
2016-01-17 05:11:31 +00:00
|
|
|
(Relaxed, Relaxed) => intrinsics::atomic_cxchg_relaxed(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(SeqCst, SeqCst) => intrinsics::atomic_cxchg(dst, old, new),
|
2016-01-17 05:11:31 +00:00
|
|
|
(Acquire, Relaxed) => intrinsics::atomic_cxchg_acq_failrelaxed(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(AcqRel, Relaxed) => intrinsics::atomic_cxchg_acqrel_failrelaxed(dst, old, new),
|
|
|
|
(SeqCst, Relaxed) => intrinsics::atomic_cxchg_failrelaxed(dst, old, new),
|
|
|
|
(SeqCst, Acquire) => intrinsics::atomic_cxchg_failacq(dst, old, new),
|
2016-04-06 00:06:10 +00:00
|
|
|
(_, AcqRel) => panic!("there is no such thing as an acquire/release failure ordering"),
|
|
|
|
(_, Release) => panic!("there is no such thing as a release failure ordering"),
|
2016-01-17 05:11:31 +00:00
|
|
|
_ => panic!("a failure ordering can't be stronger than a success ordering"),
|
2016-03-14 10:57:50 +00:00
|
|
|
};
|
2016-10-16 16:41:01 +00:00
|
|
|
if ok { Ok(val) } else { Err(val) }
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2016-01-17 05:11:31 +00:00
|
|
|
unsafe fn atomic_compare_exchange_weak<T>(dst: *mut T,
|
|
|
|
old: T,
|
|
|
|
new: T,
|
|
|
|
success: Ordering,
|
2016-10-16 16:41:01 +00:00
|
|
|
failure: Ordering)
|
|
|
|
-> Result<T, T> {
|
2016-03-14 10:57:50 +00:00
|
|
|
let (val, ok) = match (success, failure) {
|
2016-01-17 05:11:31 +00:00
|
|
|
(Acquire, Acquire) => intrinsics::atomic_cxchgweak_acq(dst, old, new),
|
|
|
|
(Release, Relaxed) => intrinsics::atomic_cxchgweak_rel(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(AcqRel, Acquire) => intrinsics::atomic_cxchgweak_acqrel(dst, old, new),
|
2016-01-17 05:11:31 +00:00
|
|
|
(Relaxed, Relaxed) => intrinsics::atomic_cxchgweak_relaxed(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(SeqCst, SeqCst) => intrinsics::atomic_cxchgweak(dst, old, new),
|
2016-01-17 05:11:31 +00:00
|
|
|
(Acquire, Relaxed) => intrinsics::atomic_cxchgweak_acq_failrelaxed(dst, old, new),
|
2016-10-16 16:41:01 +00:00
|
|
|
(AcqRel, Relaxed) => intrinsics::atomic_cxchgweak_acqrel_failrelaxed(dst, old, new),
|
|
|
|
(SeqCst, Relaxed) => intrinsics::atomic_cxchgweak_failrelaxed(dst, old, new),
|
|
|
|
(SeqCst, Acquire) => intrinsics::atomic_cxchgweak_failacq(dst, old, new),
|
2016-04-06 00:06:10 +00:00
|
|
|
(_, AcqRel) => panic!("there is no such thing as an acquire/release failure ordering"),
|
|
|
|
(_, Release) => panic!("there is no such thing as a release failure ordering"),
|
2016-01-17 05:11:31 +00:00
|
|
|
_ => panic!("a failure ordering can't be stronger than a success ordering"),
|
2016-03-14 10:57:50 +00:00
|
|
|
};
|
2016-10-16 16:41:01 +00:00
|
|
|
if ok { Ok(val) } else { Err(val) }
|
2016-01-17 05:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_and<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_and_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_and_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_and_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_and_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_and(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:19:52 +00:00
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-02-09 18:19:52 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_or_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_or_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_or_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_or_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_or(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xor_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xor_rel(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_xor_acqrel(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
Relaxed => intrinsics::atomic_xor_relaxed(dst, val),
|
2016-10-16 16:41:01 +00:00
|
|
|
SeqCst => intrinsics::atomic_xor(dst, val),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:52:27 +00:00
|
|
|
/// returns the max value (signed comparison)
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
unsafe fn atomic_max<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_max_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_max_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_max_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_max_relaxed(dst, val),
|
|
|
|
SeqCst => intrinsics::atomic_max(dst, val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns the min value (signed comparison)
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
unsafe fn atomic_min<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_min_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_min_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_min_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_min_relaxed(dst, val),
|
|
|
|
SeqCst => intrinsics::atomic_min(dst, val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns the max value (signed comparison)
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
unsafe fn atomic_umax<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_umax_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_umax_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_umax_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_umax_relaxed(dst, val),
|
|
|
|
SeqCst => intrinsics::atomic_umax(dst, val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns the min value (signed comparison)
|
|
|
|
#[inline]
|
2018-09-16 21:04:11 +00:00
|
|
|
#[cfg(target_has_atomic = "cas")]
|
2018-03-02 05:52:27 +00:00
|
|
|
unsafe fn atomic_umin<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_umin_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_umin_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_umin_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_umin_relaxed(dst, val),
|
|
|
|
SeqCst => intrinsics::atomic_umin(dst, val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:30:48 +00:00
|
|
|
/// An atomic fence.
|
|
|
|
///
|
2017-04-11 12:45:12 +00:00
|
|
|
/// Depending on the specified order, a fence prevents the compiler and CPU from
|
|
|
|
/// reordering certain types of memory operations around it.
|
|
|
|
/// That creates synchronizes-with relationships between it and atomic operations
|
|
|
|
/// or fences in other threads.
|
|
|
|
///
|
|
|
|
/// A fence 'A' which has (at least) [`Release`] ordering semantics, synchronizes
|
|
|
|
/// with a fence 'B' with (at least) [`Acquire`] semantics, if and only if there
|
|
|
|
/// exist operations X and Y, both operating on some atomic object 'M' such
|
2014-11-06 23:14:02 +00:00
|
|
|
/// that A is sequenced before X, Y is synchronized before B and Y observes
|
2014-05-13 04:30:48 +00:00
|
|
|
/// the change to M. This provides a happens-before dependence between A and B.
|
|
|
|
///
|
2017-04-11 12:45:12 +00:00
|
|
|
/// ```text
|
|
|
|
/// Thread 1 Thread 2
|
|
|
|
///
|
|
|
|
/// fence(Release); A --------------
|
|
|
|
/// x.store(3, Relaxed); X --------- |
|
|
|
|
/// | |
|
|
|
|
/// | |
|
|
|
|
/// -------------> Y if x.load(Relaxed) == 3 {
|
|
|
|
/// |-------> B fence(Acquire);
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-12-29 10:31:16 +00:00
|
|
|
/// Atomic operations with [`Release`] or [`Acquire`] semantics can also synchronize
|
2014-05-13 04:30:48 +00:00
|
|
|
/// with a fence.
|
|
|
|
///
|
2016-12-29 10:31:16 +00:00
|
|
|
/// A fence which has [`SeqCst`] ordering, in addition to having both [`Acquire`]
|
|
|
|
/// and [`Release`] semantics, participates in the global program order of the
|
|
|
|
/// other [`SeqCst`] operations and/or fences.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2016-12-29 10:31:16 +00:00
|
|
|
/// Accepts [`Acquire`], [`Release`], [`AcqRel`] and [`SeqCst`] orderings.
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2014-11-11 18:36:09 +00:00
|
|
|
/// # Panics
|
2014-05-13 04:30:48 +00:00
|
|
|
///
|
2016-12-29 10:31:16 +00:00
|
|
|
/// Panics if `order` is [`Relaxed`].
|
|
|
|
///
|
2017-04-11 12:45:12 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::AtomicBool;
|
|
|
|
/// use std::sync::atomic::fence;
|
|
|
|
/// use std::sync::atomic::Ordering;
|
|
|
|
///
|
|
|
|
/// // A mutual exclusion primitive based on spinlock.
|
|
|
|
/// pub struct Mutex {
|
|
|
|
/// flag: AtomicBool,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Mutex {
|
|
|
|
/// pub fn new() -> Mutex {
|
|
|
|
/// Mutex {
|
|
|
|
/// flag: AtomicBool::new(false),
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// pub fn lock(&self) {
|
|
|
|
/// while !self.flag.compare_and_swap(false, true, Ordering::Relaxed) {}
|
2017-08-15 19:45:21 +00:00
|
|
|
/// // This fence synchronizes-with store in `unlock`.
|
2017-04-11 12:45:12 +00:00
|
|
|
/// fence(Ordering::Acquire);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// pub fn unlock(&self) {
|
|
|
|
/// self.flag.store(false, Ordering::Release);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-12-29 10:31:16 +00:00
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2014-05-13 04:30:48 +00:00
|
|
|
#[inline]
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-08-15 17:51:24 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", allow(unused_variables))]
|
2014-05-13 04:30:48 +00:00
|
|
|
pub fn fence(order: Ordering) {
|
2018-08-15 17:51:24 +00:00
|
|
|
// On wasm32 it looks like fences aren't implemented in LLVM yet in that
|
|
|
|
// they will cause LLVM to abort. The wasm instruction set doesn't have
|
|
|
|
// fences right now. There's discussion online about the best way for tools
|
|
|
|
// to conventionally implement fences at
|
|
|
|
// https://github.com/WebAssembly/tool-conventions/issues/59. We should
|
|
|
|
// follow that discussion and implement a solution when one comes about!
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2014-05-13 04:30:48 +00:00
|
|
|
unsafe {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_fence_acq(),
|
|
|
|
Release => intrinsics::atomic_fence_rel(),
|
2016-10-16 16:41:01 +00:00
|
|
|
AcqRel => intrinsics::atomic_fence_acqrel(),
|
|
|
|
SeqCst => intrinsics::atomic_fence(),
|
|
|
|
Relaxed => panic!("there is no such thing as a relaxed fence"),
|
2014-05-13 04:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 16:11:30 +00:00
|
|
|
|
|
|
|
|
2017-04-12 23:04:57 +00:00
|
|
|
/// A compiler memory fence.
|
2017-04-05 19:33:24 +00:00
|
|
|
///
|
2017-09-16 20:16:49 +00:00
|
|
|
/// `compiler_fence` does not emit any machine code, but restricts the kinds
|
|
|
|
/// of memory re-ordering the compiler is allowed to do. Specifically, depending on
|
|
|
|
/// the given [`Ordering`] semantics, the compiler may be disallowed from moving reads
|
|
|
|
/// or writes from before or after the call to the other side of the call to
|
|
|
|
/// `compiler_fence`. Note that it does **not** prevent the *hardware*
|
|
|
|
/// from doing such re-ordering. This is not a problem in a single-threaded,
|
|
|
|
/// execution context, but when other threads may modify memory at the same
|
|
|
|
/// time, stronger synchronization primitives such as [`fence`] are required.
|
2017-04-05 19:33:24 +00:00
|
|
|
///
|
|
|
|
/// The re-ordering prevented by the different ordering semantics are:
|
|
|
|
///
|
|
|
|
/// - with [`SeqCst`], no re-ordering of reads and writes across this point is allowed.
|
|
|
|
/// - with [`Release`], preceding reads and writes cannot be moved past subsequent writes.
|
|
|
|
/// - with [`Acquire`], subsequent reads and writes cannot be moved ahead of preceding reads.
|
|
|
|
/// - with [`AcqRel`], both of the above rules are enforced.
|
|
|
|
///
|
2017-09-16 20:16:49 +00:00
|
|
|
/// `compiler_fence` is generally only useful for preventing a thread from
|
|
|
|
/// racing *with itself*. That is, if a given thread is executing one piece
|
|
|
|
/// of code, and is then interrupted, and starts executing code elsewhere
|
|
|
|
/// (while still in the same thread, and conceptually still on the same
|
|
|
|
/// core). In traditional programs, this can only occur when a signal
|
|
|
|
/// handler is registered. In more low-level code, such situations can also
|
|
|
|
/// arise when handling interrupts, when implementing green threads with
|
|
|
|
/// pre-emption, etc. Curious readers are encouraged to read the Linux kernel's
|
|
|
|
/// discussion of [memory barriers].
|
|
|
|
///
|
2017-04-05 19:33:24 +00:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `order` is [`Relaxed`].
|
|
|
|
///
|
2017-09-15 15:03:09 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Without `compiler_fence`, the `assert_eq!` in following code
|
|
|
|
/// is *not* guaranteed to succeed, despite everything happening in a single thread.
|
|
|
|
/// To see why, remember that the compiler is free to swap the stores to
|
|
|
|
/// `IMPORTANT_VARIABLE` and `IS_READ` since they are both
|
|
|
|
/// `Ordering::Relaxed`. If it does, and the signal handler is invoked right
|
|
|
|
/// after `IS_READY` is updated, then the signal handler will see
|
|
|
|
/// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
|
|
|
|
/// Using a `compiler_fence` remedies this situation.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomic::{AtomicBool, AtomicUsize};
|
|
|
|
/// use std::sync::atomic::Ordering;
|
|
|
|
/// use std::sync::atomic::compiler_fence;
|
|
|
|
///
|
2019-01-26 16:14:49 +00:00
|
|
|
/// static IMPORTANT_VARIABLE: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
/// static IS_READY: AtomicBool = AtomicBool::new(false);
|
2017-09-15 15:03:09 +00:00
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
|
|
|
|
/// // prevent earlier writes from being moved beyond this point
|
|
|
|
/// compiler_fence(Ordering::Release);
|
|
|
|
/// IS_READY.store(true, Ordering::Relaxed);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn signal_handler() {
|
|
|
|
/// if IS_READY.load(Ordering::Relaxed) {
|
|
|
|
/// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-04-05 19:33:24 +00:00
|
|
|
/// [`fence`]: fn.fence.html
|
|
|
|
/// [`Ordering`]: enum.Ordering.html
|
|
|
|
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
|
|
|
|
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
|
|
|
|
/// [`Release`]: enum.Ordering.html#variant.Release
|
|
|
|
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
|
|
|
|
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
|
2017-09-16 20:16:49 +00:00
|
|
|
/// [memory barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt
|
2017-04-05 19:33:24 +00:00
|
|
|
#[inline]
|
2017-09-25 05:23:26 +00:00
|
|
|
#[stable(feature = "compiler_fences", since = "1.21.0")]
|
2017-04-12 23:04:57 +00:00
|
|
|
pub fn compiler_fence(order: Ordering) {
|
2017-04-05 19:33:24 +00:00
|
|
|
unsafe {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_singlethreadfence_acq(),
|
|
|
|
Release => intrinsics::atomic_singlethreadfence_rel(),
|
|
|
|
AcqRel => intrinsics::atomic_singlethreadfence_acqrel(),
|
|
|
|
SeqCst => intrinsics::atomic_singlethreadfence(),
|
2017-04-12 23:04:57 +00:00
|
|
|
Relaxed => panic!("there is no such thing as a relaxed compiler fence"),
|
2017-04-05 19:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "8")]
|
2016-04-16 04:21:53 +00:00
|
|
|
#[stable(feature = "atomic_debug", since = "1.3.0")]
|
|
|
|
impl fmt::Debug for AtomicBool {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-02-26 19:35:36 +00:00
|
|
|
fmt::Debug::fmt(&self.load(Ordering::SeqCst), f)
|
2016-04-16 04:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-19 16:11:30 +00:00
|
|
|
|
2016-05-24 05:28:15 +00:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2015-07-19 16:11:30 +00:00
|
|
|
#[stable(feature = "atomic_debug", since = "1.3.0")]
|
|
|
|
impl<T> fmt::Debug for AtomicPtr<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-02-26 19:35:36 +00:00
|
|
|
fmt::Debug::fmt(&self.load(Ordering::SeqCst), f)
|
2015-07-19 16:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-10 23:21:26 +00:00
|
|
|
|
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
|
|
|
#[stable(feature = "atomic_pointer", since = "1.24.0")]
|
|
|
|
impl<T> fmt::Pointer for AtomicPtr<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Pointer::fmt(&self.load(Ordering::SeqCst), f)
|
|
|
|
}
|
|
|
|
}
|