Skip to content

Commit 6de107a

Browse files
authored
Rollup merge of rust-lang#98555 - mkroening:hermit-lock-init, r=m-ou-se
Hermit: Fix initializing lazy locks Closes hermit-os/hermit-rs#322. The initialization function of hermit's `Condvar` is not called since rust-lang#97647 and was erroneously removed in rust-lang#97879. r? `@m-ou-se` CC: `@stlankes`
2 parents a0a5cff + 0c88602 commit 6de107a

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::ptr;
33
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
44
use crate::sys::hermit::abi;
55
use crate::sys::locks::Mutex;
6+
use crate::sys_common::lazy_box::{LazyBox, LazyInit};
67
use crate::time::Duration;
78

89
// The implementation is inspired by Andrew D. Birrell's paper
@@ -14,14 +15,26 @@ pub struct Condvar {
1415
sem2: *const c_void,
1516
}
1617

17-
pub type MovableCondvar = Condvar;
18+
pub(crate) type MovableCondvar = LazyBox<Condvar>;
19+
20+
impl LazyInit for Condvar {
21+
fn init() -> Box<Self> {
22+
Box::new(Self::new())
23+
}
24+
}
1825

1926
unsafe impl Send for Condvar {}
2027
unsafe impl Sync for Condvar {}
2128

2229
impl Condvar {
23-
pub const fn new() -> Condvar {
24-
Condvar { counter: AtomicUsize::new(0), sem1: ptr::null(), sem2: ptr::null() }
30+
pub fn new() -> Self {
31+
let mut condvar =
32+
Self { counter: AtomicUsize::new(0), sem1: ptr::null(), sem2: ptr::null() };
33+
unsafe {
34+
let _ = abi::sem_init(&mut condvar.sem1, 0);
35+
let _ = abi::sem_init(&mut condvar.sem2, 0);
36+
}
37+
condvar
2538
}
2639

2740
pub unsafe fn notify_one(&self) {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ impl Mutex {
175175
}
176176

177177
#[inline]
178-
pub unsafe fn init(&mut self) {
179-
self.inner = Spinlock::new(MutexInner::new());
180-
}
178+
pub unsafe fn init(&mut self) {}
181179

182180
#[inline]
183181
pub unsafe fn lock(&self) {

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::cell::UnsafeCell;
2-
use crate::sys::locks::{Condvar, Mutex};
2+
use crate::sys::locks::{MovableCondvar, Mutex};
3+
use crate::sys_common::lazy_box::{LazyBox, LazyInit};
34

45
pub struct RwLock {
56
lock: Mutex,
6-
cond: Condvar,
7+
cond: MovableCondvar,
78
state: UnsafeCell<State>,
89
}
910

@@ -28,7 +29,11 @@ unsafe impl Sync for RwLock {}
2829

2930
impl RwLock {
3031
pub const fn new() -> RwLock {
31-
RwLock { lock: Mutex::new(), cond: Condvar::new(), state: UnsafeCell::new(State::Unlocked) }
32+
RwLock {
33+
lock: Mutex::new(),
34+
cond: MovableCondvar::new(),
35+
state: UnsafeCell::new(State::Unlocked),
36+
}
3237
}
3338

3439
#[inline]

0 commit comments

Comments
 (0)