Skip to content

Commit 1476eb9

Browse files
authored
Rollup merge of #84402 - CDirkx:rwlock, r=dtolnay
Move `sys_common::rwlock::StaticRWLock` etc. to `sys::unix::rwlock` This moves `sys_common::rwlock::StaticRwLock`, `RWLockReadGuard` and `RWLockWriteGuard` to `sys::unix::rwlock`. They are already `#[cfg(unix)]` and don't need to be in `sys_common`.
2 parents c978584 + eb9b0f6 commit 1476eb9

File tree

3 files changed

+53
-60
lines changed

3 files changed

+53
-60
lines changed

library/std/src/sys/unix/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::slice;
2020
use crate::str;
2121
use crate::sys::cvt;
2222
use crate::sys::fd;
23+
use crate::sys::rwlock::{RWLockReadGuard, StaticRWLock};
2324
use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
24-
use crate::sys_common::rwlock::{RWLockReadGuard, StaticRWLock};
2525
use crate::vec;
2626

2727
use libc::{c_char, c_int, c_void};

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

+52
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,55 @@ impl RWLock {
139139
}
140140
}
141141
}
142+
143+
pub struct StaticRWLock(RWLock);
144+
145+
impl StaticRWLock {
146+
pub const fn new() -> StaticRWLock {
147+
StaticRWLock(RWLock::new())
148+
}
149+
150+
/// Acquires shared access to the underlying lock, blocking the current
151+
/// thread to do so.
152+
///
153+
/// The lock is automatically unlocked when the returned guard is dropped.
154+
#[inline]
155+
pub fn read_with_guard(&'static self) -> RWLockReadGuard {
156+
// SAFETY: All methods require static references, therefore self
157+
// cannot be moved between invocations.
158+
unsafe {
159+
self.0.read();
160+
}
161+
RWLockReadGuard(&self.0)
162+
}
163+
164+
/// Acquires write access to the underlying lock, blocking the current thread
165+
/// to do so.
166+
///
167+
/// The lock is automatically unlocked when the returned guard is dropped.
168+
#[inline]
169+
pub fn write_with_guard(&'static self) -> RWLockWriteGuard {
170+
// SAFETY: All methods require static references, therefore self
171+
// cannot be moved between invocations.
172+
unsafe {
173+
self.0.write();
174+
}
175+
RWLockWriteGuard(&self.0)
176+
}
177+
}
178+
179+
pub struct RWLockReadGuard(&'static RWLock);
180+
181+
impl Drop for RWLockReadGuard {
182+
fn drop(&mut self) {
183+
unsafe { self.0.read_unlock() }
184+
}
185+
}
186+
187+
pub struct RWLockWriteGuard(&'static RWLock);
188+
189+
impl Drop for RWLockWriteGuard {
190+
fn drop(&mut self) {
191+
unsafe { self.0.write_unlock() }
192+
}
193+
}

library/std/src/sys_common/rwlock.rs

-59
Original file line numberDiff line numberDiff line change
@@ -86,62 +86,3 @@ impl RWLock {
8686
self.0.destroy()
8787
}
8888
}
89-
90-
// the cfg annotations only exist due to dead code warnings. the code itself is portable
91-
#[cfg(unix)]
92-
pub struct StaticRWLock(RWLock);
93-
94-
#[cfg(unix)]
95-
impl StaticRWLock {
96-
pub const fn new() -> StaticRWLock {
97-
StaticRWLock(RWLock::new())
98-
}
99-
100-
/// Acquires shared access to the underlying lock, blocking the current
101-
/// thread to do so.
102-
///
103-
/// The lock is automatically unlocked when the returned guard is dropped.
104-
#[inline]
105-
pub fn read_with_guard(&'static self) -> RWLockReadGuard {
106-
// SAFETY: All methods require static references, therefore self
107-
// cannot be moved between invocations.
108-
unsafe {
109-
self.0.read();
110-
}
111-
RWLockReadGuard(&self.0)
112-
}
113-
114-
/// Acquires write access to the underlying lock, blocking the current thread
115-
/// to do so.
116-
///
117-
/// The lock is automatically unlocked when the returned guard is dropped.
118-
#[inline]
119-
pub fn write_with_guard(&'static self) -> RWLockWriteGuard {
120-
// SAFETY: All methods require static references, therefore self
121-
// cannot be moved between invocations.
122-
unsafe {
123-
self.0.write();
124-
}
125-
RWLockWriteGuard(&self.0)
126-
}
127-
}
128-
129-
#[cfg(unix)]
130-
pub struct RWLockReadGuard(&'static RWLock);
131-
132-
#[cfg(unix)]
133-
impl Drop for RWLockReadGuard {
134-
fn drop(&mut self) {
135-
unsafe { self.0.read_unlock() }
136-
}
137-
}
138-
139-
#[cfg(unix)]
140-
pub struct RWLockWriteGuard(&'static RWLock);
141-
142-
#[cfg(unix)]
143-
impl Drop for RWLockWriteGuard {
144-
fn drop(&mut self) {
145-
unsafe { self.0.write_unlock() }
146-
}
147-
}

0 commit comments

Comments
 (0)