| 1 |
// Copyright 2016 Amanieu d'Antras |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 4 |
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 |
// http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 6 |
// copied, modified, or distributed except according to those terms. |
| 7 |
|
| 8 |
//! Generic `Atomic<T>` wrapper type |
| 9 |
//! |
| 10 |
//! Atomic types provide primitive shared-memory communication between |
| 11 |
//! threads, and are the building blocks of other concurrent types. |
| 12 |
//! |
| 13 |
//! This library defines a generic atomic wrapper type `Atomic<T>` for all |
| 14 |
//! `T: Copy` types. |
| 15 |
//! Atomic types present operations that, when used correctly, synchronize |
| 16 |
//! updates between threads. |
| 17 |
//! |
| 18 |
//! Each method takes an `Ordering` which represents the strength of |
| 19 |
//! the memory barrier for that operation. These orderings are the |
| 20 |
//! same as [LLVM atomic orderings][1]. |
| 21 |
//! |
| 22 |
//! [1]: http://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations |
| 23 |
//! |
| 24 |
//! Atomic variables are safe to share between threads (they implement `Sync`) |
| 25 |
//! but they do not themselves provide the mechanism for sharing. The most |
| 26 |
//! common way to share an atomic variable is to put it into an `Arc` (an |
| 27 |
//! atomically-reference-counted shared pointer). |
| 28 |
//! |
| 29 |
//! Most atomic types may be stored in static variables, initialized using |
| 30 |
//! the `const fn` constructors (only available on nightly). Atomic statics |
| 31 |
//! are often used for lazy global initialization. |
| 32 |
|
| 33 |
#![warn(missing_docs)] |
| 34 |
#![no_std] |
| 35 |
#![cfg_attr( |
| 36 |
feature = "nightly", feature(const_fn, cfg_target_has_atomic, atomic_min_max) |
| 37 |
)] |
| 38 |
|
| 39 |
#[cfg(any(test, feature = "std"))] |
| 40 |
#[macro_use] |
| 41 |
extern crate std; |
| 42 |
|
| 43 |
// Re-export some useful definitions from libcore |
| 44 |
pub use core::sync::atomic::{fence, Ordering}; |
| 45 |
|
| 46 |
use core::cell::UnsafeCell; |
| 47 |
use core::fmt; |
| 48 |
|
| 49 |
#[cfg(feature = "std")] |
| 50 |
use std::panic::RefUnwindSafe; |
| 51 |
|
| 52 |
mod fallback; |
| 53 |
mod ops; |
| 54 |
|
| 55 |
/// A generic atomic wrapper type which allows an object to be safely shared |
| 56 |
/// between threads. |
| 57 |
pub struct Atomic<T: Copy> { |
| 58 |
v: UnsafeCell<T>, |
| 59 |
} |
| 60 |
|
| 61 |
// Atomic<T> is only Sync if T is Send |
| 62 |
unsafe impl<T: Copy + Send> Sync for Atomic<T> {} |
| 63 |
|
| 64 |
// Given that atomicity is guaranteed, Atomic<T> is RefUnwindSafe if T is |
| 65 |
// |
| 66 |
// This is trivially correct for native lock-free atomic types. For those whose |
| 67 |
// atomicity is emulated using a spinlock, it is still correct because the |
| 68 |
// `Atomic` API does not allow doing any panic-inducing operation after writing |
| 69 |
// to the target object. |
| 70 |
#[cfg(feature = "std")] |
| 71 |
impl<T: Copy + RefUnwindSafe> RefUnwindSafe for Atomic<T> {} |
| 72 |
|
| 73 |
impl<T: Copy + Default> Default for Atomic<T> { |
| 74 |
#[inline] |
| 75 |
fn default() -> Self { |
| 76 |
Self::new(Default::default()) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl<T: Copy + fmt::Debug> fmt::Debug for Atomic<T> { |
| 81 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 82 |
f.debug_tuple("Atomic") |
| 83 |
.field(&self.load(Ordering::SeqCst)) |
| 84 |
.finish() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl<T: Copy> Atomic<T> { |
| 89 |
/// Creates a new `Atomic`. |
| 90 |
#[inline] |
| 91 |
#[cfg(feature = "nightly")] |
| 92 |
pub const fn new(v: T) -> Atomic<T> { |
| 93 |
Atomic { |
| 94 |
v: UnsafeCell::new(v), |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/// Creates a new `Atomic`. |
| 99 |
#[inline] |
| 100 |
#[cfg(not(feature = "nightly"))] |
| 101 |
pub fn new(v: T) -> Atomic<T> { |
| 102 |
Atomic { |
| 103 |
v: UnsafeCell::new(v), |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/// Checks if `Atomic` objects of this type are lock-free. |
| 108 |
/// |
| 109 |
/// If an `Atomic` is not lock-free then it may be implemented using locks |
| 110 |
/// internally, which makes it unsuitable for some situations (such as |
| 111 |
/// communicating with a signal handler). |
| 112 |
#[inline] |
| 113 |
#[cfg(feature = "nightly")] |
| 114 |
pub const fn is_lock_free() -> bool { |
| 115 |
ops::atomic_is_lock_free::<T>() |
| 116 |
} |
| 117 |
|
| 118 |
/// Checks if `Atomic` objects of this type are lock-free. |
| 119 |
/// |
| 120 |
/// If an `Atomic` is not lock-free then it may be implemented using locks |
| 121 |
/// internally, which makes it unsuitable for some situations (such as |
| 122 |
/// communicating with a signal handler). |
| 123 |
#[inline] |
| 124 |
#[cfg(not(feature = "nightly"))] |
| 125 |
pub fn is_lock_free() -> bool { |
| 126 |
ops::atomic_is_lock_free::<T>() |
| 127 |
} |
| 128 |
|
| 129 |
/// Returns a mutable reference to the underlying type. |
| 130 |
/// |
| 131 |
/// This is safe because the mutable reference guarantees that no other threads are |
| 132 |
/// concurrently accessing the atomic data. |
| 133 |
#[inline] |
| 134 |
pub fn get_mut(&mut self) -> &mut T { |
| 135 |
unsafe { &mut *self.v.get() } |
| 136 |
} |
| 137 |
|
| 138 |
/// Consumes the atomic and returns the contained value. |
| 139 |
/// |
| 140 |
/// This is safe because passing `self` by value guarantees that no other threads are |
| 141 |
/// concurrently accessing the atomic data. |
| 142 |
#[inline] |
| 143 |
pub fn into_inner(self) -> T { |
| 144 |
self.v.into_inner() |
| 145 |
} |
| 146 |
|
| 147 |
/// Loads a value from the `Atomic`. |
| 148 |
/// |
| 149 |
/// `load` takes an `Ordering` argument which describes the memory ordering |
| 150 |
/// of this operation. |
| 151 |
/// |
| 152 |
/// # Panics |
| 153 |
/// |
| 154 |
/// Panics if `order` is `Release` or `AcqRel`. |
| 155 |
#[inline] |
| 156 |
pub fn load(&self, order: Ordering) -> T { |
| 157 |
unsafe { ops::atomic_load(self.v.get(), order) } |
| 158 |
} |
| 159 |
|
| 160 |
/// Stores a value into the `Atomic`. |
| 161 |
/// |
| 162 |
/// `store` takes an `Ordering` argument which describes the memory ordering |
| 163 |
/// of this operation. |
| 164 |
/// |
| 165 |
/// # Panics |
| 166 |
/// |
| 167 |
/// Panics if `order` is `Acquire` or `AcqRel`. |
| 168 |
#[inline] |
| 169 |
pub fn store(&self, val: T, order: Ordering) { |
| 170 |
unsafe { |
| 171 |
ops::atomic_store(self.v.get(), val, order); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/// Stores a value into the `Atomic`, returning the old value. |
| 176 |
/// |
| 177 |
/// `swap` takes an `Ordering` argument which describes the memory ordering |
| 178 |
/// of this operation. |
| 179 |
#[inline] |
| 180 |
pub fn swap(&self, val: T, order: Ordering) -> T { |
| 181 |
unsafe { ops::atomic_swap(self.v.get(), val, order) } |
| 182 |
} |
| 183 |
|
| 184 |
/// Stores a value into the `Atomic` if the current value is the same as the |
| 185 |
/// `current` value. |
| 186 |
/// |
| 187 |
/// The return value is a result indicating whether the new value was |
| 188 |
/// written and containing the previous value. On success this value is |
| 189 |
/// guaranteed to be equal to `new`. |
| 190 |
/// |
| 191 |
/// `compare_exchange` takes two `Ordering` arguments to describe the memory |
| 192 |
/// ordering of this operation. The first describes the required ordering if |
| 193 |
/// the operation succeeds while the second describes the required ordering |
| 194 |
/// when the operation fails. The failure ordering can't be `Acquire` or |
| 195 |
/// `AcqRel` and must be equivalent or weaker than the success ordering. |
| 196 |
#[inline] |
| 197 |
pub fn compare_exchange( |
| 198 |
&self, |
| 199 |
current: T, |
| 200 |
new: T, |
| 201 |
success: Ordering, |
| 202 |
failure: Ordering, |
| 203 |
) -> Result<T, T> { |
| 204 |
unsafe { ops::atomic_compare_exchange(self.v.get(), current, new, success, failure) } |
| 205 |
} |
| 206 |
|
| 207 |
/// Stores a value into the `Atomic` if the current value is the same as the |
| 208 |
/// `current` value. |
| 209 |
/// |
| 210 |
/// Unlike `compare_exchange`, this function is allowed to spuriously fail |
| 211 |
/// even when the comparison succeeds, which can result in more efficient |
| 212 |
/// code on some platforms. The return value is a result indicating whether |
| 213 |
/// the new value was written and containing the previous value. |
| 214 |
/// |
| 215 |
/// `compare_exchange` takes two `Ordering` arguments to describe the memory |
| 216 |
/// ordering of this operation. The first describes the required ordering if |
| 217 |
/// the operation succeeds while the second describes the required ordering |
| 218 |
/// when the operation fails. The failure ordering can't be `Acquire` or |
| 219 |
/// `AcqRel` and must be equivalent or weaker than the success ordering. |
| 220 |
/// success ordering. |
| 221 |
#[inline] |
| 222 |
pub fn compare_exchange_weak( |
| 223 |
&self, |
| 224 |
current: T, |
| 225 |
new: T, |
| 226 |
success: Ordering, |
| 227 |
failure: Ordering, |
| 228 |
) -> Result<T, T> { |
| 229 |
unsafe { ops::atomic_compare_exchange_weak(self.v.get(), current, new, success, failure) } |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl Atomic<bool> { |
| 234 |
/// Logical "and" with a boolean value. |
| 235 |
/// |
| 236 |
/// Performs a logical "and" operation on the current value and the argument |
| 237 |
/// `val`, and sets the new value to the result. |
| 238 |
/// |
| 239 |
/// Returns the previous value. |
| 240 |
#[inline] |
| 241 |
pub fn fetch_and(&self, val: bool, order: Ordering) -> bool { |
| 242 |
unsafe { ops::atomic_and(self.v.get(), val, order) } |
| 243 |
} |
| 244 |
|
| 245 |
/// Logical "or" with a boolean value. |
| 246 |
/// |
| 247 |
/// Performs a logical "or" operation on the current value and the argument |
| 248 |
/// `val`, and sets the new value to the result. |
| 249 |
/// |
| 250 |
/// Returns the previous value. |
| 251 |
#[inline] |
| 252 |
pub fn fetch_or(&self, val: bool, order: Ordering) -> bool { |
| 253 |
unsafe { ops::atomic_or(self.v.get(), val, order) } |
| 254 |
} |
| 255 |
|
| 256 |
/// Logical "xor" with a boolean value. |
| 257 |
/// |
| 258 |
/// Performs a logical "xor" operation on the current value and the argument |
| 259 |
/// `val`, and sets the new value to the result. |
| 260 |
/// |
| 261 |
/// Returns the previous value. |
| 262 |
#[inline] |
| 263 |
pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool { |
| 264 |
unsafe { ops::atomic_xor(self.v.get(), val, order) } |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
macro_rules! atomic_ops_common { |
| 269 |
($($t:ty)*) => ($( |
| 270 |
impl Atomic<$t> { |
| 271 |
/// Add to the current value, returning the previous value. |
| 272 |
#[inline] |
| 273 |
pub fn fetch_add(&self, val: $t, order: Ordering) -> $t { |
| 274 |
unsafe { ops::atomic_add(self.v.get(), val, order) } |
| 275 |
} |
| 276 |
|
| 277 |
/// Subtract from the current value, returning the previous value. |
| 278 |
#[inline] |
| 279 |
pub fn fetch_sub(&self, val: $t, order: Ordering) -> $t { |
| 280 |
unsafe { ops::atomic_sub(self.v.get(), val, order) } |
| 281 |
} |
| 282 |
|
| 283 |
/// Bitwise and with the current value, returning the previous value. |
| 284 |
#[inline] |
| 285 |
pub fn fetch_and(&self, val: $t, order: Ordering) -> $t { |
| 286 |
unsafe { ops::atomic_and(self.v.get(), val, order) } |
| 287 |
} |
| 288 |
|
| 289 |
/// Bitwise or with the current value, returning the previous value. |
| 290 |
#[inline] |
| 291 |
pub fn fetch_or(&self, val: $t, order: Ordering) -> $t { |
| 292 |
unsafe { ops::atomic_or(self.v.get(), val, order) } |
| 293 |
} |
| 294 |
|
| 295 |
/// Bitwise xor with the current value, returning the previous value. |
| 296 |
#[inline] |
| 297 |
pub fn fetch_xor(&self, val: $t, order: Ordering) -> $t { |
| 298 |
unsafe { ops::atomic_xor(self.v.get(), val, order) } |
| 299 |
} |
| 300 |
} |
| 301 |
)*); |
| 302 |
} |
| 303 |
macro_rules! atomic_ops_signed { |
| 304 |
($($t:ty)*) => ( |
| 305 |
atomic_ops_common!{ $($t)* } |
| 306 |
$( |
| 307 |
impl Atomic<$t> { |
| 308 |
/// Minimum with the current value. |
| 309 |
#[inline] |
| 310 |
pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { |
| 311 |
unsafe { ops::atomic_min(self.v.get(), val, order) } |
| 312 |
} |
| 313 |
|
| 314 |
/// Maximum with the current value. |
| 315 |
#[inline] |
| 316 |
pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { |
| 317 |
unsafe { ops::atomic_max(self.v.get(), val, order) } |
| 318 |
} |
| 319 |
} |
| 320 |
)* |
| 321 |
); |
| 322 |
} |
| 323 |
macro_rules! atomic_ops_unsigned { |
| 324 |
($($t:ty)*) => ( |
| 325 |
atomic_ops_common!{ $($t)* } |
| 326 |
$( |
| 327 |
impl Atomic<$t> { |
| 328 |
/// Minimum with the current value. |
| 329 |
#[inline] |
| 330 |
pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { |
| 331 |
unsafe { ops::atomic_umin(self.v.get(), val, order) } |
| 332 |
} |
| 333 |
|
| 334 |
/// Maximum with the current value. |
| 335 |
#[inline] |
| 336 |
pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { |
| 337 |
unsafe { ops::atomic_umax(self.v.get(), val, order) } |
| 338 |
} |
| 339 |
} |
| 340 |
)* |
| 341 |
); |
| 342 |
} |
| 343 |
atomic_ops_signed!{ i8 i16 i32 i64 isize i128 } |
| 344 |
atomic_ops_unsigned!{ u8 u16 u32 u64 usize u128 } |
| 345 |
|
| 346 |
#[cfg(test)] |
| 347 |
mod tests { |
| 348 |
use core::mem; |
| 349 |
use Atomic; |
| 350 |
use Ordering::*; |
| 351 |
|
| 352 |
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)] |
| 353 |
struct Foo(u8, u8); |
| 354 |
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)] |
| 355 |
struct Bar(u64, u64); |
| 356 |
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)] |
| 357 |
struct Quux(u32); |
| 358 |
|
| 359 |
#[test] |
| 360 |
fn atomic_bool() { |
| 361 |
let a = Atomic::new(false); |
| 362 |
assert_eq!(Atomic::<bool>::is_lock_free(), cfg!(feature = "nightly")); |
| 363 |
assert_eq!(format!("{:?}", a), "Atomic(false)"); |
| 364 |
assert_eq!(a.load(SeqCst), false); |
| 365 |
a.store(true, SeqCst); |
| 366 |
assert_eq!(a.swap(false, SeqCst), true); |
| 367 |
assert_eq!(a.compare_exchange(true, false, SeqCst, SeqCst), Err(false)); |
| 368 |
assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Ok(false)); |
| 369 |
assert_eq!(a.fetch_and(false, SeqCst), true); |
| 370 |
assert_eq!(a.fetch_or(true, SeqCst), false); |
| 371 |
assert_eq!(a.fetch_xor(false, SeqCst), true); |
| 372 |
assert_eq!(a.load(SeqCst), true); |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn atomic_i8() { |
| 377 |
let a = Atomic::new(0i8); |
| 378 |
assert_eq!( |
| 379 |
Atomic::<i8>::is_lock_free(), |
| 380 |
cfg!(any( |
| 381 |
target_pointer_width = "8", |
| 382 |
all(feature = "nightly", target_has_atomic = "8") |
| 383 |
)) |
| 384 |
); |
| 385 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 386 |
assert_eq!(a.load(SeqCst), 0); |
| 387 |
a.store(1, SeqCst); |
| 388 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 389 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 390 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 391 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 392 |
// Make sure overflows are handled correctly |
| 393 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 394 |
assert_eq!(a.fetch_and(7, SeqCst), -74); |
| 395 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 396 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 397 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 398 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 399 |
assert_eq!(a.load(SeqCst), 30); |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn atomic_i16() { |
| 404 |
let a = Atomic::new(0i16); |
| 405 |
assert_eq!( |
| 406 |
Atomic::<i16>::is_lock_free(), |
| 407 |
cfg!(any( |
| 408 |
target_pointer_width = "16", |
| 409 |
all(feature = "nightly", target_has_atomic = "16") |
| 410 |
)) |
| 411 |
); |
| 412 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 413 |
assert_eq!(a.load(SeqCst), 0); |
| 414 |
a.store(1, SeqCst); |
| 415 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 416 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 417 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 418 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 419 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 420 |
assert_eq!(a.fetch_and(7, SeqCst), 182); |
| 421 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 422 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 423 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 424 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 425 |
assert_eq!(a.load(SeqCst), 30); |
| 426 |
} |
| 427 |
|
| 428 |
#[test] |
| 429 |
fn atomic_i32() { |
| 430 |
let a = Atomic::new(0i32); |
| 431 |
assert_eq!( |
| 432 |
Atomic::<i32>::is_lock_free(), |
| 433 |
cfg!(any( |
| 434 |
target_pointer_width = "32", |
| 435 |
all(feature = "nightly", target_has_atomic = "32") |
| 436 |
)) |
| 437 |
); |
| 438 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 439 |
assert_eq!(a.load(SeqCst), 0); |
| 440 |
a.store(1, SeqCst); |
| 441 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 442 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 443 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 444 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 445 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 446 |
assert_eq!(a.fetch_and(7, SeqCst), 182); |
| 447 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 448 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 449 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 450 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 451 |
assert_eq!(a.load(SeqCst), 30); |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
fn atomic_i64() { |
| 456 |
let a = Atomic::new(0i64); |
| 457 |
assert_eq!( |
| 458 |
Atomic::<i64>::is_lock_free(), |
| 459 |
cfg!(any( |
| 460 |
target_pointer_width = "64", |
| 461 |
all(feature = "nightly", target_has_atomic = "64") |
| 462 |
)) && mem::align_of::<i64>() == 8 |
| 463 |
); |
| 464 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 465 |
assert_eq!(a.load(SeqCst), 0); |
| 466 |
a.store(1, SeqCst); |
| 467 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 468 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 469 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 470 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 471 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 472 |
assert_eq!(a.fetch_and(7, SeqCst), 182); |
| 473 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 474 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 475 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 476 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 477 |
assert_eq!(a.load(SeqCst), 30); |
| 478 |
} |
| 479 |
|
| 480 |
#[test] |
| 481 |
fn atomic_i128() { |
| 482 |
let a = Atomic::new(0i128); |
| 483 |
assert_eq!( |
| 484 |
Atomic::<i128>::is_lock_free(), |
| 485 |
cfg!(any( |
| 486 |
target_pointer_width = "128", |
| 487 |
all(feature = "nightly", target_has_atomic = "128") |
| 488 |
)) |
| 489 |
); |
| 490 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 491 |
assert_eq!(a.load(SeqCst), 0); |
| 492 |
a.store(1, SeqCst); |
| 493 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 494 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 495 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 496 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 497 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 498 |
assert_eq!(a.fetch_and(7, SeqCst), 182); |
| 499 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 500 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 501 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 502 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 503 |
assert_eq!(a.load(SeqCst), 30); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn atomic_isize() { |
| 508 |
let a = Atomic::new(0isize); |
| 509 |
assert!(Atomic::<isize>::is_lock_free()); |
| 510 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 511 |
assert_eq!(a.load(SeqCst), 0); |
| 512 |
a.store(1, SeqCst); |
| 513 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 514 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 515 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 516 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 517 |
assert_eq!(a.fetch_sub(-56, SeqCst), 126); |
| 518 |
assert_eq!(a.fetch_and(7, SeqCst), 182); |
| 519 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 520 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 521 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 522 |
assert_eq!(a.fetch_max(-25, SeqCst), 30); |
| 523 |
assert_eq!(a.load(SeqCst), 30); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn atomic_u8() { |
| 528 |
let a = Atomic::new(0u8); |
| 529 |
assert_eq!( |
| 530 |
Atomic::<u8>::is_lock_free(), |
| 531 |
cfg!(any( |
| 532 |
target_pointer_width = "8", |
| 533 |
all(feature = "nightly", target_has_atomic = "8") |
| 534 |
)) |
| 535 |
); |
| 536 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 537 |
assert_eq!(a.load(SeqCst), 0); |
| 538 |
a.store(1, SeqCst); |
| 539 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 540 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 541 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 542 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 543 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 544 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 545 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 546 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 547 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 548 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 549 |
assert_eq!(a.load(SeqCst), 30); |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn atomic_u16() { |
| 554 |
let a = Atomic::new(0u16); |
| 555 |
assert_eq!( |
| 556 |
Atomic::<u16>::is_lock_free(), |
| 557 |
cfg!(any( |
| 558 |
target_pointer_width = "16", |
| 559 |
all(feature = "nightly", target_has_atomic = "16") |
| 560 |
)) |
| 561 |
); |
| 562 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 563 |
assert_eq!(a.load(SeqCst), 0); |
| 564 |
a.store(1, SeqCst); |
| 565 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 566 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 567 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 568 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 569 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 570 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 571 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 572 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 573 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 574 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 575 |
assert_eq!(a.load(SeqCst), 30); |
| 576 |
} |
| 577 |
|
| 578 |
#[test] |
| 579 |
fn atomic_u32() { |
| 580 |
let a = Atomic::new(0u32); |
| 581 |
assert_eq!( |
| 582 |
Atomic::<u32>::is_lock_free(), |
| 583 |
cfg!(any( |
| 584 |
target_pointer_width = "32", |
| 585 |
all(feature = "nightly", target_has_atomic = "32") |
| 586 |
)) |
| 587 |
); |
| 588 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 589 |
assert_eq!(a.load(SeqCst), 0); |
| 590 |
a.store(1, SeqCst); |
| 591 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 592 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 593 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 594 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 595 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 596 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 597 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 598 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 599 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 600 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 601 |
assert_eq!(a.load(SeqCst), 30); |
| 602 |
} |
| 603 |
|
| 604 |
#[test] |
| 605 |
fn atomic_u64() { |
| 606 |
let a = Atomic::new(0u64); |
| 607 |
assert_eq!( |
| 608 |
Atomic::<u64>::is_lock_free(), |
| 609 |
cfg!(any( |
| 610 |
target_pointer_width = "64", |
| 611 |
all(feature = "nightly", target_has_atomic = "64") |
| 612 |
)) && mem::align_of::<u64>() == 8 |
| 613 |
); |
| 614 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 615 |
assert_eq!(a.load(SeqCst), 0); |
| 616 |
a.store(1, SeqCst); |
| 617 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 618 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 619 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 620 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 621 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 622 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 623 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 624 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 625 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 626 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 627 |
assert_eq!(a.load(SeqCst), 30); |
| 628 |
} |
| 629 |
|
| 630 |
#[test] |
| 631 |
fn atomic_u128() { |
| 632 |
let a = Atomic::new(0u128); |
| 633 |
assert_eq!( |
| 634 |
Atomic::<u128>::is_lock_free(), |
| 635 |
cfg!(any( |
| 636 |
target_pointer_width = "128", |
| 637 |
all(feature = "nightly", target_has_atomic = "128") |
| 638 |
)) |
| 639 |
); |
| 640 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 641 |
assert_eq!(a.load(SeqCst), 0); |
| 642 |
a.store(1, SeqCst); |
| 643 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 644 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 645 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 646 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 647 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 648 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 649 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 650 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 651 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 652 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 653 |
assert_eq!(a.load(SeqCst), 30); |
| 654 |
} |
| 655 |
|
| 656 |
#[test] |
| 657 |
fn atomic_usize() { |
| 658 |
let a = Atomic::new(0usize); |
| 659 |
assert!(Atomic::<usize>::is_lock_free()); |
| 660 |
assert_eq!(format!("{:?}", a), "Atomic(0)"); |
| 661 |
assert_eq!(a.load(SeqCst), 0); |
| 662 |
a.store(1, SeqCst); |
| 663 |
assert_eq!(a.swap(2, SeqCst), 1); |
| 664 |
assert_eq!(a.compare_exchange(5, 45, SeqCst, SeqCst), Err(2)); |
| 665 |
assert_eq!(a.compare_exchange(2, 3, SeqCst, SeqCst), Ok(2)); |
| 666 |
assert_eq!(a.fetch_add(123, SeqCst), 3); |
| 667 |
assert_eq!(a.fetch_sub(56, SeqCst), 126); |
| 668 |
assert_eq!(a.fetch_and(7, SeqCst), 70); |
| 669 |
assert_eq!(a.fetch_or(64, SeqCst), 6); |
| 670 |
assert_eq!(a.fetch_xor(1, SeqCst), 70); |
| 671 |
assert_eq!(a.fetch_min(30, SeqCst), 71); |
| 672 |
assert_eq!(a.fetch_max(25, SeqCst), 30); |
| 673 |
assert_eq!(a.load(SeqCst), 30); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
fn atomic_foo() { |
| 678 |
let a = Atomic::default(); |
| 679 |
assert_eq!(Atomic::<Foo>::is_lock_free(), false); |
| 680 |
assert_eq!(format!("{:?}", a), "Atomic(Foo(0, 0))"); |
| 681 |
assert_eq!(a.load(SeqCst), Foo(0, 0)); |
| 682 |
a.store(Foo(1, 1), SeqCst); |
| 683 |
assert_eq!(a.swap(Foo(2, 2), SeqCst), Foo(1, 1)); |
| 684 |
assert_eq!( |
| 685 |
a.compare_exchange(Foo(5, 5), Foo(45, 45), SeqCst, SeqCst), |
| 686 |
Err(Foo(2, 2)) |
| 687 |
); |
| 688 |
assert_eq!( |
| 689 |
a.compare_exchange(Foo(2, 2), Foo(3, 3), SeqCst, SeqCst), |
| 690 |
Ok(Foo(2, 2)) |
| 691 |
); |
| 692 |
assert_eq!(a.load(SeqCst), Foo(3, 3)); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn atomic_bar() { |
| 697 |
let a = Atomic::default(); |
| 698 |
assert_eq!(Atomic::<Bar>::is_lock_free(), false); |
| 699 |
assert_eq!(format!("{:?}", a), "Atomic(Bar(0, 0))"); |
| 700 |
assert_eq!(a.load(SeqCst), Bar(0, 0)); |
| 701 |
a.store(Bar(1, 1), SeqCst); |
| 702 |
assert_eq!(a.swap(Bar(2, 2), SeqCst), Bar(1, 1)); |
| 703 |
assert_eq!( |
| 704 |
a.compare_exchange(Bar(5, 5), Bar(45, 45), SeqCst, SeqCst), |
| 705 |
Err(Bar(2, 2)) |
| 706 |
); |
| 707 |
assert_eq!( |
| 708 |
a.compare_exchange(Bar(2, 2), Bar(3, 3), SeqCst, SeqCst), |
| 709 |
Ok(Bar(2, 2)) |
| 710 |
); |
| 711 |
assert_eq!(a.load(SeqCst), Bar(3, 3)); |
| 712 |
} |
| 713 |
|
| 714 |
#[test] |
| 715 |
fn atomic_quxx() { |
| 716 |
let a = Atomic::default(); |
| 717 |
assert_eq!( |
| 718 |
Atomic::<Quux>::is_lock_free(), |
| 719 |
cfg!(any(feature = "nightly", target_pointer_width = "32")) |
| 720 |
); |
| 721 |
assert_eq!(format!("{:?}", a), "Atomic(Quux(0))"); |
| 722 |
assert_eq!(a.load(SeqCst), Quux(0)); |
| 723 |
a.store(Quux(1), SeqCst); |
| 724 |
assert_eq!(a.swap(Quux(2), SeqCst), Quux(1)); |
| 725 |
assert_eq!( |
| 726 |
a.compare_exchange(Quux(5), Quux(45), SeqCst, SeqCst), |
| 727 |
Err(Quux(2)) |
| 728 |
); |
| 729 |
assert_eq!( |
| 730 |
a.compare_exchange(Quux(2), Quux(3), SeqCst, SeqCst), |
| 731 |
Ok(Quux(2)) |
| 732 |
); |
| 733 |
assert_eq!(a.load(SeqCst), Quux(3)); |
| 734 |
} |
| 735 |
} |