Skip to content

Commit ac5aa1d

Browse files
committed
Use Drop instead of destroy() for locks.
1 parent fb19760 commit ac5aa1d

24 files changed

+39
-87
lines changed

library/std/src/sys/hermit/condvar.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ impl Condvar {
7070
mutex.lock();
7171
res == 0
7272
}
73+
}
7374

74-
pub unsafe fn destroy(&self) {
75-
let _ = abi::sem_destroy(self.sem1);
76-
let _ = abi::sem_destroy(self.sem2);
75+
impl Drop for Condvar {
76+
fn drop(&mut self) {
77+
unsafe {
78+
let _ = abi::sem_destroy(self.sem1);
79+
let _ = abi::sem_destroy(self.sem2);
80+
}
7781
}
7882
}

library/std/src/sys/hermit/mutex.rs

-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,4 @@ impl Mutex {
215215
}
216216
guard.locked
217217
}
218-
219-
#[inline]
220-
pub unsafe fn destroy(&self) {}
221218
}

library/std/src/sys/hermit/rwlock.rs

-6
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ impl RwLock {
8484
// FIXME: should only wake up one of these some of the time
8585
self.cond.notify_all();
8686
}
87-
88-
#[inline]
89-
pub unsafe fn destroy(&self) {
90-
self.lock.destroy();
91-
self.cond.destroy();
92-
}
9387
}
9488

9589
impl State {

library/std/src/sys/itron/condvar.rs

-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ impl Condvar {
117117
unsafe { mutex.lock() };
118118
success
119119
}
120-
121-
pub unsafe fn destroy(&self) {}
122120
}
123121

124122
mod waiter_queue {

library/std/src/sys/itron/mutex.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ impl Mutex {
6464
}
6565
}
6666
}
67+
}
6768

68-
pub unsafe fn destroy(&self) {
69+
impl Drop for Mutex {
70+
fn drop(&mut self) {
6971
if let Some(mtx) = self.mtx.get().map(|x| x.0) {
7072
expect_success_aborting(unsafe { abi::del_mtx(mtx) }, &"del_mtx");
7173
}

library/std/src/sys/sgx/condvar.rs

-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,4 @@ impl Condvar {
3838
unsafe { mutex.lock() };
3939
success
4040
}
41-
42-
#[inline]
43-
pub unsafe fn destroy(&self) {}
4441
}

library/std/src/sys/sgx/mutex.rs

-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,4 @@ impl Mutex {
5252
true
5353
}
5454
}
55-
56-
#[inline]
57-
pub unsafe fn destroy(&self) {}
5855
}

library/std/src/sys/sgx/rwlock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ impl RwLock {
168168
unsafe { self.__read_unlock(rguard, wguard) };
169169
}
170170
}
171-
172-
#[inline]
173-
pub unsafe fn destroy(&self) {}
174171
}
175172

176173
// The following functions are needed by libunwind. These symbols are named

library/std/src/sys/solid/rwlock.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ impl RwLock {
8282
let rwl = self.raw();
8383
expect_success_aborting(unsafe { abi::rwl_unl_rwl(rwl) }, &"rwl_unl_rwl");
8484
}
85+
}
8586

