1 use crate::sys::locks as imp;
3 /// An OS-based reader-writer lock.
5 /// This rwlock cleans up its resources in its `Drop` implementation and may
6 /// safely be moved (when not borrowed).
8 /// This rwlock does not implement poisoning.
10 /// This is either a wrapper around `LazyBox<imp::RwLock>` or `imp::RwLock`,
11 /// depending on the platform. It is boxed on platforms where `imp::RwLock` may
13 pub struct MovableRwLock(imp::MovableRwLock);
16 /// Creates a new reader-writer lock for use.
18 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
19 pub const fn new() -> Self {
20 Self(imp::MovableRwLock::new())
23 /// Acquires shared access to the underlying lock, blocking the current
27 unsafe { self.0.read() }
30 /// Attempts to acquire shared access to this lock, returning whether it
33 /// This function does not block the current thread.
35 pub fn try_read(&self) -> bool {
36 unsafe { self.0.try_read() }
39 /// Acquires write access to the underlying lock, blocking the current thread
43 unsafe { self.0.write() }
46 /// Attempts to acquire exclusive access to this lock, returning whether it
49 /// This function does not block the current thread.
51 pub fn try_write(&self) -> bool {
52 unsafe { self.0.try_write() }
55 /// Unlocks previously acquired shared access to this lock.
57 /// Behavior is undefined if the current thread does not have shared access.
59 pub unsafe fn read_unlock(&self) {
63 /// Unlocks previously acquired exclusive access to this lock.
65 /// Behavior is undefined if the current thread does not currently have
68 pub unsafe fn write_unlock(&self) {