2013-05-25 17:51:26 +12:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
//! Atomic types
|
|
|
|
//!
|
|
|
|
//! Atomic types provide primitive shared-memory communication between
|
|
|
|
//! threads, and are the building blocks of other concurrent
|
|
|
|
//! types.
|
|
|
|
//!
|
|
|
|
//! This module defines atomic versions of a select number of primitive
|
|
|
|
//! types, including `AtomicBool`, `AtomicInt`, `AtomicUint`, and `AtomicOption`.
|
|
|
|
//! Atomic types present operations that, when used correctly, synchronize
|
|
|
|
//! updates between threads.
|
|
|
|
//!
|
|
|
|
//! Each method takes an `Ordering` which represents the strength of
|
|
|
|
//! the memory barrier for that operation. These orderings are the
|
|
|
|
//! same as [C++11 atomic orderings][1].
|
|
|
|
//!
|
|
|
|
//! [1]: http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync
|
|
|
|
//!
|
|
|
|
//! Atomic variables are safe to share between threads (they implement `Share`)
|
|
|
|
//! but they do not themselves provide the mechanism for sharing. The most
|
|
|
|
//! common way to share an atomic variable is to put it into an `Arc` (an
|
|
|
|
//! atomically-reference-counted shared pointer).
|
|
|
|
//!
|
|
|
|
//! Most atomic types may be stored in static variables, initialized using
|
|
|
|
//! the provided static initializers like `INIT_ATOMIC_BOOL`. Atomic statics
|
|
|
|
//! are often used for lazy global initialization.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! A simple spinlock:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! # // FIXME: Needs PR #12430
|
|
|
|
//! extern crate sync;
|
|
|
|
//!
|
|
|
|
//! use sync::Arc;
|
|
|
|
//! use std::sync::atomics::{AtomicUint, SeqCst};
|
|
|
|
//! use std::task::deschedule;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let spinlock = Arc::new(AtomicUint::new(1));
|
|
|
|
//!
|
|
|
|
//! let spinlock_clone = spinlock.clone();
|
|
|
|
//! spawn(proc() {
|
|
|
|
//! spinlock_clone.store(0, SeqCst);
|
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! // Wait for the other task to release the lock
|
|
|
|
//! while spinlock.load(SeqCst) != 0 {
|
|
|
|
//! // Since tasks may not be preemptive (if they are green threads)
|
|
|
|
//! // yield to the scheduler to let the other task run. Low level
|
|
|
|
//! // concurrent code needs to take into account Rust's two threading
|
|
|
|
//! // models.
|
|
|
|
//! deschedule();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Transferring a heap object with `AtomicOption`:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! # // FIXME: Needs PR #12430
|
|
|
|
//! extern crate sync;
|
|
|
|
//!
|
|
|
|
//! use sync::Arc;
|
|
|
|
//! use std::sync::atomics::{AtomicOption, SeqCst};
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! struct BigObject;
|
|
|
|
//!
|
|
|
|
//! let shared_big_object = Arc::new(AtomicOption::empty());
|
|
|
|
//!
|
|
|
|
//! let shared_big_object_clone = shared_big_object.clone();
|
|
|
|
//! spawn(proc() {
|
|
|
|
//! let unwrapped_big_object = shared_big_object_clone.take(SeqCst);
|
|
|
|
//! if unwrapped_big_object.is_some() {
|
|
|
|
//! println!("got a big object from another task");
|
|
|
|
//! } else {
|
|
|
|
//! println!("other task hasn't sent big object yet");
|
|
|
|
//! }
|
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! shared_big_object.swap(~BigObject, SeqCst);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Keep a global count of live tasks:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::sync::atomics::{AtomicUint, SeqCst, INIT_ATOMIC_UINT};
|
|
|
|
//!
|
|
|
|
//! static mut GLOBAL_TASK_COUNT: AtomicUint = INIT_ATOMIC_UINT;
|
|
|
|
//!
|
|
|
|
//! unsafe {
|
|
|
|
//! let old_task_count = GLOBAL_TASK_COUNT.fetch_add(1, SeqCst);
|
|
|
|
//! println!("live tasks: {}", old_task_count + 1);
|
|
|
|
//! }
|
|
|
|
//! ```
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2013-12-12 17:27:37 -08:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2014-02-15 23:49:08 -08:00
|
|
|
use intrinsics;
|
2013-05-25 17:51:26 +12:00
|
|
|
use cast;
|
2014-01-26 11:42:46 +01:00
|
|
|
use std::kinds::marker;
|
2013-05-25 17:51:26 +12:00
|
|
|
use option::{Option,Some,None};
|
2013-05-26 12:39:53 +12:00
|
|
|
use ops::Drop;
|
2014-03-14 22:59:50 +01:00
|
|
|
use ty::Unsafe;
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An atomic boolean type.
|
2013-05-25 17:51:26 +12:00
|
|
|
pub struct AtomicBool {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv v: Unsafe<uint>,
|
2014-01-26 11:42:46 +01:00
|
|
|
priv nopod: marker::NoPod
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// A signed atomic integer type, supporting basic atomic arithmetic operations
|
2013-05-25 17:51:26 +12:00
|
|
|
pub struct AtomicInt {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv v: Unsafe<int>,
|
2014-01-26 11:42:46 +01:00
|
|
|
priv nopod: marker::NoPod
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An unsigned atomic integer type, supporting basic atomic arithmetic operations
|
2013-05-25 17:51:26 +12:00
|
|
|
pub struct AtomicUint {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv v: Unsafe<uint>,
|
2014-01-26 11:42:46 +01:00
|
|
|
priv nopod: marker::NoPod
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An unsigned atomic integer type that is forced to be 64-bits. This does not
|
|
|
|
/// support all operations.
|
2014-01-15 15:32:44 -08:00
|
|
|
pub struct AtomicU64 {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv v: Unsafe<u64>,
|
2014-01-26 11:42:46 +01:00
|
|
|
priv nopod: marker::NoPod
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An unsafe atomic pointer. Only supports basic atomic operations
|
2014-01-15 15:32:44 -08:00
|
|
|
pub struct AtomicPtr<T> {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv p: Unsafe<uint>,
|
2014-01-26 11:42:46 +01:00
|
|
|
priv nopod: marker::NoPod
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An atomic, nullable unique pointer
|
|
|
|
///
|
|
|
|
/// This can be used as the concurrency primitive for operations that transfer
|
|
|
|
/// owned heap objects across tasks.
|
2013-06-27 20:47:45 +03:00
|
|
|
#[unsafe_no_drop_flag]
|
2014-01-15 15:32:44 -08:00
|
|
|
pub struct AtomicOption<T> {
|
2014-03-14 22:59:50 +01:00
|
|
|
priv p: Unsafe<uint>,
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
2013-05-26 12:39:53 +12:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Atomic memory orderings
|
|
|
|
///
|
|
|
|
/// Memory orderings limit the ways that both the compiler and CPU may reorder
|
|
|
|
/// instructions around atomic operations. At its most restrictive,
|
|
|
|
/// "sequentially consistent" atomics allow neither reads nor writes
|
|
|
|
/// to be moved either before or after the atomic operation; on the other end
|
|
|
|
/// "relaxed" atomics allow all reorderings.
|
|
|
|
///
|
|
|
|
/// Rust's memory orderings are the same as in C++[1].
|
|
|
|
///
|
|
|
|
/// [1]: http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync
|
2013-05-25 17:51:26 +12:00
|
|
|
pub enum Ordering {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// No ordering constraints, only atomic operations
|
2013-07-23 12:34:40 +02:00
|
|
|
Relaxed,
|
2014-03-16 16:05:01 -07:00
|
|
|
/// When coupled with a store, all previous writes become visible
|
|
|
|
/// to another thread that performs a load with `Acquire` ordering
|
|
|
|
/// on the same value
|
2013-05-25 17:51:26 +12:00
|
|
|
Release,
|
2014-03-16 16:05:01 -07:00
|
|
|
/// When coupled with a load, all subsequent loads will see data
|
|
|
|
/// written before a store with `Release` ordering on the same value
|
|
|
|
/// in another thread
|
2013-05-25 17:51:26 +12:00
|
|
|
Acquire,
|
2014-03-16 16:05:01 -07:00
|
|
|
/// When coupled with a load, uses `Acquire` ordering, and with a store
|
|
|
|
/// `Release` ordering
|
2013-07-23 12:34:40 +02:00
|
|
|
AcqRel,
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Like `AcqRel` with the additional guarantee that all threads see all
|
|
|
|
/// sequentially consistent operations in the same order.
|
2013-05-25 17:51:26 +12:00
|
|
|
SeqCst
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An `AtomicBool` initialized to `false`
|
2014-03-14 22:59:50 +01:00
|
|
|
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: Unsafe{value: 0,
|
|
|
|
marker1: marker::InvariantType},
|
|
|
|
nopod: marker::NoPod };
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An `AtomicInt` initialized to `0`
|
2014-03-14 22:59:50 +01:00
|
|
|
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: Unsafe{value: 0,
|
|
|
|
marker1: marker::InvariantType},
|
|
|
|
nopod: marker::NoPod };
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An `AtomicUint` initialized to `0`
|
2014-03-14 22:59:50 +01:00
|
|
|
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: Unsafe{value: 0,
|
|
|
|
marker1: marker::InvariantType},
|
|
|
|
nopod: marker::NoPod };
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An `AtomicU64` initialized to `0`
|
2014-03-14 22:59:50 +01:00
|
|
|
pub static INIT_ATOMIC_U64 : AtomicU64 = AtomicU64 { v: Unsafe{value: 0,
|
|
|
|
marker1: marker::InvariantType},
|
|
|
|
nopod: marker::NoPod };
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
|
|
|
|
// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
|
|
|
|
static UINT_TRUE: uint = -1;
|
|
|
|
|
2013-05-25 17:51:26 +12:00
|
|
|
impl AtomicBool {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicBool`
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn new(v: bool) -> AtomicBool {
|
2014-03-14 22:59:50 +01:00
|
|
|
let val = if v { UINT_TRUE } else { 0 };
|
|
|
|
AtomicBool { v: Unsafe::new(val), nopod: marker::NoPod }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Load the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn load(&self, order: Ordering) -> bool {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_load(self.v.get() as *uint, order) > 0 }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn store(&self, val: bool, order: Ordering) {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_store(self.v.get(), val, order); }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store a value, returning the old value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, val: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_swap(self.v.get(), val, order) > 0 }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// If the current value is the same as expected, store a new value
|
|
|
|
///
|
|
|
|
/// Compare the current value with `old`; if they are the same then
|
|
|
|
/// replace the current value with `new`. Return the previous value.
|
|
|
|
/// If the return value is equal to `old` then the value was updated.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// # // FIXME: Needs PR #12430
|
|
|
|
/// extern crate sync;
|
|
|
|
///
|
|
|
|
/// use sync::Arc;
|
|
|
|
/// use std::sync::atomics::{AtomicBool, SeqCst};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let spinlock = Arc::new(AtomicBool::new(false));
|
|
|
|
/// let spinlock_clone = spin_lock.clone();
|
|
|
|
///
|
|
|
|
/// spawn(proc() {
|
|
|
|
/// with_lock(&spinlock, || println!("task 1 in lock"));
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// spawn(proc() {
|
|
|
|
/// with_lock(&spinlock_clone, || println!("task 2 in lock"));
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn with_lock(spinlock: &Arc<AtomicBool>, f: || -> ()) {
|
|
|
|
/// // CAS loop until we are able to replace `false` with `true`
|
|
|
|
/// while spinlock.compare_and_swap(false, true, SeqCst) == false {
|
|
|
|
/// // Since tasks may not be preemptive (if they are green threads)
|
|
|
|
/// // yield to the scheduler to let the other task run. Low level
|
|
|
|
/// // concurrent code needs to take into account Rust's two threading
|
|
|
|
/// // models.
|
|
|
|
/// deschedule();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Now we have the spinlock
|
|
|
|
/// f();
|
|
|
|
///
|
|
|
|
/// // Release the lock
|
|
|
|
/// spinlock.store(false);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn compare_and_swap(&self, old: bool, new: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let old = if old { UINT_TRUE } else { 0 };
|
|
|
|
let new = if new { UINT_TRUE } else { 0 };
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) > 0 }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// A logical "and" operation
|
|
|
|
///
|
|
|
|
/// Performs a logical "and" operation on the current value and the
|
|
|
|
/// argument `val`, and sets the new value to the result.
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicBool, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_and(false, SeqCst));
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_and(true, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(false);
|
|
|
|
/// assert_eq!(false, foo.fetch_and(false, SeqCst));
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-07-25 10:46:31 +02:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_and(self.v.get(), val, order) > 0 }
|
2013-07-25 10:46:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// A logical "nand" operation
|
|
|
|
///
|
|
|
|
/// Performs a logical "nand" operation on the current value and the
|
|
|
|
/// argument `val`, and sets the new value to the result.
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicBool, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_nand(false, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_nand(true, SeqCst));
|
|
|
|
/// assert_eq!(0, foo.load(SeqCst) as int);
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(false);
|
|
|
|
/// assert_eq!(false, foo.fetch_nand(false, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-07-25 10:46:31 +02:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_nand(self.v.get(), val, order) > 0 }
|
2013-07-25 10:46:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// A logical "or" operation
|
|
|
|
///
|
|
|
|
/// Performs a logical "or" operation on the current value and the
|
|
|
|
/// argument `val`, and sets the new value to the result.
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicBool, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_or(false, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_or(true, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(false);
|
|
|
|
/// assert_eq!(false, foo.fetch_or(false, SeqCst));
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-07-25 10:46:31 +02:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_or(self.v.get(), val, order) > 0 }
|
2013-07-25 10:46:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// A logical "xor" operation
|
|
|
|
///
|
|
|
|
/// Performs a logical "xor" operation on the current value and the
|
|
|
|
/// argument `val`, and sets the new value to the result.
|
|
|
|
/// Returns the previous value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicBool, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_xor(false, SeqCst));
|
|
|
|
/// assert_eq!(true, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(true);
|
|
|
|
/// assert_eq!(true, foo.fetch_xor(true, SeqCst));
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicBool::new(false);
|
|
|
|
/// assert_eq!(false, foo.fetch_xor(false, SeqCst));
|
|
|
|
/// assert_eq!(false, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-07-25 10:46:31 +02:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
|
2014-03-16 16:05:01 -07:00
|
|
|
let val = if val { UINT_TRUE } else { 0 };
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_xor(self.v.get(), val, order) > 0 }
|
2013-07-25 10:46:31 +02:00
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AtomicInt {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicInt`
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn new(v: int) -> AtomicInt {
|
2014-03-14 22:59:50 +01:00
|
|
|
AtomicInt {v: Unsafe::new(v), nopod: marker::NoPod}
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Load the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn load(&self, order: Ordering) -> int {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_load(self.v.get() as *int, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn store(&self, val: int, order: Ordering) {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_store(self.v.get(), val, order); }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store a value, returning the old value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, val: int, order: Ordering) -> int {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_swap(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// If the current value is the same as expected, store a new value
|
|
|
|
///
|
|
|
|
/// Compare the current value with `old`; if they are the same then
|
|
|
|
/// replace the current value with `new`. Return the previous value.
|
|
|
|
/// If the return value is equal to `old` then the value was updated.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn compare_and_swap(&self, old: int, new: int, order: Ordering) -> int {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Add to the current value, returning the previous
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicInt, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicInt::new(0);
|
|
|
|
/// assert_eq!(0, foo.fetch_add(10, SeqCst));
|
|
|
|
/// assert_eq!(10, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_add(&self, val: int, order: Ordering) -> int {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_add(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Subtract from the current value, returning the previous
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicInt, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicInt::new(0);
|
|
|
|
/// assert_eq!(0, foo.fetch_sub(10, SeqCst));
|
|
|
|
/// assert_eq!(-10, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_sub(&self, val: int, order: Ordering) -> int {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_sub(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:35:24 +08:00
|
|
|
// temporary workaround
|
|
|
|
// it causes link failure on MIPS target
|
|
|
|
// libgcc doesn't implement 64-bit atomic operations for MIPS32
|
|
|
|
#[cfg(not(target_arch = "mips"))]
|
2014-01-15 15:32:44 -08:00
|
|
|
impl AtomicU64 {
|
|
|
|
pub fn new(v: u64) -> AtomicU64 {
|
2014-03-14 22:59:50 +01:00
|
|
|
AtomicU64 { v: Unsafe::new(v), nopod: marker::NoPod }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn load(&self, order: Ordering) -> u64 {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_load(self.v.get(), order) }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn store(&self, val: u64, order: Ordering) {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_store(self.v.get(), val, order); }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, val: u64, order: Ordering) -> u64 {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_swap(self.v.get(), val, order) }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn compare_and_swap(&self, old: u64, new: u64, order: Ordering) -> u64 {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_add(&self, val: u64, order: Ordering) -> u64 {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_add(self.v.get(), val, order) }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_sub(&self, val: u64, order: Ordering) -> u64 {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_sub(self.v.get(), val, order) }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 17:51:26 +12:00
|
|
|
impl AtomicUint {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicUint`
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn new(v: uint) -> AtomicUint {
|
2014-03-14 22:59:50 +01:00
|
|
|
AtomicUint { v: Unsafe::new(v), nopod: marker::NoPod }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Load the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn load(&self, order: Ordering) -> uint {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_load(self.v.get() as *uint, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn store(&self, val: uint, order: Ordering) {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_store(self.v.get(), val, order); }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store a value, returning the old value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, val: uint, order: Ordering) -> uint {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_swap(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// If the current value is the same as expected, store a new value
|
|
|
|
///
|
|
|
|
/// Compare the current value with `old`; if they are the same then
|
|
|
|
/// replace the current value with `new`. Return the previous value.
|
|
|
|
/// If the return value is equal to `old` then the value was updated.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn compare_and_swap(&self, old: uint, new: uint, order: Ordering) -> uint {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_compare_and_swap(self.v.get(), old, new, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Add to the current value, returning the previous
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicUint, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicUint::new(0);
|
|
|
|
/// assert_eq!(0, foo.fetch_add(10, SeqCst));
|
|
|
|
/// assert_eq!(10, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_add(&self, val: uint, order: Ordering) -> uint {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_add(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Subtract from the current value, returning the previous
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::sync::atomics::{AtomicUint, SeqCst};
|
|
|
|
///
|
|
|
|
/// let mut foo = AtomicUint::new(10);
|
|
|
|
/// assert_eq!(10, foo.fetch_sub(10, SeqCst));
|
|
|
|
/// assert_eq!(0, foo.load(SeqCst));
|
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fetch_sub(&self, val: uint, order: Ordering) -> uint {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_sub(self.v.get(), val, order) }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AtomicPtr<T> {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicPtr`
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn new(p: *mut T) -> AtomicPtr<T> {
|
2014-03-14 22:59:50 +01:00
|
|
|
AtomicPtr { p: Unsafe::new(p as uint), nopod: marker::NoPod }
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Load the value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-01-15 15:32:44 -08:00
|
|
|
pub fn load(&self, order: Ordering) -> *mut T {
|
|
|
|
unsafe {
|
2014-02-24 18:20:52 -08:00
|
|
|
atomic_load(self.p.get() as **mut T, order) as *mut T
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store the value
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn store(&self, ptr: *mut T, order: Ordering) {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_store(self.p.get(), ptr as uint, order); }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store a value, returning the old value
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_swap(self.p.get(), ptr as uint, order) as *mut T }
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// If the current value is the same as expected, store a new value
|
|
|
|
///
|
|
|
|
/// Compare the current value with `old`; if they are the same then
|
|
|
|
/// replace the current value with `new`. Return the previous value.
|
|
|
|
/// If the return value is equal to `old` then the value was updated.
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T {
|
2014-01-15 15:32:44 -08:00
|
|
|
unsafe {
|
2014-02-24 18:20:52 -08:00
|
|
|
atomic_compare_and_swap(self.p.get(), old as uint,
|
2014-01-15 15:32:44 -08:00
|
|
|
new as uint, order) as *mut T
|
|
|
|
}
|
|
|
|
}
|
2013-05-26 12:39:53 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AtomicOption<T> {
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicOption`
|
2013-06-05 00:37:52 +02:00
|
|
|
pub fn new(p: ~T) -> AtomicOption<T> {
|
2014-03-14 22:59:50 +01:00
|
|
|
unsafe { AtomicOption { p: Unsafe::new(cast::transmute(p)) } }
|
2013-05-26 12:39:53 +12:00
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Create a new `AtomicOption` that doesn't contain a value
|
2014-03-14 22:59:50 +01:00
|
|
|
pub fn empty() -> AtomicOption<T> { AtomicOption { p: Unsafe::new(0) } }
|
2013-05-26 12:39:53 +12:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Store a value, returning the old value
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn swap(&self, val: ~T, order: Ordering) -> Option<~T> {
|
2013-05-26 12:39:53 +12:00
|
|
|
unsafe {
|
|
|
|
let val = cast::transmute(val);
|
|
|
|
|
2014-02-24 18:20:52 -08:00
|
|
|
let p = atomic_swap(self.p.get(), val, order);
|
2014-01-15 15:32:44 -08:00
|
|
|
if p as uint == 0 {
|
2013-05-26 12:39:53 +12:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(cast::transmute(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Remove the value, leaving the `AtomicOption` empty.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn take(&self, order: Ordering) -> Option<~T> {
|
2014-01-15 15:32:44 -08:00
|
|
|
unsafe { self.swap(cast::transmute(0), order) }
|
2013-05-26 12:39:53 +12:00
|
|
|
}
|
2013-07-02 13:13:07 -04:00
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Replace an empty value with a non-empty value.
|
|
|
|
///
|
|
|
|
/// Succeeds if the option is `None` and returns `None` if so. If
|
|
|
|
/// the option was already `Some`, returns `Some` of the rejected
|
2013-07-02 13:13:07 -04:00
|
|
|
/// value.
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn fill(&self, val: ~T, order: Ordering) -> Option<~T> {
|
2013-07-02 13:13:07 -04:00
|
|
|
unsafe {
|
|
|
|
let val = cast::transmute(val);
|
|
|
|
let expected = cast::transmute(0);
|
2014-02-24 18:20:52 -08:00
|
|
|
let oldval = atomic_compare_and_swap(self.p.get(), expected, val, order);
|
2013-07-02 13:13:07 -04:00
|
|
|
if oldval == expected {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(cast::transmute(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// Returns `true` if the `AtomicOption` is empty.
|
|
|
|
///
|
2013-07-02 13:13:07 -04:00
|
|
|
/// Be careful: The caller must have some external method of ensuring the
|
|
|
|
/// result does not get invalidated by another task after this returns.
|
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub fn is_empty(&self, order: Ordering) -> bool {
|
2014-02-24 18:20:52 -08:00
|
|
|
unsafe { atomic_load(self.p.get() as *uint, order) as uint == 0 }
|
2013-07-02 13:13:07 -04:00
|
|
|
}
|
2013-05-26 12:39:53 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<T> Drop for AtomicOption<T> {
|
2013-09-16 21:18:07 -04:00
|
|
|
fn drop(&mut self) {
|
2013-09-17 11:44:59 -04:00
|
|
|
let _ = self.take(SeqCst);
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_store<T>(dst: *mut T, val: T, order:Ordering) {
|
2013-05-25 17:51:26 +12:00
|
|
|
match order {
|
|
|
|
Release => intrinsics::atomic_store_rel(dst, val),
|
2013-07-23 12:34:40 +02:00
|
|
|
Relaxed => intrinsics::atomic_store_relaxed(dst, val),
|
2013-05-25 17:51:26 +12:00
|
|
|
_ => intrinsics::atomic_store(dst, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_load<T>(dst: *mut T, order:Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_load_acq(dst),
|
|
|
|
Relaxed => intrinsics::atomic_load_relaxed(dst),
|
|
|
|
_ => intrinsics::atomic_load(dst)
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_swap<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xchg_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xchg_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_xchg_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_xchg_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_xchg(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2013-06-10 22:13:17 -04:00
|
|
|
/// Returns the old value (like __sync_fetch_and_add).
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_add<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xadd_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xadd_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_xadd_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_xadd_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_xadd(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2013-06-10 22:13:17 -04:00
|
|
|
/// Returns the old value (like __sync_fetch_and_sub).
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_sub<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xsub_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xsub_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_xsub_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_xsub_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_xsub(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_compare_and_swap<T>(dst: *mut T, old:T, new:T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_cxchg_acq(dst, old, new),
|
|
|
|
Release => intrinsics::atomic_cxchg_rel(dst, old, new),
|
|
|
|
AcqRel => intrinsics::atomic_cxchg_acqrel(dst, old, new),
|
|
|
|
Relaxed => intrinsics::atomic_cxchg_relaxed(dst, old, new),
|
|
|
|
_ => intrinsics::atomic_cxchg(dst, old, new),
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_and<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_and_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_and_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_and_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_and_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_and(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-17 01:37:26 -08:00
|
|
|
pub unsafe fn atomic_nand<T>(dst: &T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_nand_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_nand_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_nand_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_nand_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_nand(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_or<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_or_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_or_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_or_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_or_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_or(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
|
|
|
|
2014-01-15 15:32:44 -08:00
|
|
|
#[inline]
|
2014-02-24 18:20:52 -08:00
|
|
|
pub unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
|
2014-01-15 15:32:44 -08:00
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_xor_acq(dst, val),
|
|
|
|
Release => intrinsics::atomic_xor_rel(dst, val),
|
|
|
|
AcqRel => intrinsics::atomic_xor_acqrel(dst, val),
|
|
|
|
Relaxed => intrinsics::atomic_xor_relaxed(dst, val),
|
|
|
|
_ => intrinsics::atomic_xor(dst, val)
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
|
|
|
|
2014-03-16 16:05:01 -07:00
|
|
|
/// An atomic fence.
|
|
|
|
///
|
|
|
|
/// A fence 'A' which has `Release` ordering semantics, synchronizes with a
|
|
|
|
/// fence 'B' with (at least) `Acquire` semantics, if and only if there exists
|
|
|
|
/// atomic operations X and Y, both operating on some atomic object 'M' such
|
|
|
|
/// that A is sequenced before X, Y is synchronized before B and Y observers
|
|
|
|
/// the change to M. This provides a happens-before dependence between A and B.
|
|
|
|
///
|
|
|
|
/// Atomic operations with `Release` or `Acquire` semantics can also synchronize
|
|
|
|
/// with a fence.
|
|
|
|
///
|
|
|
|
/// A fence with has `SeqCst` ordering, in addition to having both `Acquire` and
|
|
|
|
/// `Release` semantics, participates in the global program order of the other
|
|
|
|
/// `SeqCst` operations and/or fences.
|
|
|
|
///
|
|
|
|
/// Accepts `Acquire`, `Release`, `AcqRel` and `SeqCst` orderings.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if `order` is `Relaxed`
|
2013-08-03 12:21:57 -04:00
|
|
|
#[inline]
|
2013-07-28 19:48:16 +12:00
|
|
|
pub fn fence(order: Ordering) {
|
2013-07-28 20:26:29 +12:00
|
|
|
unsafe {
|
|
|
|
match order {
|
|
|
|
Acquire => intrinsics::atomic_fence_acq(),
|
|
|
|
Release => intrinsics::atomic_fence_rel(),
|
2014-03-16 16:05:01 -07:00
|
|
|
AcqRel => intrinsics::atomic_fence_acqrel(),
|
|
|
|
SeqCst => intrinsics::atomic_fence(),
|
|
|
|
Relaxed => fail!("there is no such thing as a relaxed fence")
|
2013-07-28 20:26:29 +12:00
|
|
|
}
|
2013-07-28 19:48:16 +12:00
|
|
|
}
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
2013-05-25 17:51:26 +12:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use option::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2014-03-16 18:03:58 -04:00
|
|
|
fn bool_() {
|
|
|
|
let mut a = AtomicBool::new(false);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), true);
|
2013-05-25 17:51:26 +12:00
|
|
|
|
2014-03-16 18:03:58 -04:00
|
|
|
a.store(false, SeqCst);
|
|
|
|
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|
|
|
|
|
2013-07-02 13:13:07 -04:00
|
|
|
#[test]
|
|
|
|
fn option_empty() {
|
2013-08-08 11:38:10 -07:00
|
|
|
let mut option: AtomicOption<()> = AtomicOption::empty();
|
|
|
|
assert!(option.is_empty(SeqCst));
|
2013-07-02 13:13:07 -04:00
|
|
|
}
|
|
|
|
|
2013-05-25 17:51:26 +12:00
|
|
|
#[test]
|
2013-05-26 12:39:53 +12:00
|
|
|
fn option_swap() {
|
|
|
|
let mut p = AtomicOption::new(~1);
|
2013-05-25 17:51:26 +12:00
|
|
|
let a = ~2;
|
|
|
|
|
|
|
|
let b = p.swap(a, SeqCst);
|
|
|
|
|
|
|
|
assert_eq!(b, Some(~1));
|
|
|
|
assert_eq!(p.take(SeqCst), Some(~2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-05-26 12:39:53 +12:00
|
|
|
fn option_take() {
|
|
|
|
let mut p = AtomicOption::new(~1);
|
2013-05-25 17:51:26 +12:00
|
|
|
|
|
|
|
assert_eq!(p.take(SeqCst), Some(~1));
|
|
|
|
assert_eq!(p.take(SeqCst), None);
|
|
|
|
|
|
|
|
let p2 = ~2;
|
2013-05-26 12:39:53 +12:00
|
|
|
p.swap(p2, SeqCst);
|
2013-05-25 17:51:26 +12:00
|
|
|
|
|
|
|
assert_eq!(p.take(SeqCst), Some(~2));
|
|
|
|
}
|
|
|
|
|
2013-07-02 13:13:07 -04:00
|
|
|
#[test]
|
|
|
|
fn option_fill() {
|
|
|
|
let mut p = AtomicOption::new(~1);
|
|
|
|
assert!(p.fill(~2, SeqCst).is_some()); // should fail; shouldn't leak!
|
|
|
|
assert_eq!(p.take(SeqCst), Some(~1));
|
|
|
|
|
|
|
|
assert!(p.fill(~2, SeqCst).is_none()); // shouldn't fail
|
|
|
|
assert_eq!(p.take(SeqCst), Some(~2));
|
|
|
|
}
|
2013-07-25 10:46:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bool_and() {
|
|
|
|
let mut a = AtomicBool::new(true);
|
|
|
|
assert_eq!(a.fetch_and(false, SeqCst),true);
|
|
|
|
assert_eq!(a.load(SeqCst),false);
|
|
|
|
}
|
2013-07-28 20:26:29 +12:00
|
|
|
|
|
|
|
static mut S_BOOL : AtomicBool = INIT_ATOMIC_BOOL;
|
|
|
|
static mut S_INT : AtomicInt = INIT_ATOMIC_INT;
|
|
|
|
static mut S_UINT : AtomicUint = INIT_ATOMIC_UINT;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_init() {
|
|
|
|
unsafe {
|
|
|
|
assert!(!S_BOOL.load(SeqCst));
|
|
|
|
assert!(S_INT.load(SeqCst) == 0);
|
|
|
|
assert!(S_UINT.load(SeqCst) == 0);
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 15:32:44 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn different_sizes() {
|
|
|
|
unsafe {
|
|
|
|
let mut slot = 0u16;
|
2014-02-24 18:20:52 -08:00
|
|
|
assert_eq!(super::atomic_swap(&mut slot, 1, SeqCst), 0);
|
2014-01-15 15:32:44 -08:00
|
|
|
|
|
|
|
let mut slot = 0u8;
|
2014-02-24 18:20:52 -08:00
|
|
|
assert_eq!(super::atomic_compare_and_swap(&mut slot, 1, 2, SeqCst), 0);
|
2014-01-15 15:32:44 -08:00
|
|
|
|
|
|
|
let mut slot = 0u32;
|
2014-02-17 01:37:26 -08:00
|
|
|
assert_eq!(super::atomic_load(&slot, SeqCst), 0);
|
2014-01-15 15:32:44 -08:00
|
|
|
|
|
|
|
let mut slot = 0u64;
|
2014-02-24 18:20:52 -08:00
|
|
|
super::atomic_store(&mut slot, 2, SeqCst);
|
2014-01-15 15:32:44 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-25 17:51:26 +12:00
|
|
|
}
|