Skip to content

Commit 9881574

Browse files
committed
std: remove lock wrappers in sys_common
1 parent 534ddc6 commit 9881574

34 files changed

+404
-653
lines changed

library/std/src/sync/condvar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod tests;
33

44
use crate::fmt;
55
use crate::sync::{mutex, poison, LockResult, MutexGuard, PoisonError};
6-
use crate::sys_common::condvar as sys;
6+
use crate::sys::locks as sys;
77
use crate::time::{Duration, Instant};
88

99
/// A type indicating whether a timed wait on a condition variable returned

library/std/src/sync/mutex.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::cell::UnsafeCell;
55
use crate::fmt;
66
use crate::ops::{Deref, DerefMut};
77
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
8-
use crate::sys_common::mutex as sys;
8+
use crate::sys::locks as sys;
99

1010
/// A mutual exclusion primitive useful for protecting shared data
1111
///
@@ -163,7 +163,7 @@ use crate::sys_common::mutex as sys;
163163
#[stable(feature = "rust1", since = "1.0.0")]
164164
#[cfg_attr(not(test), rustc_diagnostic_item = "Mutex")]
165165
pub struct Mutex<T: ?Sized> {
166-
inner: sys::MovableMutex,
166+
inner: sys::Mutex,
167167
poison: poison::Flag,
168168
data: UnsafeCell<T>,
169169
}
@@ -217,11 +217,7 @@ impl<T> Mutex<T> {
217217
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
218218
#[inline]
219219
pub const fn new(t: T) -> Mutex<T> {
220-
Mutex {
221-
inner: sys::MovableMutex::new(),
222-
poison: poison::Flag::new(),
223-
data: UnsafeCell::new(t),
224-
}
220+
Mutex { inner: sys::Mutex::new(), poison: poison::Flag::new(), data: UnsafeCell::new(t) }
225221
}
226222
}
227223

@@ -264,7 +260,7 @@ impl<T: ?Sized> Mutex<T> {
264260
#[stable(feature = "rust1", since = "1.0.0")]
265261
pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
266262
unsafe {
267-
self.inner.raw_lock();
263+
self.inner.lock();
268264
MutexGuard::new(self)
269265
}
270266
}
@@ -526,7 +522,7 @@ impl<T: ?Sized> Drop for MutexGuard<'_, T> {
526522
fn drop(&mut self) {
527523
unsafe {
528524
self.lock.poison.done(&self.poison);
529-
self.lock.inner.raw_unlock();
525+
self.lock.inner.unlock();
530526
}
531527
}
532528
}
@@ -545,7 +541,7 @@ impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
545541
}
546542
}
547543

548-
pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::MovableMutex {
544+
pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
549545
&guard.lock.inner
550546
}
551547

library/std/src/sync/rwlock.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::fmt;
66
use crate::ops::{Deref, DerefMut};
77
use crate::ptr::NonNull;
88
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
9-
use crate::sys_common::rwlock as sys;
9+
use crate::sys::locks as sys;
1010

1111
/// A reader-writer lock
1212
///
@@ -78,7 +78,7 @@ use crate::sys_common::rwlock as sys;
7878
#[stable(feature = "rust1", since = "1.0.0")]
7979
#[cfg_attr(not(test), rustc_diagnostic_item = "RwLock")]
8080
pub struct RwLock<T: ?Sized> {
81-
inner: sys::MovableRwLock,
81+
inner: sys::RwLock,
8282
poison: poison::Flag,
8383
data: UnsafeCell<T>,
8484
}
@@ -109,7 +109,7 @@ pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
109109
// `NonNull` is also covariant over `T`, just like we would have with `&T`. `NonNull`
110110
// is preferable over `const* T` to allow for niche optimization.
111111
data: NonNull<T>,
112-
inner_lock: &'a sys::MovableRwLock,
112+
inner_lock: &'a sys::RwLock,
113113
}
114114

