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`, `AtomicInt`, `AtomicUint`, and `AtomicOption`.
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 [C++11 atomic orderings][1].
26 //! [1]: http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync
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::{AtomicUint, Ordering};
45 //! use std::thread::Thread;
48 //! let spinlock = Arc::new(AtomicUint::new(1));
50 //! let spinlock_clone = spinlock.clone();
51 //! Thread::spawn(move|| {
52 //! spinlock_clone.store(0, Ordering::SeqCst);
55 //! // Wait for the other task to release the lock
56 //! while spinlock.load(Ordering::SeqCst) != 0 {}
60 //! Keep a global count of live tasks:
63 //! use std::sync::atomic::{AtomicUint, Ordering, ATOMIC_UINT_INIT};
65 //! static GLOBAL_TASK_COUNT: AtomicUint = ATOMIC_UINT_INIT;
67 //! let old_task_count = GLOBAL_TASK_COUNT.fetch_add(1, Ordering::SeqCst);
68 //! println!("live tasks: {}", old_task_count + 1);
73 use self::Ordering::*;
80 /// A boolean type which can be safely shared between threads.
82 pub struct AtomicBool {
86 unsafe impl Sync for AtomicBool {}
88 /// A signed integer type which can be safely shared between threads.
90 pub struct AtomicInt {
94 unsafe impl Sync for AtomicInt {}
96 /// An unsigned integer type which can be safely shared between threads.
98 pub struct AtomicUint {
102 unsafe impl Sync for AtomicUint {}
104 /// A raw pointer type which can be safely shared between threads.
106 pub struct AtomicPtr<T> {
110 unsafe impl<T> Sync for AtomicPtr<T> {}
112 /// Atomic memory orderings
114 /// Memory orderings limit the ways that both the compiler and CPU may reorder
115 /// instructions around atomic operations. At its most restrictive,
116 /// "sequentially consistent" atomics allow neither reads nor writes
117 /// to be moved either before or after the atomic operation; on the other end
118 /// "relaxed" atomics allow all reorderings.
120 /// Rust's memory orderings are [the same as
121 /// C++'s](http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync).
125 /// No ordering constraints, only atomic operations.
128 /// When coupled with a store, all previous writes become visible
129 /// to another thread that performs a load with `Acquire` ordering
130 /// on the same value.
133 /// When coupled with a load, all subsequent loads will see data
134 /// written before a store with `Release` ordering on the same value
135 /// in another thread.
138 /// When coupled with a load, uses `Acquire` ordering, and with a store
139 /// `Release` ordering.
142 /// Like `AcqRel` with the additional guarantee that all threads see all
143 /// sequentially consistent operations in the same order.
148 /// An `AtomicBool` initialized to `false`.
149 #[unstable = "may be renamed, pending conventions for static initalizers"]
150 pub const ATOMIC_BOOL_INIT: AtomicBool =
151 AtomicBool { v: UnsafeCell { value: 0 } };
152 /// An `AtomicInt` initialized to `0`.
153 #[unstable = "may be renamed, pending conventions for static initalizers"]
154 pub const ATOMIC_INT_INIT: AtomicInt =
155 AtomicInt { v: UnsafeCell { value: 0 } };
156 /// An `AtomicUint` initialized to `0`.
157 #[unstable = "may be renamed, pending conventions for static initalizers"]
158 pub const ATOMIC_UINT_INIT: AtomicUint =
159 AtomicUint { v: UnsafeCell { value: 0, } };
162 #[deprecated = "renamed to ATOMIC_BOOL_INIT"]
163 pub const INIT_ATOMIC_BOOL: AtomicBool = ATOMIC_BOOL_INIT;
165 #[deprecated = "renamed to ATOMIC_INT_INIT"]
166 pub const INIT_ATOMIC_INT: AtomicInt = ATOMIC_INT_INIT;
168 #[deprecated = "renamed to ATOMIC_UINT_INIT"]
169 pub const INIT_ATOMIC_UINT: AtomicUint = ATOMIC_UINT_INIT;
171 // NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
172 const UINT_TRUE: uint = -1;
175 /// Creates a new `AtomicBool`.
180 /// use std::sync::atomic::AtomicBool;
182 /// let atomic_true = AtomicBool::new(true);
183 /// let atomic_false = AtomicBool::new(false);
187 pub fn new(v: bool) -> AtomicBool {
188 let val = if v { UINT_TRUE } else { 0 };
189 AtomicBool { v: UnsafeCell::new(val) }
192 /// Loads a value from the bool.
194 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
198 /// Panics if `order` is `Release` or `AcqRel`.
203 /// use std::sync::atomic::{AtomicBool, Ordering};
205 /// let some_bool = AtomicBool::new(true);
207 /// let value = some_bool.load(Ordering::Relaxed);
211 pub fn load(&self, order: Ordering) -> bool {
212 unsafe { atomic_load(self.v.get() as *const uint, order) > 0 }
215 /// Stores a value into the bool.
217 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
222 /// use std::sync::atomic::{AtomicBool, Ordering};
224 /// let some_bool = AtomicBool::new(true);
226 /// some_bool.store(false, Ordering::Relaxed);
231 /// Panics if `order` is `Acquire` or `AcqRel`.
234 pub fn store(&self, val: bool, order: Ordering) {
235 let val = if val { UINT_TRUE } else { 0 };
237 unsafe { atomic_store(self.v.get(), val, order); }
240 /// Stores a value into the bool, returning the old value.
242 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
247 /// use std::sync::atomic::{AtomicBool, Ordering};
249 /// let some_bool = AtomicBool::new(true);
251 /// let value = some_bool.swap(false, Ordering::Relaxed);
255 pub fn swap(&self, val: bool, order: Ordering) -> bool {
256 let val = if val { UINT_TRUE } else { 0 };
258 unsafe { atomic_swap(self.v.get(), val, order) > 0 }
261 /// Stores a value into the bool if the current value is the same as the expected value.
263 /// If the return value is equal to `old` then the value was updated.
265 /// `swap` also takes an `Ordering` argument which describes the memory ordering of this
271 /// use std::sync::atomic::{AtomicBool, Ordering};
273 /// let some_bool = AtomicBool::new(true);
275 /// let value = some_bool.store(false, Ordering::Relaxed);
279 pub fn compare_and_swap(&self, old: bool, new: bool, order: Ordering) -> bool {
280 let old = if old { UINT_TRUE } else { 0 };
281 let new = if new { UINT_TRUE } else { 0 };
283 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) > 0 }
286 /// Logical "and" with a boolean value.
288 /// Performs a logical "and" operation on the current value and the argument `val`, and sets
289 /// the new value to the result.
291 /// Returns the previous value.
296 /// use std::sync::atomic::{AtomicBool, Ordering};
298 /// let foo = AtomicBool::new(true);
299 /// assert_eq!(true, foo.fetch_and(false, Ordering::SeqCst));
300 /// assert_eq!(false, foo.load(Ordering::SeqCst));
302 /// let foo = AtomicBool::new(true);
303 /// assert_eq!(true, foo.fetch_and(true, Ordering::SeqCst));
304 /// assert_eq!(true, foo.load(Ordering::SeqCst));
306 /// let foo = AtomicBool::new(false);
307 /// assert_eq!(false, foo.fetch_and(false, Ordering::SeqCst));
308 /// assert_eq!(false, foo.load(Ordering::SeqCst));
312 pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
313 let val = if val { UINT_TRUE } else { 0 };
315 unsafe { atomic_and(self.v.get(), val, order) > 0 }
318 /// Logical "nand" with a boolean value.
320 /// Performs a logical "nand" operation on the current value and the argument `val`, and sets
321 /// the new value to the result.
323 /// Returns the previous value.
328 /// use std::sync::atomic::{AtomicBool, Ordering};
330 /// let foo = AtomicBool::new(true);
331 /// assert_eq!(true, foo.fetch_nand(false, Ordering::SeqCst));
332 /// assert_eq!(true, foo.load(Ordering::SeqCst));
334 /// let foo = AtomicBool::new(true);
335 /// assert_eq!(true, foo.fetch_nand(true, Ordering::SeqCst));
336 /// assert_eq!(0, foo.load(Ordering::SeqCst) as int);
337 /// assert_eq!(false, foo.load(Ordering::SeqCst));
339 /// let foo = AtomicBool::new(false);
340 /// assert_eq!(false, foo.fetch_nand(false, Ordering::SeqCst));
341 /// assert_eq!(true, foo.load(Ordering::SeqCst));
345 pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
346 let val = if val { UINT_TRUE } else { 0 };
348 unsafe { atomic_nand(self.v.get(), val, order) > 0 }
351 /// Logical "or" with a boolean value.
353 /// Performs a logical "or" operation on the current value and the argument `val`, and sets the
354 /// new value to the result.
356 /// Returns the previous value.
361 /// use std::sync::atomic::{AtomicBool, Ordering};
363 /// let foo = AtomicBool::new(true);
364 /// assert_eq!(true, foo.fetch_or(false, Ordering::SeqCst));
365 /// assert_eq!(true, foo.load(Ordering::SeqCst));
367 /// let foo = AtomicBool::new(true);
368 /// assert_eq!(true, foo.fetch_or(true, Ordering::SeqCst));
369 /// assert_eq!(true, foo.load(Ordering::SeqCst));
371 /// let foo = AtomicBool::new(false);
372 /// assert_eq!(false, foo.fetch_or(false, Ordering::SeqCst));
373 /// assert_eq!(false, foo.load(Ordering::SeqCst));
377 pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
378 let val = if val { UINT_TRUE } else { 0 };
380 unsafe { atomic_or(self.v.get(), val, order) > 0 }
383 /// Logical "xor" with a boolean value.
385 /// Performs a logical "xor" operation on the current value and the argument `val`, and sets
386 /// the new value to the result.
388 /// Returns the previous value.
393 /// use std::sync::atomic::{AtomicBool, Ordering};
395 /// let foo = AtomicBool::new(true);
396 /// assert_eq!(true, foo.fetch_xor(false, Ordering::SeqCst));
397 /// assert_eq!(true, foo.load(Ordering::SeqCst));
399 /// let foo = AtomicBool::new(true);
400 /// assert_eq!(true, foo.fetch_xor(true, Ordering::SeqCst));
401 /// assert_eq!(false, foo.load(Ordering::SeqCst));
403 /// let foo = AtomicBool::new(false);
404 /// assert_eq!(false, foo.fetch_xor(false, Ordering::SeqCst));
405 /// assert_eq!(false, foo.load(Ordering::SeqCst));
409 pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
410 let val = if val { UINT_TRUE } else { 0 };
412 unsafe { atomic_xor(self.v.get(), val, order) > 0 }
417 /// Creates a new `AtomicInt`.
422 /// use std::sync::atomic::AtomicInt;
424 /// let atomic_forty_two = AtomicInt::new(42);
428 pub fn new(v: int) -> AtomicInt {
429 AtomicInt {v: UnsafeCell::new(v)}
432 /// Loads a value from the int.
434 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
438 /// Panics if `order` is `Release` or `AcqRel`.
443 /// use std::sync::atomic::{AtomicInt, Ordering};
445 /// let some_int = AtomicInt::new(5);
447 /// let value = some_int.load(Ordering::Relaxed);
451 pub fn load(&self, order: Ordering) -> int {
452 unsafe { atomic_load(self.v.get() as *const int, order) }
455 /// Stores a value into the int.
457 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
462 /// use std::sync::atomic::{AtomicInt, Ordering};
464 /// let some_int = AtomicInt::new(5);
466 /// some_int.store(10, Ordering::Relaxed);
471 /// Panics if `order` is `Acquire` or `AcqRel`.
474 pub fn store(&self, val: int, order: Ordering) {
475 unsafe { atomic_store(self.v.get(), val, order); }
478 /// Stores a value into the int, returning the old value.
480 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
485 /// use std::sync::atomic::{AtomicInt, Ordering};
487 /// let some_int = AtomicInt::new(5);
489 /// let value = some_int.swap(10, Ordering::Relaxed);
493 pub fn swap(&self, val: int, order: Ordering) -> int {
494 unsafe { atomic_swap(self.v.get(), val, order) }
497 /// Stores a value into the int if the current value is the same as the expected value.
499 /// If the return value is equal to `old` then the value was updated.
501 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
507 /// use std::sync::atomic::{AtomicInt, Ordering};
509 /// let some_int = AtomicInt::new(5);
511 /// let value = some_int.compare_and_swap(5, 10, Ordering::Relaxed);
515 pub fn compare_and_swap(&self, old: int, new: int, order: Ordering) -> int {
516 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
519 /// Add an int to the current value, returning the previous value.
524 /// use std::sync::atomic::{AtomicInt, Ordering};
526 /// let foo = AtomicInt::new(0);
527 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
528 /// assert_eq!(10, foo.load(Ordering::SeqCst));
532 pub fn fetch_add(&self, val: int, order: Ordering) -> int {
533 unsafe { atomic_add(self.v.get(), val, order) }
536 /// Subtract an int from the current value, returning the previous value.
541 /// use std::sync::atomic::{AtomicInt, Ordering};
543 /// let foo = AtomicInt::new(0);
544 /// assert_eq!(0, foo.fetch_sub(10, Ordering::SeqCst));
545 /// assert_eq!(-10, foo.load(Ordering::SeqCst));
549 pub fn fetch_sub(&self, val: int, order: Ordering) -> int {
550 unsafe { atomic_sub(self.v.get(), val, order) }
553 /// Bitwise and with the current int, returning the previous value.
558 /// use std::sync::atomic::{AtomicInt, Ordering};
560 /// let foo = AtomicInt::new(0b101101);
561 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
562 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
565 pub fn fetch_and(&self, val: int, order: Ordering) -> int {
566 unsafe { atomic_and(self.v.get(), val, order) }
569 /// Bitwise or with the current int, returning the previous value.
574 /// use std::sync::atomic::{AtomicInt, Ordering};
576 /// let foo = AtomicInt::new(0b101101);
577 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
578 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
581 pub fn fetch_or(&self, val: int, order: Ordering) -> int {
582 unsafe { atomic_or(self.v.get(), val, order) }
585 /// Bitwise xor with the current int, returning the previous value.
590 /// use std::sync::atomic::{AtomicInt, Ordering};
592 /// let foo = AtomicInt::new(0b101101);
593 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
594 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
597 pub fn fetch_xor(&self, val: int, order: Ordering) -> int {
598 unsafe { atomic_xor(self.v.get(), val, order) }
603 /// Creates a new `AtomicUint`.
608 /// use std::sync::atomic::AtomicUint;
610 /// let atomic_forty_two = AtomicUint::new(42u);
614 pub fn new(v: uint) -> AtomicUint {
615 AtomicUint { v: UnsafeCell::new(v) }
618 /// Loads a value from the uint.
620 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
624 /// Panics if `order` is `Release` or `AcqRel`.
629 /// use std::sync::atomic::{AtomicUint, Ordering};
631 /// let some_uint = AtomicUint::new(5);
633 /// let value = some_uint.load(Ordering::Relaxed);
637 pub fn load(&self, order: Ordering) -> uint {
638 unsafe { atomic_load(self.v.get() as *const uint, order) }
641 /// Stores a value into the uint.
643 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
648 /// use std::sync::atomic::{AtomicUint, Ordering};
650 /// let some_uint = AtomicUint::new(5);
652 /// some_uint.store(10, Ordering::Relaxed);
657 /// Panics if `order` is `Acquire` or `AcqRel`.
660 pub fn store(&self, val: uint, order: Ordering) {
661 unsafe { atomic_store(self.v.get(), val, order); }
664 /// Stores a value into the uint, returning the old value.
666 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
671 /// use std::sync::atomic::{AtomicUint, Ordering};
673 /// let some_uint = AtomicUint::new(5);
675 /// let value = some_uint.swap(10, Ordering::Relaxed);
679 pub fn swap(&self, val: uint, order: Ordering) -> uint {
680 unsafe { atomic_swap(self.v.get(), val, order) }
683 /// Stores a value into the uint if the current value is the same as the expected value.
685 /// If the return value is equal to `old` then the value was updated.
687 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
693 /// use std::sync::atomic::{AtomicUint, Ordering};
695 /// let some_uint = AtomicUint::new(5);
697 /// let value = some_uint.compare_and_swap(5, 10, Ordering::Relaxed);
701 pub fn compare_and_swap(&self, old: uint, new: uint, order: Ordering) -> uint {
702 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
705 /// Add to the current uint, returning the previous value.
710 /// use std::sync::atomic::{AtomicUint, Ordering};
712 /// let foo = AtomicUint::new(0);
713 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
714 /// assert_eq!(10, foo.load(Ordering::SeqCst));
718 pub fn fetch_add(&self, val: uint, order: Ordering) -> uint {
719 unsafe { atomic_add(self.v.get(), val, order) }
722 /// Subtract from the current uint, returning the previous value.
727 /// use std::sync::atomic::{AtomicUint, Ordering};
729 /// let foo = AtomicUint::new(10);
730 /// assert_eq!(10, foo.fetch_sub(10, Ordering::SeqCst));
731 /// assert_eq!(0, foo.load(Ordering::SeqCst));
735 pub fn fetch_sub(&self, val: uint, order: Ordering) -> uint {
736 unsafe { atomic_sub(self.v.get(), val, order) }
739 /// Bitwise and with the current uint, returning the previous value.
744 /// use std::sync::atomic::{AtomicUint, Ordering};
746 /// let foo = AtomicUint::new(0b101101);
747 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
748 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
751 pub fn fetch_and(&self, val: uint, order: Ordering) -> uint {
752 unsafe { atomic_and(self.v.get(), val, order) }
755 /// Bitwise or with the current uint, returning the previous value.
760 /// use std::sync::atomic::{AtomicUint, Ordering};
762 /// let foo = AtomicUint::new(0b101101);
763 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
764 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
767 pub fn fetch_or(&self, val: uint, order: Ordering) -> uint {
768 unsafe { atomic_or(self.v.get(), val, order) }
771 /// Bitwise xor with the current uint, returning the previous value.
776 /// use std::sync::atomic::{AtomicUint, Ordering};
778 /// let foo = AtomicUint::new(0b101101);
779 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
780 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
783 pub fn fetch_xor(&self, val: uint, order: Ordering) -> uint {
784 unsafe { atomic_xor(self.v.get(), val, order) }
788 impl<T> AtomicPtr<T> {
789 /// Creates a new `AtomicPtr`.
794 /// use std::sync::atomic::AtomicPtr;
796 /// let ptr = &mut 5i;
797 /// let atomic_ptr = AtomicPtr::new(ptr);
801 pub fn new(p: *mut T) -> AtomicPtr<T> {
802 AtomicPtr { p: UnsafeCell::new(p as uint) }
805 /// Loads a value from the pointer.
807 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
811 /// Panics if `order` is `Release` or `AcqRel`.
816 /// use std::sync::atomic::{AtomicPtr, Ordering};
818 /// let ptr = &mut 5i;
819 /// let some_ptr = AtomicPtr::new(ptr);
821 /// let value = some_ptr.load(Ordering::Relaxed);
825 pub fn load(&self, order: Ordering) -> *mut T {
827 atomic_load(self.p.get() as *const *mut T, order) as *mut T
831 /// Stores a value into the pointer.
833 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
838 /// use std::sync::atomic::{AtomicPtr, Ordering};
840 /// let ptr = &mut 5i;
841 /// let some_ptr = AtomicPtr::new(ptr);
843 /// let other_ptr = &mut 10i;
845 /// some_ptr.store(other_ptr, Ordering::Relaxed);
850 /// Panics if `order` is `Acquire` or `AcqRel`.
853 pub fn store(&self, ptr: *mut T, order: Ordering) {
854 unsafe { atomic_store(self.p.get(), ptr as uint, order); }
857 /// Stores a value into the pointer, returning the old value.
859 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
864 /// use std::sync::atomic::{AtomicPtr, Ordering};
866 /// let ptr = &mut 5i;
867 /// let some_ptr = AtomicPtr::new(ptr);
869 /// let other_ptr = &mut 10i;
871 /// let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
875 pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
876 unsafe { atomic_swap(self.p.get(), ptr as uint, order) as *mut T }
879 /// Stores a value into the pointer if the current value is the same as the expected value.
881 /// If the return value is equal to `old` then the value was updated.
883 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
889 /// use std::sync::atomic::{AtomicPtr, Ordering};
891 /// let ptr = &mut 5i;
892 /// let some_ptr = AtomicPtr::new(ptr);
894 /// let other_ptr = &mut 10i;
895 /// let another_ptr = &mut 10i;
897 /// let value = some_ptr.compare_and_swap(other_ptr, another_ptr, Ordering::Relaxed);
901 pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T {
903 atomic_compare_and_swap(self.p.get(), old as uint,
904 new as uint, order) as *mut T
910 unsafe fn atomic_store<T>(dst: *mut T, val: T, order:Ordering) {
912 Release => intrinsics::atomic_store_rel(dst, val),
913 Relaxed => intrinsics::atomic_store_relaxed(dst, val),
914 SeqCst => intrinsics::atomic_store(dst, val),
915 Acquire => panic!("there is no such thing as an acquire store"),
916 AcqRel => panic!("there is no such thing as an acquire/release store"),
922 unsafe fn atomic_load<T>(dst: *const T, order:Ordering) -> T {
924 Acquire => intrinsics::atomic_load_acq(dst),
925 Relaxed => intrinsics::atomic_load_relaxed(dst),
926 SeqCst => intrinsics::atomic_load(dst),
927 Release => panic!("there is no such thing as a release load"),
928 AcqRel => panic!("there is no such thing as an acquire/release load"),
934 unsafe fn atomic_swap<T>(dst: *mut T, val: T, order: Ordering) -> T {
936 Acquire => intrinsics::atomic_xchg_acq(dst, val),
937 Release => intrinsics::atomic_xchg_rel(dst, val),
938 AcqRel => intrinsics::atomic_xchg_acqrel(dst, val),
939 Relaxed => intrinsics::atomic_xchg_relaxed(dst, val),
940 SeqCst => intrinsics::atomic_xchg(dst, val)
944 /// Returns the old value (like __sync_fetch_and_add).
947 unsafe fn atomic_add<T>(dst: *mut T, val: T, order: Ordering) -> T {
949 Acquire => intrinsics::atomic_xadd_acq(dst, val),
950 Release => intrinsics::atomic_xadd_rel(dst, val),
951 AcqRel => intrinsics::atomic_xadd_acqrel(dst, val),
952 Relaxed => intrinsics::atomic_xadd_relaxed(dst, val),
953 SeqCst => intrinsics::atomic_xadd(dst, val)
957 /// Returns the old value (like __sync_fetch_and_sub).
960 unsafe fn atomic_sub<T>(dst: *mut T, val: T, order: Ordering) -> T {
962 Acquire => intrinsics::atomic_xsub_acq(dst, val),
963 Release => intrinsics::atomic_xsub_rel(dst, val),
964 AcqRel => intrinsics::atomic_xsub_acqrel(dst, val),
965 Relaxed => intrinsics::atomic_xsub_relaxed(dst, val),
966 SeqCst => intrinsics::atomic_xsub(dst, val)
972 unsafe fn atomic_compare_and_swap<T>(dst: *mut T, old:T, new:T, order: Ordering) -> T {
974 Acquire => intrinsics::atomic_cxchg_acq(dst, old, new),
975 Release => intrinsics::atomic_cxchg_rel(dst, old, new),
976 AcqRel => intrinsics::atomic_cxchg_acqrel(dst, old, new),
977 Relaxed => intrinsics::atomic_cxchg_relaxed(dst, old, new),
978 SeqCst => intrinsics::atomic_cxchg(dst, old, new),
984 unsafe fn atomic_and<T>(dst: *mut T, val: T, order: Ordering) -> T {
986 Acquire => intrinsics::atomic_and_acq(dst, val),
987 Release => intrinsics::atomic_and_rel(dst, val),
988 AcqRel => intrinsics::atomic_and_acqrel(dst, val),
989 Relaxed => intrinsics::atomic_and_relaxed(dst, val),
990 SeqCst => intrinsics::atomic_and(dst, val)
996 unsafe fn atomic_nand<T>(dst: *mut T, val: T, order: Ordering) -> T {
998 Acquire => intrinsics::atomic_nand_acq(dst, val),
999 Release => intrinsics::atomic_nand_rel(dst, val),
1000 AcqRel => intrinsics::atomic_nand_acqrel(dst, val),
1001 Relaxed => intrinsics::atomic_nand_relaxed(dst, val),
1002 SeqCst => intrinsics::atomic_nand(dst, val)
1009 unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
1011 Acquire => intrinsics::atomic_or_acq(dst, val),
1012 Release => intrinsics::atomic_or_rel(dst, val),
1013 AcqRel => intrinsics::atomic_or_acqrel(dst, val),
1014 Relaxed => intrinsics::atomic_or_relaxed(dst, val),
1015 SeqCst => intrinsics::atomic_or(dst, val)
1022 unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
1024 Acquire => intrinsics::atomic_xor_acq(dst, val),
1025 Release => intrinsics::atomic_xor_rel(dst, val),
1026 AcqRel => intrinsics::atomic_xor_acqrel(dst, val),
1027 Relaxed => intrinsics::atomic_xor_relaxed(dst, val),
1028 SeqCst => intrinsics::atomic_xor(dst, val)
1033 /// An atomic fence.
1035 /// A fence 'A' which has `Release` ordering semantics, synchronizes with a
1036 /// fence 'B' with (at least) `Acquire` semantics, if and only if there exists
1037 /// atomic operations X and Y, both operating on some atomic object 'M' such
1038 /// that A is sequenced before X, Y is synchronized before B and Y observes
1039 /// the change to M. This provides a happens-before dependence between A and B.
1041 /// Atomic operations with `Release` or `Acquire` semantics can also synchronize
1044 /// A fence which has `SeqCst` ordering, in addition to having both `Acquire`
1045 /// and `Release` semantics, participates in the global program order of the
1046 /// other `SeqCst` operations and/or fences.
1048 /// Accepts `Acquire`, `Release`, `AcqRel` and `SeqCst` orderings.
1052 /// Panics if `order` is `Relaxed`.
1055 pub fn fence(order: Ordering) {
1058 Acquire => intrinsics::atomic_fence_acq(),
1059 Release => intrinsics::atomic_fence_rel(),
1060 AcqRel => intrinsics::atomic_fence_acqrel(),
1061 SeqCst => intrinsics::atomic_fence(),
1062 Relaxed => panic!("there is no such thing as a relaxed fence")