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
//! [`Queue`] is a lock-free concurrent first-in-first-out container.
use super::ebr::{AtomicShared, Guard, Ptr, Shared, Tag};
use super::linked_list::{Entry, LinkedList};
use std::fmt::{self, Debug};
use std::iter::FusedIterator;
use std::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed};
/// [`Queue`] is a lock-free concurrent first-in-first-out container.
pub struct Queue<T> {
/// `oldest` points to the oldest entry in the [`Queue`].
oldest: AtomicShared<Entry<T>>,
/// `newest` *eventually* points to the newest entry in the [`Queue`].
newest: AtomicShared<Entry<T>>,
}
/// An iterator over the entries of a [`Queue`].
///
/// [`Iter`] reads the oldest entry first.
pub struct Iter<'g, T> {
current: Ptr<'g, Entry<T>>,
guard: &'g Guard,
}
impl<T: 'static> Queue<T> {
/// Pushes an instance of `T`.
///
/// Returns a [`Shared`] holding a strong reference to the newly pushed entry.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// assert_eq!(**queue.push(11), 11);
/// ```
#[inline]
pub fn push(&self, val: T) -> Shared<Entry<T>> {
match self.push_if_internal(val, |_| true, &Guard::new()) {
Ok(entry) => entry,
Err(_) => {
unreachable!();
}
}
}
/// Pushes an instance of `T` if the newest entry satisfies the given condition.
///
/// # Errors
///
/// Returns an error along with the supplied instance if the condition is not met.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// queue.push(11);
///
/// assert!(queue.push_if(17, |e| e.map_or(false, |x| **x == 11)).is_ok());
/// assert!(queue.push_if(29, |e| e.map_or(false, |x| **x == 11)).is_err());
/// ```
#[inline]
pub fn push_if<F: FnMut(Option<&Entry<T>>) -> bool>(
&self,
val: T,
cond: F,
) -> Result<Shared<Entry<T>>, T> {
self.push_if_internal(val, cond, &Guard::new())
}
/// Returns a guarded reference to the oldest entry.
///
/// Returns `None` if the [`Queue`] is empty. The returned reference can survive as long as the
/// associated [`Guard`] is alive.
///
/// # Examples
///
/// ```
/// use scc::ebr::Guard;
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// assert!(queue.peek(&Guard::new()).is_none());
///
/// queue.push(37);
/// queue.push(3);
///
/// assert_eq!(**queue.peek(&Guard::new()).unwrap(), 37);
/// ```
#[inline]
pub fn peek<'g>(&self, guard: &'g Guard) -> Option<&'g Entry<T>> {
let mut current = self.oldest.load(Acquire, guard);
while let Some(oldest_entry) = current.as_ref() {
if oldest_entry.is_deleted(Relaxed) {
current = self.cleanup_oldest(guard);
continue;
}
return Some(oldest_entry);
}
None
}
}
impl<T> Queue<T> {
/// Pushes an instance of `T` without checking the lifetime of `T`.
///
/// Returns a [`Shared`] holding a strong reference to the newly pushed entry.
///
/// # Safety
///
/// `T::drop` can be run after the [`Queue`] is dropped, therefore it is safe only if `T::drop`
/// does not access short-lived data or [`std::mem::needs_drop`] is `false` for `T`,
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let hello = String::from("hello");
/// let queue: Queue<&str> = Queue::default();
///
/// assert_eq!(unsafe { **queue.push_unchecked(hello.as_str()) }, "hello");
/// ```
#[inline]
pub unsafe fn push_unchecked(&self, val: T) -> Shared<Entry<T>> {
match self.push_if_internal(val, |_| true, &Guard::new()) {
Ok(entry) => entry,
Err(_) => {
unreachable!();
}
}
}
/// Pushes an instance of `T` if the newest entry satisfies the given condition without
/// checking the lifetime of `T`
///
/// # Errors
///
/// Returns an error along with the supplied instance if the condition is not met.
///
/// # Safety
///
/// `T::drop` can be run after the [`Queue`] is dropped, therefore it is safe only if `T::drop`
/// does not access short-lived data or [`std::mem::needs_drop`] is `false` for `T`,
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let hello = String::from("hello");
/// let queue: Queue<&str> = Queue::default();
///
/// assert!(unsafe { queue.push_if_unchecked(hello.as_str(), |e| e.is_none()).is_ok() });
/// ```
#[inline]
pub unsafe fn push_if_unchecked<F: FnMut(Option<&Entry<T>>) -> bool>(
&self,
val: T,
cond: F,
) -> Result<Shared<Entry<T>>, T> {
self.push_if_internal(val, cond, &Guard::new())
}
/// Pops the oldest entry.
///
/// Returns `None` if the [`Queue`] is empty.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// queue.push(37);
/// queue.push(3);
/// queue.push(1);
///
/// assert_eq!(queue.pop().map(|e| **e), Some(37));
/// assert_eq!(queue.pop().map(|e| **e), Some(3));
/// assert_eq!(queue.pop().map(|e| **e), Some(1));
/// assert!(queue.pop().is_none());
/// ```
#[inline]
pub fn pop(&self) -> Option<Shared<Entry<T>>> {
match self.pop_if(|_| true) {
Ok(result) => result,
Err(_) => unreachable!(),
}
}
/// Pops the oldest entry if the entry satisfies the given condition.
///
/// Returns `None` if the [`Queue`] is empty.
///
/// # Errors
///
/// Returns an error along with the oldest entry if the given condition is not met.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// queue.push(3);
/// queue.push(1);
///
/// assert!(queue.pop_if(|v| **v == 1).is_err());
/// assert_eq!(queue.pop().map(|e| **e), Some(3));
/// assert_eq!(queue.pop_if(|v| **v == 1).ok().and_then(|e| e).map(|e| **e), Some(1));
/// ```
#[inline]
pub fn pop_if<F: FnMut(&Entry<T>) -> bool>(
&self,
mut cond: F,
) -> Result<Option<Shared<Entry<T>>>, Shared<Entry<T>>> {
let guard = Guard::new();
let mut current = self.oldest.load(Acquire, &guard);
while !current.is_null() {
if let Some(oldest_entry) = current.get_shared() {
if !oldest_entry.is_deleted(Relaxed) && !cond(&*oldest_entry) {
return Err(oldest_entry);
}
if oldest_entry.delete_self(Relaxed) {
self.cleanup_oldest(&guard);
return Ok(Some(oldest_entry));
}
}
current = self.cleanup_oldest(&guard);
}
Ok(None)
}
/// Peeks the oldest entry.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
///
/// assert!(queue.peek_with(|v| v.is_none()));
///
/// queue.push(37);
/// queue.push(3);
///
/// assert_eq!(queue.peek_with(|v| **v.unwrap()), 37);
/// ```
#[inline]
pub fn peek_with<R, F: FnOnce(Option<&Entry<T>>) -> R>(&self, reader: F) -> R {
let guard = Guard::new();
let mut current = self.oldest.load(Acquire, &guard);
while let Some(oldest_entry) = current.as_ref() {
if oldest_entry.is_deleted(Relaxed) {
current = self.cleanup_oldest(&guard);
continue;
}
return reader(Some(oldest_entry));
}
reader(None)
}
/// Returns the number of entries in the [`Queue`].
///
/// This method iterates over all the entries in the [`Queue`] to count them, therefore its
/// time complexity is `O(N)`.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
/// assert_eq!(queue.len(), 0);
///
/// queue.push(7);
/// queue.push(11);
/// assert_eq!(queue.len(), 2);
///
/// queue.pop();
/// queue.pop();
/// assert_eq!(queue.len(), 0);
/// ```
#[inline]
pub fn len(&self) -> usize {
self.iter(&Guard::new()).count()
}
/// Returns `true` if the [`Queue`] is empty.
///
/// # Examples
///
/// ```
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
/// assert!(queue.is_empty());
///
/// queue.push(7);
/// assert!(!queue.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.newest.is_null(Acquire)
}
/// Returns an [`Iter`].
///
/// # Examples
///
/// ```
/// use scc::ebr::Guard;
/// use scc::Queue;
///
/// let queue: Queue<usize> = Queue::default();
/// assert_eq!(queue.iter(&Guard::new()).count(), 0);
///
/// queue.push(7);
/// queue.push(11);
/// queue.push(17);
///
/// let guard = Guard::new();
/// let mut iter = queue.iter(&guard);
/// assert_eq!(*iter.next().unwrap(), 7);
/// assert_eq!(*iter.next().unwrap(), 11);
/// assert_eq!(*iter.next().unwrap(), 17);
/// assert!(iter.next().is_none());
/// ```
#[inline]
pub fn iter<'g>(&self, guard: &'g Guard) -> Iter<'g, T> {
Iter {
current: self.cleanup_oldest(guard),
guard,
}
}
/// Pushes an entry into the [`Queue`].
fn push_if_internal<F: FnMut(Option<&Entry<T>>) -> bool>(
&self,
val: T,
mut cond: F,
guard: &Guard,
) -> Result<Shared<Entry<T>>, T> {
let mut newest_ptr = self.newest.load(Acquire, guard);
if newest_ptr.is_null() {
// Traverse from the oldest.
newest_ptr = self.oldest.load(Acquire, guard);
}
newest_ptr = Self::traverse(newest_ptr, guard);
if !cond(newest_ptr.as_ref()) {
// The condition is not met.
return Err(val);
}
let mut new_entry = unsafe { Shared::new_unchecked(Entry::new(val)) };
loop {
let result = if let Some(newest_entry) = newest_ptr.as_ref() {
newest_entry.next().compare_exchange(
Ptr::null(),
(Some(new_entry.clone()), Tag::None),
AcqRel,
Acquire,
guard,
)
} else {
self.oldest.compare_exchange(
newest_ptr,
(Some(new_entry.clone()), Tag::None),
AcqRel,
Acquire,
guard,
)
};
match result {
Ok(_) => {
self.newest
.swap((Some(new_entry.clone()), Tag::None), AcqRel);
if self.oldest.is_null(Relaxed) {
// The `Queue` was emptied in the meantime.
self.newest.swap((None, Tag::None), Acquire);
}
return Ok(new_entry);
}
Err((_, actual_ptr)) => {
newest_ptr = if actual_ptr.tag() == Tag::First {
self.cleanup_oldest(guard)
} else if actual_ptr.is_null() {
self.oldest.load(Acquire, guard)
} else {
actual_ptr
};
newest_ptr = Self::traverse(newest_ptr, guard);
if !cond(newest_ptr.as_ref()) {
// The condition is not met.
break;
}
}
}
}
// Extract the instance from the temporary entry.
Err(unsafe { new_entry.get_mut().unwrap_unchecked().take_inner() })
}
/// Cleans up logically removed entries that are attached to `oldest`.
fn cleanup_oldest<'g>(&self, guard: &'g Guard) -> Ptr<'g, Entry<T>> {
let oldest_ptr = self.oldest.load(Acquire, guard);
if let Some(oldest_entry) = oldest_ptr.as_ref() {
if oldest_entry.is_deleted(Relaxed) {
match self.oldest.compare_exchange(
oldest_ptr,
(oldest_entry.next_shared(Acquire, guard), Tag::None),
AcqRel,
Acquire,
guard,
) {
Ok((_, new_ptr)) => {
if new_ptr.is_null() {
// Reset `newest`.
self.newest.swap((None, Tag::None), Acquire);
}
return new_ptr;
}
Err((_, actual_ptr)) => {
return actual_ptr;
}
}
}
}
oldest_ptr
}
/// Traverses the linked list to the end.
fn traverse<'g>(start: Ptr<'g, Entry<T>>, guard: &'g Guard) -> Ptr<'g, Entry<T>> {
let mut current = start;
while let Some(entry) = current.as_ref() {
let next = entry.next_ptr(Acquire, guard);
if next.is_null() {
break;
}
current = next;
}
current
}
}
impl<T: Clone> Clone for Queue<T> {
#[inline]
fn clone(&self) -> Self {
let self_clone = Self::default();
let guard = Guard::new();
let mut current = self.oldest.load(Acquire, &guard);
while let Some(entry) = current.as_ref() {
let next = entry.next_ptr(Acquire, &guard);
let _result = self_clone.push_if_internal((**entry).clone(), |_| true, &guard);
current = next;
}
self_clone
}
}
impl<T: Debug> Debug for Queue<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_set();
let guard = Guard::new();
let mut current = self.oldest.load(Acquire, &guard);
while let Some(entry) = current.as_ref() {
let next = entry.next_ptr(Acquire, &guard);
d.entry(entry);
current = next;
}
d.finish()
}
}
impl<T> Default for Queue<T> {
#[inline]
fn default() -> Self {
Self {
oldest: AtomicShared::default(),
newest: AtomicShared::default(),
}
}
}
impl<T> Drop for Queue<T> {
#[inline]
fn drop(&mut self) {
if !self.oldest.is_null(Relaxed) {
let guard = Guard::new();
let mut iter = self.iter(&guard);
while let Some(entry) = iter.current.as_ref() {
entry.delete_self(Relaxed);
iter.next();
}
}
}
}
impl<'g, T> FusedIterator for Iter<'g, T> {}
impl<'g, T> Iterator for Iter<'g, T> {
type Item = &'g T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(current) = self.current.as_ref() {
self.current = current.next_ptr(Acquire, self.guard);
Some(current)
} else {
None
}
}
}