87+
impl Drop for RwLock {
8688
#[inline]
87-
pub unsafe fn destroy(&self) {
89+
fn drop(&mut self) {
8890
if let Some(rwl) = self.rwl.get().map(|x| x.0) {
8991
expect_success_aborting(unsafe { abi::rwl_del_rwl(rwl) }, &"rwl_del_rwl");
9092
}

library/std/src/sys/unix/locks/futex.rs

-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ impl Mutex {
2424
#[inline]
2525
pub unsafe fn init(&mut self) {}
2626

27-
#[inline]
28-
pub unsafe fn destroy(&self) {}
29-
3027
#[inline]
3128
pub unsafe fn try_lock(&self) -> bool {
3229
self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_ok()
@@ -121,9 +118,6 @@ impl Condvar {
121118
#[inline]
122119
pub unsafe fn init(&mut self) {}
123120

124-
#[inline]
125-
pub unsafe fn destroy(&self) {}
126-
127121
// All the memory orderings here are `Relaxed`,
128122
// because synchronization is done by unlocking and locking the mutex.
129123

library/std/src/sys/unix/locks/futex_rwlock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ impl RwLock {
6363
Self { state: AtomicU32::new(0), writer_notify: AtomicU32::new(0) }
6464
}
6565

66-
#[inline]
67-
pub unsafe fn destroy(&self) {}
68-
6966
#[inline]
7067
pub unsafe fn try_read(&self) -> bool {
7168
self.state

library/std/src/sys/unix/locks/pthread_condvar.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ impl Condvar {
179179

180180
#[inline]
181181
#[cfg(not(target_os = "dragonfly"))]
182-
pub unsafe fn destroy(&self) {
182+
unsafe fn destroy(&mut self) {
183183
let r = libc::pthread_cond_destroy(self.inner.get());
184184
debug_assert_eq!(r, 0);
185185
}
186186

187187
#[inline]
188188
#[cfg(target_os = "dragonfly")]
189-
pub unsafe fn destroy(&self) {
189+
unsafe fn destroy(&mut self) {
190190
let r = libc::pthread_cond_destroy(self.inner.get());
191191
// On DragonFly pthread_cond_destroy() returns EINVAL if called on
192192
// a condvar that was just initialized with
@@ -195,3 +195,10 @@ impl Condvar {
195195
debug_assert!(r == 0 || r == libc::EINVAL);
196196
}
197197
}
198+
199+
impl Drop for Condvar {
200+
#[inline]
201+
fn drop(&mut self) {
202+
unsafe { self.destroy() };
203+
}
204+
}

library/std/src/sys/unix/locks/pthread_mutex.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ impl Mutex {
7373
}
7474
#[inline]
7575
#[cfg(not(target_os = "dragonfly"))]
76-
pub unsafe fn destroy(&self) {
76+
unsafe fn destroy(&mut self) {
7777
let r = libc::pthread_mutex_destroy(self.inner.get());
7878
debug_assert_eq!(r, 0);
7979
}
8080
#[inline]
8181
#[cfg(target_os = "dragonfly")]
82-
pub unsafe fn destroy(&self) {
82+
unsafe fn destroy(&mut self) {
8383
let r = libc::pthread_mutex_destroy(self.inner.get());
8484
// On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
8585
// mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
@@ -89,6 +89,13 @@ impl Mutex {
8989
}
9090
}
9191

92+
impl Drop for Mutex {
93+
#[inline]
94+
fn drop(&mut self) {
95+
unsafe { self.destroy() };
96+
}
97+
}
98+
9299
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
93100

94101
impl Drop for PthreadMutexAttr<'_> {

library/std/src/sys/unix/locks/pthread_rwlock.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl RwLock {
128128
self.raw_unlock();
129129
}
130130
#[inline]
131-
pub unsafe fn destroy(&self) {
131+
unsafe fn destroy(&mut self) {
132132
let r = libc::pthread_rwlock_destroy(self.inner.get());
133133
// On DragonFly pthread_rwlock_destroy() returns EINVAL if called on a
134134
// rwlock that was just initialized with
@@ -141,3 +141,10 @@ impl RwLock {
141141
}
142142
}
143143
}
144+
145+
impl Drop for RwLock {
146+
#[inline]
147+
fn drop(&mut self) {
148+
unsafe { self.destroy() };
149+
}
150+
}

library/std/src/sys/unsupported/locks/condvar.rs

-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ impl Condvar {
2626
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
2727
panic!("condvar wait not supported");
2828
}
29-
30-
#[inline]
31-
pub unsafe fn destroy(&self) {}
3229
}

library/std/src/sys/unsupported/locks/mutex.rs

-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,4 @@ impl Mutex {
3232
pub unsafe fn try_lock(&self) -> bool {
3333
self.locked.replace(true) == false
3434
}
35-
36-
#[inline]
37-
pub unsafe fn destroy(&self) {}
3835
}

library/std/src/sys/unsupported/locks/rwlock.rs

-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,4 @@ impl RwLock {
6262
pub unsafe fn write_unlock(&self) {
6363
assert_eq!(self.mode.replace(0), -1);
6464
}
65-
66-
#[inline]
67-
pub unsafe fn destroy(&self) {}
6865
}

library/std/src/sys/windows/locks/condvar.rs

-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,4 @@ impl Condvar {
5151
pub unsafe fn notify_all(&self) {
5252
c::WakeAllConditionVariable(self.inner.get())
5353
}
54-
55-
pub unsafe fn destroy(&self) {
56-
// ...
57-
}
5854
}

library/std/src/sys/windows/locks/mutex.rs

-5
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,4 @@ impl Mutex {
5353
pub unsafe fn unlock(&self) {
5454
c::ReleaseSRWLockExclusive(raw(self));
5555
}
56-
57-
#[inline]
58-
pub unsafe fn destroy(&self) {
59-
// SRWLock does not need to be destroyed.
60-
}
6156
}

library/std/src/sys/windows/locks/rwlock.rs

-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@ impl RwLock {
3838
pub unsafe fn write_unlock(&self) {
3939
c::ReleaseSRWLockExclusive(self.inner.get())
4040
}
41-
42-
#[inline]
43-
pub unsafe fn destroy(&self) {
44-
// ...
45-
}
4641
}

library/std/src/sys_common/condvar.rs

-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,3 @@ impl Condvar {
5555
self.inner.wait_timeout(mutex.raw(), dur)
5656
}
5757
}
58-
59-
impl Drop for Condvar {
60-
fn drop(&mut self) {
61-
unsafe { self.inner.destroy() };
62-
}
63-
}

library/std/src/sys_common/mutex.rs

-6
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,3 @@ impl MovableMutex {
9292
self.0.unlock()
9393
}
9494
}
95-
96-
impl Drop for MovableMutex {
97-
fn drop(&mut self) {
98-
unsafe { self.0.destroy() };
99-
}
100-
}

library/std/src/sys_common/remutex.rs

-7
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ impl<T> ReentrantMutex<T> {
168168
}
169169
}
170170

171-
impl<T> Drop for ReentrantMutex<T> {
172-
fn drop(&mut self) {
173-
// Safety: We're the unique owner of this mutex and not going to use it afterwards.
174-
unsafe { self.mutex.destroy() }
175-
}
176-
}
177-
178171
impl<T> Deref for ReentrantMutexGuard<'_, T> {
179172
type Target = T;
180173

library/std/src/sys_common/rwlock.rs

-6
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,3 @@ impl MovableRwLock {
126126
self.0.write_unlock()
127127
}
128128
}
129-
130-
impl Drop for MovableRwLock {
131-
fn drop(&mut self) {
132-
unsafe { self.0.destroy() };
133-
}
134-
}

0 commit comments

Comments
 (0)