1 use crate::sys::rwlock as imp;
3 /// An OS-based reader-writer lock.
5 /// This structure is entirely unsafe and serves as the lowest layer of a
6 /// cross-platform binding of system rwlocks. It is recommended to use the
7 /// safer types at the top level of this crate instead of this type.
8 pub struct RWLock(imp::RWLock);
11 /// Creates a new reader-writer lock for use.
13 /// Behavior is undefined if the reader-writer lock is moved after it is
14 /// first used with any of the functions below.
15 pub const fn new() -> RWLock { RWLock(imp::RWLock::new()) }
17 /// Acquires shared access to the underlying lock, blocking the current
20 /// Behavior is undefined if the rwlock has been moved between this and any
21 /// previous method call.
23 pub unsafe fn read(&self) { self.0.read() }
25 /// Attempts to acquire shared access to this lock, returning whether it
28 /// This function does not block the current thread.
30 /// Behavior is undefined if the rwlock has been moved between this and any
31 /// previous method call.
33 pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
35 /// Acquires write access to the underlying lock, blocking the current thread
38 /// Behavior is undefined if the rwlock has been moved between this and any
39 /// previous method call.
41 pub unsafe fn write(&self) { self.0.write() }
43 /// Attempts to acquire exclusive access to this lock, returning whether it
46 /// This function does not block the current thread.
48 /// Behavior is undefined if the rwlock has been moved between this and any
49 /// previous method call.
51 pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
53 /// Unlocks previously acquired shared access to this lock.
55 /// Behavior is undefined if the current thread does not have shared access.
57 pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
59 /// Unlocks previously acquired exclusive access to this lock.
61 /// Behavior is undefined if the current thread does not currently have
64 pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
66 /// Destroys OS-related resources with this RWLock.
68 /// Behavior is undefined if there are any currently active users of this
71 pub unsafe fn destroy(&self) { self.0.destroy() }