]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys_common/rwlock.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / src / libstd / sys_common / rwlock.rs
CommitLineData
1a4d82fc
JJ
1use sys::rwlock as imp;
2
3/// An OS-based reader-writer lock.
4///
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.
8pub struct RWLock(imp::RWLock);
9
1a4d82fc 10impl RWLock {
62682a34
SL
11 /// Creates a new reader-writer lock for use.
12 ///
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()) }
16
9346a6ac 17 /// Acquires shared access to the underlying lock, blocking the current
1a4d82fc
JJ
18 /// thread to do so.
19 ///
20 /// Behavior is undefined if the rwlock has been moved between this and any
c34b1796 21 /// previous method call.
1a4d82fc
JJ
22 #[inline]
23 pub unsafe fn read(&self) { self.0.read() }
24
9346a6ac 25 /// Attempts to acquire shared access to this lock, returning whether it
1a4d82fc
JJ
26 /// succeeded or not.
27 ///
28 /// This function does not block the current thread.
29 ///
30 /// Behavior is undefined if the rwlock has been moved between this and any
c34b1796 31 /// previous method call.
1a4d82fc
JJ
32 #[inline]
33 pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
34
9346a6ac 35 /// Acquires write access to the underlying lock, blocking the current thread
1a4d82fc
JJ
36 /// to do so.
37 ///
38 /// Behavior is undefined if the rwlock has been moved between this and any
c34b1796 39 /// previous method call.
1a4d82fc
JJ
40 #[inline]
41 pub unsafe fn write(&self) { self.0.write() }
42
9346a6ac 43 /// Attempts to acquire exclusive access to this lock, returning whether it
1a4d82fc
JJ
44 /// succeeded or not.
45 ///
46 /// This function does not block the current thread.
47 ///
48 /// Behavior is undefined if the rwlock has been moved between this and any
c34b1796 49 /// previous method call.
1a4d82fc
JJ
50 #[inline]
51 pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
52
9346a6ac 53 /// Unlocks previously acquired shared access to this lock.
1a4d82fc
JJ
54 ///
55 /// Behavior is undefined if the current thread does not have shared access.
56 #[inline]
57 pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
58
9346a6ac 59 /// Unlocks previously acquired exclusive access to this lock.
1a4d82fc
JJ
60 ///
61 /// Behavior is undefined if the current thread does not currently have
62 /// exclusive access.
63 #[inline]
64 pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
65
9346a6ac 66 /// Destroys OS-related resources with this RWLock.
1a4d82fc
JJ
67 ///
68 /// Behavior is undefined if there are any currently active users of this
69 /// lock.
70 #[inline]
71 pub unsafe fn destroy(&self) { self.0.destroy() }
72}