115115
#[stable(feature = "rust1", since = "1.0.0")]
@@ -158,11 +158,7 @@ impl<T> RwLock<T> {
158158
#[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
159159
#[inline]
160160
pub const fn new(t: T) -> RwLock<T> {
161-
RwLock {
162-
inner: sys::MovableRwLock::new(),
163-
poison: poison::Flag::new(),
164-
data: UnsafeCell::new(t),
165-
}
161+
RwLock { inner: sys::RwLock::new(), poison: poison::Flag::new(), data: UnsafeCell::new(t) }
166162
}
167163
}
168164

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ pub mod locks {
5151
mod futex_condvar;
5252
mod futex_mutex;
5353
mod futex_rwlock;
54-
pub(crate) use futex_condvar::MovableCondvar;
55-
pub(crate) use futex_mutex::{MovableMutex, Mutex};
56-
pub(crate) use futex_rwlock::{MovableRwLock, RwLock};
54+
pub(crate) use futex_condvar::Condvar;
55+
pub(crate) use futex_mutex::Mutex;
56+
pub(crate) use futex_rwlock::RwLock;
5757
}
5858

5959
use crate::io::ErrorKind;

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ pub struct Condvar {
1212
unsafe impl Send for Condvar {}
1313
unsafe impl Sync for Condvar {}
1414

15-
pub type MovableCondvar = Condvar;
16-
1715
impl Condvar {
1816
#[inline]
1917
pub const fn new() -> Condvar {
2018
Condvar { waiters: SpinMutex::new(waiter_queue::WaiterQueue::new()) }
2119
}
2220

23-
#[inline]
24-
pub unsafe fn init(&mut self) {}
25-
26-
pub unsafe fn notify_one(&self) {
21+
pub fn notify_one(&self) {
2722
self.waiters.with_locked(|waiters| {
2823
if let Some(task) = waiters.pop_front() {
2924
// Unpark the task
@@ -39,7 +34,7 @@ impl Condvar {
3934
});
4035
}
4136

42-
pub unsafe fn notify_all(&self) {
37+
pub fn notify_all(&self) {
4338
self.waiters.with_locked(|waiters| {
4439
while let Some(task) = waiters.pop_front() {
4540
// Unpark the task

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ pub struct Mutex {
1111
mtx: SpinIdOnceCell<()>,
1212
}
1313

14-
pub type MovableMutex = Mutex;
15-
1614
/// Create a mutex object. This function never panics.
1715
fn new_mtx() -> Result<abi::ID, ItronError> {
1816
ItronError::err_if_negative(unsafe {
@@ -39,7 +37,7 @@ impl Mutex {
3937
}
4038
}
4139

42-
pub unsafe fn lock(&self) {
40+
pub fn lock(&self) {
4341
let mtx = self.raw();
4442
expect_success(unsafe { abi::loc_mtx(mtx) }, &"loc_mtx");
4543
}
@@ -49,7 +47,7 @@ impl Mutex {
4947
expect_success_aborting(unsafe { abi::unl_mtx(mtx) }, &"unl_mtx");
5048
}
5149

52-
pub unsafe fn try_lock(&self) -> bool {
50+
pub fn try_lock(&self) -> bool {
5351
let mtx = self.raw();
5452
match unsafe { abi::ploc_mtx(mtx) } {
5553
abi::E_TMOUT => false,

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,43 @@ use crate::time::Duration;
44

55
use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable};
66

7+
/// FIXME: `UnsafeList` is not movable.
8+
struct AllocatedCondvar(SpinMutex<WaitVariable<()>>);
9+
710
pub struct Condvar {
8-
inner: SpinMutex<WaitVariable<()>>,
11+
inner: LazyBox<AllocatedCondvar>,
912
}
1013

11-
pub(crate) type MovableCondvar = LazyBox<Condvar>;
12-
13-
impl LazyInit for Condvar {
14+
impl LazyInit for AllocatedCondvar {
1415
fn init() -> Box<Self> {
15-
Box::new(Self::new())
16+
Box::new(AllocatedCondvar(SpinMutex::new(WaitVariable::new(()))))
1617
}
1718
}
1819

1920
impl Condvar {
2021
pub const fn new() -> Condvar {
21-
Condvar { inner: SpinMutex::new(WaitVariable::new(())) }
22+
Condvar { inner: LazyBox::new() }
2223
}
2324

2425
#[inline]
25-
pub unsafe fn notify_one(&self) {
26-
let _ = WaitQueue::notify_one(self.inner.lock());
26+
pub fn notify_one(&self) {
27+
let _ = WaitQueue::notify_one(self.inner.0.lock());
2728
}
2829

2930
#[inline]
30-
pub unsafe fn notify_all(&self) {
31-
let _ = WaitQueue::notify_all(self.inner.lock());
31+
pub fn notify_all(&self) {
32+
let _ = WaitQueue::notify_all(self.inner.0.lock());
3233
}
3334

3435
pub unsafe fn wait(&self, mutex: &Mutex) {
35-
let guard = self.inner.lock();
36+
let guard = self.inner.0.lock();
3637
WaitQueue::wait(guard, || unsafe { mutex.unlock() });
37-
unsafe { mutex.lock() }
38+
mutex.lock()
3839
}
3940

4041
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
41-
let success = WaitQueue::wait_timeout(&self.inner, dur, || unsafe { mutex.unlock() });
42-
unsafe { mutex.lock() };
42+
let success = WaitQueue::wait_timeout(&self.inner.0, dur, || unsafe { mutex.unlock() });
43+
mutex.lock();
4344
success
4445
}
4546
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
use super::waitqueue::{try_lock_or_false, SpinMutex, WaitQueue, WaitVariable};
22
use crate::sys_common::lazy_box::{LazyBox, LazyInit};
33

4+
/// FIXME: `UnsafeList` is not movable.
5+
struct AllocatedMutex(SpinMutex<WaitVariable<bool>>);
6+
47
pub struct Mutex {
5-
inner: SpinMutex<WaitVariable<bool>>,
8+
inner: LazyBox<AllocatedMutex>,
69
}
710

8-
// not movable: see UnsafeList implementation
9-
pub(crate) type MovableMutex = LazyBox<Mutex>;
10-
11-
impl LazyInit for Mutex {
11+
impl LazyInit for AllocatedMutex {
1212
fn init() -> Box<Self> {
13-
Box::new(Self::new())
13+
Box::new(AllocatedMutex(SpinMutex::new(WaitVariable::new(false))))
1414
}
1515
}
1616

1717
// Implementation according to “Operating Systems: Three Easy Pieces”, chapter 28
1818
impl Mutex {
1919
pub const fn new() -> Mutex {
20-
Mutex { inner: SpinMutex::new(WaitVariable::new(false)) }
20+
Mutex { inner: LazyBox::new() }
2121
}
2222

2323
#[inline]
24-
pub unsafe fn lock(&self) {
25-
let mut guard = self.inner.lock();
24+
pub fn lock(&self) {
25+
let mut guard = self.inner.0.lock();
2626
if *guard.lock_var() {
2727
// Another thread has the lock, wait
2828
WaitQueue::wait(guard, || {})
@@ -35,7 +35,7 @@ impl Mutex {
3535

3636
#[inline]
3737
pub unsafe fn unlock(&self) {
38-
let guard = self.inner.lock();
38+
let guard = self.inner.0.lock();
3939
if let Err(mut guard) = WaitQueue::notify_one(guard) {
4040
// No other waiters, unlock
4141
*guard.lock_var_mut() = false;
@@ -45,8 +45,8 @@ impl Mutex {
4545
}
4646

4747
#[inline]
48-
pub unsafe fn try_lock(&self) -> bool {
49-
let mut guard = try_lock_or_false!(self.inner);
48+
pub fn try_lock(&self) -> bool {
49+
let mut guard = try_lock_or_false!(self.inner.0);
5050
if *guard.lock_var() {
5151
// Another thread has the lock
5252
false

0 commit comments

Comments
 (0)