scheduler/sync/thread_parking.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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
#[cfg(debug_assertions)]
use std::sync::Mutex;
/// Used to unpark a thread, but which hasn't been assigned a specific thread yet.
#[derive(Debug, Clone)]
pub struct ThreadUnparkerUnassigned {
ready_flag: Arc<AtomicBool>,
/// The ID of the thread which is allowed to park.
#[cfg(debug_assertions)]
shared_thread_id: Arc<Mutex<Option<std::thread::ThreadId>>>,
}
/// Used to unpark a thread.
#[derive(Debug, Clone)]
pub struct ThreadUnparker {
thread: std::thread::Thread,
ready_flag: Arc<AtomicBool>,
/// The ID of the thread which is allowed to park.
#[cfg(debug_assertions)]
shared_thread_id: Arc<Mutex<Option<std::thread::ThreadId>>>,
}
/// Used to park a thread. The `ThreadParker` is derived from a `ThreadUnparker` or
/// `ThreadUnparkerUnassigned`, and must only be used on the thread which the unparker was assigned
/// to. If the `ThreadUnparker` was assigned to thread A, then `ThreadParker::park()` must only be
/// called from thread A.
#[derive(Debug, Clone)]
pub struct ThreadParker {
ready_flag: Arc<AtomicBool>,
/// The ID of the thread which is allowed to park.
#[cfg(debug_assertions)]
shared_thread_id: Arc<Mutex<Option<std::thread::ThreadId>>>,
}
impl ThreadUnparkerUnassigned {
pub fn new() -> Self {
Self {
ready_flag: Arc::new(AtomicBool::new(false)),
// there is no assigned thread yet
#[cfg(debug_assertions)]
shared_thread_id: Arc::new(Mutex::new(None)),
}
}
/// Assign this to a thread that will be unparked.
#[must_use]
pub fn assign(self, thread: std::thread::Thread) -> ThreadUnparker {
ThreadUnparker::new(
self.ready_flag,
thread,
#[cfg(debug_assertions)]
self.shared_thread_id,
)
}
// we don't currently use this function, but I don't see a reason to delete it
#[allow(dead_code)]
/// Get a new [`ThreadParker`]. The `ThreadParker` must only be used from the thread which we
/// will later assign ourselves to using `assign()`. This is useful if you want to pass a
/// `ThreadParker` to a new thread before you have a handle to that thread.
pub fn parker(&self) -> ThreadParker {
ThreadParker::new(
Arc::clone(&self.ready_flag),
#[cfg(debug_assertions)]
Arc::clone(&self.shared_thread_id),
)
}
}
impl Default for ThreadUnparkerUnassigned {
fn default() -> Self {
Self::new()
}
}
impl ThreadUnparker {
fn new(
ready_flag: Arc<AtomicBool>,
thread: std::thread::Thread,
#[cfg(debug_assertions)] shared_thread_id: Arc<Mutex<Option<std::thread::ThreadId>>>,
) -> Self {
// set the value of `shared_thread_id`, or if it was already set, verify that it's the
// correct value
#[cfg(debug_assertions)]
{
let mut shared_thread_id = shared_thread_id.lock().unwrap();
// it's valid to park before the unparker has been assigned to a thread
// (`shared_thread_id` would be `Some` in this case), so if it was already set we should
// check that it is the correct thread
let shared_thread_id = shared_thread_id.get_or_insert_with(|| thread.id());
assert_eq!(
*shared_thread_id,
thread.id(),
"An earlier `ThreadParker::park()` was called from the wrong thread"
);
}
Self {
ready_flag,
thread,
#[cfg(debug_assertions)]
shared_thread_id,
}
}
/// Unpark the assigned thread.
pub fn unpark(&self) {
// NOTE: Rust now does guarantee some synchronization between the thread that parks and the
// thread that unparks, so the change to `ready_flag` should be seen by the parked thread:
//
// https://doc.rust-lang.org/std/thread/fn.park.html#memory-ordering
//
// > Calls to park synchronize-with calls to unpark, meaning that memory operations
// > performed before a call to unpark are made visible to the thread that consumes the
// > token and returns from park. Note that all park and unpark operations for a given
// > thread form a total order and park synchronizes-with all prior unpark operations.
// >
// > In atomic ordering terms, unpark performs a Release operation and park performs the
// > corresponding Acquire operation. Calls to unpark for the same thread form a release
// > sequence.
// >
// > Note that being unblocked does not imply a call was made to unpark, because wakeups can
// > also be spurious. For example, a valid, but inefficient, implementation could have park
// > and unpark return immediately without doing anything, making all wakeups spurious.
self.ready_flag.store(true, Ordering::Release);
self.thread.unpark();
}
/// Get a new [`ThreadParker`] for the assigned thread.
pub fn parker(&self) -> ThreadParker {
ThreadParker::new(
Arc::clone(&self.ready_flag),
#[cfg(debug_assertions)]
Arc::clone(&self.shared_thread_id),
)
}
}
impl ThreadParker {
fn new(
ready_flag: Arc<AtomicBool>,
#[cfg(debug_assertions)] shared_thread_id: Arc<Mutex<Option<std::thread::ThreadId>>>,
) -> Self {
Self {
ready_flag,
#[cfg(debug_assertions)]
shared_thread_id,
}
}
/// Park the current thread until [`ThreadUnparker::unpark()`] is called. You must only call
/// `park()` from the thread which the corresponding `ThreadUnparker` is assigned, otherwise a
/// deadlock may occur. In debug builds, this should panic instead of deadlock.
pub fn park(&self) {
while self
.ready_flag
.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{
// verify that we're parking from the proper thread (only in debug builds since this is
// slow)
#[cfg(debug_assertions)]
{
let mut shared_thread_id = self.shared_thread_id.lock().unwrap();
// it's valid to park before the unparker has been assigned to a thread
// (`shared_thread_id` would be `None` in this case), so we should set the thread ID
// here and let the unparker panic instead if this is the wrong thread
let shared_thread_id =
shared_thread_id.get_or_insert_with(|| std::thread::current().id());
assert_eq!(
*shared_thread_id,
std::thread::current().id(),
"`ThreadParker::park()` was called from the wrong thread"
);
}
// if unpark() was called before this park(), this park() will return immediately
std::thread::park();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parking() {
let unparker = ThreadUnparkerUnassigned::new();
let parker = unparker.parker();
let handle = std::thread::spawn(move || {
parker.park();
});
let unparker = unparker.assign(handle.thread().clone());
// there is no race condition here: if `unpark` happens first, `park` will return
// immediately
unparker.unpark();
handle.join().unwrap();
}
}