Compare commits

...

2 commits

Author SHA1 Message Date
Janis 4c70dbfc71 Parker: park with callback before waiting 2025-07-05 14:21:31 +02:00
Janis c9e69c3b06 cachepadded: turn into pub newtype 2025-07-05 14:21:15 +02:00
2 changed files with 34 additions and 9 deletions

View file

@ -146,9 +146,7 @@ use core::ops::{Deref, DerefMut};
)), )),
repr(align(64)) repr(align(64))
)] )]
pub struct CachePadded<T> { pub struct CachePadded<T>(pub T);
value: T,
}
unsafe impl<T: Send> Send for CachePadded<T> {} unsafe impl<T: Send> Send for CachePadded<T> {}
unsafe impl<T: Sync> Sync for CachePadded<T> {} unsafe impl<T: Sync> Sync for CachePadded<T> {}
@ -164,7 +162,7 @@ impl<T> CachePadded<T> {
/// let padded_value = CachePadded::new(1); /// let padded_value = CachePadded::new(1);
/// ``` /// ```
pub const fn new(t: T) -> CachePadded<T> { pub const fn new(t: T) -> CachePadded<T> {
CachePadded::<T> { value: t } CachePadded::<T>(t)
} }
/// Returns the inner value. /// Returns the inner value.
@ -179,7 +177,7 @@ impl<T> CachePadded<T> {
/// assert_eq!(value, 7); /// assert_eq!(value, 7);
/// ``` /// ```
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.value self.0
} }
} }
@ -187,20 +185,20 @@ impl<T> Deref for CachePadded<T> {
type Target = T; type Target = T;
fn deref(&self) -> &T { fn deref(&self) -> &T {
&self.value &self.0
} }
} }
impl<T> DerefMut for CachePadded<T> { impl<T> DerefMut for CachePadded<T> {
fn deref_mut(&mut self) -> &mut T { fn deref_mut(&mut self) -> &mut T {
&mut self.value &mut self.0
} }
} }
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> { impl<T: fmt::Debug> fmt::Debug for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CachePadded") f.debug_struct("CachePadded")
.field("value", &self.value) .field("value", &self.0)
.finish() .finish()
} }
} }
@ -213,6 +211,6 @@ impl<T> From<T> for CachePadded<T> {
impl<T: fmt::Display> fmt::Display for CachePadded<T> { impl<T: fmt::Display> fmt::Display for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f) fmt::Display::fmt(&self.0, f)
} }
} }

View file

@ -214,11 +214,38 @@ impl Parker {
} }
pub fn park(&self) { pub fn park(&self) {
self.park_inner(|| ());
}
pub fn park_with_callback<F>(&self, before_sleep: F)
where
F: FnOnce(),
{
// This function is called when the thread is about to park.
self.park_inner(before_sleep);
}
// If the caller wants to park the thread on a mutex'd condition, it is
// possible for a deadlock to occur when the mutex is dropped just before
// this parker atomically changes it's state to `PARKED`. For that reason,
// we use a callback to allow the caller to perform any necessary actions
// before parking, but after the parker is set to `PARKED`. If another
// thread then checks the condition and checks if this thread should be
// woken, we will be immediately notified.
// Thusly, it is important that any caller synchronise any conditionals with
// a mutex externally, and then unlock that mutex in `before_sleep`.
fn park_inner<F>(&self, before_sleep: F)
where
F: FnOnce(),
{
if self.mutex.fetch_sub(1, Ordering::Acquire) == Self::NOTIFIED { if self.mutex.fetch_sub(1, Ordering::Acquire) == Self::NOTIFIED {
before_sleep();
// The thread was notified, so we can return immediately. // The thread was notified, so we can return immediately.
return; return;
} }
before_sleep();
loop { loop {
atomic_wait::wait(&self.mutex, Self::PARKED); atomic_wait::wait(&self.mutex, Self::PARKED);