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`, `AtomicUsize`, 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 [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 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::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
65 //! static GLOBAL_TASK_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
67 //! let old_task_count = GLOBAL_TASK_COUNT.fetch_add(1, Ordering::SeqCst);
68 //! println!("live tasks: {}", old_task_count + 1);
71 #![stable(feature = "rust1", since = "1.0.0")]
73 use self::Ordering::*;
80 /// A boolean type which can be safely shared between threads.
81 #[stable(feature = "rust1", since = "1.0.0")]
82 pub struct AtomicBool {
86 unsafe impl Sync for AtomicBool {}
88 /// A signed integer type which can be safely shared between threads.
89 #[stable(feature = "rust1", since = "1.0.0")]
90 pub struct AtomicIsize {
94 unsafe impl Sync for AtomicIsize {}
96 /// An unsigned integer type which can be safely shared between threads.
97 #[stable(feature = "rust1", since = "1.0.0")]
98 pub struct AtomicUsize {
102 unsafe impl Sync for AtomicUsize {}
104 /// A raw pointer type which can be safely shared between threads.
105 #[stable(feature = "rust1", since = "1.0.0")]
106 pub struct AtomicPtr<T> {
107 p: UnsafeCell<usize>,
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).
122 #[stable(feature = "rust1", since = "1.0.0")]
125 /// No ordering constraints, only atomic operations.
126 #[stable(feature = "rust1", since = "1.0.0")]
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.
131 #[stable(feature = "rust1", since = "1.0.0")]
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.
136 #[stable(feature = "rust1", since = "1.0.0")]
138 /// When coupled with a load, uses `Acquire` ordering, and with a store
139 /// `Release` ordering.
140 #[stable(feature = "rust1", since = "1.0.0")]
142 /// Like `AcqRel` with the additional guarantee that all threads see all
143 /// sequentially consistent operations in the same order.
144 #[stable(feature = "rust1", since = "1.0.0")]
148 /// An `AtomicBool` initialized to `false`.
149 #[stable(feature = "rust1", since = "1.0.0")]
150 pub const ATOMIC_BOOL_INIT: AtomicBool =
151 AtomicBool { v: UnsafeCell { value: 0 } };
152 /// An `AtomicIsize` initialized to `0`.
153 #[stable(feature = "rust1", since = "1.0.0")]
154 pub const ATOMIC_ISIZE_INIT: AtomicIsize =
155 AtomicIsize { v: UnsafeCell { value: 0 } };
156 /// An `AtomicUsize` initialized to `0`.
157 #[stable(feature = "rust1", since = "1.0.0")]
158 pub const ATOMIC_USIZE_INIT: AtomicUsize =
159 AtomicUsize { v: UnsafeCell { value: 0, } };
161 // NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
162 const UINT_TRUE: usize = -1;
165 /// Creates a new `AtomicBool`.
170 /// use std::sync::atomic::AtomicBool;
172 /// let atomic_true = AtomicBool::new(true);
173 /// let atomic_false = AtomicBool::new(false);
176 #[stable(feature = "rust1", since = "1.0.0")]
177 pub fn new(v: bool) -> AtomicBool {
178 let val = if v { UINT_TRUE } else { 0 };
179 AtomicBool { v: UnsafeCell::new(val) }
182 /// Loads a value from the bool.
184 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
188 /// Panics if `order` is `Release` or `AcqRel`.
193 /// use std::sync::atomic::{AtomicBool, Ordering};
195 /// let some_bool = AtomicBool::new(true);
197 /// let value = some_bool.load(Ordering::Relaxed);
200 #[stable(feature = "rust1", since = "1.0.0")]
201 pub fn load(&self, order: Ordering) -> bool {
202 unsafe { atomic_load(self.v.get(), order) > 0 }
205 /// Stores a value into the bool.
207 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
212 /// use std::sync::atomic::{AtomicBool, Ordering};
214 /// let some_bool = AtomicBool::new(true);
216 /// some_bool.store(false, Ordering::Relaxed);
221 /// Panics if `order` is `Acquire` or `AcqRel`.
223 #[stable(feature = "rust1", since = "1.0.0")]
224 pub fn store(&self, val: bool, order: Ordering) {
225 let val = if val { UINT_TRUE } else { 0 };
227 unsafe { atomic_store(self.v.get(), val, order); }
230 /// Stores a value into the bool, returning the old value.
232 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
237 /// use std::sync::atomic::{AtomicBool, Ordering};
239 /// let some_bool = AtomicBool::new(true);
241 /// let value = some_bool.swap(false, Ordering::Relaxed);
244 #[stable(feature = "rust1", since = "1.0.0")]
245 pub fn swap(&self, val: bool, order: Ordering) -> bool {
246 let val = if val { UINT_TRUE } else { 0 };
248 unsafe { atomic_swap(self.v.get(), val, order) > 0 }
251 /// Stores a value into the bool if the current value is the same as the expected value.
253 /// If the return value is equal to `old` then the value was updated.
255 /// `swap` also takes an `Ordering` argument which describes the memory ordering of this
261 /// use std::sync::atomic::{AtomicBool, Ordering};
263 /// let some_bool = AtomicBool::new(true);
265 /// let value = some_bool.store(false, Ordering::Relaxed);
268 #[stable(feature = "rust1", since = "1.0.0")]
269 pub fn compare_and_swap(&self, old: bool, new: bool, order: Ordering) -> bool {
270 let old = if old { UINT_TRUE } else { 0 };
271 let new = if new { UINT_TRUE } else { 0 };
273 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) > 0 }
276 /// Logical "and" with a boolean value.
278 /// Performs a logical "and" operation on the current value and the argument `val`, and sets
279 /// the new value to the result.
281 /// Returns the previous value.
286 /// use std::sync::atomic::{AtomicBool, Ordering};
288 /// let foo = AtomicBool::new(true);
289 /// assert_eq!(true, foo.fetch_and(false, Ordering::SeqCst));
290 /// assert_eq!(false, foo.load(Ordering::SeqCst));
292 /// let foo = AtomicBool::new(true);
293 /// assert_eq!(true, foo.fetch_and(true, Ordering::SeqCst));
294 /// assert_eq!(true, foo.load(Ordering::SeqCst));
296 /// let foo = AtomicBool::new(false);
297 /// assert_eq!(false, foo.fetch_and(false, Ordering::SeqCst));
298 /// assert_eq!(false, foo.load(Ordering::SeqCst));
301 #[stable(feature = "rust1", since = "1.0.0")]
302 pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
303 let val = if val { UINT_TRUE } else { 0 };
305 unsafe { atomic_and(self.v.get(), val, order) > 0 }
308 /// Logical "nand" with a boolean value.
310 /// Performs a logical "nand" operation on the current value and the argument `val`, and sets
311 /// the new value to the result.
313 /// Returns the previous value.
318 /// use std::sync::atomic::{AtomicBool, Ordering};
320 /// let foo = AtomicBool::new(true);
321 /// assert_eq!(true, foo.fetch_nand(false, Ordering::SeqCst));
322 /// assert_eq!(true, foo.load(Ordering::SeqCst));
324 /// let foo = AtomicBool::new(true);
325 /// assert_eq!(true, foo.fetch_nand(true, Ordering::SeqCst));
326 /// assert_eq!(0, foo.load(Ordering::SeqCst) as usize);
327 /// assert_eq!(false, foo.load(Ordering::SeqCst));
329 /// let foo = AtomicBool::new(false);
330 /// assert_eq!(false, foo.fetch_nand(false, Ordering::SeqCst));
331 /// assert_eq!(true, foo.load(Ordering::SeqCst));
334 #[stable(feature = "rust1", since = "1.0.0")]
335 pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
336 let val = if val { UINT_TRUE } else { 0 };
338 unsafe { atomic_nand(self.v.get(), val, order) > 0 }
341 /// Logical "or" with a boolean value.
343 /// Performs a logical "or" operation on the current value and the argument `val`, and sets the
344 /// new value to the result.
346 /// Returns the previous value.
351 /// use std::sync::atomic::{AtomicBool, Ordering};
353 /// let foo = AtomicBool::new(true);
354 /// assert_eq!(true, foo.fetch_or(false, Ordering::SeqCst));
355 /// assert_eq!(true, foo.load(Ordering::SeqCst));
357 /// let foo = AtomicBool::new(true);
358 /// assert_eq!(true, foo.fetch_or(true, Ordering::SeqCst));
359 /// assert_eq!(true, foo.load(Ordering::SeqCst));
361 /// let foo = AtomicBool::new(false);
362 /// assert_eq!(false, foo.fetch_or(false, Ordering::SeqCst));
363 /// assert_eq!(false, foo.load(Ordering::SeqCst));
366 #[stable(feature = "rust1", since = "1.0.0")]
367 pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
368 let val = if val { UINT_TRUE } else { 0 };
370 unsafe { atomic_or(self.v.get(), val, order) > 0 }
373 /// Logical "xor" with a boolean value.
375 /// Performs a logical "xor" operation on the current value and the argument `val`, and sets
376 /// the new value to the result.
378 /// Returns the previous value.
383 /// use std::sync::atomic::{AtomicBool, Ordering};
385 /// let foo = AtomicBool::new(true);
386 /// assert_eq!(true, foo.fetch_xor(false, Ordering::SeqCst));
387 /// assert_eq!(true, foo.load(Ordering::SeqCst));
389 /// let foo = AtomicBool::new(true);
390 /// assert_eq!(true, foo.fetch_xor(true, Ordering::SeqCst));
391 /// assert_eq!(false, foo.load(Ordering::SeqCst));
393 /// let foo = AtomicBool::new(false);
394 /// assert_eq!(false, foo.fetch_xor(false, Ordering::SeqCst));
395 /// assert_eq!(false, foo.load(Ordering::SeqCst));
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
400 let val = if val { UINT_TRUE } else { 0 };
402 unsafe { atomic_xor(self.v.get(), val, order) > 0 }
406 #[stable(feature = "rust1", since = "1.0.0")]
408 /// Creates a new `AtomicIsize`.
413 /// use std::sync::atomic::AtomicIsize;
415 /// let atomic_forty_two = AtomicIsize::new(42);
418 #[stable(feature = "rust1", since = "1.0.0")]
419 pub fn new(v: isize) -> AtomicIsize {
420 AtomicIsize {v: UnsafeCell::new(v)}
423 /// Loads a value from the isize.
425 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
429 /// Panics if `order` is `Release` or `AcqRel`.
434 /// use std::sync::atomic::{AtomicIsize, Ordering};
436 /// let some_isize = AtomicIsize::new(5);
438 /// let value = some_isize.load(Ordering::Relaxed);
441 #[stable(feature = "rust1", since = "1.0.0")]
442 pub fn load(&self, order: Ordering) -> isize {
443 unsafe { atomic_load(self.v.get(), order) }
446 /// Stores a value into the isize.
448 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
453 /// use std::sync::atomic::{AtomicIsize, Ordering};
455 /// let some_isize = AtomicIsize::new(5);
457 /// some_isize.store(10, Ordering::Relaxed);
462 /// Panics if `order` is `Acquire` or `AcqRel`.
464 #[stable(feature = "rust1", since = "1.0.0")]
465 pub fn store(&self, val: isize, order: Ordering) {
466 unsafe { atomic_store(self.v.get(), val, order); }
469 /// Stores a value into the isize, returning the old value.
471 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
476 /// use std::sync::atomic::{AtomicIsize, Ordering};
478 /// let some_isize = AtomicIsize::new(5);
480 /// let value = some_isize.swap(10, Ordering::Relaxed);
483 #[stable(feature = "rust1", since = "1.0.0")]
484 pub fn swap(&self, val: isize, order: Ordering) -> isize {
485 unsafe { atomic_swap(self.v.get(), val, order) }
488 /// Stores a value into the isize if the current value is the same as the expected value.
490 /// If the return value is equal to `old` then the value was updated.
492 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
498 /// use std::sync::atomic::{AtomicIsize, Ordering};
500 /// let some_isize = AtomicIsize::new(5);
502 /// let value = some_isize.compare_and_swap(5, 10, Ordering::Relaxed);
505 #[stable(feature = "rust1", since = "1.0.0")]
506 pub fn compare_and_swap(&self, old: isize, new: isize, order: Ordering) -> isize {
507 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
510 /// Add an isize to the current value, returning the previous value.
515 /// use std::sync::atomic::{AtomicIsize, Ordering};
517 /// let foo = AtomicIsize::new(0);
518 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
519 /// assert_eq!(10, foo.load(Ordering::SeqCst));
522 #[stable(feature = "rust1", since = "1.0.0")]
523 pub fn fetch_add(&self, val: isize, order: Ordering) -> isize {
524 unsafe { atomic_add(self.v.get(), val, order) }
527 /// Subtract an isize from the current value, returning the previous value.
532 /// use std::sync::atomic::{AtomicIsize, Ordering};
534 /// let foo = AtomicIsize::new(0);
535 /// assert_eq!(0, foo.fetch_sub(10, Ordering::SeqCst));
536 /// assert_eq!(-10, foo.load(Ordering::SeqCst));
539 #[stable(feature = "rust1", since = "1.0.0")]
540 pub fn fetch_sub(&self, val: isize, order: Ordering) -> isize {
541 unsafe { atomic_sub(self.v.get(), val, order) }
544 /// Bitwise and with the current isize, returning the previous value.
549 /// use std::sync::atomic::{AtomicIsize, Ordering};
551 /// let foo = AtomicIsize::new(0b101101);
552 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
553 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
555 #[stable(feature = "rust1", since = "1.0.0")]
556 pub fn fetch_and(&self, val: isize, order: Ordering) -> isize {
557 unsafe { atomic_and(self.v.get(), val, order) }
560 /// Bitwise or with the current isize, returning the previous value.
565 /// use std::sync::atomic::{AtomicIsize, Ordering};
567 /// let foo = AtomicIsize::new(0b101101);
568 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
569 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
571 #[stable(feature = "rust1", since = "1.0.0")]
572 pub fn fetch_or(&self, val: isize, order: Ordering) -> isize {
573 unsafe { atomic_or(self.v.get(), val, order) }
576 /// Bitwise xor with the current isize, returning the previous value.
581 /// use std::sync::atomic::{AtomicIsize, Ordering};
583 /// let foo = AtomicIsize::new(0b101101);
584 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
585 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
587 #[stable(feature = "rust1", since = "1.0.0")]
588 pub fn fetch_xor(&self, val: isize, order: Ordering) -> isize {
589 unsafe { atomic_xor(self.v.get(), val, order) }
593 #[stable(feature = "rust1", since = "1.0.0")]
595 /// Creates a new `AtomicUsize`.
600 /// use std::sync::atomic::AtomicUsize;
602 /// let atomic_forty_two = AtomicUsize::new(42);
605 #[stable(feature = "rust1", since = "1.0.0")]
606 pub fn new(v: usize) -> AtomicUsize {
607 AtomicUsize { v: UnsafeCell::new(v) }
610 /// Loads a value from the usize.
612 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
616 /// Panics if `order` is `Release` or `AcqRel`.
621 /// use std::sync::atomic::{AtomicUsize, Ordering};
623 /// let some_usize = AtomicUsize::new(5);
625 /// let value = some_usize.load(Ordering::Relaxed);
628 #[stable(feature = "rust1", since = "1.0.0")]
629 pub fn load(&self, order: Ordering) -> usize {
630 unsafe { atomic_load(self.v.get(), order) }
633 /// Stores a value into the usize.
635 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
640 /// use std::sync::atomic::{AtomicUsize, Ordering};
642 /// let some_usize = AtomicUsize::new(5);
644 /// some_usize.store(10, Ordering::Relaxed);
649 /// Panics if `order` is `Acquire` or `AcqRel`.
651 #[stable(feature = "rust1", since = "1.0.0")]
652 pub fn store(&self, val: usize, order: Ordering) {
653 unsafe { atomic_store(self.v.get(), val, order); }
656 /// Stores a value into the usize, returning the old value.
658 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
663 /// use std::sync::atomic::{AtomicUsize, Ordering};
665 /// let some_usize= AtomicUsize::new(5);
667 /// let value = some_usize.swap(10, Ordering::Relaxed);
670 #[stable(feature = "rust1", since = "1.0.0")]
671 pub fn swap(&self, val: usize, order: Ordering) -> usize {
672 unsafe { atomic_swap(self.v.get(), val, order) }
675 /// Stores a value into the usize if the current value is the same as the expected value.
677 /// If the return value is equal to `old` then the value was updated.
679 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
685 /// use std::sync::atomic::{AtomicUsize, Ordering};
687 /// let some_usize = AtomicUsize::new(5);
689 /// let value = some_usize.compare_and_swap(5, 10, Ordering::Relaxed);
692 #[stable(feature = "rust1", since = "1.0.0")]
693 pub fn compare_and_swap(&self, old: usize, new: usize, order: Ordering) -> usize {
694 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
697 /// Add to the current usize, returning the previous value.
702 /// use std::sync::atomic::{AtomicUsize, Ordering};
704 /// let foo = AtomicUsize::new(0);
705 /// assert_eq!(0, foo.fetch_add(10, Ordering::SeqCst));
706 /// assert_eq!(10, foo.load(Ordering::SeqCst));
709 #[stable(feature = "rust1", since = "1.0.0")]
710 pub fn fetch_add(&self, val: usize, order: Ordering) -> usize {
711 unsafe { atomic_add(self.v.get(), val, order) }
714 /// Subtract from the current usize, returning the previous value.
719 /// use std::sync::atomic::{AtomicUsize, Ordering};
721 /// let foo = AtomicUsize::new(10);
722 /// assert_eq!(10, foo.fetch_sub(10, Ordering::SeqCst));
723 /// assert_eq!(0, foo.load(Ordering::SeqCst));
726 #[stable(feature = "rust1", since = "1.0.0")]
727 pub fn fetch_sub(&self, val: usize, order: Ordering) -> usize {
728 unsafe { atomic_sub(self.v.get(), val, order) }
731 /// Bitwise and with the current usize, returning the previous value.
736 /// use std::sync::atomic::{AtomicUsize, Ordering};
738 /// let foo = AtomicUsize::new(0b101101);
739 /// assert_eq!(0b101101, foo.fetch_and(0b110011, Ordering::SeqCst));
740 /// assert_eq!(0b100001, foo.load(Ordering::SeqCst));
742 #[stable(feature = "rust1", since = "1.0.0")]
743 pub fn fetch_and(&self, val: usize, order: Ordering) -> usize {
744 unsafe { atomic_and(self.v.get(), val, order) }
747 /// Bitwise or with the current usize, returning the previous value.
752 /// use std::sync::atomic::{AtomicUsize, Ordering};
754 /// let foo = AtomicUsize::new(0b101101);
755 /// assert_eq!(0b101101, foo.fetch_or(0b110011, Ordering::SeqCst));
756 /// assert_eq!(0b111111, foo.load(Ordering::SeqCst));
758 #[stable(feature = "rust1", since = "1.0.0")]
759 pub fn fetch_or(&self, val: usize, order: Ordering) -> usize {
760 unsafe { atomic_or(self.v.get(), val, order) }
763 /// Bitwise xor with the current usize, returning the previous value.
768 /// use std::sync::atomic::{AtomicUsize, Ordering};
770 /// let foo = AtomicUsize::new(0b101101);
771 /// assert_eq!(0b101101, foo.fetch_xor(0b110011, Ordering::SeqCst));
772 /// assert_eq!(0b011110, foo.load(Ordering::SeqCst));
774 #[stable(feature = "rust1", since = "1.0.0")]
775 pub fn fetch_xor(&self, val: usize, order: Ordering) -> usize {
776 unsafe { atomic_xor(self.v.get(), val, order) }
780 impl<T> AtomicPtr<T> {
781 /// Creates a new `AtomicPtr`.
786 /// use std::sync::atomic::AtomicPtr;
788 /// let ptr = &mut 5;
789 /// let atomic_ptr = AtomicPtr::new(ptr);
792 #[stable(feature = "rust1", since = "1.0.0")]
793 pub fn new(p: *mut T) -> AtomicPtr<T> {
794 AtomicPtr { p: UnsafeCell::new(p as usize) }
797 /// Loads a value from the pointer.
799 /// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
803 /// Panics if `order` is `Release` or `AcqRel`.
808 /// use std::sync::atomic::{AtomicPtr, Ordering};
810 /// let ptr = &mut 5;
811 /// let some_ptr = AtomicPtr::new(ptr);
813 /// let value = some_ptr.load(Ordering::Relaxed);
816 #[stable(feature = "rust1", since = "1.0.0")]
817 pub fn load(&self, order: Ordering) -> *mut T {
819 atomic_load(self.p.get(), order) as *mut T
823 /// Stores a value into the pointer.
825 /// `store` takes an `Ordering` argument which describes the memory ordering of this operation.
830 /// use std::sync::atomic::{AtomicPtr, Ordering};
832 /// let ptr = &mut 5;
833 /// let some_ptr = AtomicPtr::new(ptr);
835 /// let other_ptr = &mut 10;
837 /// some_ptr.store(other_ptr, Ordering::Relaxed);
842 /// Panics if `order` is `Acquire` or `AcqRel`.
844 #[stable(feature = "rust1", since = "1.0.0")]
845 pub fn store(&self, ptr: *mut T, order: Ordering) {
846 unsafe { atomic_store(self.p.get(), ptr as usize, order); }
849 /// Stores a value into the pointer, returning the old value.
851 /// `swap` takes an `Ordering` argument which describes the memory ordering of this operation.
856 /// use std::sync::atomic::{AtomicPtr, Ordering};
858 /// let ptr = &mut 5;
859 /// let some_ptr = AtomicPtr::new(ptr);
861 /// let other_ptr = &mut 10;
863 /// let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
866 #[stable(feature = "rust1", since = "1.0.0")]
867 pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
868 unsafe { atomic_swap(self.p.get(), ptr as usize, order) as *mut T }
871 /// Stores a value into the pointer if the current value is the same as the expected value.
873 /// If the return value is equal to `old` then the value was updated.
875 /// `compare_and_swap` also takes an `Ordering` argument which describes the memory ordering of
881 /// use std::sync::atomic::{AtomicPtr, Ordering};
883 /// let ptr = &mut 5;
884 /// let some_ptr = AtomicPtr::new(ptr);
886 /// let other_ptr = &mut 10;
887 /// let another_ptr = &mut 10;
889 /// let value = some_ptr.compare_and_swap(other_ptr, another_ptr, Ordering::Relaxed);
892 #[stable(feature = "rust1", since = "1.0.0")]
893 pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T {
895 atomic_compare_and_swap(self.p.get(), old as usize,
896 new as usize, order) as *mut T
902 unsafe fn atomic_store<T>(dst: *mut T, val: T, order:Ordering) {
904 Release => intrinsics::atomic_store_rel(dst, val),
905 Relaxed => intrinsics::atomic_store_relaxed(dst, val),
906 SeqCst => intrinsics::atomic_store(dst, val),
907 Acquire => panic!("there is no such thing as an acquire store"),
908 AcqRel => panic!("there is no such thing as an acquire/release store"),
913 #[stable(feature = "rust1", since = "1.0.0")]
914 unsafe fn atomic_load<T>(dst: *const T, order:Ordering) -> T {
916 Acquire => intrinsics::atomic_load_acq(dst),
917 Relaxed => intrinsics::atomic_load_relaxed(dst),
918 SeqCst => intrinsics::atomic_load(dst),
919 Release => panic!("there is no such thing as a release load"),
920 AcqRel => panic!("there is no such thing as an acquire/release load"),
925 #[stable(feature = "rust1", since = "1.0.0")]
926 unsafe fn atomic_swap<T>(dst: *mut T, val: T, order: Ordering) -> T {
928 Acquire => intrinsics::atomic_xchg_acq(dst, val),
929 Release => intrinsics::atomic_xchg_rel(dst, val),
930 AcqRel => intrinsics::atomic_xchg_acqrel(dst, val),
931 Relaxed => intrinsics::atomic_xchg_relaxed(dst, val),
932 SeqCst => intrinsics::atomic_xchg(dst, val)
936 /// Returns the old value (like __sync_fetch_and_add).
938 #[stable(feature = "rust1", since = "1.0.0")]
939 unsafe fn atomic_add<T>(dst: *mut T, val: T, order: Ordering) -> T {
941 Acquire => intrinsics::atomic_xadd_acq(dst, val),
942 Release => intrinsics::atomic_xadd_rel(dst, val),
943 AcqRel => intrinsics::atomic_xadd_acqrel(dst, val),
944 Relaxed => intrinsics::atomic_xadd_relaxed(dst, val),
945 SeqCst => intrinsics::atomic_xadd(dst, val)
949 /// Returns the old value (like __sync_fetch_and_sub).
951 #[stable(feature = "rust1", since = "1.0.0")]
952 unsafe fn atomic_sub<T>(dst: *mut T, val: T, order: Ordering) -> T {
954 Acquire => intrinsics::atomic_xsub_acq(dst, val),
955 Release => intrinsics::atomic_xsub_rel(dst, val),
956 AcqRel => intrinsics::atomic_xsub_acqrel(dst, val),
957 Relaxed => intrinsics::atomic_xsub_relaxed(dst, val),
958 SeqCst => intrinsics::atomic_xsub(dst, val)
963 #[stable(feature = "rust1", since = "1.0.0")]
964 unsafe fn atomic_compare_and_swap<T>(dst: *mut T, old:T, new:T, order: Ordering) -> T {
966 Acquire => intrinsics::atomic_cxchg_acq(dst, old, new),
967 Release => intrinsics::atomic_cxchg_rel(dst, old, new),
968 AcqRel => intrinsics::atomic_cxchg_acqrel(dst, old, new),
969 Relaxed => intrinsics::atomic_cxchg_relaxed(dst, old, new),
970 SeqCst => intrinsics::atomic_cxchg(dst, old, new),
975 #[stable(feature = "rust1", since = "1.0.0")]
976 unsafe fn atomic_and<T>(dst: *mut T, val: T, order: Ordering) -> T {
978 Acquire => intrinsics::atomic_and_acq(dst, val),
979 Release => intrinsics::atomic_and_rel(dst, val),
980 AcqRel => intrinsics::atomic_and_acqrel(dst, val),
981 Relaxed => intrinsics::atomic_and_relaxed(dst, val),
982 SeqCst => intrinsics::atomic_and(dst, val)
987 #[stable(feature = "rust1", since = "1.0.0")]
988 unsafe fn atomic_nand<T>(dst: *mut T, val: T, order: Ordering) -> T {
990 Acquire => intrinsics::atomic_nand_acq(dst, val),
991 Release => intrinsics::atomic_nand_rel(dst, val),
992 AcqRel => intrinsics::atomic_nand_acqrel(dst, val),
993 Relaxed => intrinsics::atomic_nand_relaxed(dst, val),
994 SeqCst => intrinsics::atomic_nand(dst, val)
1000 #[stable(feature = "rust1", since = "1.0.0")]
1001 unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
1003 Acquire => intrinsics::atomic_or_acq(dst, val),
1004 Release => intrinsics::atomic_or_rel(dst, val),
1005 AcqRel => intrinsics::atomic_or_acqrel(dst, val),
1006 Relaxed => intrinsics::atomic_or_relaxed(dst, val),
1007 SeqCst => intrinsics::atomic_or(dst, val)
1013 #[stable(feature = "rust1", since = "1.0.0")]
1014 unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
1016 Acquire => intrinsics::atomic_xor_acq(dst, val),
1017 Release => intrinsics::atomic_xor_rel(dst, val),
1018 AcqRel => intrinsics::atomic_xor_acqrel(dst, val),
1019 Relaxed => intrinsics::atomic_xor_relaxed(dst, val),
1020 SeqCst => intrinsics::atomic_xor(dst, val)
1025 /// An atomic fence.
1027 /// A fence 'A' which has `Release` ordering semantics, synchronizes with a
1028 /// fence 'B' with (at least) `Acquire` semantics, if and only if there exists
1029 /// atomic operations X and Y, both operating on some atomic object 'M' such
1030 /// that A is sequenced before X, Y is synchronized before B and Y observes
1031 /// the change to M. This provides a happens-before dependence between A and B.
1033 /// Atomic operations with `Release` or `Acquire` semantics can also synchronize
1036 /// A fence which has `SeqCst` ordering, in addition to having both `Acquire`
1037 /// and `Release` semantics, participates in the global program order of the
1038 /// other `SeqCst` operations and/or fences.
1040 /// Accepts `Acquire`, `Release`, `AcqRel` and `SeqCst` orderings.
1044 /// Panics if `order` is `Relaxed`.
1046 #[stable(feature = "rust1", since = "1.0.0")]
1047 pub fn fence(order: Ordering) {
1050 Acquire => intrinsics::atomic_fence_acq(),
1051 Release => intrinsics::atomic_fence_rel(),
1052 AcqRel => intrinsics::atomic_fence_acqrel(),
1053 SeqCst => intrinsics::atomic_fence(),
1054 Relaxed => panic!("there is no such thing as a relaxed fence")
1059 #[unstable(feature = "core")]
1060 #[deprecated(since = "1.0.0",
1061 reason = "renamed to AtomicIsize")]
1062 #[allow(missing_docs)]
1063 pub struct AtomicInt {
1067 unsafe impl Sync for AtomicInt {}
1069 #[unstable(feature = "core")]
1070 #[deprecated(since = "1.0.0",
1071 reason = "renamed to AtomicUsize")]
1072 #[allow(missing_docs)]
1073 pub struct AtomicUint {
1074 v: UnsafeCell<uint>,
1077 unsafe impl Sync for AtomicUint {}
1079 #[unstable(feature = "core")]
1080 #[deprecated(since = "1.0.0",
1081 reason = "use ATOMIC_ISIZE_INIT instead")]
1082 #[allow(missing_docs, deprecated)]
1083 pub const ATOMIC_INT_INIT: AtomicInt =
1084 AtomicInt { v: UnsafeCell { value: 0 } };
1085 #[unstable(feature = "core")]
1086 #[deprecated(since = "1.0.0",
1087 reason = "use ATOMIC_USIZE_INIT instead")]
1088 #[allow(missing_docs, deprecated)]
1089 pub const ATOMIC_UINT_INIT: AtomicUint =
1090 AtomicUint { v: UnsafeCell { value: 0, } };
1092 #[allow(missing_docs, deprecated)]
1095 pub fn new(v: int) -> AtomicInt {
1096 AtomicInt {v: UnsafeCell::new(v)}
1100 pub fn load(&self, order: Ordering) -> int {
1101 unsafe { atomic_load(self.v.get(), order) }
1105 pub fn store(&self, val: int, order: Ordering) {
1106 unsafe { atomic_store(self.v.get(), val, order); }
1110 pub fn swap(&self, val: int, order: Ordering) -> int {
1111 unsafe { atomic_swap(self.v.get(), val, order) }
1115 pub fn compare_and_swap(&self, old: int, new: int, order: Ordering) -> int {
1116 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
1120 pub fn fetch_add(&self, val: int, order: Ordering) -> int {
1121 unsafe { atomic_add(self.v.get(), val, order) }
1125 pub fn fetch_sub(&self, val: int, order: Ordering) -> int {
1126 unsafe { atomic_sub(self.v.get(), val, order) }
1130 pub fn fetch_and(&self, val: int, order: Ordering) -> int {
1131 unsafe { atomic_and(self.v.get(), val, order) }
1135 pub fn fetch_or(&self, val: int, order: Ordering) -> int {
1136 unsafe { atomic_or(self.v.get(), val, order) }
1140 pub fn fetch_xor(&self, val: int, order: Ordering) -> int {
1141 unsafe { atomic_xor(self.v.get(), val, order) }
1145 #[allow(missing_docs, deprecated)]
1148 pub fn new(v: uint) -> AtomicUint {
1149 AtomicUint { v: UnsafeCell::new(v) }
1153 pub fn load(&self, order: Ordering) -> uint {
1154 unsafe { atomic_load(self.v.get(), order) }
1158 pub fn store(&self, val: uint, order: Ordering) {
1159 unsafe { atomic_store(self.v.get(), val, order); }
1163 pub fn swap(&self, val: uint, order: Ordering) -> uint {
1164 unsafe { atomic_swap(self.v.get(), val, order) }
1168 pub fn compare_and_swap(&self, old: uint, new: uint, order: Ordering) -> uint {
1169 unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
1173 pub fn fetch_add(&self, val: uint, order: Ordering) -> uint {
1174 unsafe { atomic_add(self.v.get(), val, order) }
1178 pub fn fetch_sub(&self, val: uint, order: Ordering) -> uint {
1179 unsafe { atomic_sub(self.v.get(), val, order) }
1183 pub fn fetch_and(&self, val: uint, order: Ordering) -> uint {
1184 unsafe { atomic_and(self.v.get(), val, order) }
1188 pub fn fetch_or(&self, val: uint, order: Ordering) -> uint {
1189 unsafe { atomic_or(self.v.get(), val, order) }
1193 pub fn fetch_xor(&self, val: uint, order: Ordering) -> uint {
1194 unsafe { atomic_xor(self.v.get(), val, order) }