Use Drop instead of destroy() for locks.

This commit is contained in:
Mara Bos 2022-06-03 16:45:47 +02:00
parent fb1976011e
commit ac5aa1ded5
24 changed files with 39 additions and 87 deletions

View File

@ -70,9 +70,13 @@ impl Condvar {
mutex.lock(); mutex.lock();
res == 0 res == 0
} }
}
pub unsafe fn destroy(&self) { impl Drop for Condvar {
let _ = abi::sem_destroy(self.sem1); fn drop(&mut self) {
let _ = abi::sem_destroy(self.sem2); unsafe {
let _ = abi::sem_destroy(self.sem1);
let _ = abi::sem_destroy(self.sem2);
}
} }
} }

View File

@ -215,7 +215,4 @@ impl Mutex {
} }
guard.locked guard.locked
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -84,12 +84,6 @@ impl RwLock {
// FIXME: should only wake up one of these some of the time // FIXME: should only wake up one of these some of the time
self.cond.notify_all(); self.cond.notify_all();
} }
#[inline]
pub unsafe fn destroy(&self) {
self.lock.destroy();
self.cond.destroy();
}
} }
impl State { impl State {

View File

@ -117,8 +117,6 @@ impl Condvar {
unsafe { mutex.lock() }; unsafe { mutex.lock() };
success success
} }
pub unsafe fn destroy(&self) {}
} }
mod waiter_queue { mod waiter_queue {

View File

@ -64,8 +64,10 @@ impl Mutex {
} }
} }
} }
}
pub unsafe fn destroy(&self) { impl Drop for Mutex {
fn drop(&mut self) {
if let Some(mtx) = self.mtx.get().map(|x| x.0) { if let Some(mtx) = self.mtx.get().map(|x| x.0) {
expect_success_aborting(unsafe { abi::del_mtx(mtx) }, &"del_mtx"); expect_success_aborting(unsafe { abi::del_mtx(mtx) }, &"del_mtx");
} }

View File

@ -38,7 +38,4 @@ impl Condvar {
unsafe { mutex.lock() }; unsafe { mutex.lock() };
success success
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -52,7 +52,4 @@ impl Mutex {
true true
} }
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -168,9 +168,6 @@ impl RwLock {
unsafe { self.__read_unlock(rguard, wguard) }; unsafe { self.__read_unlock(rguard, wguard) };
} }
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }
// The following functions are needed by libunwind. These symbols are named // The following functions are needed by libunwind. These symbols are named

View File

@ -82,9 +82,11 @@ impl RwLock {
let rwl = self.raw(); let rwl = self.raw();
expect_success_aborting(unsafe { abi::rwl_unl_rwl(rwl) }, &"rwl_unl_rwl"); expect_success_aborting(unsafe { abi::rwl_unl_rwl(rwl) }, &"rwl_unl_rwl");
} }
}
impl Drop for RwLock {
#[inline] #[inline]
pub unsafe fn destroy(&self) { fn drop(&mut self) {
if let Some(rwl) = self.rwl.get().map(|x| x.0) { if let Some(rwl) = self.rwl.get().map(|x| x.0) {
expect_success_aborting(unsafe { abi::rwl_del_rwl(rwl) }, &"rwl_del_rwl"); expect_success_aborting(unsafe { abi::rwl_del_rwl(rwl) }, &"rwl_del_rwl");
} }

View File

@ -24,9 +24,6 @@ impl Mutex {
#[inline] #[inline]
pub unsafe fn init(&mut self) {} pub unsafe fn init(&mut self) {}
#[inline]
pub unsafe fn destroy(&self) {}
#[inline] #[inline]
pub unsafe fn try_lock(&self) -> bool { pub unsafe fn try_lock(&self) -> bool {
self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_ok() self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_ok()
@ -121,9 +118,6 @@ impl Condvar {
#[inline] #[inline]
pub unsafe fn init(&mut self) {} pub unsafe fn init(&mut self) {}
#[inline]
pub unsafe fn destroy(&self) {}
// All the memory orderings here are `Relaxed`, // All the memory orderings here are `Relaxed`,
// because synchronization is done by unlocking and locking the mutex. // because synchronization is done by unlocking and locking the mutex.

View File

@ -63,9 +63,6 @@ impl RwLock {
Self { state: AtomicU32::new(0), writer_notify: AtomicU32::new(0) } Self { state: AtomicU32::new(0), writer_notify: AtomicU32::new(0) }
} }
#[inline]
pub unsafe fn destroy(&self) {}
#[inline] #[inline]
pub unsafe fn try_read(&self) -> bool { pub unsafe fn try_read(&self) -> bool {
self.state self.state

View File

@ -179,14 +179,14 @@ impl Condvar {
#[inline] #[inline]
#[cfg(not(target_os = "dragonfly"))] #[cfg(not(target_os = "dragonfly"))]
pub unsafe fn destroy(&self) { unsafe fn destroy(&mut self) {
let r = libc::pthread_cond_destroy(self.inner.get()); let r = libc::pthread_cond_destroy(self.inner.get());
debug_assert_eq!(r, 0); debug_assert_eq!(r, 0);
} }
#[inline] #[inline]
#[cfg(target_os = "dragonfly")] #[cfg(target_os = "dragonfly")]
pub unsafe fn destroy(&self) { unsafe fn destroy(&mut self) {
let r = libc::pthread_cond_destroy(self.inner.get()); let r = libc::pthread_cond_destroy(self.inner.get());
// On DragonFly pthread_cond_destroy() returns EINVAL if called on // On DragonFly pthread_cond_destroy() returns EINVAL if called on
// a condvar that was just initialized with // a condvar that was just initialized with
@ -195,3 +195,10 @@ impl Condvar {
debug_assert!(r == 0 || r == libc::EINVAL); debug_assert!(r == 0 || r == libc::EINVAL);
} }
} }
impl Drop for Condvar {
#[inline]
fn drop(&mut self) {
unsafe { self.destroy() };
}
}

View File

@ -73,13 +73,13 @@ impl Mutex {
} }
#[inline] #[inline]
#[cfg(not(target_os = "dragonfly"))] #[cfg(not(target_os = "dragonfly"))]
pub unsafe fn destroy(&self) { unsafe fn destroy(&mut self) {
let r = libc::pthread_mutex_destroy(self.inner.get()); let r = libc::pthread_mutex_destroy(self.inner.get());
debug_assert_eq!(r, 0); debug_assert_eq!(r, 0);
} }
#[inline] #[inline]
#[cfg(target_os = "dragonfly")] #[cfg(target_os = "dragonfly")]
pub unsafe fn destroy(&self) { unsafe fn destroy(&mut self) {
let r = libc::pthread_mutex_destroy(self.inner.get()); let r = libc::pthread_mutex_destroy(self.inner.get());
// On DragonFly pthread_mutex_destroy() returns EINVAL if called on a // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
// mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER. // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
@ -89,6 +89,13 @@ impl Mutex {
} }
} }
impl Drop for Mutex {
#[inline]
fn drop(&mut self) {
unsafe { self.destroy() };
}
}
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>); pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
impl Drop for PthreadMutexAttr<'_> { impl Drop for PthreadMutexAttr<'_> {

View File

@ -128,7 +128,7 @@ impl RwLock {
self.raw_unlock(); self.raw_unlock();
} }
#[inline] #[inline]
pub unsafe fn destroy(&self) { unsafe fn destroy(&mut self) {
let r = libc::pthread_rwlock_destroy(self.inner.get()); let r = libc::pthread_rwlock_destroy(self.inner.get());
// On DragonFly pthread_rwlock_destroy() returns EINVAL if called on a // On DragonFly pthread_rwlock_destroy() returns EINVAL if called on a
// rwlock that was just initialized with // rwlock that was just initialized with
@ -141,3 +141,10 @@ impl RwLock {
} }
} }
} }
impl Drop for RwLock {
#[inline]
fn drop(&mut self) {
unsafe { self.destroy() };
}
}

View File

@ -26,7 +26,4 @@ impl Condvar {
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool { pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
panic!("condvar wait not supported"); panic!("condvar wait not supported");
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -32,7 +32,4 @@ impl Mutex {
pub unsafe fn try_lock(&self) -> bool { pub unsafe fn try_lock(&self) -> bool {
self.locked.replace(true) == false self.locked.replace(true) == false
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -62,7 +62,4 @@ impl RwLock {
pub unsafe fn write_unlock(&self) { pub unsafe fn write_unlock(&self) {
assert_eq!(self.mode.replace(0), -1); assert_eq!(self.mode.replace(0), -1);
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }

View File

@ -51,8 +51,4 @@ impl Condvar {
pub unsafe fn notify_all(&self) { pub unsafe fn notify_all(&self) {
c::WakeAllConditionVariable(self.inner.get()) c::WakeAllConditionVariable(self.inner.get())
} }
pub unsafe fn destroy(&self) {
// ...
}
} }

View File

@ -53,9 +53,4 @@ impl Mutex {
pub unsafe fn unlock(&self) { pub unsafe fn unlock(&self) {
c::ReleaseSRWLockExclusive(raw(self)); c::ReleaseSRWLockExclusive(raw(self));
} }
#[inline]
pub unsafe fn destroy(&self) {
// SRWLock does not need to be destroyed.
}
} }

View File

@ -38,9 +38,4 @@ impl RwLock {
pub unsafe fn write_unlock(&self) { pub unsafe fn write_unlock(&self) {
c::ReleaseSRWLockExclusive(self.inner.get()) c::ReleaseSRWLockExclusive(self.inner.get())
} }
#[inline]
pub unsafe fn destroy(&self) {
// ...
}
} }

View File

@ -55,9 +55,3 @@ impl Condvar {
self.inner.wait_timeout(mutex.raw(), dur) self.inner.wait_timeout(mutex.raw(), dur)
} }
} }
impl Drop for Condvar {
fn drop(&mut self) {
unsafe { self.inner.destroy() };
}
}

View File

@ -92,9 +92,3 @@ impl MovableMutex {
self.0.unlock() self.0.unlock()
} }
} }
impl Drop for MovableMutex {
fn drop(&mut self) {
unsafe { self.0.destroy() };
}
}

View File

@ -168,13 +168,6 @@ impl<T> ReentrantMutex<T> {
} }
} }
impl<T> Drop for ReentrantMutex<T> {
fn drop(&mut self) {
// Safety: We're the unique owner of this mutex and not going to use it afterwards.
unsafe { self.mutex.destroy() }
}
}
impl<T> Deref for ReentrantMutexGuard<'_, T> { impl<T> Deref for ReentrantMutexGuard<'_, T> {
type Target = T; type Target = T;

View File

@ -126,9 +126,3 @@ impl MovableRwLock {
self.0.write_unlock() self.0.write_unlock()
} }
} }
impl Drop for MovableRwLock {
fn drop(&mut self) {
unsafe { self.0.destroy() };
}
}