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
{
16 RWLock(imp
::RWLock
::new())
19 /// Acquires shared access to the underlying lock, blocking the current
22 /// Behavior is undefined if the rwlock has been moved between this and any
23 /// previous method call.
25 pub unsafe fn read(&self) {
29 /// Attempts to acquire shared access to this lock, returning whether it
32 /// This function does not block the current thread.
34 /// Behavior is undefined if the rwlock has been moved between this and any
35 /// previous method call.
37 pub unsafe fn try_read(&self) -> bool
{
41 /// Acquires write access to the underlying lock, blocking the current thread
44 /// Behavior is undefined if the rwlock has been moved between this and any
45 /// previous method call.
47 pub unsafe fn write(&self) {
51 /// Attempts to acquire exclusive access to this lock, returning whether it
54 /// This function does not block the current thread.
56 /// Behavior is undefined if the rwlock has been moved between this and any
57 /// previous method call.
59 pub unsafe fn try_write(&self) -> bool
{
63 /// Unlocks previously acquired shared access to this lock.
65 /// Behavior is undefined if the current thread does not have shared access.
67 pub unsafe fn read_unlock(&self) {
71 /// Unlocks previously acquired exclusive access to this lock.
73 /// Behavior is undefined if the current thread does not currently have
76 pub unsafe fn write_unlock(&self) {
80 /// Destroys OS-related resources with this RWLock.
82 /// Behavior is undefined if there are any currently active users of this
85 pub unsafe fn destroy(&self) {
90 // the cfg annotations only exist due to dead code warnings. the code itself is portable
92 pub struct StaticRWLock(RWLock
);
96 pub const fn new() -> StaticRWLock
{
97 StaticRWLock(RWLock
::new())
100 /// Acquires shared access to the underlying lock, blocking the current
103 /// The lock is automatically unlocked when the returned guard is dropped.
105 pub fn read_with_guard(&'
static self) -> RWLockReadGuard
{
106 // SAFETY: All methods require static references, therefore self
107 // cannot be moved between invocations.
111 RWLockReadGuard(&self.0)
114 /// Acquires write access to the underlying lock, blocking the current thread
117 /// The lock is automatically unlocked when the returned guard is dropped.
119 pub fn write_with_guard(&'
static self) -> RWLockWriteGuard
{
120 // SAFETY: All methods require static references, therefore self
121 // cannot be moved between invocations.
125 RWLockWriteGuard(&self.0)
130 pub struct RWLockReadGuard(&'
static RWLock
);
133 impl Drop
for RWLockReadGuard
{
135 unsafe { self.0.read_unlock() }
140 pub struct RWLockWriteGuard(&'
static RWLock
);
143 impl Drop
for RWLockWriteGuard
{
145 unsafe { self.0.write_unlock() }