pub struct Atomic<T> { /* private fields */ }Expand description
An atomic pointer that can be safely shared between threads.
The pointer must be properly aligned. Since it is aligned, a tag can be stored into the unused least significant bits of the address.
Any method that loads the pointer must be passed a reference to a Scope.
Implementations§
Source§impl<T> Atomic<T>
impl<T> Atomic<T>
Sourcepub fn null() -> Self
pub fn null() -> Self
Returns a new null atomic pointer.
§Examples
use coco::epoch::Atomic;
let a = Atomic::<i32>::null();Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Allocates value on the heap and returns a new atomic pointer pointing to it.
§Examples
use coco::epoch::Atomic;
let a = Atomic::new(1234);Sourcepub fn from_owned(owned: Owned<T>) -> Self
pub fn from_owned(owned: Owned<T>) -> Self
Returns a new atomic pointer pointing to owned.
§Examples
use coco::epoch::{Atomic, Owned};
let a = Atomic::from_owned(Owned::new(1234));Sourcepub fn from_ptr(ptr: Ptr<'_, T>) -> Self
pub fn from_ptr(ptr: Ptr<'_, T>) -> Self
Returns a new atomic pointer pointing to ptr.
§Examples
use coco::epoch::{Atomic, Ptr};
let a = Atomic::from_ptr(Ptr::<i32>::null());Sourcepub fn store_owned(&self, new: Owned<T>, ord: Ordering)
pub fn store_owned(&self, new: Owned<T>, ord: Ordering)
Sourcepub fn swap<'scope>(
&self,
new: Ptr<'_, T>,
ord: Ordering,
_: &'scope Scope,
) -> Ptr<'scope, T>
pub fn swap<'scope>( &self, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>
Stores a Ptr into the atomic pointer, returning the previous Ptr.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Owned, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::new(1234);
epoch::pin(|scope| {
let p = a.swap(Ptr::null(), SeqCst, scope);
});Sourcepub fn compare_and_swap<'scope>(
&self,
current: Ptr<'_, T>,
new: Ptr<'_, T>,
ord: Ordering,
_: &'scope Scope,
) -> Result<(), Ptr<'scope, T>>
pub fn compare_and_swap<'scope>( &self, current: Ptr<'_, T>, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Result<(), Ptr<'scope, T>>
Stores new into the atomic pointer if the current value is the same as current.
The return value is a result indicating whether the new pointer was written. On failure the actual current value is returned.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::new(1234);
epoch::pin(|scope| {
let mut curr = a.load(SeqCst, scope);
let res = a.compare_and_swap(curr, Ptr::null(), SeqCst, scope);
});Sourcepub fn compare_and_swap_weak<'scope>(
&self,
current: Ptr<'_, T>,
new: Ptr<'_, T>,
ord: Ordering,
_: &'scope Scope,
) -> Result<(), Ptr<'scope, T>>
pub fn compare_and_swap_weak<'scope>( &self, current: Ptr<'_, T>, new: Ptr<'_, T>, ord: Ordering, _: &'scope Scope, ) -> Result<(), Ptr<'scope, T>>
Stores new into the atomic pointer if the current value is the same as current.
Unlike compare_and_swap, this method is allowed to spuriously fail even when
comparison succeeds, which can result in more efficient code on some platforms.
The return value is a result indicating whether the new pointer was written. On failure the
actual current value is returned.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::new(1234);
epoch::pin(|scope| {
let mut curr = a.load(SeqCst, scope);
loop {
match a.compare_and_swap_weak(curr, Ptr::null(), SeqCst, scope) {
Ok(()) => break,
Err(c) => curr = c,
}
}
});Sourcepub fn compare_and_swap_owned<'scope>(
&self,
current: Ptr<'_, T>,
new: Owned<T>,
ord: Ordering,
_: &'scope Scope,
) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>
pub fn compare_and_swap_owned<'scope>( &self, current: Ptr<'_, T>, new: Owned<T>, ord: Ordering, _: &'scope Scope, ) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>
Stores new into the atomic pointer if the current value is the same as current.
The return value is a result indicating whether the new pointer was written. On success the
pointer that was written is returned. On failure new and the actual current value are
returned.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::new(1234);
epoch::pin(|scope| {
let mut curr = a.load(SeqCst, scope);
let res = a.compare_and_swap_owned(curr, Owned::new(5678), SeqCst, scope);
});Sourcepub fn compare_and_swap_weak_owned<'scope>(
&self,
current: Ptr<'_, T>,
new: Owned<T>,
ord: Ordering,
_: &'scope Scope,
) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>
pub fn compare_and_swap_weak_owned<'scope>( &self, current: Ptr<'_, T>, new: Owned<T>, ord: Ordering, _: &'scope Scope, ) -> Result<Ptr<'scope, T>, (Ptr<'scope, T>, Owned<T>)>
Stores new into the atomic pointer if the current value is the same as current.
Unlike compare_and_swap_owned, this method is allowed to spuriously fail even when
comparison succeeds, which can result in more efficient code on some platforms.
The return value is a result indicating whether the new pointer was written. On success the
pointer that was written is returned. On failure new and the actual current value are
returned.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Owned};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::new(1234);
epoch::pin(|scope| {
let mut new = Owned::new(5678);
let mut ptr = a.load(SeqCst, scope);
loop {
match a.compare_and_swap_weak_owned(ptr, new, SeqCst, scope) {
Ok(p) => {
ptr = p;
break;
}
Err((p, n)) => {
ptr = p;
new = n;
}
}
}
});Sourcepub fn fetch_and<'scope>(
&self,
val: usize,
ord: Ordering,
_: &'scope Scope,
) -> Ptr<'scope, T>
pub fn fetch_and<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>
Bitwise “and” with the current tag.
Performs a bitwise “and” operation on the current tag and the argument val, and sets the
new tag to the result. Returns the previous pointer.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(3));
epoch::pin(|scope| {
assert_eq!(a.fetch_and(2, SeqCst, scope).tag(), 3);
assert_eq!(a.load(SeqCst, scope).tag(), 2);
});Sourcepub fn fetch_or<'scope>(
&self,
val: usize,
ord: Ordering,
_: &'scope Scope,
) -> Ptr<'scope, T>
pub fn fetch_or<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>
Bitwise “or” with the current tag.
Performs a bitwise “or” operation on the current tag and the argument val, and sets the
new tag to the result. Returns the previous pointer.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(1));
epoch::pin(|scope| {
assert_eq!(a.fetch_or(2, SeqCst, scope).tag(), 1);
assert_eq!(a.load(SeqCst, scope).tag(), 3);
});Sourcepub fn fetch_xor<'scope>(
&self,
val: usize,
ord: Ordering,
_: &'scope Scope,
) -> Ptr<'scope, T>
pub fn fetch_xor<'scope>( &self, val: usize, ord: Ordering, _: &'scope Scope, ) -> Ptr<'scope, T>
Bitwise “xor” with the current tag.
Performs a bitwise “xor” operation on the current tag and the argument val, and sets the
new tag to the result. Returns the previous pointer.
This method takes an Ordering argument which describes the memory ordering of this
operation.
§Examples
use coco::epoch::{self, Atomic, Ptr};
use std::sync::atomic::Ordering::SeqCst;
let a = Atomic::<i32>::from_ptr(Ptr::null().with_tag(1));
epoch::pin(|scope| {
assert_eq!(a.fetch_xor(3, SeqCst, scope).tag(), 1);
assert_eq!(a.load(SeqCst, scope).tag(), 2);
});Trait Implementations§
impl<T: Send + Sync> Send for Atomic<T>
impl<T: Send + Sync> Sync for Atomic<T>
Auto Trait Implementations§
impl<T> !Freeze for Atomic<T>
impl<T> RefUnwindSafe for Atomic<T>where
T: RefUnwindSafe,
impl<T> Unpin for Atomic<T>
impl<T> UnwindSafe for Atomic<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more