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 //! Each method takes an `Ordering` which represents the strength of
23 //! the memory barrier for that operation. These orderings are the
24 //! same as [LLVM atomic orderings][1].
26 //! [1]: http://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations
28 //! Atomic variables are safe to share between threads (they implement `Sync`)
29 //! but they do not themselves provide the mechanism for sharing. The most
30 //! common way to share an atomic variable is to put it into an `Arc` (an
31 //! atomically-reference-counted shared pointer).
33 //! Most atomic types may be stored in static variables, initialized using
34 //! the provided static initializers like `INIT_ATOMIC_BOOL`. Atomic statics
35 //! are often used for lazy global initialization.
40 //! A simple spinlock:
43 //! use std::sync::Arc;
44 //! use std::sync::atomic::{AtomicUsize, Ordering};
48 //! let spinlock = Arc::new(AtomicUsize::new(1));
50 //! let spinlock_clone = spinlock.clone();
51 //! thread::spawn(move|| {
52 //! spinlock_clone.store(0, Ordering::SeqCst);
55 //! // Wait for the other thread to release the lock
56 //! while spinlock.load(Ordering::SeqCst) != 0 {}
60 //! Keep a global count of live threads:
63 //! use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
65 //! static GLOBAL_THREAD_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
67 //! let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
68 //! println!("live threads: {}", old_thread_count + 1);
71 #![stable(feature = "rust1", since = "1.0.0")]
73 use self::Ordering
::*;
79 use marker
::PhantomData
;
83 /// A boolean type which can be safely shared between threads.
84 #[stable(feature = "rust1", since = "1.0.0")]
85 pub struct AtomicBool
{
89 impl Default
for AtomicBool
{
90 fn default() -> AtomicBool
{
95 unsafe impl Sync
for AtomicBool {}
97 /// A signed integer type which can be safely shared between threads.
98 #[stable(feature = "rust1", since = "1.0.0")]
99 pub struct AtomicIsize
{
100 v
: UnsafeCell
<isize>,
103 impl Default
for AtomicIsize
{
104 fn default() -> AtomicIsize
{
109 unsafe impl Sync
for AtomicIsize {}
111 /// An unsigned integer type which can be safely shared between threads.
112 #[stable(feature = "rust1", since = "1.0.0")]
113 pub struct AtomicUsize
{
114 v
: UnsafeCell
<usize>,
117 impl Default
for AtomicUsize
{
118 fn default() -> AtomicUsize
{
123 unsafe impl Sync
for AtomicUsize {}
125 /// A raw pointer type which can be safely shared between threads.
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub struct AtomicPtr
<T
> {
128 p
: UnsafeCell
<usize>,
129 _marker
: PhantomData
<*mut T
>,
132 impl<T
> Default
for AtomicPtr
<T
> {
133 fn default() -> AtomicPtr
<T
> {
134 AtomicPtr
::new(::ptr
::null_mut())
138 unsafe impl<T
> Sync
for AtomicPtr
<T
> {}
140 /// Atomic memory orderings
142 /// Memory orderings limit the ways that both the compiler and CPU may reorder
143 /// instructions around atomic operations. At its most restrictive,
144 /// "sequentially consistent" atomics allow neither reads nor writes
145 /// to be moved either before or after the atomic operation; on the other end
146 /// "relaxed" atomics allow all reorderings.
148 /// Rust's memory orderings are [the same as
149 /// C++'s](http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync).
150 #[stable(feature = "rust1", since = "1.0.0")]
151 #[derive(Copy, Clone)]
153 /// No ordering constraints, only atomic operations.
154 #[stable(feature = "rust1", since = "1.0.0")]
156 /// When coupled with a store, all previous writes become visible
157 /// to another thread that performs a load with `Acquire` ordering
158 /// on the same value.
159 #[stable(feature = "rust1", since = "1.0.0")]
161 /// When coupled with a load, all subsequent loads will see data
162 /// written before a store with `Release` ordering on the same value
163 /// in another thread.
164 #[stable(feature = "rust1", since = "1.0.0")]
166 /// When coupled with a load, uses `Acquire` ordering, and with a store
167 /// `Release` ordering.
168 #[stable(feature = "rust1", since = "1.0.0")]
170 /// Like `AcqRel` with the additional guarantee that all threads see all
171 /// sequentially consistent operations in the same order.
172 #[stable(feature = "rust1", since = "1.0.0")]
176 /// An `AtomicBool` initialized to `false`.
177 #[stable(feature = "rust1", since = "1.0.0")]
178 pub const ATOMIC_BOOL_INIT
: AtomicBool
=
179 AtomicBool { v: UnsafeCell { value: 0 }
};
180 /// An `AtomicIsize` initialized to `0`.
181 #[stable(feature = "rust1", since = "1.0.0")]
182 pub const ATOMIC_ISIZE_INIT
: AtomicIsize
=
183 AtomicIsize { v: UnsafeCell { value: 0 }
};
184 /// An `AtomicUsize` initialized to `0`.
185 #[stable(feature = "rust1", since = "1.0.0")]
186 pub const ATOMIC_USIZE_INIT
: AtomicUsize
=
187 AtomicUsize { v: UnsafeCell { value: 0, }
};
189 // NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
190 const UINT_TRUE
: usize = !0;
193 /// Creates a new `AtomicBool`.
198 /// use std::sync::atomic::AtomicBool;
200 /// let atomic_true = AtomicBool::new(true);
201 /// let atomic_false = AtomicBool::new(false);
204 #[stable(feature = "rust1", since = "1.0.0")]
205 pub fn new(v
: bool
) -> AtomicBool
{
206 let val
= if v { UINT_TRUE }
else { 0 }
;
207 AtomicBool { v: UnsafeCell::new(val) }
210 /// Loads a value from the bool.
212 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
216 /// Panics if `order` is `Release` or `AcqRel`.
221 /// use std::sync::atomic::{AtomicBool, Ordering};
223 /// let some_bool = AtomicBool::new(true);
225 /// let value = some_bool.load(Ordering::Relaxed);
228 #[stable(feature = "rust1", since = "1.0.0")]
229 pub fn load(&self, order
: Ordering
) -> bool
{
230 unsafe { atomic_load(self.v.get(), order) > 0 }
233 /// Stores a value into the bool.
235 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
240 /// use std::sync::atomic::{AtomicBool, Ordering};
242 /// let some_bool = AtomicBool::new(true);
244 /// some_bool.store(false, Ordering::Relaxed);
249 /// Panics if `order` is `Acquire` or `AcqRel`.
251 #[stable(feature = "rust1", since = "1.0.0")]
252 pub fn store(&self, val
: bool
, order
: Ordering
) {
253 let val
= if val { UINT_TRUE }
else { 0 }
;
255 unsafe { atomic_store(self.v.get(), val, order); }
258 /// Stores a value into the bool, returning the old value.
260 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
265 /// use std::sync::atomic::{AtomicBool, Ordering};
267 /// let some_bool = AtomicBool::new(true);
269 /// let value = some_bool.swap(false, Ordering::Relaxed);
272 #[stable(feature = "rust1", since = "1.0.0")]
273 pub fn swap(&self, val
: bool
, order
: Ordering
) -> bool
{
274 let val
= if val { UINT_TRUE }
else { 0 }
;
276 unsafe { atomic_swap(self.v.get(), val, order) > 0 }
279 /// Stores a value into the bool if the current value is the same as the expected value.
281 /// The return value is always the previous value. If it is equal to `old`, then the value was
284 /// `swap` also takes an `Ordering` argument which describes the memory ordering of this
290 /// use std::sync::atomic::{AtomicBool, Ordering};
292 /// let some_bool = AtomicBool::new(true);
294 /// let value = some_bool.store(false, Ordering::Relaxed);
297 #[stable(feature = "rust1", since = "1.0.0")]
298 pub fn compare_and_swap(&self, old
: bool
, new
: bool
, order
: Ordering
) -> bool
{
299 let old
= if old { UINT_TRUE }
else { 0 }
;
300 let new
= if new { UINT_TRUE }
else { 0 }
;
302 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) > 0 }
305 /// Logical "and" with a boolean value.
307 /// Performs a logical "and" operation on the current value and the argument `val`, and sets
308 /// the new value to the result.
310 /// Returns the previous value.
315 /// use std::sync::atomic::{AtomicBool, Ordering};
317 /// let foo = AtomicBool::new(true);
318 /// assert_eq!(true, foo.fetch_and(false, Ordering::SeqCst));
319 /// assert_eq!(false, foo.load(Ordering::SeqCst));
321 /// let foo = AtomicBool::new(true);
322 /// assert_eq!(true, foo.fetch_and(true, Ordering::SeqCst));
323 /// assert_eq!(true, foo.load(Ordering::SeqCst));
325 /// let foo = AtomicBool::new(false);
326 /// assert_eq!(false, foo.fetch_and(false, Ordering::SeqCst));
327 /// assert_eq!(false, foo.load(Ordering::SeqCst));
330 #[stable(feature = "rust1", since = "1.0.0")]
331 pub fn fetch_and(&self, val
: bool
, order
: Ordering
) -> bool
{
332 let val
= if val { UINT_TRUE }
else { 0 }
;
334 unsafe { atomic_and(self.v.get(), val, order) > 0 }
337 /// Logical "nand" with a boolean value.
339 /// Performs a logical "nand" operation on the current value and the argument `val`, and sets
340 /// the new value to the result.
342 /// Returns the previous value.
347 /// use std::sync::atomic::{AtomicBool, Ordering};
349 /// let foo = AtomicBool::new(true);
350 /// assert_eq!(true, foo.fetch_nand(false, Ordering::SeqCst));
351 /// assert_eq!(true, foo.load(Ordering::SeqCst));
353 /// let foo = AtomicBool::new(true);
354 /// assert_eq!(true, foo.fetch_nand(true, Ordering::SeqCst));
355 /// assert_eq!(0, foo.load(Ordering::SeqCst) as usize);
356 /// assert_eq!(false, foo.load(Ordering::SeqCst));
358 /// let foo = AtomicBool::new(false);
359 /// assert_eq!(false, foo.fetch_nand(false, Ordering::SeqCst));
360 /// assert_eq!(true, foo.load(Ordering::SeqCst));
363 #[stable(feature = "rust1", since = "1.0.0")]
364 pub fn fetch_nand(&self, val
: bool
, order
: Ordering
) -> bool
{
365 let val
= if val { UINT_TRUE }
else { 0 }
;
367 unsafe { atomic_nand(self.v.get(), val, order) > 0 }
370 /// Logical "or" with a boolean value.
372 /// Performs a logical "or" operation on the current value and the argument `val`, and sets the
373 /// new value to the result.
375 /// Returns the previous value.
380 /// use std::sync::atomic::{AtomicBool, Ordering};
382 /// let foo = AtomicBool::new(true);
383 /// assert_eq!(true, foo.fetch_or(false, Ordering::SeqCst));
384 /// assert_eq!(true, foo.load(Ordering::SeqCst));
386 /// let foo = AtomicBool::new(true);
387 /// assert_eq!(true, foo.fetch_or(true, Ordering::SeqCst));
388 /// assert_eq!(true, foo.load(Ordering::SeqCst));
390 /// let foo = AtomicBool::new(false);
391 /// assert_eq!(false, foo.fetch_or(false, Ordering::SeqCst));
392 /// assert_eq!(false, foo.load(Ordering::SeqCst));
395 #[stable(feature = "rust1", since = "1.0.0")]
396 pub fn fetch_or(&self, val
: bool
, order
: Ordering
) -> bool
{
397 let val
= if val { UINT_TRUE }
else { 0 }
;
399 unsafe { atomic_or(self.v.get(), val, order) > 0 }
402 /// Logical "xor" with a boolean value.
404 /// Performs a logical "xor" operation on the current value and the argument `val`, and sets
405 /// the new value to the result.
407 /// Returns the previous value.
412 /// use std::sync::atomic::{AtomicBool, Ordering};
414 /// let foo = AtomicBool::new(true);
415 /// assert_eq!(true, foo.fetch_xor(false, Ordering::SeqCst));
416 /// assert_eq!(true, foo.load(Ordering::SeqCst));
418 /// let foo = AtomicBool::new(true);
419 /// assert_eq!(true, foo.fetch_xor(true, Ordering::SeqCst));
420 /// assert_eq!(false, foo.load(Ordering::SeqCst));
422 /// let foo = AtomicBool::new(false);
423 /// assert_eq!(false, foo.fetch_xor(false, Ordering::SeqCst));
424 /// assert_eq!(false, foo.load(Ordering::SeqCst));
427 #[stable(feature = "rust1", since = "1.0.0")]
428 pub fn fetch_xor(&self, val
: bool
, order
: Ordering
) -> bool
{
429 let val
= if val { UINT_TRUE }
else { 0 }
;
431 unsafe { atomic_xor(self.v.get(), val, order) > 0 }
435 #[stable(feature = "rust1", since = "1.0.0")]
437 /// Creates a new `AtomicIsize`.
442 /// use std::sync::atomic::AtomicIsize;
444 /// let atomic_forty_two = AtomicIsize::new(42);
447 #[stable(feature = "rust1", since = "1.0.0")]
448 pub fn new(v
: isize) -> AtomicIsize
{
449 AtomicIsize {v: UnsafeCell::new(v)}
452 /// Loads a value from the isize.
454 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
458 /// Panics if `order` is `Release` or `AcqRel`.
463 /// use std::sync::atomic::{AtomicIsize, Ordering};
465 /// let some_isize = AtomicIsize::new(5);
467 /// let value = some_isize.load(Ordering::Relaxed);
470 #[stable(feature = "rust1", since = "1.0.0")]
471 pub fn load(&self, order
: Ordering
) -> isize {
472 unsafe { atomic_load(self.v.get(), order) }
475 /// Stores a value into the isize.
477 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
482 /// use std::sync::atomic::{AtomicIsize, Ordering};
484 /// let some_isize = AtomicIsize::new(5);
486 /// some_isize.store(10, Ordering::Relaxed);
491 /// Panics if `order` is `Acquire` or `AcqRel`.
493 #[stable(feature = "rust1", since = "1.0.0")]
494 pub fn store(&self, val
: isize, order
: Ordering
) {
495 unsafe { atomic_store(self.v.get(), val, order); }
498 /// Stores a value into the isize, returning the old value.
500 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
505 /// use std::sync::atomic::{AtomicIsize, Ordering};
507 /// let some_isize = AtomicIsize::new(5);
509 /// let value = some_isize.swap(10, Ordering::Relaxed);
512 #[stable(feature = "rust1", since = "1.0.0")]
513 pub fn swap(&self, val
: isize, order
: Ordering
) -> isize {
514 unsafe { atomic_swap(self.v.get(), val, order) }
517 /// Stores a value into the isize if the current value is the same as the expected value.
519 /// The return value is always the previous value. If it is equal to `old`, then the value was
522 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
528 /// use std::sync::atomic::{AtomicIsize, Ordering};
530 /// let some_isize = AtomicIsize::new(5);
532 /// let value = some_isize.compare_and_swap(5, 10, Ordering::Relaxed);
535 #[stable(feature = "rust1", since = "1.0.0")]
536 pub fn compare_and_swap(&self, old
: isize, new
: isize, order
: Ordering
) -> isize {
537 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
540 /// Add an isize to the current value, returning the previous value.
545 /// use std::sync::atomic::{AtomicIsize, Ordering};
547 /// let foo = AtomicIsize::new(0);
548 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
549 /// assert_eq!(10, foo.load(Ordering::SeqCst));
552 #[stable(feature = "rust1", since = "1.0.0")]
553 pub fn fetch_add(&self, val
: isize, order
: Ordering
) -> isize {
554 unsafe { atomic_add(self.v.get(), val, order) }
557 /// Subtract an isize from the current value, returning the previous value.
562 /// use std::sync::atomic::{AtomicIsize, Ordering};
564 /// let foo = AtomicIsize::new(0);
565 /// assert_eq!(0, foo.fetch_sub(10, Ordering::SeqCst));
566 /// assert_eq!(-10, foo.load(Ordering::SeqCst));
569 #[stable(feature = "rust1", since = "1.0.0")]
570 pub fn fetch_sub(&self, val
: isize, order
: Ordering
) -> isize {
571 unsafe { atomic_sub(self.v.get(), val, order) }
574 /// Bitwise and with the current isize, returning the previous value.
579 /// use std::sync::atomic::{AtomicIsize, Ordering};
581 /// let foo = AtomicIsize::new(0b101101);
582 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
583 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
585 #[stable(feature = "rust1", since = "1.0.0")]
586 pub fn fetch_and(&self, val
: isize, order
: Ordering
) -> isize {
587 unsafe { atomic_and(self.v.get(), val, order) }
590 /// Bitwise or with the current isize, returning the previous value.
595 /// use std::sync::atomic::{AtomicIsize, Ordering};
597 /// let foo = AtomicIsize::new(0b101101);
598 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
599 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
601 #[stable(feature = "rust1", since = "1.0.0")]
602 pub fn fetch_or(&self, val
: isize, order
: Ordering
) -> isize {
603 unsafe { atomic_or(self.v.get(), val, order) }
606 /// Bitwise xor with the current isize, returning the previous value.
611 /// use std::sync::atomic::{AtomicIsize, Ordering};
613 /// let foo = AtomicIsize::new(0b101101);
614 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
615 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
617 #[stable(feature = "rust1", since = "1.0.0")]
618 pub fn fetch_xor(&self, val
: isize, order
: Ordering
) -> isize {
619 unsafe { atomic_xor(self.v.get(), val, order) }
623 #[stable(feature = "rust1", since = "1.0.0")]
625 /// Creates a new `AtomicUsize`.
630 /// use std::sync::atomic::AtomicUsize;
632 /// let atomic_forty_two = AtomicUsize::new(42);
635 #[stable(feature = "rust1", since = "1.0.0")]
636 pub fn new(v
: usize) -> AtomicUsize
{
637 AtomicUsize { v: UnsafeCell::new(v) }
640 /// Loads a value from the usize.
642 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
646 /// Panics if `order` is `Release` or `AcqRel`.
651 /// use std::sync::atomic::{AtomicUsize, Ordering};
653 /// let some_usize = AtomicUsize::new(5);
655 /// let value = some_usize.load(Ordering::Relaxed);
658 #[stable(feature = "rust1", since = "1.0.0")]
659 pub fn load(&self, order
: Ordering
) -> usize {
660 unsafe { atomic_load(self.v.get(), order) }
663 /// Stores a value into the usize.
665 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
670 /// use std::sync::atomic::{AtomicUsize, Ordering};
672 /// let some_usize = AtomicUsize::new(5);
674 /// some_usize.store(10, Ordering::Relaxed);
679 /// Panics if `order` is `Acquire` or `AcqRel`.
681 #[stable(feature = "rust1", since = "1.0.0")]
682 pub fn store(&self, val
: usize, order
: Ordering
) {
683 unsafe { atomic_store(self.v.get(), val, order); }
686 /// Stores a value into the usize, returning the old value.
688 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
693 /// use std::sync::atomic::{AtomicUsize, Ordering};
695 /// let some_usize= AtomicUsize::new(5);
697 /// let value = some_usize.swap(10, Ordering::Relaxed);
700 #[stable(feature = "rust1", since = "1.0.0")]
701 pub fn swap(&self, val
: usize, order
: Ordering
) -> usize {
702 unsafe { atomic_swap(self.v.get(), val, order) }
705 /// Stores a value into the usize if the current value is the same as the expected value.
707 /// The return value is always the previous value. If it is equal to `old`, then the value was
710 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
716 /// use std::sync::atomic::{AtomicUsize, Ordering};
718 /// let some_usize = AtomicUsize::new(5);
720 /// let value = some_usize.compare_and_swap(5, 10, Ordering::Relaxed);
723 #[stable(feature = "rust1", since = "1.0.0")]
724 pub fn compare_and_swap(&self, old
: usize, new
: usize, order
: Ordering
) -> usize {
725 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
728 /// Add to the current usize, returning the previous value.
733 /// use std::sync::atomic::{AtomicUsize, Ordering};
735 /// let foo = AtomicUsize::new(0);
736 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
737 /// assert_eq!(10, foo.load(Ordering::SeqCst));
740 #[stable(feature = "rust1", since = "1.0.0")]
741 pub fn fetch_add(&self, val
: usize, order
: Ordering
) -> usize {
742 unsafe { atomic_add(self.v.get(), val, order) }
745 /// Subtract from the current usize, returning the previous value.
750 /// use std::sync::atomic::{AtomicUsize, Ordering};
752 /// let foo = AtomicUsize::new(10);
753 /// assert_eq!(10, foo.fetch_sub(10, Ordering::SeqCst));
754 /// assert_eq!(0, foo.load(Ordering::SeqCst));
757 #[stable(feature = "rust1", since = "1.0.0")]
758 pub fn fetch_sub(&self, val
: usize, order
: Ordering
) -> usize {
759 unsafe { atomic_sub(self.v.get(), val, order) }
762 /// Bitwise and with the current usize, returning the previous value.
767 /// use std::sync::atomic::{AtomicUsize, Ordering};
769 /// let foo = AtomicUsize::new(0b101101);
770 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
771 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
773 #[stable(feature = "rust1", since = "1.0.0")]
774 pub fn fetch_and(&self, val
: usize, order
: Ordering
) -> usize {
775 unsafe { atomic_and(self.v.get(), val, order) }
778 /// Bitwise or with the current usize, returning the previous value.
783 /// use std::sync::atomic::{AtomicUsize, Ordering};
785 /// let foo = AtomicUsize::new(0b101101);
786 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
787 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
789 #[stable(feature = "rust1", since = "1.0.0")]
790 pub fn fetch_or(&self, val
: usize, order
: Ordering
) -> usize {
791 unsafe { atomic_or(self.v.get(), val, order) }
794 /// Bitwise xor with the current usize, returning the previous value.
799 /// use std::sync::atomic::{AtomicUsize, Ordering};
801 /// let foo = AtomicUsize::new(0b101101);
802 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
803 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
805 #[stable(feature = "rust1", since = "1.0.0")]
806 pub fn fetch_xor(&self, val
: usize, order
: Ordering
) -> usize {
807 unsafe { atomic_xor(self.v.get(), val, order) }
811 impl<T
> AtomicPtr
<T
> {
812 /// Creates a new `AtomicPtr`.
817 /// use std::sync::atomic::AtomicPtr;
819 /// let ptr = &mut 5;
820 /// let atomic_ptr = AtomicPtr::new(ptr);
823 #[stable(feature = "rust1", since = "1.0.0")]
824 pub fn new(p
: *mut T
) -> AtomicPtr
<T
> {
825 AtomicPtr
{ p
: UnsafeCell
::new(p
as usize),
826 _marker
: PhantomData
}
829 /// Loads a value from the pointer.
831 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
835 /// Panics if `order` is `Release` or `AcqRel`.
840 /// use std::sync::atomic::{AtomicPtr, Ordering};
842 /// let ptr = &mut 5;
843 /// let some_ptr = AtomicPtr::new(ptr);
845 /// let value = some_ptr.load(Ordering::Relaxed);
848 #[stable(feature = "rust1", since = "1.0.0")]
849 pub fn load(&self, order
: Ordering
) -> *mut T
{
851 atomic_load(self.p
.get(), order
) as *mut T
855 /// Stores a value into the pointer.
857 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
862 /// use std::sync::atomic::{AtomicPtr, Ordering};
864 /// let ptr = &mut 5;
865 /// let some_ptr = AtomicPtr::new(ptr);
867 /// let other_ptr = &mut 10;
869 /// some_ptr.store(other_ptr, Ordering::Relaxed);
874 /// Panics if `order` is `Acquire` or `AcqRel`.
876 #[stable(feature = "rust1", since = "1.0.0")]
877 pub fn store(&self, ptr
: *mut T
, order
: Ordering
) {
878 unsafe { atomic_store(self.p.get(), ptr as usize, order); }
881 /// Stores a value into the pointer, returning the old value.
883 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
888 /// use std::sync::atomic::{AtomicPtr, Ordering};
890 /// let ptr = &mut 5;
891 /// let some_ptr = AtomicPtr::new(ptr);
893 /// let other_ptr = &mut 10;
895 /// let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
898 #[stable(feature = "rust1", since = "1.0.0")]
899 pub fn swap(&self, ptr
: *mut T
, order
: Ordering
) -> *mut T
{
900 unsafe { atomic_swap(self.p.get(), ptr as usize, order) as *mut T }
903 /// Stores a value into the pointer if the current value is the same as the expected value.
905 /// The return value is always the previous value. If it is equal to `old`, then the value was
908 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
914 /// use std::sync::atomic::{AtomicPtr, Ordering};
916 /// let ptr = &mut 5;
917 /// let some_ptr = AtomicPtr::new(ptr);
919 /// let other_ptr = &mut 10;
920 /// let another_ptr = &mut 10;
922 /// let value = some_ptr.compare_and_swap(other_ptr, another_ptr, Ordering::Relaxed);
925 #[stable(feature = "rust1", since = "1.0.0")]
926 pub fn compare_and_swap(&self, old
: *mut T
, new
: *mut T
, order
: Ordering
) -> *mut T
{
928 atomic_compare_and_swap(self.p
.get(), old
as usize,
929 new
as usize, order
) as *mut T
935 unsafe fn atomic_store
<T
>(dst
: *mut T
, val
: T
, order
:Ordering
) {
937 Release
=> intrinsics
::atomic_store_rel(dst
, val
),
938 Relaxed
=> intrinsics
::atomic_store_relaxed(dst
, val
),
939 SeqCst
=> intrinsics
::atomic_store(dst
, val
),
940 Acquire
=> panic
!("there is no such thing as an acquire store"),
941 AcqRel
=> panic
!("there is no such thing as an acquire/release store"),
946 #[stable(feature = "rust1", since = "1.0.0")]
947 unsafe fn atomic_load
<T
>(dst
: *const T
, order
:Ordering
) -> T
{
949 Acquire
=> intrinsics
::atomic_load_acq(dst
),
950 Relaxed
=> intrinsics
::atomic_load_relaxed(dst
),
951 SeqCst
=> intrinsics
::atomic_load(dst
),
952 Release
=> panic
!("there is no such thing as a release load"),
953 AcqRel
=> panic
!("there is no such thing as an acquire/release load"),
958 #[stable(feature = "rust1", since = "1.0.0")]
959 unsafe fn atomic_swap
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
961 Acquire
=> intrinsics
::atomic_xchg_acq(dst
, val
),
962 Release
=> intrinsics
::atomic_xchg_rel(dst
, val
),
963 AcqRel
=> intrinsics
::atomic_xchg_acqrel(dst
, val
),
964 Relaxed
=> intrinsics
::atomic_xchg_relaxed(dst
, val
),
965 SeqCst
=> intrinsics
::atomic_xchg(dst
, val
)
969 /// Returns the old value (like __sync_fetch_and_add).
971 #[stable(feature = "rust1", since = "1.0.0")]
972 unsafe fn atomic_add
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
974 Acquire
=> intrinsics
::atomic_xadd_acq(dst
, val
),
975 Release
=> intrinsics
::atomic_xadd_rel(dst
, val
),
976 AcqRel
=> intrinsics
::atomic_xadd_acqrel(dst
, val
),
977 Relaxed
=> intrinsics
::atomic_xadd_relaxed(dst
, val
),
978 SeqCst
=> intrinsics
::atomic_xadd(dst
, val
)
982 /// Returns the old value (like __sync_fetch_and_sub).
984 #[stable(feature = "rust1", since = "1.0.0")]
985 unsafe fn atomic_sub
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
987 Acquire
=> intrinsics
::atomic_xsub_acq(dst
, val
),
988 Release
=> intrinsics
::atomic_xsub_rel(dst
, val
),
989 AcqRel
=> intrinsics
::atomic_xsub_acqrel(dst
, val
),
990 Relaxed
=> intrinsics
::atomic_xsub_relaxed(dst
, val
),
991 SeqCst
=> intrinsics
::atomic_xsub(dst
, val
)
996 #[stable(feature = "rust1", since = "1.0.0")]
997 unsafe fn atomic_compare_and_swap
<T
>(dst
: *mut T
, old
:T
, new
:T
, order
: Ordering
) -> T
{
999 Acquire
=> intrinsics
::atomic_cxchg_acq(dst
, old
, new
),
1000 Release
=> intrinsics
::atomic_cxchg_rel(dst
, old
, new
),
1001 AcqRel
=> intrinsics
::atomic_cxchg_acqrel(dst
, old
, new
),
1002 Relaxed
=> intrinsics
::atomic_cxchg_relaxed(dst
, old
, new
),
1003 SeqCst
=> intrinsics
::atomic_cxchg(dst
, old
, new
),
1008 #[stable(feature = "rust1", since = "1.0.0")]
1009 unsafe fn atomic_and
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1011 Acquire
=> intrinsics
::atomic_and_acq(dst
, val
),
1012 Release
=> intrinsics
::atomic_and_rel(dst
, val
),
1013 AcqRel
=> intrinsics
::atomic_and_acqrel(dst
, val
),
1014 Relaxed
=> intrinsics
::atomic_and_relaxed(dst
, val
),
1015 SeqCst
=> intrinsics
::atomic_and(dst
, val
)
1020 #[stable(feature = "rust1", since = "1.0.0")]
1021 unsafe fn atomic_nand
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1023 Acquire
=> intrinsics
::atomic_nand_acq(dst
, val
),
1024 Release
=> intrinsics
::atomic_nand_rel(dst
, val
),
1025 AcqRel
=> intrinsics
::atomic_nand_acqrel(dst
, val
),
1026 Relaxed
=> intrinsics
::atomic_nand_relaxed(dst
, val
),
1027 SeqCst
=> intrinsics
::atomic_nand(dst
, val
)
1033 #[stable(feature = "rust1", since = "1.0.0")]
1034 unsafe fn atomic_or
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1036 Acquire
=> intrinsics
::atomic_or_acq(dst
, val
),
1037 Release
=> intrinsics
::atomic_or_rel(dst
, val
),
1038 AcqRel
=> intrinsics
::atomic_or_acqrel(dst
, val
),
1039 Relaxed
=> intrinsics
::atomic_or_relaxed(dst
, val
),
1040 SeqCst
=> intrinsics
::atomic_or(dst
, val
)
1046 #[stable(feature = "rust1", since = "1.0.0")]
1047 unsafe fn atomic_xor
<T
>(dst
: *mut T
, val
: T
, order
: Ordering
) -> T
{
1049 Acquire
=> intrinsics
::atomic_xor_acq(dst
, val
),
1050 Release
=> intrinsics
::atomic_xor_rel(dst
, val
),
1051 AcqRel
=> intrinsics
::atomic_xor_acqrel(dst
, val
),
1052 Relaxed
=> intrinsics
::atomic_xor_relaxed(dst
, val
),
1053 SeqCst
=> intrinsics
::atomic_xor(dst
, val
)
1058 /// An atomic fence.
1060 /// A fence 'A' which has `Release` ordering semantics, synchronizes with a
1061 /// fence 'B' with (at least) `Acquire` semantics, if and only if there exists
1062 /// atomic operations X and Y, both operating on some atomic object 'M' such
1063 /// that A is sequenced before X, Y is synchronized before B and Y observes
1064 /// the change to M. This provides a happens-before dependence between A and B.
1066 /// Atomic operations with `Release` or `Acquire` semantics can also synchronize
1069 /// A fence which has `SeqCst` ordering, in addition to having both `Acquire`
1070 /// and `Release` semantics, participates in the global program order of the
1071 /// other `SeqCst` operations and/or fences.
1073 /// Accepts `Acquire`, `Release`, `AcqRel` and `SeqCst` orderings.
1077 /// Panics if `order` is `Relaxed`.
1079 #[stable(feature = "rust1", since = "1.0.0")]
1080 pub fn fence(order
: Ordering
) {
1083 Acquire
=> intrinsics
::atomic_fence_acq(),
1084 Release
=> intrinsics
::atomic_fence_rel(),
1085 AcqRel
=> intrinsics
::atomic_fence_acqrel(),
1086 SeqCst
=> intrinsics
::atomic_fence(),
1087 Relaxed
=> panic
!("there is no such thing as a relaxed fence")