Compare commits
2 commits
eee2f8995a
...
4c70dbfc71
Author | SHA1 | Date | |
---|---|---|---|
|
4c70dbfc71 | ||
|
c9e69c3b06 |
|
@ -146,9 +146,7 @@ use core::ops::{Deref, DerefMut};
|
|||
)),
|
||||
repr(align(64))
|
||||
)]
|
||||
pub struct CachePadded<T> {
|
||||
value: T,
|
||||
}
|
||||
pub struct CachePadded<T>(pub T);
|
||||
|
||||
unsafe impl<T: Send> Send 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);
|
||||
/// ```
|
||||
pub const fn new(t: T) -> CachePadded<T> {
|
||||
CachePadded::<T> { value: t }
|
||||
CachePadded::<T>(t)
|
||||
}
|
||||
|
||||
/// Returns the inner value.
|
||||
|
@ -179,7 +177,7 @@ impl<T> CachePadded<T> {
|
|||
/// assert_eq!(value, 7);
|
||||
/// ```
|
||||
pub fn into_inner(self) -> T {
|
||||
self.value
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,20 +185,20 @@ impl<T> Deref for CachePadded<T> {
|
|||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &T {
|
||||
&self.value
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for CachePadded<T> {
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
&mut self.value
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("CachePadded")
|
||||
.field("value", &self.value)
|
||||
.field("value", &self.0)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
@ -213,6 +211,6 @@ impl<T> From<T> for CachePadded<T> {
|
|||
|
||||
impl<T: fmt::Display> fmt::Display for CachePadded<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.value, f)
|
||||
fmt::Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
|
27
src/sync.rs
27
src/sync.rs
|
@ -214,11 +214,38 @@ impl Parker {
|
|||
}
|
||||
|
||||
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 {
|
||||
before_sleep();
|
||||
// The thread was notified, so we can return immediately.
|
||||
return;
|
||||
}
|
||||
|
||||
before_sleep();
|
||||
|
||||
loop {
|
||||
atomic_wait::wait(&self.mutex, Self::PARKED);
|
||||
|
||||
|
|
Loading…
Reference in a new issue