1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
mod compare; mod guard; mod store; use core::fmt; use core::marker::PhantomData; use core::sync::atomic::Ordering; use typenum::Unsigned; use crate::internal::{Compare, GuardRef, Internal, Store}; use crate::leak::Leaking; use crate::pointer::{AtomicMarkedPtr, Marked, MarkedNonNull, MarkedPointer, MarkedPtr}; use crate::{AcquireResult, NotEqualError, Owned, Reclaim, Shared, Unlinked, Unprotected}; //////////////////////////////////////////////////////////////////////////////////////////////////// // Atomic //////////////////////////////////////////////////////////////////////////////////////////////////// /// An atomic markable pointer type to an owned heap allocated value similar to /// [`AtomicPtr`](core::sync::atomic::AtomicPtr). /// /// The `Atomic` type has similarities to [`Option<Box>`][Option], as it is a /// pointer that is either `null` or otherwise must point to a valid, heap /// allocated value. /// Note, that the type does not implement the [`Drop`](core::ops::Drop) trait, /// meaning it does not automatically take care of memory de-allocation when it /// goes out of scope. /// Use the [`take`][Atomic::take] method to extract an (optional) [`Owned`] /// value, which *does* correctly deallocate memory when it goes out of scope. pub struct Atomic<T, R, N> { inner: AtomicMarkedPtr<T, N>, _marker: PhantomData<(T, R)>, } unsafe impl<T, R: Reclaim, N: Unsigned> Send for Atomic<T, R, N> where T: Send + Sync {} unsafe impl<T, R: Reclaim, N: Unsigned> Sync for Atomic<T, R, N> where T: Send + Sync {} impl<T, R, N> Atomic<T, R, N> { /// Creates a new `null` pointer. #[inline] pub const fn null() -> Self { Self { inner: AtomicMarkedPtr::null(), _marker: PhantomData } } /// Gets a reference to the underlying (raw) atomic markable pointer. #[inline] pub const fn as_raw(&self) -> &AtomicMarkedPtr<T, N> { &self.inner } } impl<T, R: Reclaim, N: Unsigned> Atomic<T, R, N> { /// Allocates a new [`Owned`] containing the given `val` and immediately /// storing it an `Atomic`. #[inline] pub fn new(val: T) -> Self { Self::from(Owned::from(val)) } /// Creates a new [`Atomic`] from the given `ptr`. /// /// # Safety /// /// The given `ptr` argument must be a pointer to a valid heap allocated /// instance of `T` that was allocated as part of a [`Record`][crate::Record], /// e.g. through an [`Owned`]. /// The same pointer should also not be used to create more than one /// [`Atomic`]s. #[inline] pub unsafe fn from_raw(ptr: MarkedPtr<T, N>) -> Self { Self { inner: AtomicMarkedPtr::new(ptr), _marker: PhantomData } } /// Loads a raw marked value from the pointer. /// /// `load_raw` takes an [`Ordering`][ordering] argument, which describes the /// memory ordering of this operation. /// /// # Panics /// /// Panics if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel /// /// # Example /// /// Commonly, this is likely going to be used in conjunction with /// [`load_if_equal`][Atomic::load_if_equal] or /// [`acquire_if_equal`][Protect::acquire_if_equal]. /// /// ``` /// use std::sync::atomic::Ordering::Relaxed; /// /// use reclaim::typenum::U0; /// use reclaim::leak::Guard; /// /// type Atomic<T> = reclaim::leak::Atomic<T, U0>; /// /// let atomic = Atomic::new("string"); /// let guard = &Guard::new(); /// /// let ptr = atomic.load_raw(Relaxed); /// let res = atomic.load_if_equal(ptr, Relaxed, guard); /// /// assert!(res.is_ok()); /// # assert_eq!(&"string", &*res.unwrap().unwrap()); /// ``` #[inline] pub fn load_raw(&self, order: Ordering) -> MarkedPtr<T, N> { self.inner.load(order) } /// Loads an optional [`Unprotected`] reference from the `Atomic`. /// /// The returned reference is explicitly **not** protected from reclamation, /// meaning another thread could free the value's memory at any time. /// /// This method is similar to [`load_raw`][Atomic::load_raw], but the /// resulting [`Unprotected`] type has stronger guarantees than a raw /// [`MarkedPtr`]. /// It can be useful to load an unprotected pointer if that pointer does not /// need to be de-referenced, but is only used to reinsert it in a different /// spot, which is e.g. done when removing a value from a linked list. /// /// # Panics /// /// Panics if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_unprotected(&self, order: Ordering) -> Option<Unprotected<T, R, N>> { self.load_marked_unprotected(order).value() } /// Loads an [`Unprotected`] reference wrapped in a [`Marked`] from the /// `Atomic`. /// /// The returned reference is explicitly **not** protected from reclamation, /// meaning another thread could free the value's memory at any time. /// /// This method is similar to [`load_raw`][Atomic::load_raw], but the /// resulting [`Unprotected`] type has stronger guarantees than a raw /// [`MarkedPtr`]. /// It can be useful to load an unprotected pointer if that pointer does not /// need to be de-referenced, but is only used to reinsert it in a different /// spot, which is e.g. done when removing a value from a linked list. /// /// # Panics /// /// Panics if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_marked_unprotected(&self, order: Ordering) -> Marked<Unprotected<T, R, N>> { MarkedNonNull::new(self.inner.load(order)) .map(|ptr| Unprotected { inner: ptr, _marker: PhantomData }) } /// Loads a value from the pointer and uses `guard` to protect it. /// /// If the loaded value is non-null, the value is guaranteed to be protected /// from reclamation during the lifetime of `guard`. /// /// `load` takes an [`Ordering`][ordering] argument, which describes the /// memory ordering of this operation. /// /// # Panics /// /// *May* panic if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load<'g>( &self, order: Ordering, guard: impl GuardRef<'g, Reclaimer = R>, ) -> Option<Shared<'g, T, R, N>> { guard.load_protected(self, order).value() } /// Loads a value from the pointer and uses `guard` to protect it, but only /// if the loaded value equals `expected`. /// /// If the loaded value is non-null, the value is guaranteed to be protected /// from reclamation during the lifetime of `guard`. /// /// `load_if_equal` takes an [`Ordering`][ordering] argument, which /// describes the memory ordering of this operation. /// /// # Panics /// /// *May* panic if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_if_equal<'g>( &self, expected: MarkedPtr<T, N>, order: Ordering, guard: impl GuardRef<'g, Reclaimer = R>, ) -> Result<Option<Shared<'g, T, R, N>>, NotEqualError> { guard.load_protected_if_equal(self, expected, order).map(Marked::value) } /// Loads a value from the pointer and uses `guard` to protect it. /// The (optional) protected [`Shared`] value is wrapped in a [`Marked]. /// /// If the loaded value is non-null, the value is guaranteed to be protected /// from reclamation during the lifetime of `guard`. /// /// The primary difference to [`load`][Atomic::load] is, that the returned /// [`Marked`] type is additionally able to represent marked `null` /// pointers. /// /// `load_marked` takes an [`Ordering`][ordering] argument, which describes /// the memory ordering of this operation. /// /// # Panics /// /// *May* panic if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_marked<'g>( &self, order: Ordering, guard: impl GuardRef<'g, Reclaimer = R>, ) -> Marked<Shared<'g, T, R, N>> { guard.load_protected(self, order) } /// Loads a value from the pointer and uses `guard` to protect it, but only /// if the loaded value equals `expected`. /// The (optional) protected [`Shared`] value is wrapped in a [`Marked]. /// /// If the loaded value is non-null, the value is guaranteed to be protected /// from reclamation during the lifetime of `guard`. /// /// The primary difference to [`load_if_equal`][Atomic::load_if_equal] is, /// that the returned [`Marked`] type is additionally able to represent /// marked `null` pointers. /// /// `load_marked_if_equal` takes an [`Ordering`][ordering] argument, which /// describes the memory ordering of this operation. /// /// # Panics /// /// *May* panic if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_marked_if_equal<'g>( &self, expected: MarkedPtr<T, N>, order: Ordering, guard: impl GuardRef<'g, Reclaimer = R>, ) -> AcquireResult<'g, T, R, N> { guard.load_protected_if_equal(self, expected, order) } /// Stores either `null` or a valid pointer to an owned heap allocated value /// into the pointer. /// /// Note, that overwriting a non-null value through `store` will very likely /// lead to memory leaks, since instances of [`Atomic`] will most commonly /// be associated wit some kind of uniqueness invariants in order to be sound. /// /// `store` takes an [`Ordering`][ordering] argument, which /// describes the memory ordering of this operation. /// /// # Panics /// /// Panics if `order` is [`Acquire`][acquire] or [`AcqRel`][acq_rel] /// /// [ordering]: core::sync::atomic::Ordering /// [acquire]: core::sync::atomic::Ordering::Acquire /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn store(&self, ptr: impl Store<Item = T, MarkBits = N, Reclaimer = R>, order: Ordering) { self.inner.store(MarkedPointer::into_marked_ptr(ptr), order); } /// Stores either `null` or a valid pointer to an owned heap allocated value /// into the pointer, returning the previous value. /// /// The returned value can be safely reclaimed as long as the *uniqueness* /// invariant is maintained. /// /// `swap` takes an [`Ordering`][ordering] argument which describes the memory /// ordering of this operation. All ordering modes are possible. Note that using /// [`Acquire`][acquire] makes the store part of this operation [`Relaxed`][relaxed], /// and using [`Release`][release] makes the load part [`Relaxed`][relaxed]. /// /// [ordering]: core::sync::atomic::Ordering /// [relaxed]: core::sync::atomic::Ordering::Relaxed /// [acquire]: core::sync::atomic::Ordering::Acquire /// [release]: core::sync::atomic::Ordering::Release #[inline] pub fn swap( &self, ptr: impl Store<Item = T, Reclaimer = R, MarkBits = N>, order: Ordering, ) -> Option<Unlinked<T, R, N>> { let res = self.inner.swap(MarkedPointer::into_marked_ptr(ptr), order); // this is safe because the pointer is no longer accessible by other threads // (there can still be outstanding references that were loaded before the swap) unsafe { Option::from_marked_ptr(res) } } /// Stores a value (either null or valid) into the pointer if the current /// value is the same as `current`. /// /// The return value is a result indicating whether the `new` value was /// written and containing the previous and now unlinked value. /// On success this value is guaranteed to be equal to `current` and can be /// safely reclaimed as long as the *uniqueness* invariant is maintained. /// On failure, a [struct](CompareExchangeFailure) is returned that contains /// both the actual value and the value that was previously attempted to be /// inserted (`new`). /// This is necessary, because it is possible to attempt insertion of /// move-only types such as [`Owned`] or [`Unlinked`], which would otherwise /// be irretrievably lost when the `compare_exchange` fails. /// The actually loaded value is [`Unprotected`]. /// /// `compare_exchange` takes two [`Ordering`][ordering] arguments to /// describe the memory ordering of this operation. /// The first describes the required ordering if the operation succeeds /// while the second describes the required ordering when the operation /// fails. /// Using [`Acquire`][acquire] as success ordering makes the store part of /// this operation [`Relaxed`][relaxed], and using [`Release`][release] /// makes the successful load [`Relaxed`][relaxed]. /// The failure ordering can only be [`SeqCst`][seq_cst], /// [`Acquire`][acquire] or [`Relaxed`][relaxed] and must be equivalent to /// or weaker than the success ordering. /// /// [ordering]: core::sync::atomic::Ordering /// [relaxed]: core::sync::atomic::Ordering::Relaxed /// [acquire]: core::sync::atomic::Ordering::Acquire /// [release]: core::sync::atomic::Ordering::Release /// [seq_cst]: core::sync::atomic::Ordering::SeqCst #[inline] pub fn compare_exchange<C, S>( &self, current: C, new: S, success: Ordering, failure: Ordering, ) -> Result<C::Unlinked, CompareExchangeFailure<T, R, S, N>> where C: Compare<Item = T, MarkBits = N, Reclaimer = R>, S: Store<Item = T, MarkBits = N, Reclaimer = R>, { let current = MarkedPointer::into_marked_ptr(current); let new = MarkedPointer::into_marked_ptr(new); self.inner .compare_exchange(current, new, success, failure) .map(|ptr| unsafe { C::Unlinked::from_marked_ptr(ptr) }) .map_err(|ptr| CompareExchangeFailure { loaded: unsafe { Option::from_marked_ptr(ptr) }, input: unsafe { S::from_marked_ptr(new) }, _marker: PhantomData, }) } /// Stores a value (either null or valid) into the pointer if the current /// value is the same as `current`. /// /// Unlike [`compare_exchange`](Atomic::compare_exchange), this function is /// allowed to spuriously fail even when the comparision succeeds, which can /// result in more efficient code on some platforms. /// The return value is a result indicating whether the `new` value was /// written and containing the previous and now unlinked value. /// On success this value is guaranteed to be equal to `current` and can be /// safely reclaimed as long as the *uniqueness* invariant is maintained. /// On failure, a [struct](CompareExchangeFailure) is returned that contains /// both the actual value and the value that was previously attempted to be /// inserted (`new`). /// This is necessary, because it is possible to attempt insertion of /// move-only types such as [`Owned`] or [`Unlinked`], which would otherwise /// be irretrievably lost when the `compare_exchange` fails. /// The actually loaded value is [`Unprotected`]. /// /// `compare_exchange` takes two [`Ordering`][ordering] arguments to /// describe the memory ordering of this operation. /// The first describes the required ordering if the operation succeeds /// while the second describes the required ordering when the operation /// fails. /// Using [`Acquire`][acquire] as success ordering makes the store part of /// this operation [`Relaxed`][relaxed], and using [`Release`][release] /// makes the successful load [`Relaxed`][relaxed]. /// The failure ordering can only be [`SeqCst`][seq_cst], /// [`Acquire`][acquire] or [`Relaxed`][relaxed] and must be equivalent to /// or weaker than the success ordering. /// /// [ordering]: core::sync::atomic::Ordering /// [relaxed]: core::sync::atomic::Ordering::Relaxed /// [acquire]: core::sync::atomic::Ordering::Acquire /// [release]: core::sync::atomic::Ordering::Release /// [seq_cst]: core::sync::atomic::Ordering::SeqCst #[inline] pub fn compare_exchange_weak<C, S>( &self, current: C, new: S, success: Ordering, failure: Ordering, ) -> Result<C::Unlinked, CompareExchangeFailure<T, R, S, N>> where C: Compare<Item = T, MarkBits = N, Reclaimer = R>, S: Store<Item = T, MarkBits = N, Reclaimer = R>, { let current = MarkedPointer::into_marked_ptr(current); let new = MarkedPointer::into_marked_ptr(new); self.inner .compare_exchange_weak(current, new, success, failure) .map(|ptr| unsafe { C::Unlinked::from_marked_ptr(ptr) }) .map_err(|ptr| CompareExchangeFailure { loaded: unsafe { Option::from_marked_ptr(ptr) }, input: unsafe { S::from_marked_ptr(new) }, _marker: PhantomData, }) } /// Takes the value out of the pointer as an optional [`Owned`], leaving a /// `null` pointer in its place. /// /// This is similar to [`Option::take`][Option::take] and is useful for /// manually dropping the value pointed-to by the [`Atomic`], since /// [`Owned`] values behave like `Box` when they are dropped. #[inline] pub fn take(&mut self) -> Option<Owned<T, R, N>> { // this is safe because the mutable reference ensures no concurrent access is possible MarkedNonNull::new(self.inner.swap(MarkedPtr::null(), Ordering::Relaxed)) .map(|ptr| unsafe { Owned::from_marked_non_null(ptr) }) .value() } } impl<T, N: Unsigned> Atomic<T, Leaking, N> { /// Loads an optional [`Shared`] reference from the `Atomic`. /// /// Since [`Leaking`] never frees memory of retired records, this is always /// safe even without any guards. /// /// # Panics /// /// Panics if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_shared(&self, order: Ordering) -> Option<Shared<T, Leaking, N>> { self.load_marked_shared(order).value() } /// Loads a [`Shared`] reference wrapped in a [`Marked`] from the `Atomic`. /// /// Since [`Leaking`] never frees memory of retired records, this is always /// safe even without any guards. /// /// # Panics /// /// Panics if `order` is [`Release`][release] or [`AcqRel`][acq_rel]. /// /// [ordering]: core::sync::atomic::Ordering /// [release]: core::sync::atomic::Ordering::Release /// [acq_rel]: core::sync::atomic::Ordering::AcqRel #[inline] pub fn load_marked_shared(&self, order: Ordering) -> Marked<Shared<T, Leaking, N>> { MarkedNonNull::new(self.inner.load(order)) .map(|ptr| Shared { inner: ptr, _marker: PhantomData }) } } //////////////////////////////////////////////////////////////////////////////////////////////////// // impl Default //////////////////////////////////////////////////////////////////////////////////////////////////// impl<T, R: Reclaim, N: Unsigned> Default for Atomic<T, R, N> { #[inline] fn default() -> Self { Self::null() } } //////////////////////////////////////////////////////////////////////////////////////////////////// // impl From //////////////////////////////////////////////////////////////////////////////////////////////////// impl<T, R: Reclaim, N: Unsigned> From<T> for Atomic<T, R, N> { #[inline] fn from(val: T) -> Self { Self::new(val) } } impl<T, R: Reclaim, N: Unsigned> From<Owned<T, R, N>> for Atomic<T, R, N> { #[inline] fn from(owned: Owned<T, R, N>) -> Self { Self { inner: AtomicMarkedPtr::from(Owned::into_marked_ptr(owned)), _marker: PhantomData } } } //////////////////////////////////////////////////////////////////////////////////////////////////// // impl Debug & Pointer //////////////////////////////////////////////////////////////////////////////////////////////////// impl<T, R: Reclaim, N: Unsigned> fmt::Debug for Atomic<T, R, N> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let (ptr, tag) = self.inner.load(Ordering::SeqCst).decompose(); f.debug_struct("Atomic").field("ptr", &ptr).field("tag", &tag).finish() } } impl<T, R: Reclaim, N: Unsigned> fmt::Pointer for Atomic<T, R, N> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&self.inner.load(Ordering::SeqCst), f) } } //////////////////////////////////////////////////////////////////////////////////////////////////// // impl Internal //////////////////////////////////////////////////////////////////////////////////////////////////// impl<T, R: Reclaim, N: Unsigned> Internal for Atomic<T, R, N> {} //////////////////////////////////////////////////////////////////////////////////////////////////// // CompareExchangeFailure //////////////////////////////////////////////////////////////////////////////////////////////////// /// The returned error type for a failed [`compare_exchange`](Atomic::compare_exchange) or /// [`compare_exchange_weak`](Atomic::compare_exchange_weak) operation. #[derive(Debug)] pub struct CompareExchangeFailure<T, R, S, N> where R: Reclaim, S: Store<Item = T, MarkBits = N, Reclaimer = R>, N: Unsigned, { /// The actually loaded value pub loaded: Option<Unprotected<T, R, N>>, /// The value with which the failed swap was attempted pub input: S, // prevents construction outside of the current module _marker: PhantomData<R>, }