Skip to content

Commit 060e8cb

Browse files
committed
Get rid of raw pointers and UnsafeCell in cloudabi condvar.
1 parent 41066be commit 060e8cb

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::cell::UnsafeCell;
21
use crate::mem;
32
use crate::sync::atomic::{AtomicU32, Ordering};
43
use crate::sys::cloudabi::abi;
@@ -12,37 +11,32 @@ extern "C" {
1211
}
1312

1413
pub struct Condvar {
15-
condvar: UnsafeCell<AtomicU32>,
14+
condvar: AtomicU32,
1615
}
1716

1817
pub type MovableCondvar = Condvar;
1918

2019
unsafe impl Send for Condvar {}
2120
unsafe impl Sync for Condvar {}
2221

23-
const NEW: Condvar =
24-
Condvar { condvar: UnsafeCell::new(AtomicU32::new(abi::CONDVAR_HAS_NO_WAITERS.0)) };
25-
2622
impl Condvar {
2723
pub const fn new() -> Condvar {
28-
NEW
24+
Condvar { condvar: AtomicU32::new(abi::CONDVAR_HAS_NO_WAITERS.0) }
2925
}
3026

3127
pub unsafe fn init(&mut self) {}
3228

3329
pub unsafe fn notify_one(&self) {
34-
let condvar = self.condvar.get();
35-
if (*condvar).load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 {
36-
let ret = abi::condvar_signal(condvar as *mut abi::condvar, abi::scope::PRIVATE, 1);
30+
if self.condvar.load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 {
31+
let ret = abi::condvar_signal(&self.condvar as *const AtomicU32 as *mut abi::condvar, abi::scope::PRIVATE, 1);
3732
assert_eq!(ret, abi::errno::SUCCESS, "Failed to signal on condition variable");
3833
}
3934
}
4035

4136
pub unsafe fn notify_all(&self) {
42-
let condvar = self.condvar.get();
43-
if (*condvar).load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 {
37+
if self.condvar.load(Ordering::Relaxed) != abi::CONDVAR_HAS_NO_WAITERS.0 {
4438
let ret = abi::condvar_signal(
45-
condvar as *mut abi::condvar,
39+
&self.condvar as *const AtomicU32 as *mut abi::condvar,
4640
abi::scope::PRIVATE,
4741
abi::nthreads::MAX,
4842
);
@@ -53,20 +47,19 @@ impl Condvar {
5347
pub unsafe fn wait(&self, mutex: &Mutex) {
5448
let mutex = mutex::raw(mutex);
5549
assert_eq!(
56-
(*mutex).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
50+
mutex.load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
5751
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
5852
"This lock is not write-locked by this thread"
5953
);
6054

6155
// Call into the kernel to wait on the condition variable.
62-
let condvar = self.condvar.get();
6356
let subscription = abi::subscription {
6457
type_: abi::eventtype::CONDVAR,
6558
union: abi::subscription_union {
6659
condvar: abi::subscription_condvar {
67-
condvar: condvar as *mut abi::condvar,
60+
condvar: &self.condvar as *const AtomicU32 as *mut abi::condvar,
6861
condvar_scope: abi::scope::PRIVATE,
69-
lock: mutex as *mut abi::lock,
62+
lock: mutex as *const AtomicU32 as *mut abi::lock,
7063
lock_scope: abi::scope::PRIVATE,
7164
},
7265
},
@@ -86,23 +79,22 @@ impl Condvar {
8679
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
8780
let mutex = mutex::raw(mutex);
8881
assert_eq!(
89-
(*mutex).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
82+
mutex.load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
9083
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
9184
"This lock is not write-locked by this thread"
9285
);
9386

9487
// Call into the kernel to wait on the condition variable.
95-
let condvar = self.condvar.get();
9688
let timeout =
9789
checked_dur2intervals(&dur).expect("overflow converting duration to nanoseconds");
9890
let subscriptions = [
9991
abi::subscription {
10092
type_: abi::eventtype::CONDVAR,
10193
union: abi::subscription_union {
10294
condvar: abi::subscription_condvar {
103-
condvar: condvar as *mut abi::condvar,
95+
condvar: &self.condvar as *const AtomicU32 as *mut abi::condvar,
10496
condvar_scope: abi::scope::PRIVATE,
105-
lock: mutex as *mut abi::lock,
97+
lock: mutex as *const AtomicU32 as *mut abi::lock,
10698
lock_scope: abi::scope::PRIVATE,
10799
},
108100
},
@@ -144,9 +136,8 @@ impl Condvar {
144136
}
145137

146138
pub unsafe fn destroy(&self) {
147-
let condvar = self.condvar.get();
148139
assert_eq!(
149-
(*condvar).load(Ordering::Relaxed),
140+
self.condvar.load(Ordering::Relaxed),
150141
abi::CONDVAR_HAS_NO_WAITERS.0,
151142
"Attempted to destroy a condition variable with blocked threads"
152143
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Mutex(RWLock);
1717

1818
pub type MovableMutex = Mutex;
1919

20-
pub unsafe fn raw(m: &Mutex) -> *mut AtomicU32 {
20+
pub unsafe fn raw(m: &Mutex) -> &AtomicU32 {
2121
rwlock::raw(&m.0)
2222
}
2323

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub struct RWLock {
1515
lock: AtomicU32,
1616
}
1717

18-
pub unsafe fn raw(r: &RWLock) -> *mut AtomicU32 {
19-
&r.lock as *const AtomicU32 as *mut AtomicU32
18+
pub unsafe fn raw(r: &RWLock) -> &AtomicU32 {
19+
&r.lock
2020
}
2121

2222
unsafe impl Send for RWLock {}

0 commit comments

Comments
 (0)