]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/rwlock.rs
Rollup merge of #58802 - nnethercote:inline-record_layout, r=oli-obk
[rust.git] / src / libstd / sys_common / rwlock.rs
1 use crate::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.
8 pub struct RWLock(imp::RWLock);
9
10 impl RWLock {
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
17     /// Acquires shared access to the underlying lock, blocking the current
18     /// thread to do so.
19     ///
20     /// Behavior is undefined if the rwlock has been moved between this and any
21     /// previous method call.
22     #[inline]
23     pub unsafe fn read(&self) { self.0.read() }
24
25     /// Attempts to acquire shared access to this lock, returning whether it
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
31     /// previous method call.
32     #[inline]
33     pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
34
35     /// Acquires write access to the underlying lock, blocking the current thread
36     /// to do so.
37     ///
38     /// Behavior is undefined if the rwlock has been moved between this and any
39     /// previous method call.
40     #[inline]
41     pub unsafe fn write(&self) { self.0.write() }
42
43     /// Attempts to acquire exclusive access to this lock, returning whether it
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
49     /// previous method call.
50     #[inline]
51     pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
52
53     /// Unlocks previously acquired shared access to this lock.
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
59     /// Unlocks previously acquired exclusive access to this lock.
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
66     /// Destroys OS-related resources with this RWLock.
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 }