Compare commits
No commits in common. "4c70dbfc71e7536acf6dfcacb7fcfbc03aaedce5" and "eee2f8995a7d28faddb17f156d1fd537baf46233" have entirely different histories.
4c70dbfc71
...
eee2f8995a
|
@ -146,7 +146,9 @@ use core::ops::{Deref, DerefMut};
|
||||||
)),
|
)),
|
||||||
repr(align(64))
|
repr(align(64))
|
||||||
)]
|
)]
|
||||||
pub struct CachePadded<T>(pub T);
|
pub struct CachePadded<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> {}
|
||||||
|
@ -162,7 +164,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>(t)
|
CachePadded::<T> { value: t }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the inner value.
|
/// Returns the inner value.
|
||||||
|
@ -177,7 +179,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.0
|
self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,20 +187,20 @@ impl<T> Deref for CachePadded<T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
fn deref(&self) -> &T {
|
fn deref(&self) -> &T {
|
||||||
&self.0
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.0
|
&mut self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.0)
|
.field("value", &self.value)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,6 +213,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.0, f)
|
fmt::Display::fmt(&self.value, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
27
src/sync.rs
27
src/sync.rs
|
@ -214,38 +214,11 @@ 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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue