1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 //! Atomic types provide primitive shared-memory communication between
14 //! threads, and are the building blocks of other concurrent
17 //! This module defines atomic versions of a select number of primitive
18 //! types, including [`AtomicBool`], [`AtomicIsize`], and [`AtomicUsize`].
19 //! Atomic types present operations that, when used correctly, synchronize
20 //! updates between threads.
22 //! [`AtomicBool`]: struct.AtomicBool.html
23 //! [`AtomicIsize`]: struct.AtomicIsize.html
24 //! [`AtomicUsize`]: struct.AtomicUsize.html
26 //! Each method takes an [`Ordering`] which represents the strength of
27 //! the memory barrier for that operation. These orderings are the
28 //! same as [LLVM atomic orderings][1]. For more information see the [nomicon][2].
30 //! [`Ordering`]: enum.Ordering.html
32 //! [1]: http://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations
33 //! [2]: ../../../nomicon/atomics.html
35 //! Atomic variables are safe to share between threads (they implement [`Sync`])
36 //! but they do not themselves provide the mechanism for sharing and follow the
37 //! [threading model](../../../std/thread/index.html#the-threading-model) of rust.
38 //! The most common way to share an atomic variable is to put it into an [`Arc`][arc] (an
39 //! atomically-reference-counted shared pointer).
41 //! [`Sync`]: ../../marker/trait.Sync.html
42 //! [arc]: ../../../std/sync/struct.Arc.html
44 //! Most atomic types may be stored in static variables, initialized using
45 //! the provided static initializers like [`ATOMIC_BOOL_INIT`]. Atomic statics
46 //! are often used for lazy global initialization.
48 //! [`ATOMIC_BOOL_INIT`]: constant.ATOMIC_BOOL_INIT.html
52 //! A simple spinlock:
55 //! use std::sync::Arc;
56 //! use std::sync::atomic::{AtomicUsize, Ordering};
60 //! let spinlock = Arc::new(AtomicUsize::new(1));
62 //! let spinlock_clone = spinlock.clone();
63 //! let thread = thread::spawn(move|| {
64 //! spinlock_clone.store(0, Ordering::SeqCst);
67 //! // Wait for the other thread to release the lock
68 //! while spinlock.load(Ordering::SeqCst) != 0 {}
70 //! if let Err(panic) = thread.join() {
71 //! println!("Thread had an error: {:?}", panic);
76 //! Keep a global count of live threads:
79 //! use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
81 //! static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
83 //! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
84 //! println!("live threads: {}", old_thread_count + 1);
87 #![stable(feature = "rust1", since = "1.0.0")]
88 #![cfg_attr(not(target_has_atomic = "8"), allow(dead_code))]
89 #![cfg_attr(not(target_has_atomic = "8"), allow(unused_imports))]
91 use self::Ordering
::*;
97 /// Save power or switch hyperthreads in a busy-wait spin-loop.
99 /// This function is deliberately more primitive than
100 /// [`std::thread::yield_now`](../../../std/thread/fn.yield_now.html) and
101 /// does not directly yield to the system's scheduler.
102 /// In some cases it might be useful to use a combination of both functions.
103 /// Careful benchmarking is advised.
105 /// On some platforms this function may not do anything at all.
107 #[stable(feature = "spin_loop_hint", since = "1.24.0")]
108 pub fn spin_loop_hint() {
109 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
111 asm
!("pause" ::: "memory" : "volatile");
114 #[cfg(target_arch = "aarch64")]
116 asm
!("yield" ::: "memory" : "volatile");
120 /// A boolean type which can be safely shared between threads.
122 /// This type has the same in-memory representation as a [`bool`].
124 /// [`bool`]: ../../../std/primitive.bool.html
125 #[cfg(target_has_atomic = "8")]
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub struct AtomicBool
{
131 #[cfg(target_has_atomic = "8")]
132 #[stable(feature = "rust1", since = "1.0.0")]
133 impl Default
for AtomicBool
{
134 /// Creates an `AtomicBool` initialized to `false`.
135 fn default() -> Self {
140 // Send is implicitly implemented for AtomicBool.
141 #[cfg(target_has_atomic = "8")]
142 #[stable(feature = "rust1", since = "1.0.0")]
143 unsafe impl Sync
for AtomicBool {}
145 /// A raw pointer type which can be safely shared between threads.
147 /// This type has the same in-memory representation as a `*mut T`.
148 #[cfg(target_has_atomic = "ptr")]
149 #[stable(feature = "rust1", since = "1.0.0")]
150 pub struct AtomicPtr
<T
> {
151 p
: UnsafeCell
<*mut T
>,
154 #[cfg(target_has_atomic = "ptr")]
155 #[stable(feature = "rust1", since = "1.0.0")]
156 impl<T
> Default
for AtomicPtr
<T
> {
157 /// Creates a null `AtomicPtr<T>`.
158 fn default() -> AtomicPtr
<T
> {
159 AtomicPtr
::new(::ptr
::null_mut())
163 #[cfg(target_has_atomic = "ptr")]
164 #[stable(feature = "rust1", since = "1.0.0")]
165 unsafe impl<T
> Send
for AtomicPtr
<T
> {}
166 #[cfg(target_has_atomic = "ptr")]
167 #[stable(feature = "rust1", since = "1.0.0")]
168 unsafe impl<T
> Sync
for AtomicPtr
<T
> {}
170 /// Atomic memory orderings
172 /// Memory orderings limit the ways that both the compiler and CPU may reorder
173 /// instructions around atomic operations. At its most restrictive,
174 /// "sequentially consistent" atomics allow neither reads nor writes
175 /// to be moved either before or after the atomic operation; on the other end
176 /// "relaxed" atomics allow all reorderings.
178 /// Rust's memory orderings are [the same as
179 /// LLVM's](http://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations).
181 /// For more information see the [nomicon].
183 /// [nomicon]: ../../../nomicon/atomics.html
184 #[stable(feature = "rust1", since = "1.0.0")]
185 #[derive(Copy, Clone, Debug)]
187 /// No ordering constraints, only atomic operations.
189 /// Corresponds to LLVM's [`Monotonic`] ordering.
191 /// [`Monotonic`]: http://llvm.org/docs/Atomics.html#monotonic
192 #[stable(feature = "rust1", since = "1.0.0")]
194 /// When coupled with a store, all previous writes become visible
195 /// to the other threads that perform a load with [`Acquire`] ordering
196 /// on the same value.
198 /// [`Acquire`]: http://llvm.org/docs/Atomics.html#acquire
199 #[stable(feature = "rust1", since = "1.0.0")]
201 /// When coupled with a load, all subsequent loads will see data
202 /// written before a store with [`Release`] ordering on the same value
203 /// in other threads.
205 /// [`Release`]: http://llvm.org/docs/Atomics.html#release
206 #[stable(feature = "rust1", since = "1.0.0")]
208 /// Has the effects of both [`Acquire`] and [`Release`] together.
210 /// This ordering is only applicable for operations that combine both loads and stores.
212 /// For loads it uses [`Acquire`] ordering. For stores it uses the [`Release`] ordering.
214 /// [`Acquire`]: http://llvm.org/docs/Atomics.html#acquire
215 /// [`Release`]: http://llvm.org/docs/Atomics.html#release
216 #[stable(feature = "rust1", since = "1.0.0")]
218 /// Like `AcqRel` with the additional guarantee that all threads see all
219 /// sequentially consistent operations in the same order.
220 #[stable(feature = "rust1", since = "1.0.0")]
222 // Prevent exhaustive matching to allow for future extension
224 #[unstable(feature = "future_atomic_orderings", issue = "0")]
228 /// An [`AtomicBool`] initialized to `false`.
230 /// [`AtomicBool`]: struct.AtomicBool.html
231 #[cfg(target_has_atomic = "8")]
232 #[stable(feature = "rust1", since = "1.0.0")]
233 pub const ATOMIC_BOOL_INIT
: AtomicBool
= AtomicBool
::new(false);
235 #[cfg(target_has_atomic = "8")]
237 /// Creates a new `AtomicBool`.
242 /// use std::sync::atomic::AtomicBool;
244 /// let atomic_true = AtomicBool::new(true);
245 /// let atomic_false = AtomicBool::new(false);
248 #[stable(feature = "rust1", since = "1.0.0")]
249 pub const fn new(v
: bool
) -> AtomicBool
{
250 AtomicBool { v: UnsafeCell::new(v as u8) }
253 /// Returns a mutable reference to the underlying [`bool`].
255 /// This is safe because the mutable reference guarantees that no other threads are
256 /// concurrently accessing the atomic data.
258 /// [`bool`]: ../../../std/primitive.bool.html
263 /// use std::sync::atomic::{AtomicBool, Ordering};
265 /// let mut some_bool = AtomicBool::new(true);
266 /// assert_eq!(*some_bool.get_mut(), true);
267 /// *some_bool.get_mut() = false;
268 /// assert_eq!(some_bool.load(Ordering::SeqCst), false);
271 #[stable(feature = "atomic_access", since = "1.15.0")]
272 pub fn get_mut(&mut self) -> &mut bool
{
273 unsafe { &mut *(self.v.get() as *mut bool) }
276 /// Consumes the atomic and returns the contained value.
278 /// This is safe because passing `self` by value guarantees that no other threads are
279 /// concurrently accessing the atomic data.
284 /// use std::sync::atomic::AtomicBool;
286 /// let some_bool = AtomicBool::new(true);
287 /// assert_eq!(some_bool.into_inner(), true);
290 #[stable(feature = "atomic_access", since = "1.15.0")]
291 pub fn into_inner(self) -> bool
{
292 self.v
.into_inner() != 0
295 /// Loads a value from the bool.
297 /// `load` takes an [`Ordering`] argument which describes the memory ordering
298 /// of this operation.
302 /// Panics if `order` is [`Release`] or [`AcqRel`].
304 /// [`Ordering`]: enum.Ordering.html
305 /// [`Release`]: enum.Ordering.html#variant.Release
306 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
311 /// use std::sync::atomic::{AtomicBool, Ordering};
313 /// let some_bool = AtomicBool::new(true);
315 /// assert_eq!(some_bool.load(Ordering::Relaxed), true);
318 #[stable(feature = "rust1", since = "1.0.0")]
319 pub fn load(&self, order
: Ordering
) -> bool
{
320 unsafe { atomic_load(self.v.get(), order) != 0 }
323 /// Stores a value into the bool.
325 /// `store` takes an [`Ordering`] argument which describes the memory ordering
326 /// of this operation.
328 /// [`Ordering`]: enum.Ordering.html
333 /// use std::sync::atomic::{AtomicBool, Ordering};
335 /// let some_bool = AtomicBool::new(true);
337 /// some_bool.store(false, Ordering::Relaxed);
338 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
343 /// Panics if `order` is [`Acquire`] or [`AcqRel`].
345 /// [`Acquire`]: enum.Ordering.html#variant.Acquire
346 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
348 #[stable(feature = "rust1", since = "1.0.0")]
349 pub fn store(&self, val
: bool
, order
: Ordering
) {
351 atomic_store(self.v
.get(), val
as u8, order
);
355 /// Stores a value into the bool, returning the previous value.
357 /// `swap` takes an [`Ordering`] argument which describes the memory ordering
358 /// of this operation.
360 /// [`Ordering`]: enum.Ordering.html
365 /// use std::sync::atomic::{AtomicBool, Ordering};
367 /// let some_bool = AtomicBool::new(true);
369 /// assert_eq!(some_bool.swap(false, Ordering::Relaxed), true);
370 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
373 #[stable(feature = "rust1", since = "1.0.0")]
374 pub fn swap(&self, val
: bool
, order
: Ordering
) -> bool
{
375 unsafe { atomic_swap(self.v.get(), val as u8, order) != 0 }
378 /// Stores a value into the [`bool`] if the current value is the same as the `current` value.
380 /// The return value is always the previous value. If it is equal to `current`, then the value
383 /// `compare_and_swap` also takes an [`Ordering`] argument which describes the memory
384 /// ordering of this operation.
386 /// [`Ordering`]: enum.Ordering.html
387 /// [`bool`]: ../../../std/primitive.bool.html
392 /// use std::sync::atomic::{AtomicBool, Ordering};
394 /// let some_bool = AtomicBool::new(true);
396 /// assert_eq!(some_bool.compare_and_swap(true, false, Ordering::Relaxed), true);
397 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
399 /// assert_eq!(some_bool.compare_and_swap(true, true, Ordering::Relaxed), false);
400 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
403 #[stable(feature = "rust1", since = "1.0.0")]
404 pub fn compare_and_swap(&self, current
: bool
, new
: bool
, order
: Ordering
) -> bool
{
405 match self.compare_exchange(current
, new
, order
, strongest_failure_ordering(order
)) {
411 /// Stores a value into the [`bool`] if the current value is the same as the `current` value.
413 /// The return value is a result indicating whether the new value was written and containing
414 /// the previous value. On success this value is guaranteed to be equal to `current`.
416 /// `compare_exchange` takes two [`Ordering`] arguments to describe the memory
417 /// ordering of this operation. The first describes the required ordering if the
418 /// operation succeeds while the second describes the required ordering when the
419 /// operation fails. The failure ordering can't be [`Release`] or [`AcqRel`] and must
420 /// be equivalent or weaker than the success ordering.
422 /// [`bool`]: ../../../std/primitive.bool.html
423 /// [`Ordering`]: enum.Ordering.html
424 /// [`Release`]: enum.Ordering.html#variant.Release
425 /// [`AcqRel`]: enum.Ordering.html#variant.Release
430 /// use std::sync::atomic::{AtomicBool, Ordering};
432 /// let some_bool = AtomicBool::new(true);
434 /// assert_eq!(some_bool.compare_exchange(true,
436 /// Ordering::Acquire,
437 /// Ordering::Relaxed),
439 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
441 /// assert_eq!(some_bool.compare_exchange(true, true,
442 /// Ordering::SeqCst,
443 /// Ordering::Acquire),
445 /// assert_eq!(some_bool.load(Ordering::Relaxed), false);
448 #[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
449 pub fn compare_exchange(&self,
454 -> Result
<bool
, bool
> {
456 atomic_compare_exchange(self.v
.get(), current
as u8, new
as u8, success
, failure
)
459 Err(x
) => Err(x
!= 0),
463 /// Stores a value into the [`bool`] if the current value is the same as the `current` value.
465 /// Unlike [`compare_exchange`], this function is allowed to spuriously fail even when the
466 /// comparison succeeds, which can result in more efficient code on some platforms. The
467 /// return value is a result indicating whether the new value was written and containing the
470 /// `compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
471 /// ordering of this operation. The first describes the required ordering if the operation
472 /// succeeds while the second describes the required ordering when the operation fails. The
473 /// failure ordering can't be [`Release`] or [`AcqRel`] and must be equivalent or
474 /// weaker than the success ordering.
476 /// [`bool`]: ../../../std/primitive.bool.html
477 /// [`compare_exchange`]: #method.compare_exchange
478 /// [`Ordering`]: enum.Ordering.html
479 /// [`Release`]: enum.Ordering.html#variant.Release
480 /// [`AcqRel`]: enum.Ordering.html#variant.Release
485 /// use std::sync::atomic::{AtomicBool, Ordering};
487 /// let val = AtomicBool::new(false);
490 /// let mut old = val.load(Ordering::Relaxed);
492 /// match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
494 /// Err(x) => old = x,
499 #[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
500 pub fn compare_exchange_weak(&self,
505 -> Result
<bool
, bool
> {
507 atomic_compare_exchange_weak(self.v
.get(), current
as u8, new
as u8, success
, failure
)
510 Err(x
) => Err(x
!= 0),
514 /// Logical "and" with a boolean value.
516 /// Performs a logical "and" operation on the current value and the argument `val`, and sets
517 /// the new value to the result.
519 /// Returns the previous value.
524 /// use std::sync::atomic::{AtomicBool, Ordering};
526 /// let foo = AtomicBool::new(true);
527 /// assert_eq!(foo.fetch_and(false, Ordering::SeqCst), true);
528 /// assert_eq!(foo.load(Ordering::SeqCst), false);
530 /// let foo = AtomicBool::new(true);
531 /// assert_eq!(foo.fetch_and(true, Ordering::SeqCst), true);
532 /// assert_eq!(foo.load(Ordering::SeqCst), true);
534 /// let foo = AtomicBool::new(false);
535 /// assert_eq!(foo.fetch_and(false, Ordering::SeqCst), false);
536 /// assert_eq!(foo.load(Ordering::SeqCst), false);
539 #[stable(feature = "rust1", since = "1.0.0")]
540 pub fn fetch_and(&self, val
: bool
, order
: Ordering
) -> bool
{
541 unsafe { atomic_and(self.v.get(), val as u8, order) != 0 }
544 /// Logical "nand" with a boolean value.
546 /// Performs a logical "nand" operation on the current value and the argument `val`, and sets
547 /// the new value to the result.
549 /// Returns the previous value.
554 /// use std::sync::atomic::{AtomicBool, Ordering};
556 /// let foo = AtomicBool::new(true);
557 /// assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), true);
558 /// assert_eq!(foo.load(Ordering::SeqCst), true);
560 /// let foo = AtomicBool::new(true);
561 /// assert_eq!(foo.fetch_nand(true, Ordering::SeqCst), true);
562 /// assert_eq!(foo.load(Ordering::SeqCst) as usize, 0);
563 /// assert_eq!(foo.load(Ordering::SeqCst), false);
565 /// let foo = AtomicBool::new(false);
566 /// assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), false);
567 /// assert_eq!(foo.load(Ordering::SeqCst), true);
570 #[stable(feature = "rust1", since = "1.0.0")]
571 pub fn fetch_nand(&self, val
: bool
, order
: Ordering
) -> bool
{
572 // We can't use atomic_nand here because it can result in a bool with
573 // an invalid value. This happens because the atomic operation is done
574 // with an 8-bit integer internally, which would set the upper 7 bits.
575 // So we just use fetch_xor or swap instead.
578 // We must invert the bool.
579 self.fetch_xor(true, order
)
581 // !(x & false) == true
582 // We must set the bool to true.
583 self.swap(true, order
)
587 /// Logical "or" with a boolean value.
589 /// Performs a logical "or" operation on the current value and the argument `val`, and sets the
590 /// new value to the result.
592 /// Returns the previous value.
597 /// use std::sync::atomic::{AtomicBool, Ordering};
599 /// let foo = AtomicBool::new(true);
600 /// assert_eq!(foo.fetch_or(false, Ordering::SeqCst), true);
601 /// assert_eq!(foo.load(Ordering::SeqCst), true);
603 /// let foo = AtomicBool::new(true);
604 /// assert_eq!(foo.fetch_or(true, Ordering::SeqCst), true);
605 /// assert_eq!(foo.load(Ordering::SeqCst), true);
607 /// let foo = AtomicBool::new(false);
608 /// assert_eq!(foo.fetch_or(false, Ordering::SeqCst), false);
609 /// assert_eq!(foo.load(Ordering::SeqCst), false);
612 #[stable(feature = "rust1", since = "1.0.0")]
613 pub fn fetch_or(&self, val
: bool
, order
: Ordering
) -> bool
{
614 unsafe { atomic_or(self.v.get(), val as u8, order) != 0 }
617 /// Logical "xor" with a boolean value.
619 /// Performs a logical "xor" operation on the current value and the argument `val`, and sets
620 /// the new value to the result.
622 /// Returns the previous value.
627 /// use std::sync::atomic::{AtomicBool, Ordering};
629 /// let foo = AtomicBool::new(true);
630 /// assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), true);
631 /// assert_eq!(foo.load(Ordering::SeqCst), true);
633 /// let foo = AtomicBool::new(true);
634 /// assert_eq!(foo.fetch_xor(true, Ordering::SeqCst), true);
635 /// assert_eq!(foo.load(Ordering::SeqCst), false);
637 /// let foo = AtomicBool::new(false);
638 /// assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), false);
639 /// assert_eq!(foo.load(Ordering::SeqCst), false);
642 #[stable(feature = "rust1", since = "1.0.0")]
643 pub fn fetch_xor(&self, val
: bool
, order
: Ordering
) -> bool
{
644 unsafe { atomic_xor(self.v.get(), val as u8, order) != 0 }
648 #[cfg(target_has_atomic = "ptr")]
649 impl<T
> AtomicPtr
<T
> {
650 /// Creates a new `AtomicPtr`.
655 /// use std::sync::atomic::AtomicPtr;
657 /// let ptr = &mut 5;
658 /// let atomic_ptr = AtomicPtr::new(ptr);
661 #[stable(feature = "rust1", since = "1.0.0")]
662 pub const fn new(p
: *mut T
) -> AtomicPtr
<T
> {
663 AtomicPtr { p: UnsafeCell::new(p) }
666 /// Returns a mutable reference to the underlying pointer.
668 /// This is safe because the mutable reference guarantees that no other threads are
669 /// concurrently accessing the atomic data.
674 /// use std::sync::atomic::{AtomicPtr, Ordering};
676 /// let mut atomic_ptr = AtomicPtr::new(&mut 10);
677 /// *atomic_ptr.get_mut() = &mut 5;
678 /// assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5);
681 #[stable(feature = "atomic_access", since = "1.15.0")]
682 pub fn get_mut(&mut self) -> &mut *mut T
{
683 unsafe { &mut *self.p.get() }
686 /// Consumes the atomic and returns the contained value.
688 /// This is safe because passing `self` by value guarantees that no other threads are
689 /// concurrently accessing the atomic data.
694 /// use std::sync::atomic::AtomicPtr;
696 /// let atomic_ptr = AtomicPtr::new(&mut 5);
697 /// assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);
700 #[stable(feature = "atomic_access", since = "1.15.0")]
701 pub fn into_inner(self) -> *mut T
{
705 /// Loads a value from the pointer.
707 /// `load` takes an [`Ordering`] argument which describes the memory ordering
708 /// of this operation.
712 /// Panics if `order` is [`Release`] or [`AcqRel`].
714 /// [`Ordering`]: enum.Ordering.html
715 /// [`Release`]: enum.Ordering.html#variant.Release
716 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
721 /// use std::sync::atomic::{AtomicPtr, Ordering};
723 /// let ptr = &mut 5;
724 /// let some_ptr = AtomicPtr::new(ptr);
726 /// let value = some_ptr.load(Ordering::Relaxed);
729 #[stable(feature = "rust1", since = "1.0.0")]
730 pub fn load(&self, order
: Ordering
) -> *mut T
{
731 unsafe { atomic_load(self.p.get() as *mut usize, order) as *mut T }
734 /// Stores a value into the pointer.
736 /// `store` takes an [`Ordering`] argument which describes the memory ordering
737 /// of this operation.
739 /// [`Ordering`]: enum.Ordering.html
744 /// use std::sync::atomic::{AtomicPtr, Ordering};
746 /// let ptr = &mut 5;
747 /// let some_ptr = AtomicPtr::new(ptr);
749 /// let other_ptr = &mut 10;
751 /// some_ptr.store(other_ptr, Ordering::Relaxed);
756 /// Panics if `order` is [`Acquire`] or [`AcqRel`].
758 /// [`Acquire`]: enum.Ordering.html#variant.Acquire
759 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
761 #[stable(feature = "rust1", since = "1.0.0")]
762 pub fn store(&self, ptr
: *mut T
, order
: Ordering
) {
764 atomic_store(self.p
.get() as *mut usize, ptr
as usize, order
);
768 /// Stores a value into the pointer, returning the previous value.
770 /// `swap` takes an [`Ordering`] argument which describes the memory ordering
771 /// of this operation.
773 /// [`Ordering`]: enum.Ordering.html
778 /// use std::sync::atomic::{AtomicPtr, Ordering};
780 /// let ptr = &mut 5;
781 /// let some_ptr = AtomicPtr::new(ptr);
783 /// let other_ptr = &mut 10;
785 /// let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
788 #[stable(feature = "rust1", since = "1.0.0")]
789 pub fn swap(&self, ptr
: *mut T
, order
: Ordering
) -> *mut T
{
790 unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T }
793 /// Stores a value into the pointer if the current value is the same as the `current` value.
795 /// The return value is always the previous value. If it is equal to `current`, then the value
798 /// `compare_and_swap` also takes an [`Ordering`] argument which describes the memory
799 /// ordering of this operation.
801 /// [`Ordering`]: enum.Ordering.html
806 /// use std::sync::atomic::{AtomicPtr, Ordering};
808 /// let ptr = &mut 5;
809 /// let some_ptr = AtomicPtr::new(ptr);
811 /// let other_ptr = &mut 10;
812 /// let another_ptr = &mut 10;
814 /// let value = some_ptr.compare_and_swap(other_ptr, another_ptr, Ordering::Relaxed);
817 #[stable(feature = "rust1", since = "1.0.0")]
818 pub fn compare_and_swap(&self, current
: *mut T
, new
: *mut T
, order
: Ordering
) -> *mut T
{
819 match self.compare_exchange(current
, new
, order
, strongest_failure_ordering(order
)) {
825 /// Stores a value into the pointer if the current value is the same as the `current` value.
827 /// The return value is a result indicating whether the new value was written and containing
828 /// the previous value. On success this value is guaranteed to be equal to `current`.
830 /// `compare_exchange` takes two [`Ordering`] arguments to describe the memory
831 /// ordering of this operation. The first describes the required ordering if
832 /// the operation succeeds while the second describes the required ordering when
833 /// the operation fails. The failure ordering can't be [`Release`] or [`AcqRel`]
834 /// and must be equivalent or weaker than the success ordering.
836 /// [`Ordering`]: enum.Ordering.html
837 /// [`Release`]: enum.Ordering.html#variant.Release
838 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
843 /// use std::sync::atomic::{AtomicPtr, Ordering};
845 /// let ptr = &mut 5;
846 /// let some_ptr = AtomicPtr::new(ptr);
848 /// let other_ptr = &mut 10;
849 /// let another_ptr = &mut 10;
851 /// let value = some_ptr.compare_exchange(other_ptr, another_ptr,
852 /// Ordering::SeqCst, Ordering::Relaxed);
855 #[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
856 pub fn compare_exchange(&self,
861 -> Result
<*mut T
, *mut T
> {
863 let res
= atomic_compare_exchange(self.p
.get() as *mut usize,
869 Ok(x
) => Ok(x
as *mut T
),
870 Err(x
) => Err(x
as *mut T
),
875 /// Stores a value into the pointer if the current value is the same as the `current` value.
877 /// Unlike [`compare_exchange`], this function is allowed to spuriously fail even when the
878 /// comparison succeeds, which can result in more efficient code on some platforms. The
879 /// return value is a result indicating whether the new value was written and containing the
882 /// `compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
883 /// ordering of this operation. The first describes the required ordering if the operation
884 /// succeeds while the second describes the required ordering when the operation fails. The
885 /// failure ordering can't be [`Release`] or [`AcqRel`] and must be equivalent or
886 /// weaker than the success ordering.
888 /// [`compare_exchange`]: #method.compare_exchange
889 /// [`Ordering`]: enum.Ordering.html
890 /// [`Release`]: enum.Ordering.html#variant.Release
891 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
896 /// use std::sync::atomic::{AtomicPtr, Ordering};
898 /// let some_ptr = AtomicPtr::new(&mut 5);
900 /// let new = &mut 10;
901 /// let mut old = some_ptr.load(Ordering::Relaxed);
903 /// match some_ptr.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
905 /// Err(x) => old = x,
910 #[stable(feature = "extended_compare_and_swap", since = "1.10.0")]
911 pub fn compare_exchange_weak(&self,
916 -> Result
<*mut T
, *mut T
> {
918 let res
= atomic_compare_exchange_weak(self.p
.get() as *mut usize,
924 Ok(x
) => Ok(x
as *mut T
),
925 Err(x
) => Err(x
as *mut T
),
931 #[cfg(target_has_atomic = "8")]
932 #[stable(feature = "atomic_bool_from", since = "1.24.0")]
933 impl From
<bool
> for AtomicBool
{
935 fn from(b
: bool
) -> Self { Self::new(b) }
938 #[cfg(target_has_atomic = "ptr")]
939 #[stable(feature = "atomic_from", since = "1.23.0")]
940 impl<T
> From
<*mut T
> for AtomicPtr
<T
> {
942 fn from(p
: *mut T
) -> Self { Self::new(p) }
945 #[cfg(target_has_atomic = "ptr")]
946 macro_rules
! atomic_int
{
953 $s_int_type
:expr
, $int_ref
:expr
,
955 $min_fn
:ident
, $max_fn
:ident
,
956 $int_type
:ident $atomic_type
:ident $atomic_init
:ident
) => {
957 /// An integer type which can be safely shared between threads.
959 /// This type has the same in-memory representation as the underlying
964 /// ). For more about the differences between atomic types and
965 /// non-atomic types, please see the [module-level documentation].
967 /// [module-level documentation]: index.html
969 pub struct $atomic_type
{
970 v
: UnsafeCell
<$int_type
>,
973 /// An atomic integer initialized to `0`.
975 pub const $atomic_init
: $atomic_type
= $atomic_type
::new(0);
978 impl Default
for $atomic_type
{
979 fn default() -> Self {
980 Self::new(Default
::default())
985 impl From
<$int_type
> for $atomic_type
{
987 fn from(v
: $int_type
) -> Self { Self::new(v) }
991 impl fmt
::Debug
for $atomic_type
{
992 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
993 fmt
::Debug
::fmt(&self.load(Ordering
::SeqCst
), f
)
997 // Send is implicitly implemented.
999 unsafe impl Sync
for $atomic_type {}
1003 concat
!("Creates a new atomic integer.
1008 ", $extra_feature
, "use std::sync::atomic::", stringify
!($atomic_type
), ";
1010 let atomic_forty_two = ", stringify
!($atomic_type
), "::new(42);
1014 pub const fn new(v
: $int_type
) -> Self {
1015 $atomic_type {v: UnsafeCell::new(v)}
1020 concat
!("Returns a mutable reference to the underlying integer.
1022 This is safe because the mutable reference guarantees that no other threads are
1023 concurrently accessing the atomic data.
1028 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1030 let mut some_var = ", stringify
!($atomic_type
), "::new(10);
1031 assert_eq!(*some_var.get_mut(), 10);
1032 *some_var.get_mut() = 5;
1033 assert_eq!(some_var.load(Ordering::SeqCst), 5);
1037 pub fn get_mut(&mut self) -> &mut $int_type
{
1038 unsafe { &mut *self.v.get() }
1043 concat
!("Consumes the atomic and returns the contained value.
1045 This is safe because passing `self` by value guarantees that no other threads are
1046 concurrently accessing the atomic data.
1051 ", $extra_feature
, "use std::sync::atomic::", stringify
!($atomic_type
), ";
1053 let some_var = ", stringify
!($atomic_type
), "::new(5);
1054 assert_eq!(some_var.into_inner(), 5);
1058 pub fn into_inner(self) -> $int_type
{
1064 concat
!("Loads a value from the atomic integer.
1066 `load` takes an [`Ordering`] argument which describes the memory ordering of this operation.
1070 Panics if `order` is [`Release`] or [`AcqRel`].
1072 [`Ordering`]: enum.Ordering.html
1073 [`Release`]: enum.Ordering.html#variant.Release
1074 [`AcqRel`]: enum.Ordering.html#variant.AcqRel
1079 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1081 let some_var = ", stringify
!($atomic_type
), "::new(5);
1083 assert_eq!(some_var.load(Ordering::Relaxed), 5);
1087 pub fn load(&self, order
: Ordering
) -> $int_type
{
1088 unsafe { atomic_load(self.v.get(), order) }
1093 concat
!("Stores a value into the atomic integer.
1095 `store` takes an [`Ordering`] argument which describes the memory ordering of this operation.
1097 [`Ordering`]: enum.Ordering.html
1102 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1104 let some_var = ", stringify
!($atomic_type
), "::new(5);
1106 some_var.store(10, Ordering::Relaxed);
1107 assert_eq!(some_var.load(Ordering::Relaxed), 10);
1112 Panics if `order` is [`Acquire`] or [`AcqRel`].
1114 [`Acquire`]: enum.Ordering.html#variant.Acquire
1115 [`AcqRel`]: enum.Ordering.html#variant.AcqRel"),
1118 pub fn store(&self, val
: $int_type
, order
: Ordering
) {
1119 unsafe { atomic_store(self.v.get(), val, order); }
1124 concat
!("Stores a value into the atomic integer, returning the previous value.
1126 `swap` takes an [`Ordering`] argument which describes the memory ordering of this operation.
1128 [`Ordering`]: enum.Ordering.html
1133 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1135 let some_var = ", stringify
!($atomic_type
), "::new(5);
1137 assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1141 pub fn swap(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1142 unsafe { atomic_swap(self.v.get(), val, order) }
1147 concat
!("Stores a value into the atomic integer if the current value is the same as
1148 the `current` value.
1150 The return value is always the previous value. If it is equal to `current`, then the
1153 `compare_and_swap` also takes an [`Ordering`] argument which describes the memory
1154 ordering of this operation.
1156 [`Ordering`]: enum.Ordering.html
1161 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1163 let some_var = ", stringify
!($atomic_type
), "::new(5);
1165 assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
1166 assert_eq!(some_var.load(Ordering::Relaxed), 10);
1168 assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
1169 assert_eq!(some_var.load(Ordering::Relaxed), 10);
1173 pub fn compare_and_swap(&self,
1176 order
: Ordering
) -> $int_type
{
1177 match self.compare_exchange(current
,
1180 strongest_failure_ordering(order
)) {
1188 concat
!("Stores a value into the atomic integer if the current value is the same as
1189 the `current` value.
1191 The return value is a result indicating whether the new value was written and
1192 containing the previous value. On success this value is guaranteed to be equal to
1195 `compare_exchange` takes two [`Ordering`] arguments to describe the memory
1196 ordering of this operation. The first describes the required ordering if
1197 the operation succeeds while the second describes the required ordering when
1198 the operation fails. The failure ordering can't be [`Release`] or [`AcqRel`] and
1199 must be equivalent or weaker than the success ordering.
1201 [`Ordering`]: enum.Ordering.html
1202 [`Release`]: enum.Ordering.html#variant.Release
1203 [`AcqRel`]: enum.Ordering.html#variant.AcqRel
1208 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1210 let some_var = ", stringify
!($atomic_type
), "::new(5);
1212 assert_eq!(some_var.compare_exchange(5, 10,
1216 assert_eq!(some_var.load(Ordering::Relaxed), 10);
1218 assert_eq!(some_var.compare_exchange(6, 12,
1222 assert_eq!(some_var.load(Ordering::Relaxed), 10);
1226 pub fn compare_exchange(&self,
1230 failure
: Ordering
) -> Result
<$int_type
, $int_type
> {
1231 unsafe { atomic_compare_exchange(self.v.get(), current, new, success, failure) }
1236 concat
!("Stores a value into the atomic integer if the current value is the same as
1237 the `current` value.
1239 Unlike [`compare_exchange`], this function is allowed to spuriously fail even
1240 when the comparison succeeds, which can result in more efficient code on some
1241 platforms. The return value is a result indicating whether the new value was
1242 written and containing the previous value.
1244 `compare_exchange_weak` takes two [`Ordering`] arguments to describe the memory
1245 ordering of this operation. The first describes the required ordering if the
1246 operation succeeds while the second describes the required ordering when the
1247 operation fails. The failure ordering can't be [`Release`] or [`AcqRel`] and
1248 must be equivalent or weaker than the success ordering.
1250 [`compare_exchange`]: #method.compare_exchange
1251 [`Ordering`]: enum.Ordering.html
1252 [`Release`]: enum.Ordering.html#variant.Release
1253 [`AcqRel`]: enum.Ordering.html#variant.AcqRel
1258 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1260 let val = ", stringify
!($atomic_type
), "::new(4);
1262 let mut old = val.load(Ordering::Relaxed);
1265 match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
1273 pub fn compare_exchange_weak(&self,
1277 failure
: Ordering
) -> Result
<$int_type
, $int_type
> {
1279 atomic_compare_exchange_weak(self.v
.get(), current
, new
, success
, failure
)
1285 concat
!("Adds to the current value, returning the previous value.
1287 This operation wraps around on overflow.
1292 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1294 let foo = ", stringify
!($atomic_type
), "::new(0);
1295 assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
1296 assert_eq!(foo.load(Ordering::SeqCst), 10);
1300 pub fn fetch_add(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1301 unsafe { atomic_add(self.v.get(), val, order) }
1306 concat
!("Subtracts from the current value, returning the previous value.
1308 This operation wraps around on overflow.
1313 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1315 let foo = ", stringify
!($atomic_type
), "::new(20);
1316 assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
1317 assert_eq!(foo.load(Ordering::SeqCst), 10);
1321 pub fn fetch_sub(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1322 unsafe { atomic_sub(self.v.get(), val, order) }
1327 concat
!("Bitwise \"and\" with the current value.
1329 Performs a bitwise \"and\" operation on the current value and the argument `val`, and
1330 sets the new value to the result.
1332 Returns the previous value.
1337 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1339 let foo = ", stringify
!($atomic_type
), "::new(0b101101);
1340 assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
1341 assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1345 pub fn fetch_and(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1346 unsafe { atomic_and(self.v.get(), val, order) }
1351 concat
!("Bitwise \"nand\" with the current value.
1353 Performs a bitwise \"nand\" operation on the current value and the argument `val`, and
1354 sets the new value to the result.
1356 Returns the previous value.
1361 ", $extra_feature
, "
1362 use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1364 let foo = ", stringify
!($atomic_type
), "::new(0x13);
1365 assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
1366 assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1370 pub fn fetch_nand(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1371 unsafe { atomic_nand(self.v.get(), val, order) }
1376 concat
!("Bitwise \"or\" with the current value.
1378 Performs a bitwise \"or\" operation on the current value and the argument `val`, and
1379 sets the new value to the result.
1381 Returns the previous value.
1386 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1388 let foo = ", stringify
!($atomic_type
), "::new(0b101101);
1389 assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
1390 assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1394 pub fn fetch_or(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1395 unsafe { atomic_or(self.v.get(), val, order) }
1400 concat
!("Bitwise \"xor\" with the current value.
1402 Performs a bitwise \"xor\" operation on the current value and the argument `val`, and
1403 sets the new value to the result.
1405 Returns the previous value.
1410 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1412 let foo = ", stringify
!($atomic_type
), "::new(0b101101);
1413 assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
1414 assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1418 pub fn fetch_xor(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1419 unsafe { atomic_xor(self.v.get(), val, order) }
1424 concat
!("Fetches the value, and applies a function to it that returns an optional
1425 new value. Returns a `Result` of `Ok(previous_value)` if the function returned `Some(_)`, else
1426 `Err(previous_value)`.
1428 Note: This may call the function multiple times if the value has been changed from other threads in
1429 the meantime, as long as the function returns `Some(_)`, but the function will have been applied
1430 but once to the stored value.
1435 #![feature(no_more_cas)]
1436 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1438 let x = ", stringify
!($atomic_type
), "::new(7);
1439 assert_eq!(x.fetch_update(|_| None, Ordering::SeqCst, Ordering::SeqCst), Err(7));
1440 assert_eq!(x.fetch_update(|x| Some(x + 1), Ordering::SeqCst, Ordering::SeqCst), Ok(7));
1441 assert_eq!(x.fetch_update(|x| Some(x + 1), Ordering::SeqCst, Ordering::SeqCst), Ok(8));
1442 assert_eq!(x.load(Ordering::SeqCst), 9);
1445 #[unstable(feature = "no_more_cas",
1446 reason
= "no more CAS loops in user code",
1448 pub fn fetch_update
<F
>(&self,
1450 fetch_order
: Ordering
,
1451 set_order
: Ordering
) -> Result
<$int_type
, $int_type
>
1452 where F
: FnMut($int_type
) -> Option
<$int_type
> {
1453 let mut prev
= self.load(fetch_order
);
1454 while let Some(next
) = f(prev
) {
1455 match self.compare_exchange_weak(prev
, next
, set_order
, fetch_order
) {
1456 x @
Ok(_
) => return x
,
1457 Err(next_prev
) => prev
= next_prev
1465 concat
!("Maximum with the current value.
1467 Finds the maximum of the current value and the argument `val`, and
1468 sets the new value to the result.
1470 Returns the previous value.
1475 #![feature(atomic_min_max)]
1476 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1478 let foo = ", stringify
!($atomic_type
), "::new(23);
1479 assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
1480 assert_eq!(foo.load(Ordering::SeqCst), 42);
1483 If you want to obtain the maximum value in one step, you can use the following:
1486 #![feature(atomic_min_max)]
1487 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1489 let foo = ", stringify
!($atomic_type
), "::new(23);
1491 let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
1492 assert!(max_foo == 42);
1495 #[unstable(feature = "atomic_min_max",
1496 reason
= "easier and faster min/max than writing manual CAS loop",
1498 pub fn fetch_max(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1499 unsafe { $max_fn(self.v.get(), val, order) }
1504 concat
!("Minimum with the current value.
1506 Finds the minimum of the current value and the argument `val`, and
1507 sets the new value to the result.
1509 Returns the previous value.
1514 #![feature(atomic_min_max)]
1515 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1517 let foo = ", stringify
!($atomic_type
), "::new(23);
1518 assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
1519 assert_eq!(foo.load(Ordering::Relaxed), 23);
1520 assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
1521 assert_eq!(foo.load(Ordering::Relaxed), 22);
1524 If you want to obtain the minimum value in one step, you can use the following:
1527 #![feature(atomic_min_max)]
1528 ", $extra_feature
, "use std::sync::atomic::{", stringify!($atomic_type), ", Ordering};
1530 let foo = ", stringify
!($atomic_type
), "::new(23);
1532 let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
1533 assert_eq!(min_foo, 12);
1536 #[unstable(feature = "atomic_min_max",
1537 reason
= "easier and faster min/max than writing manual CAS loop",
1539 pub fn fetch_min(&self, val
: $int_type
, order
: Ordering
) -> $int_type
{
1540 unsafe { $min_fn(self.v.get(), val, order) }
1548 #[cfg(target_has_atomic = "8")]
1550 unstable(feature
= "integer_atomics", issue
= "32976"),
1551 unstable(feature
= "integer_atomics", issue
= "32976"),
1552 unstable(feature
= "integer_atomics", issue
= "32976"),
1553 unstable(feature
= "integer_atomics", issue
= "32976"),
1554 unstable(feature
= "integer_atomics", issue
= "32976"),
1555 unstable(feature
= "integer_atomics", issue
= "32976"),
1556 "i8", "../../../std/primitive.i8.html",
1557 "#![feature(integer_atomics)]\n\n",
1558 atomic_min
, atomic_max
,
1559 i8 AtomicI8 ATOMIC_I8_INIT
1561 #[cfg(target_has_atomic = "8")]
1563 unstable(feature
= "integer_atomics", issue
= "32976"),
1564 unstable(feature
= "integer_atomics", issue
= "32976"),
1565 unstable(feature
= "integer_atomics", issue
= "32976"),
1566 unstable(feature
= "integer_atomics", issue
= "32976"),
1567 unstable(feature
= "integer_atomics", issue
= "32976"),
1568 unstable(feature
= "integer_atomics", issue
= "32976"),
1569 "u8", "../../../std/primitive.u8.html",
1570 "#![feature(integer_atomics)]\n\n",
1571 atomic_umin
, atomic_umax
,
1572 u8 AtomicU8 ATOMIC_U8_INIT
1574 #[cfg(target_has_atomic = "16")]
1576 unstable(feature
= "integer_atomics", issue
= "32976"),
1577 unstable(feature
= "integer_atomics", issue
= "32976"),
1578 unstable(feature
= "integer_atomics", issue
= "32976"),
1579 unstable(feature
= "integer_atomics", issue
= "32976"),
1580 unstable(feature
= "integer_atomics", issue
= "32976"),
1581 unstable(feature
= "integer_atomics", issue
= "32976"),
1582 "i16", "../../../std/primitive.i16.html",
1583 "#![feature(integer_atomics)]\n\n",
1584 atomic_min
, atomic_max
,
1585 i16 AtomicI16 ATOMIC_I16_INIT
1587 #[cfg(target_has_atomic = "16")]
1589 unstable(feature
= "integer_atomics", issue
= "32976"),
1590 unstable(feature
= "integer_atomics", issue
= "32976"),
1591 unstable(feature
= "integer_atomics", issue
= "32976"),
1592 unstable(feature
= "integer_atomics", issue
= "32976"),
1593 unstable(feature
= "integer_atomics", issue
= "32976"),
1594 unstable(feature
= "integer_atomics", issue
= "32976"),
1595 "u16", "../../../std/primitive.u16.html",
1596 "#![feature(integer_atomics)]\n\n",
1597 atomic_umin
, atomic_umax
,
1598 u16 AtomicU16 ATOMIC_U16_INIT
1600 #[cfg(target_has_atomic = "32")]
1602 unstable(feature
= "integer_atomics", issue
= "32976"),
1603 unstable(feature
= "integer_atomics", issue
= "32976"),
1604 unstable(feature
= "integer_atomics", issue
= "32976"),
1605 unstable(feature
= "integer_atomics", issue
= "32976"),
1606 unstable(feature
= "integer_atomics", issue
= "32976"),
1607 unstable(feature
= "integer_atomics", issue
= "32976"),
1608 "i32", "../../../std/primitive.i32.html",
1609 "#![feature(integer_atomics)]\n\n",
1610 atomic_min
, atomic_max
,
1611 i32 AtomicI32 ATOMIC_I32_INIT
1613 #[cfg(target_has_atomic = "32")]
1615 unstable(feature
= "integer_atomics", issue
= "32976"),
1616 unstable(feature
= "integer_atomics", issue
= "32976"),
1617 unstable(feature
= "integer_atomics", issue
= "32976"),
1618 unstable(feature
= "integer_atomics", issue
= "32976"),
1619 unstable(feature
= "integer_atomics", issue
= "32976"),
1620 unstable(feature
= "integer_atomics", issue
= "32976"),
1621 "u32", "../../../std/primitive.u32.html",
1622 "#![feature(integer_atomics)]\n\n",
1623 atomic_umin
, atomic_umax
,
1624 u32 AtomicU32 ATOMIC_U32_INIT
1626 #[cfg(target_has_atomic = "64")]
1628 unstable(feature
= "integer_atomics", issue
= "32976"),
1629 unstable(feature
= "integer_atomics", issue
= "32976"),
1630 unstable(feature
= "integer_atomics", issue
= "32976"),
1631 unstable(feature
= "integer_atomics", issue
= "32976"),
1632 unstable(feature
= "integer_atomics", issue
= "32976"),
1633 unstable(feature
= "integer_atomics", issue
= "32976"),
1634 "i64", "../../../std/primitive.i64.html",
1635 "#![feature(integer_atomics)]\n\n",
1636 atomic_min
, atomic_max
,
1637 i64 AtomicI64 ATOMIC_I64_INIT
1639 #[cfg(target_has_atomic = "64")]
1641 unstable(feature
= "integer_atomics", issue
= "32976"),
1642 unstable(feature
= "integer_atomics", issue
= "32976"),
1643 unstable(feature
= "integer_atomics", issue
= "32976"),
1644 unstable(feature
= "integer_atomics", issue
= "32976"),
1645 unstable(feature
= "integer_atomics", issue
= "32976"),
1646 unstable(feature
= "integer_atomics", issue
= "32976"),
1647 "u64", "../../../std/primitive.u64.html",
1648 "#![feature(integer_atomics)]\n\n",
1649 atomic_umin
, atomic_umax
,
1650 u64 AtomicU64 ATOMIC_U64_INIT
1652 #[cfg(target_has_atomic = "ptr")]
1654 stable(feature
= "rust1", since
= "1.0.0"),
1655 stable(feature
= "extended_compare_and_swap", since
= "1.10.0"),
1656 stable(feature
= "atomic_debug", since
= "1.3.0"),
1657 stable(feature
= "atomic_access", since
= "1.15.0"),
1658 stable(feature
= "atomic_from", since
= "1.23.0"),
1659 stable(feature
= "atomic_nand", since
= "1.27.0"),
1660 "isize", "../../../std/primitive.isize.html",
1662 atomic_min
, atomic_max
,
1663 isize AtomicIsize ATOMIC_ISIZE_INIT
1665 #[cfg(target_has_atomic = "ptr")]
1667 stable(feature
= "rust1", since
= "1.0.0"),
1668 stable(feature
= "extended_compare_and_swap", since
= "1.10.0"),
1669 stable(feature
= "atomic_debug", since
= "1.3.0"),
1670 stable(feature
= "atomic_access", since
= "1.15.0"),
1671 stable(feature
= "atomic_from", since
= "1.23.0"),
1672 stable(feature
= "atomic_nand", since
= "1.27.0"),
1673 "usize", "../../../std/primitive.usize.html",
1675 atomic_umin
, atomic_umax
,
1676 usize AtomicUsize ATOMIC_USIZE_INIT
1680 fn strongest_failure_ordering(order
: Ordering
) -> Ordering
{
1687 __Nonexhaustive
=> __Nonexhaustive
,
1692 unsafe fn atomic_store
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) {
1694 Release
=> intrinsics
::atomic_store_rel(dst
, val
),
1695 Relaxed
=> intrinsics
::atomic_store_relaxed(dst
, val
),
1696 SeqCst
=> intrinsics
::atomic_store(dst
, val
),
1697 Acquire
=> panic
!("there is no such thing as an acquire store"),
1698 AcqRel
=> panic
!("there is no such thing as an acquire/release store"),
1699 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1704 unsafe fn atomic_load
<T
>(dst
: *const T
, order
: Ordering
) -> T
{
1706 Acquire
=> intrinsics
::atomic_load_acq(dst
),
1707 Relaxed
=> intrinsics
::atomic_load_relaxed(dst
),
1708 SeqCst
=> intrinsics
::atomic_load(dst
),
1709 Release
=> panic
!("there is no such thing as a release load"),
1710 AcqRel
=> panic
!("there is no such thing as an acquire/release load"),
1711 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1716 unsafe fn atomic_swap
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1718 Acquire
=> intrinsics
::atomic_xchg_acq(dst
, val
),
1719 Release
=> intrinsics
::atomic_xchg_rel(dst
, val
),
1720 AcqRel
=> intrinsics
::atomic_xchg_acqrel(dst
, val
),
1721 Relaxed
=> intrinsics
::atomic_xchg_relaxed(dst
, val
),
1722 SeqCst
=> intrinsics
::atomic_xchg(dst
, val
),
1723 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1727 /// Returns the previous value (like __sync_fetch_and_add).
1729 unsafe fn atomic_add
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1731 Acquire
=> intrinsics
::atomic_xadd_acq(dst
, val
),
1732 Release
=> intrinsics
::atomic_xadd_rel(dst
, val
),
1733 AcqRel
=> intrinsics
::atomic_xadd_acqrel(dst
, val
),
1734 Relaxed
=> intrinsics
::atomic_xadd_relaxed(dst
, val
),
1735 SeqCst
=> intrinsics
::atomic_xadd(dst
, val
),
1736 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1740 /// Returns the previous value (like __sync_fetch_and_sub).
1742 unsafe fn atomic_sub
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1744 Acquire
=> intrinsics
::atomic_xsub_acq(dst
, val
),
1745 Release
=> intrinsics
::atomic_xsub_rel(dst
, val
),
1746 AcqRel
=> intrinsics
::atomic_xsub_acqrel(dst
, val
),
1747 Relaxed
=> intrinsics
::atomic_xsub_relaxed(dst
, val
),
1748 SeqCst
=> intrinsics
::atomic_xsub(dst
, val
),
1749 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1754 unsafe fn atomic_compare_exchange
<T
>(dst
: *mut T
,
1760 let (val
, ok
) = match (success
, failure
) {
1761 (Acquire
, Acquire
) => intrinsics
::atomic_cxchg_acq(dst
, old
, new
),
1762 (Release
, Relaxed
) => intrinsics
::atomic_cxchg_rel(dst
, old
, new
),
1763 (AcqRel
, Acquire
) => intrinsics
::atomic_cxchg_acqrel(dst
, old
, new
),
1764 (Relaxed
, Relaxed
) => intrinsics
::atomic_cxchg_relaxed(dst
, old
, new
),
1765 (SeqCst
, SeqCst
) => intrinsics
::atomic_cxchg(dst
, old
, new
),
1766 (Acquire
, Relaxed
) => intrinsics
::atomic_cxchg_acq_failrelaxed(dst
, old
, new
),
1767 (AcqRel
, Relaxed
) => intrinsics
::atomic_cxchg_acqrel_failrelaxed(dst
, old
, new
),
1768 (SeqCst
, Relaxed
) => intrinsics
::atomic_cxchg_failrelaxed(dst
, old
, new
),
1769 (SeqCst
, Acquire
) => intrinsics
::atomic_cxchg_failacq(dst
, old
, new
),
1770 (__Nonexhaustive
, _
) => panic
!("invalid memory ordering"),
1771 (_
, __Nonexhaustive
) => panic
!("invalid memory ordering"),
1772 (_
, AcqRel
) => panic
!("there is no such thing as an acquire/release failure ordering"),
1773 (_
, Release
) => panic
!("there is no such thing as a release failure ordering"),
1774 _
=> panic
!("a failure ordering can't be stronger than a success ordering"),
1776 if ok { Ok(val) }
else { Err(val) }
1780 unsafe fn atomic_compare_exchange_weak
<T
>(dst
: *mut T
,
1786 let (val
, ok
) = match (success
, failure
) {
1787 (Acquire
, Acquire
) => intrinsics
::atomic_cxchgweak_acq(dst
, old
, new
),
1788 (Release
, Relaxed
) => intrinsics
::atomic_cxchgweak_rel(dst
, old
, new
),
1789 (AcqRel
, Acquire
) => intrinsics
::atomic_cxchgweak_acqrel(dst
, old
, new
),
1790 (Relaxed
, Relaxed
) => intrinsics
::atomic_cxchgweak_relaxed(dst
, old
, new
),
1791 (SeqCst
, SeqCst
) => intrinsics
::atomic_cxchgweak(dst
, old
, new
),
1792 (Acquire
, Relaxed
) => intrinsics
::atomic_cxchgweak_acq_failrelaxed(dst
, old
, new
),
1793 (AcqRel
, Relaxed
) => intrinsics
::atomic_cxchgweak_acqrel_failrelaxed(dst
, old
, new
),
1794 (SeqCst
, Relaxed
) => intrinsics
::atomic_cxchgweak_failrelaxed(dst
, old
, new
),
1795 (SeqCst
, Acquire
) => intrinsics
::atomic_cxchgweak_failacq(dst
, old
, new
),
1796 (__Nonexhaustive
, _
) => panic
!("invalid memory ordering"),
1797 (_
, __Nonexhaustive
) => panic
!("invalid memory ordering"),
1798 (_
, AcqRel
) => panic
!("there is no such thing as an acquire/release failure ordering"),
1799 (_
, Release
) => panic
!("there is no such thing as a release failure ordering"),
1800 _
=> panic
!("a failure ordering can't be stronger than a success ordering"),
1802 if ok { Ok(val) }
else { Err(val) }
1806 unsafe fn atomic_and
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1808 Acquire
=> intrinsics
::atomic_and_acq(dst
, val
),
1809 Release
=> intrinsics
::atomic_and_rel(dst
, val
),
1810 AcqRel
=> intrinsics
::atomic_and_acqrel(dst
, val
),
1811 Relaxed
=> intrinsics
::atomic_and_relaxed(dst
, val
),
1812 SeqCst
=> intrinsics
::atomic_and(dst
, val
),
1813 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1818 unsafe fn atomic_nand
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1820 Acquire
=> intrinsics
::atomic_nand_acq(dst
, val
),
1821 Release
=> intrinsics
::atomic_nand_rel(dst
, val
),
1822 AcqRel
=> intrinsics
::atomic_nand_acqrel(dst
, val
),
1823 Relaxed
=> intrinsics
::atomic_nand_relaxed(dst
, val
),
1824 SeqCst
=> intrinsics
::atomic_nand(dst
, val
),
1825 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1830 unsafe fn atomic_or
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1832 Acquire
=> intrinsics
::atomic_or_acq(dst
, val
),
1833 Release
=> intrinsics
::atomic_or_rel(dst
, val
),
1834 AcqRel
=> intrinsics
::atomic_or_acqrel(dst
, val
),
1835 Relaxed
=> intrinsics
::atomic_or_relaxed(dst
, val
),
1836 SeqCst
=> intrinsics
::atomic_or(dst
, val
),
1837 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1842 unsafe fn atomic_xor
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1844 Acquire
=> intrinsics
::atomic_xor_acq(dst
, val
),
1845 Release
=> intrinsics
::atomic_xor_rel(dst
, val
),
1846 AcqRel
=> intrinsics
::atomic_xor_acqrel(dst
, val
),
1847 Relaxed
=> intrinsics
::atomic_xor_relaxed(dst
, val
),
1848 SeqCst
=> intrinsics
::atomic_xor(dst
, val
),
1849 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1853 /// returns the max value (signed comparison)
1855 unsafe fn atomic_max
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1857 Acquire
=> intrinsics
::atomic_max_acq(dst
, val
),
1858 Release
=> intrinsics
::atomic_max_rel(dst
, val
),
1859 AcqRel
=> intrinsics
::atomic_max_acqrel(dst
, val
),
1860 Relaxed
=> intrinsics
::atomic_max_relaxed(dst
, val
),
1861 SeqCst
=> intrinsics
::atomic_max(dst
, val
),
1862 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1866 /// returns the min value (signed comparison)
1868 unsafe fn atomic_min
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1870 Acquire
=> intrinsics
::atomic_min_acq(dst
, val
),
1871 Release
=> intrinsics
::atomic_min_rel(dst
, val
),
1872 AcqRel
=> intrinsics
::atomic_min_acqrel(dst
, val
),
1873 Relaxed
=> intrinsics
::atomic_min_relaxed(dst
, val
),
1874 SeqCst
=> intrinsics
::atomic_min(dst
, val
),
1875 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1879 /// returns the max value (signed comparison)
1881 unsafe fn atomic_umax
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1883 Acquire
=> intrinsics
::atomic_umax_acq(dst
, val
),
1884 Release
=> intrinsics
::atomic_umax_rel(dst
, val
),
1885 AcqRel
=> intrinsics
::atomic_umax_acqrel(dst
, val
),
1886 Relaxed
=> intrinsics
::atomic_umax_relaxed(dst
, val
),
1887 SeqCst
=> intrinsics
::atomic_umax(dst
, val
),
1888 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1892 /// returns the min value (signed comparison)
1894 unsafe fn atomic_umin
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1896 Acquire
=> intrinsics
::atomic_umin_acq(dst
, val
),
1897 Release
=> intrinsics
::atomic_umin_rel(dst
, val
),
1898 AcqRel
=> intrinsics
::atomic_umin_acqrel(dst
, val
),
1899 Relaxed
=> intrinsics
::atomic_umin_relaxed(dst
, val
),
1900 SeqCst
=> intrinsics
::atomic_umin(dst
, val
),
1901 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1905 /// An atomic fence.
1907 /// Depending on the specified order, a fence prevents the compiler and CPU from
1908 /// reordering certain types of memory operations around it.
1909 /// That creates synchronizes-with relationships between it and atomic operations
1910 /// or fences in other threads.
1912 /// A fence 'A' which has (at least) [`Release`] ordering semantics, synchronizes
1913 /// with a fence 'B' with (at least) [`Acquire`] semantics, if and only if there
1914 /// exist operations X and Y, both operating on some atomic object 'M' such
1915 /// that A is sequenced before X, Y is synchronized before B and Y observes
1916 /// the change to M. This provides a happens-before dependence between A and B.
1919 /// Thread 1 Thread 2
1921 /// fence(Release); A --------------
1922 /// x.store(3, Relaxed); X --------- |
1925 /// -------------> Y if x.load(Relaxed) == 3 {
1926 /// |-------> B fence(Acquire);
1931 /// Atomic operations with [`Release`] or [`Acquire`] semantics can also synchronize
1934 /// A fence which has [`SeqCst`] ordering, in addition to having both [`Acquire`]
1935 /// and [`Release`] semantics, participates in the global program order of the
1936 /// other [`SeqCst`] operations and/or fences.
1938 /// Accepts [`Acquire`], [`Release`], [`AcqRel`] and [`SeqCst`] orderings.
1942 /// Panics if `order` is [`Relaxed`].
1947 /// use std::sync::atomic::AtomicBool;
1948 /// use std::sync::atomic::fence;
1949 /// use std::sync::atomic::Ordering;
1951 /// // A mutual exclusion primitive based on spinlock.
1952 /// pub struct Mutex {
1953 /// flag: AtomicBool,
1957 /// pub fn new() -> Mutex {
1959 /// flag: AtomicBool::new(false),
1963 /// pub fn lock(&self) {
1964 /// while !self.flag.compare_and_swap(false, true, Ordering::Relaxed) {}
1965 /// // This fence synchronizes-with store in `unlock`.
1966 /// fence(Ordering::Acquire);
1969 /// pub fn unlock(&self) {
1970 /// self.flag.store(false, Ordering::Release);
1975 /// [`Ordering`]: enum.Ordering.html
1976 /// [`Acquire`]: enum.Ordering.html#variant.Acquire
1977 /// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
1978 /// [`Release`]: enum.Ordering.html#variant.Release
1979 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
1980 /// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
1982 #[stable(feature = "rust1", since = "1.0.0")]
1983 pub fn fence(order
: Ordering
) {
1986 Acquire
=> intrinsics
::atomic_fence_acq(),
1987 Release
=> intrinsics
::atomic_fence_rel(),
1988 AcqRel
=> intrinsics
::atomic_fence_acqrel(),
1989 SeqCst
=> intrinsics
::atomic_fence(),
1990 Relaxed
=> panic
!("there is no such thing as a relaxed fence"),
1991 __Nonexhaustive
=> panic
!("invalid memory ordering"),
1997 /// A compiler memory fence.
1999 /// `compiler_fence` does not emit any machine code, but restricts the kinds
2000 /// of memory re-ordering the compiler is allowed to do. Specifically, depending on
2001 /// the given [`Ordering`] semantics, the compiler may be disallowed from moving reads
2002 /// or writes from before or after the call to the other side of the call to
2003 /// `compiler_fence`. Note that it does **not** prevent the *hardware*
2004 /// from doing such re-ordering. This is not a problem in a single-threaded,
2005 /// execution context, but when other threads may modify memory at the same
2006 /// time, stronger synchronization primitives such as [`fence`] are required.
2008 /// The re-ordering prevented by the different ordering semantics are:
2010 /// - with [`SeqCst`], no re-ordering of reads and writes across this point is allowed.
2011 /// - with [`Release`], preceding reads and writes cannot be moved past subsequent writes.
2012 /// - with [`Acquire`], subsequent reads and writes cannot be moved ahead of preceding reads.
2013 /// - with [`AcqRel`], both of the above rules are enforced.
2015 /// `compiler_fence` is generally only useful for preventing a thread from
2016 /// racing *with itself*. That is, if a given thread is executing one piece
2017 /// of code, and is then interrupted, and starts executing code elsewhere
2018 /// (while still in the same thread, and conceptually still on the same
2019 /// core). In traditional programs, this can only occur when a signal
2020 /// handler is registered. In more low-level code, such situations can also
2021 /// arise when handling interrupts, when implementing green threads with
2022 /// pre-emption, etc. Curious readers are encouraged to read the Linux kernel's
2023 /// discussion of [memory barriers].
2027 /// Panics if `order` is [`Relaxed`].
2031 /// Without `compiler_fence`, the `assert_eq!` in following code
2032 /// is *not* guaranteed to succeed, despite everything happening in a single thread.
2033 /// To see why, remember that the compiler is free to swap the stores to
2034 /// `IMPORTANT_VARIABLE` and `IS_READ` since they are both
2035 /// `Ordering::Relaxed`. If it does, and the signal handler is invoked right
2036 /// after `IS_READY` is updated, then the signal handler will see
2037 /// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
2038 /// Using a `compiler_fence` remedies this situation.
2041 /// use std::sync::atomic::{AtomicBool, AtomicUsize};
2042 /// use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
2043 /// use std::sync::atomic::Ordering;
2044 /// use std::sync::atomic::compiler_fence;
2046 /// static IMPORTANT_VARIABLE: AtomicUsize = ATOMIC_USIZE_INIT;
2047 /// static IS_READY: AtomicBool = ATOMIC_BOOL_INIT;
2050 /// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
2051 /// // prevent earlier writes from being moved beyond this point
2052 /// compiler_fence(Ordering::Release);
2053 /// IS_READY.store(true, Ordering::Relaxed);
2056 /// fn signal_handler() {
2057 /// if IS_READY.load(Ordering::Relaxed) {
2058 /// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42);
2063 /// [`fence`]: fn.fence.html
2064 /// [`Ordering`]: enum.Ordering.html
2065 /// [`Acquire`]: enum.Ordering.html#variant.Acquire
2066 /// [`SeqCst`]: enum.Ordering.html#variant.SeqCst
2067 /// [`Release`]: enum.Ordering.html#variant.Release
2068 /// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
2069 /// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
2070 /// [memory barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt
2072 #[stable(feature = "compiler_fences", since = "1.21.0")]
2073 pub fn compiler_fence(order
: Ordering
) {
2076 Acquire
=> intrinsics
::atomic_singlethreadfence_acq(),
2077 Release
=> intrinsics
::atomic_singlethreadfence_rel(),
2078 AcqRel
=> intrinsics
::atomic_singlethreadfence_acqrel(),
2079 SeqCst
=> intrinsics
::atomic_singlethreadfence(),
2080 Relaxed
=> panic
!("there is no such thing as a relaxed compiler fence"),
2081 __Nonexhaustive
=> panic
!("invalid memory ordering"),
2087 #[cfg(target_has_atomic = "8")]
2088 #[stable(feature = "atomic_debug", since = "1.3.0")]
2089 impl fmt
::Debug
for AtomicBool
{
2090 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
2091 fmt
::Debug
::fmt(&self.load(Ordering
::SeqCst
), f
)
2095 #[cfg(target_has_atomic = "ptr")]
2096 #[stable(feature = "atomic_debug", since = "1.3.0")]
2097 impl<T
> fmt
::Debug
for AtomicPtr
<T
> {
2098 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
2099 fmt
::Debug
::fmt(&self.load(Ordering
::SeqCst
), f
)
2103 #[cfg(target_has_atomic = "ptr")]
2104 #[stable(feature = "atomic_pointer", since = "1.24.0")]
2105 impl<T
> fmt
::Pointer
for AtomicPtr
<T
> {
2106 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
2107 fmt
::Pointer
::fmt(&self.load(Ordering
::SeqCst
), f
)