Lines Matching defs:T

34 /// This has the same size, alignment and bit validity as the underlying type `T`. And it disables
46 /// `self.0` is a valid `T`.
52 pub struct Atomic<T: AtomicType>(AtomicRepr<T::Repr>);
54 // SAFETY: `Atomic<T>` is safe to transfer between execution contexts because of the safety
56 unsafe impl<T: AtomicType> Send for Atomic<T> {}
58 // SAFETY: `Atomic<T>` is safe to share among execution contexts because all accesses are atomic.
59 unsafe impl<T: AtomicType> Sync for Atomic<T> {}
65 /// `T` is round-trip transmutable to `U` if and only if both of these properties hold:
67 /// - Any valid bit pattern for `T` is also a valid bit pattern for `U`.
68 /// - Transmuting (e.g. using [`transmute()`]) a value of type `T` to `U` and then to `T` again
77 /// `!Send` (e.g. raw pointers and [`NonNull<T>`]) but requiring `unsafe` to do anything
82 /// [`transmute()`] is always sound between `U` and `T`) because of the support for atomic
120 /// [`NonNull<T>`]: core::ptr::NonNull
140 const fn into_repr<T: AtomicType>(v: T) -> T::Repr {
141 // SAFETY: Per the safety requirement of `AtomicType`, `T` is round-trip transmutable to
142 // `T::Repr`, therefore the transmute operation is sound.
148 /// `r` must be a valid bit pattern of `T`.
150 const unsafe fn from_repr<T: AtomicType>(r: T::Repr) -> T {
155 impl<T: AtomicType> Atomic<T> {
156 /// Creates a new atomic `T`.
157 pub const fn new(v: T) -> Self {
158 // INVARIANT: Per the safety requirement of `AtomicType`, `into_repr(v)` is a valid `T`.
162 /// Creates a reference to an atomic `T` from a pointer of `T`.
169 /// - `ptr` is aligned to `align_of::<T>()`.
217 pub unsafe fn from_ptr<'a>(ptr: *mut T) -> &'a Self {
218 // CAST: `T` and `Atomic<T>` have the same size, alignment and bit validity.
220 // live long enough. It's safe to return a `&Atomic<T>` because function safety requirement
225 /// Returns a pointer to the underlying atomic `T`.
231 /// The returned pointer is valid and properly aligned (i.e. aligned to [`align_of::<T>()`]).
234 /// [`align_of::<T>()`]: core::mem::align_of
235 pub const fn as_ptr(&self) -> *mut T {
237 // must be a valid and properly aligned pointer for `T::Repr`, and per the safety guarantee
238 // of `AtomicType`, it's a valid and properly aligned pointer of `T`.
242 /// Returns a mutable reference to the underlying atomic `T`.
244 /// This is safe because the mutable reference of the atomic `T` guarantees exclusive access.
256 pub fn get_mut(&mut self) -> &mut T {
257 // CAST: `T` and `T::Repr` has the same size and alignment per the safety requirement of
258 // `AtomicType`, and per the type invariants `self.0` is a valid `T`, therefore the casting
259 // result is a valid pointer of `T`.
266 impl<T: AtomicType> Atomic<T>
268 T::Repr: AtomicBasicOps,
270 /// Loads the value from the atomic `T`.
287 pub fn load<Ordering: ordering::AcquireOrRelaxed>(&self, _: Ordering) -> T {
290 OrderingType::Relaxed => T::Repr::atomic_read(&self.0),
291 OrderingType::Acquire => T::Repr::atomic_read_acquire(&self.0),
296 // SAFETY: `v` comes from reading `self.0`, which is a valid `T` per the type invariants.
300 /// Stores a value to the atomic `T`.
317 pub fn store<Ordering: ordering::ReleaseOrRelaxed>(&self, v: T, _: Ordering) {
320 // INVARIANT: `v` is a valid `T`, and is stored to `self.0` by `atomic_set*()`.
322 OrderingType::Relaxed => T::Repr::atomic_set(&self.0, v),
323 OrderingType::Release => T::Repr::atomic_set_release(&self.0, v),
329 impl<T: AtomicType + core::fmt::Debug> core::fmt::Debug for Atomic<T>
331 T::Repr: AtomicBasicOps,
338 impl<T: AtomicType> Atomic<T>
340 T::Repr: AtomicExchangeOps,
358 pub fn xchg<Ordering: ordering::Ordering>(&self, v: T, _: Ordering) -> T {
361 // INVARIANT: `self.0` is a valid `T` after `atomic_xchg*()` because `v` is transmutable to
362 // `T`.
365 OrderingType::Full => T::Repr::atomic_xchg(&self.0, v),
366 OrderingType::Acquire => T::Repr::atomic_xchg_acquire(&self.0, v),
367 OrderingType::Release => T::Repr::atomic_xchg_release(&self.0, v),
368 OrderingType::Relaxed => T::Repr::atomic_xchg_relaxed(&self.0, v),
372 // SAFETY: `ret` comes from reading `*self`, which is a valid `T` per type invariants.
431 mut old: T,
432 new: T,
434 ) -> Result<T, T> {
476 fn try_cmpxchg<Ordering: ordering::Ordering>(&self, old: &mut T, new: T, _: Ordering) -> bool {
480 // INVARIANT: `self.0` is a valid `T` after `atomic_try_cmpxchg*()` because `new` is
481 // transmutable to `T`.
484 OrderingType::Full => T::Repr::atomic_try_cmpxchg(&self.0, &mut tmp, new),
486 T::Repr::atomic_try_cmpxchg_acquire(&self.0, &mut tmp, new)
489 T::Repr::atomic_try_cmpxchg_release(&self.0, &mut tmp, new)
492 T::Repr::atomic_try_cmpxchg_relaxed(&self.0, &mut tmp, new)
497 // SAFETY: `tmp` comes from reading `*self`, which is a valid `T` per type invariants.
504 impl<T: AtomicType> Atomic<T>
506 T::Repr: AtomicArithmeticOps,
528 T: AtomicAdd<Rhs>,
530 let v = T::rhs_into_delta(v);
532 // INVARIANT: `self.0` is a valid `T` after `atomic_add()` due to safety requirement of
534 T::Repr::atomic_add(&self.0, v);
558 pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) -> T
560 T: AtomicAdd<Rhs>,
562 let v = T::rhs_into_delta(v);
564 // INVARIANT: `self.0` is a valid `T` after `atomic_fetch_add*()` due to safety requirement
568 OrderingType::Full => T::Repr::atomic_fetch_add(&self.0, v),
569 OrderingType::Acquire => T::Repr::atomic_fetch_add_acquire(&self.0, v),
570 OrderingType::Release => T::Repr::atomic_fetch_add_release(&self.0, v),
571 OrderingType::Relaxed => T::Repr::atomic_fetch_add_relaxed(&self.0, v),
575 // SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
600 pub fn fetch_sub<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) -> T
603 T: AtomicAdd<Rhs>,
605 let v = T::rhs_into_delta(v);
607 // INVARIANT: `self.0` is a valid `T` after `atomic_fetch_sub*()` due to safety requirement
611 OrderingType::Full => T::Repr::atomic_fetch_sub(&self.0, v),
612 OrderingType::Acquire => T::Repr::atomic_fetch_sub_acquire(&self.0, v),
613 OrderingType::Release => T::Repr::atomic_fetch_sub_release(&self.0, v),
614 OrderingType::Relaxed => T::Repr::atomic_fetch_sub_relaxed(&self.0, v),
618 // SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
758 /// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`.
762 pub unsafe fn atomic_load<T: AtomicType, Ordering: ordering::AcquireOrRelaxed>(
763 ptr: *mut T,
765 ) -> T
767 T::Repr: AtomicBasicOps,
770 // `align_of::<T>()`, and all concurrent stores from kernel are atomic, hence no data race per
785 /// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`.
789 pub unsafe fn atomic_store<T: AtomicType, Ordering: ordering::ReleaseOrRelaxed>(
790 ptr: *mut T,
791 v: T,
794 T::Repr: AtomicBasicOps,
797 // `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race
809 /// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`.
812 pub unsafe fn xchg<T: AtomicType, Ordering: ordering::Ordering>(
813 ptr: *mut T,
814 new: T,
816 ) -> T
818 T::Repr: AtomicExchangeOps,
821 // `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race
833 /// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`.
837 pub unsafe fn cmpxchg<T: AtomicType, Ordering: ordering::Ordering>(
838 ptr: *mut T,
839 old: T,
840 new: T,
842 ) -> Result<T, T>
844 T::Repr: AtomicExchangeOps,
847 // `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race