Skip to content

Switch from 'Option' to 'ManuallyDrop' for blocking guard's inner #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions godot-cell/src/blocking_guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Condvar, Mutex};

Expand All @@ -16,7 +17,7 @@ use crate::guards::{MutGuard, RefGuard};
/// See [`panicking::RefGuard`](crate::panicking::RefGuard) for more details.
#[derive(Debug)]
pub struct RefGuardBlocking<'a, T> {
inner: Option<RefGuard<'a, T>>,
inner: ManuallyDrop<RefGuard<'a, T>>,
mut_condition: Arc<Condvar>,
state: Arc<Mutex<ThreadTracker>>,
}
Expand All @@ -28,7 +29,7 @@ impl<'a, T> RefGuardBlocking<'a, T> {
state: Arc<Mutex<ThreadTracker>>,
) -> Self {
Self {
inner: Some(inner),
inner: ManuallyDrop::new(inner),
mut_condition,
state,
}
Expand All @@ -39,7 +40,7 @@ impl<'a, T> Deref for RefGuardBlocking<'a, T> {
type Target = <RefGuard<'a, T> as Deref>::Target;

fn deref(&self) -> &Self::Target {
self.inner.as_ref().unwrap().deref()
self.inner.deref().deref()
}
}

Expand All @@ -49,7 +50,9 @@ impl<T> Drop for RefGuardBlocking<'_, T> {

state_lock.decrement_current_thread_shared_count();

drop(self.inner.take());
unsafe {
ManuallyDrop::drop(&mut self.inner);
}
Comment on lines +53 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here.


self.mut_condition.notify_one();
drop(state_lock);
Expand All @@ -61,7 +64,7 @@ impl<T> Drop for RefGuardBlocking<'_, T> {
/// See [`panicking::MutGuard`](crate::panicking::MutGuard) for more details.
#[derive(Debug)]
pub struct MutGuardBlocking<'a, T> {
inner: Option<MutGuard<'a, T>>,
inner: ManuallyDrop<MutGuard<'a, T>>,
mut_condition: Arc<Condvar>,
immut_condition: Arc<Condvar>,
}
Expand All @@ -73,7 +76,7 @@ impl<'a, T> MutGuardBlocking<'a, T> {
immut_condition: Arc<Condvar>,
) -> Self {
Self {
inner: Some(inner),
inner: ManuallyDrop::new(inner),
immut_condition,
mut_condition,
}
Expand All @@ -84,19 +87,21 @@ impl<'a, T> Deref for MutGuardBlocking<'a, T> {
type Target = <MutGuard<'a, T> as Deref>::Target;

fn deref(&self) -> &Self::Target {
self.inner.as_ref().unwrap().deref()
self.inner.deref().deref()
}
}

impl<T> DerefMut for MutGuardBlocking<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner.as_mut().unwrap().deref_mut()
self.inner.deref_mut().deref_mut()
}
}

impl<T> Drop for MutGuardBlocking<'_, T> {
fn drop(&mut self) {
drop(self.inner.take());
unsafe {
ManuallyDrop::drop(&mut self.inner);
}
Comment on lines +102 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a safety statement:

Suggested change
unsafe {
ManuallyDrop::drop(&mut self.inner);
}
// SAFETY: guard is dropped exactly once, here.
unsafe {
ManuallyDrop::drop(&mut self.inner);
}


self.mut_condition.notify_one();
self.immut_condition.notify_all();
Expand Down
Loading