提交 ac5aa1de 编写于 作者: M Mara Bos

Use Drop instead of destroy() for locks.

上级 fb197601
...@@ -70,9 +70,13 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { ...@@ -70,9 +70,13 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
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);
}
} }
} }
...@@ -215,7 +215,4 @@ pub unsafe fn try_lock(&self) -> bool { ...@@ -215,7 +215,4 @@ pub unsafe fn try_lock(&self) -> bool {
} }
guard.locked guard.locked
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }
...@@ -84,12 +84,6 @@ pub unsafe fn write_unlock(&self) { ...@@ -84,12 +84,6 @@ pub unsafe fn write_unlock(&self) {
// 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 {
......
...@@ -117,8 +117,6 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { ...@@ -117,8 +117,6 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
unsafe { mutex.lock() }; unsafe { mutex.lock() };
success success
} }
pub unsafe fn destroy(&self) {}
} }
mod waiter_queue { mod waiter_queue {
......
...@@ -64,8 +64,10 @@ pub unsafe fn try_lock(&self) -> bool { ...@@ -64,8 +64,10 @@ pub unsafe fn try_lock(&self) -> bool {
} }
} }
} }
}
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");
} }
......
...@@ -38,7 +38,4 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { ...@@ -38,7 +38,4 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
unsafe { mutex.lock() }; unsafe { mutex.lock() };
success success
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }
...@@ -52,7 +52,4 @@ pub unsafe fn try_lock(&self) -> bool { ...@@ -52,7 +52,4 @@ pub unsafe fn try_lock(&self) -> bool {
true true
} }
} }
#[inline]
pub unsafe fn destroy(&self) {}
} }
...@@ -168,9 +168,6 @@ unsafe fn unlock(&self) { ...@@ -168,9 +168,6 @@ unsafe fn unlock(&self) {
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
......
...@@ -82,9 +82,11 @@ pub unsafe fn write_unlock(&self) { ...@@ -82,9 +82,11 @@ pub unsafe fn write_unlock(&self) {
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");
} }
......
...@@ -24,9 +24,6 @@ pub const fn new() -> Self { ...@@ -24,9 +24,6 @@ pub const fn new() -> Self {
#[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 @@ pub const fn new() -> Self { ...@@ -121,9 +118,6 @@ pub const fn new() -> Self {
#[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.
......
...@@ -63,9 +63,6 @@ pub const fn new() -> Self { ...@@ -63,9 +63,6 @@ pub const fn new() -> Self {
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
......
...@@ -179,14 +179,14 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, mut dur: Duration) -> bool { ...@@ -179,14 +179,14 @@ pub unsafe fn wait_timeout(&self, mutex: &Mutex, mut dur: Duration) -> bool {
#[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 @@ pub unsafe fn destroy(&self) { ...@@ -195,3 +195,10 @@ pub unsafe fn destroy(&self) {
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() };
}
}
...@@ -73,13 +73,13 @@ pub unsafe fn try_lock(&self) -> bool { ...@@ -73,13 +73,13 @@ pub unsafe fn try_lock(&self) -> bool {
} }
#[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 @@ pub unsafe fn destroy(&self) { ...@@ -89,6 +89,13 @@ pub unsafe fn destroy(&self) {
} }
} }
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<'_> {
......
...@@ -128,7 +128,7 @@ pub unsafe fn write_unlock(&self) { ...@@ -128,7 +128,7 @@ pub unsafe fn write_unlock(&self) {
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 @@ pub unsafe fn destroy(&self) { ...@@ -141,3 +141,10 @@ pub unsafe fn destroy(&self) {
} }
} }
} }
impl Drop for RwLock {
#[inline]
fn drop(&mut self) {
unsafe { self.destroy() };
}
}
...@@ -26,7 +26,4 @@ pub unsafe fn wait(&self, _mutex: &Mutex) { ...@@ -26,7 +26,4 @@ pub unsafe fn wait(&self, _mutex: &Mutex) {
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) {}
} }
...@@ -32,7 +32,4 @@ pub unsafe fn unlock(&self) { ...@@ -32,7 +32,4 @@ pub unsafe fn unlock(&self) {
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) {}
} }
...@@ -62,7 +62,4 @@ pub unsafe fn read_unlock(&self) { ...@@ -62,7 +62,4 @@ pub unsafe fn read_unlock(&self) {
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) {}
} }
...@@ -51,8 +51,4 @@ pub unsafe fn notify_one(&self) { ...@@ -51,8 +51,4 @@ pub unsafe fn notify_one(&self) {
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) {
// ...
}
} }
...@@ -53,9 +53,4 @@ pub unsafe fn try_lock(&self) -> bool { ...@@ -53,9 +53,4 @@ pub unsafe fn try_lock(&self) -> bool {
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.
}
} }
...@@ -38,9 +38,4 @@ pub unsafe fn read_unlock(&self) { ...@@ -38,9 +38,4 @@ pub unsafe fn read_unlock(&self) {
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) {
// ...
}
} }
...@@ -55,9 +55,3 @@ pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool { ...@@ -55,9 +55,3 @@ pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool {
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() };
}
}
...@@ -92,9 +92,3 @@ pub unsafe fn raw_unlock(&self) { ...@@ -92,9 +92,3 @@ pub unsafe fn raw_unlock(&self) {
self.0.unlock() self.0.unlock()
} }
} }
impl Drop for MovableMutex {
fn drop(&mut self) {
unsafe { self.0.destroy() };
}
}
...@@ -168,13 +168,6 @@ unsafe fn increment_lock_count(&self) { ...@@ -168,13 +168,6 @@ unsafe fn increment_lock_count(&self) {
} }
} }
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;
......
...@@ -126,9 +126,3 @@ pub unsafe fn write_unlock(&self) { ...@@ -126,9 +126,3 @@ pub unsafe fn write_unlock(&self) {
self.0.write_unlock() self.0.write_unlock()
} }
} }
impl Drop for MovableRwLock {
fn drop(&mut self) {
unsafe { self.0.destroy() };
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册