scc/wait_queue.rs
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
use crate::ebr::Guard;
use crate::maybe_std::yield_now;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::{AcqRel, Relaxed};
use std::sync::{Condvar, Mutex};
use std::task::{Context, Poll, Waker};
use std::thread;
/// `ASYNC` is a flag indicating that the referenced instance corresponds to an asynchronous
/// operation.
const ASYNC: usize = 1_usize;
/// [`WaitQueue`] implements an unfair wait queue.
///
/// The sole purpose of the data structure is to avoid busy-waiting. [`WaitQueue`] should always
/// protected by [`ebr`](crate::ebr).
#[derive(Debug, Default)]
pub(crate) struct WaitQueue {
/// Stores the pointer value of the actual wait queue entry and a flag indicating that the
/// entry is asynchronous.
wait_queue: AtomicUsize,
}
impl WaitQueue {
/// Waits for the condition to be met or signaled.
#[inline]
pub(crate) fn wait_sync<T, F: FnOnce() -> Result<T, ()>>(&self, f: F) -> Result<T, ()> {
if cfg!(miri) || cfg!(feature = "loom") {
yield_now();
return f();
}
let mut current = self.wait_queue.load(Relaxed);
let mut entry = SyncWait::new(current);
let mut entry_mut = Pin::new(&mut entry);
while let Err(actual) = self.wait_queue.compare_exchange_weak(
current,
entry_mut.as_mut().get_mut() as *mut SyncWait as usize,
AcqRel,
Relaxed,
) {
current = actual;
entry_mut.next.store(current, Relaxed);
}
// Execute the closure.
let result = f();
if result.is_ok() {
self.signal();
}
entry_mut.wait();
result
}
/// Pushes an [`AsyncWait`] into the [`WaitQueue`].
///
/// If it happens to acquire the desired resource, it returns an `Ok(T)` after waking up all
/// the entries in the [`WaitQueue`].
#[inline]
pub(crate) fn push_async_entry<T, F: FnOnce() -> Result<T, ()>>(
&self,
async_wait: &mut AsyncWait,
f: F,
) -> Result<T, ()> {
debug_assert!(async_wait.mutex.is_none());
let mut current = self.wait_queue.load(Relaxed);
let wait_queue_ref: &WaitQueue = self;
async_wait.next.store(current, Relaxed);
async_wait.mutex.replace(Mutex::new((
Some(unsafe { std::mem::transmute::<&WaitQueue, &WaitQueue>(wait_queue_ref) }),
None,
)));
while let Err(actual) = self.wait_queue.compare_exchange_weak(
current,
(async_wait as *mut AsyncWait as usize) | ASYNC,
AcqRel,
Relaxed,
) {
current = actual;
async_wait.next.store(current, Relaxed);
}
// Execute the closure.
if let Ok(result) = f() {
self.signal();
if async_wait.try_wait() {
async_wait.mutex.take();
return Ok(result);
}
// Another task is waking up `async_wait`: dispose of `result` which is holding the
// desired resource.
}
// The caller has to await.
Err(())
}
/// Signals the threads in the wait queue.
#[inline]
pub(crate) fn signal(&self) {
if cfg!(miri) || cfg!(feature = "loom") {
return;
}
let mut current = self.wait_queue.swap(0, AcqRel);
// Flip the queue to prioritize oldest entries.
let mut prev = 0;
while (current & (!ASYNC)) != 0 {
current = if (current & ASYNC) == 0 {
// Synchronous.
let entry_ptr = current as *const SyncWait;
let next = unsafe {
let next = (*entry_ptr).next.load(Relaxed);
(*entry_ptr).next.store(prev, Relaxed);
next
};
prev = current;
next
} else {
// Asynchronous.
let entry_ptr = (current & (!ASYNC)) as *const AsyncWait;
let next = unsafe {
let next = (*entry_ptr).next.load(Relaxed);
(*entry_ptr).next.store(prev, Relaxed);
next
};
prev = current;
next
};
}
// Wake up all the tasks.
current = prev;
while (current & (!ASYNC)) != 0 {
current = if (current & ASYNC) == 0 {
// Synchronous.
let entry_ptr = current as *const SyncWait;
unsafe {
let next = (*entry_ptr).next.load(Relaxed);
(*entry_ptr).signal();
next
}
} else {
// Asynchronous.
let entry_ptr = (current & (!ASYNC)) as *const AsyncWait;
unsafe {
let next = (*entry_ptr).next.load(Relaxed);
(*entry_ptr).signal();
next
}
};
}
}
}
/// [`DeriveAsyncWait`] derives a mutable reference to [`AsyncWait`].
pub(crate) trait DeriveAsyncWait {
/// Returns a mutable reference to [`AsyncWait`] if available.
fn derive(&mut self) -> Option<&mut AsyncWait>;
}
impl DeriveAsyncWait for Pin<&mut AsyncWait> {
#[inline]
fn derive(&mut self) -> Option<&mut AsyncWait> {
unsafe { Some(self.as_mut().get_unchecked_mut()) }
}
}
impl DeriveAsyncWait for () {
#[inline]
fn derive(&mut self) -> Option<&mut AsyncWait> {
None
}
}
/// [`AsyncWait`] is inserted into [`WaitQueue`] for the caller to await until woken up.
///
/// [`AsyncWait`] has to be pinned outside in order to use it correctly. The type is `Unpin`,
/// therefore it can be moved, however the [`DeriveAsyncWait`] trait forces [`AsyncWait`] to be
/// pinned.
#[derive(Debug, Default)]
pub(crate) struct AsyncWait {
next: AtomicUsize,
mutex: Option<Mutex<(Option<&'static WaitQueue>, Option<Waker>)>>,
}
impl AsyncWait {
/// Sends a signal.
fn signal(&self) {
if let Some(mutex) = self.mutex.as_ref() {
if let Ok(mut locked) = mutex.lock() {
// Disassociate itself from the `WaitQueue`.
locked.0.take();
if let Some(waker) = locked.1.take() {
waker.wake();
}
}
} else {
unreachable!();
}
}
/// Tries to receive a signal.
fn try_wait(&self) -> bool {
if let Some(mutex) = self.mutex.as_ref() {
if let Ok(locked) = mutex.lock() {
if locked.0.is_none() {
// The wait queue entry is not associated with any `WaitQueue`.
return true;
}
}
}
false
}
/// Pulls `self` out of the [`WaitQueue`].
///
/// This method is only invoked when `self` is being dropped.
fn pull(&self) {
// The `WaitQueue` instance must be pinned in memory.
let _guard = Guard::new();
let wait_queue = if let Some(mutex) = self.mutex.as_ref() {
if let Ok(locked) = mutex.lock() {
locked.0
} else {
None
}
} else {
None
};
if let Some(wait_queue) = wait_queue {
wait_queue.signal();
// Data race with another thread.
// - Another thread pulls `self` from the `WaitQueue` to send a signal.
// - This thread completes `wait_queue.signal()` which does not contain `self`
// - This thread drops `self`.
// - The other thread reads `self`.
while !self.try_wait() {
thread::yield_now();
}
}
}
}
impl Drop for AsyncWait {
#[inline]
fn drop(&mut self) {
if self.mutex.is_some() {
self.pull();
};
}
}
impl Future for AsyncWait {
type Output = ();
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(mutex) = self.mutex.as_ref() {
if let Ok(mut locked) = mutex.lock() {
if locked.0.is_none() {
// The wait queue entry is not associated with any `WaitQueue`.
return Poll::Ready(());
}
locked.1.replace(cx.waker().clone());
}
Poll::Pending
} else {
Poll::Ready(())
}
}
}
/// [`SyncWait`] is inserted into [`WaitQueue`] for the caller to synchronously wait until
/// signaled.
#[derive(Debug)]
struct SyncWait {
next: AtomicUsize,
condvar: Condvar,
mutex: Mutex<bool>,
}
impl SyncWait {
/// Creates a new [`SyncWait`].
const fn new(next: usize) -> Self {
#[allow(clippy::mutex_atomic)]
Self {
next: AtomicUsize::new(next),
condvar: Condvar::new(),
mutex: Mutex::new(false),
}
}
/// Waits for a signal.
fn wait(&self) {
#[allow(clippy::mutex_atomic)]
let mut completed = unsafe { self.mutex.lock().unwrap_unchecked() };
while !*completed {
completed = unsafe { self.condvar.wait(completed).unwrap_unchecked() };
}
}
/// Sends a signal.
fn signal(&self) {
#[allow(clippy::mutex_atomic)]
let mut completed = unsafe { self.mutex.lock().unwrap_unchecked() };
*completed = true;
self.condvar.notify_one();
}
}
#[cfg(not(feature = "loom"))]
#[cfg(test)]
mod test {
use super::*;
use std::sync::atomic::Ordering::Release;
use std::sync::Arc;
use std::sync::Barrier;
use std::thread::yield_now;
#[cfg_attr(miri, ignore)]
#[test]
fn wait_queue_sync() {
let num_tasks = 8;
let barrier = Arc::new(Barrier::new(num_tasks + 1));
let wait_queue = Arc::new(WaitQueue::default());
let data = Arc::new(AtomicUsize::new(0));
let mut task_handles = Vec::with_capacity(num_tasks);
for task_id in 1..=num_tasks {
let barrier_clone = barrier.clone();
let wait_queue_clone = wait_queue.clone();
let data_clone = data.clone();
task_handles.push(std::thread::spawn(move || {
barrier_clone.wait();
while wait_queue_clone
.wait_sync(|| {
if data_clone
.compare_exchange(task_id, task_id + 1, Relaxed, Relaxed)
.is_ok()
{
Ok(())
} else {
Err(())
}
})
.is_err()
{
yield_now();
}
wait_queue_clone.signal();
}));
}
barrier.wait();
data.fetch_add(1, Release);
wait_queue.signal();
task_handles
.into_iter()
.for_each(|t| assert!(t.join().is_ok()));
}
#[cfg_attr(miri, ignore)]
#[tokio::test(flavor = "multi_thread", worker_threads = 16)]
async fn wait_queue_async() {
let num_tasks = 8;
let barrier = Arc::new(tokio::sync::Barrier::new(num_tasks + 1));
let wait_queue = Arc::new(WaitQueue::default());
let data = Arc::new(AtomicUsize::new(0));
let mut task_handles = Vec::with_capacity(num_tasks);
for task_id in 1..=num_tasks {
let barrier_clone = barrier.clone();
let wait_queue_clone = wait_queue.clone();
let data_clone = data.clone();
task_handles.push(tokio::spawn(async move {
barrier_clone.wait().await;
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
while wait_queue_clone
.push_async_entry(&mut async_wait_pinned, || {
if data_clone
.compare_exchange(task_id, task_id + 1, Relaxed, Relaxed)
.is_ok()
{
Ok(())
} else {
Err(())
}
})
.is_err()
{
async_wait_pinned.as_mut().await;
if data_clone.load(Relaxed) > task_id {
// The operation was successful, but was signaled by another thread.
break;
}
async_wait_pinned.mutex.take();
}
wait_queue_clone.signal();
}));
}
barrier.wait().await;
data.fetch_add(1, Release);
wait_queue.signal();
for r in futures::future::join_all(task_handles).await {
assert!(r.is_ok());
}
}
#[cfg_attr(miri, ignore)]
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn wait_queue_async_drop() {
let num_tasks = 8;
let barrier = Arc::new(tokio::sync::Barrier::new(num_tasks));
let wait_queue = Arc::new(WaitQueue::default());
let mut task_handles = Vec::with_capacity(num_tasks);
for task_id in 0..num_tasks {
let barrier_clone = barrier.clone();
let wait_queue_clone = wait_queue.clone();
task_handles.push(tokio::spawn(async move {
barrier_clone.wait().await;
for _ in 0..num_tasks {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if wait_queue_clone
.push_async_entry(&mut async_wait_pinned, || {
if task_id % 2 == 0 {
Ok(())
} else {
Err(())
}
})
.is_ok()
{
assert_eq!(task_id % 2, 0);
}
}
wait_queue_clone.signal();
}));
}
for r in futures::future::join_all(task_handles).await {
assert!(r.is_ok());
}
drop(wait_queue);
}
}