]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | use sys::rwlock as imp; | |
12 | ||
13 | /// An OS-based reader-writer lock. | |
14 | /// | |
15 | /// This structure is entirely unsafe and serves as the lowest layer of a | |
16 | /// cross-platform binding of system rwlocks. It is recommended to use the | |
17 | /// safer types at the top level of this crate instead of this type. | |
18 | pub struct RWLock(imp::RWLock); | |
19 | ||
20 | /// Constant initializer for static RWLocks. | |
21 | pub const RWLOCK_INIT: RWLock = RWLock(imp::RWLOCK_INIT); | |
22 | ||
23 | impl RWLock { | |
9346a6ac | 24 | /// Acquires shared access to the underlying lock, blocking the current |
1a4d82fc JJ |
25 | /// thread to do so. |
26 | /// | |
27 | /// Behavior is undefined if the rwlock has been moved between this and any | |
c34b1796 | 28 | /// previous method call. |
1a4d82fc JJ |
29 | #[inline] |
30 | pub unsafe fn read(&self) { self.0.read() } | |
31 | ||
9346a6ac | 32 | /// Attempts to acquire shared access to this lock, returning whether it |
1a4d82fc JJ |
33 | /// succeeded or not. |
34 | /// | |
35 | /// This function does not block the current thread. | |
36 | /// | |
37 | /// Behavior is undefined if the rwlock has been moved between this and any | |
c34b1796 | 38 | /// previous method call. |
1a4d82fc JJ |
39 | #[inline] |
40 | pub unsafe fn try_read(&self) -> bool { self.0.try_read() } | |
41 | ||
9346a6ac | 42 | /// Acquires write access to the underlying lock, blocking the current thread |
1a4d82fc JJ |
43 | /// to do so. |
44 | /// | |
45 | /// Behavior is undefined if the rwlock has been moved between this and any | |
c34b1796 | 46 | /// previous method call. |
1a4d82fc JJ |
47 | #[inline] |
48 | pub unsafe fn write(&self) { self.0.write() } | |
49 | ||
9346a6ac | 50 | /// Attempts to acquire exclusive access to this lock, returning whether it |
1a4d82fc JJ |
51 | /// succeeded or not. |
52 | /// | |
53 | /// This function does not block the current thread. | |
54 | /// | |
55 | /// Behavior is undefined if the rwlock has been moved between this and any | |
c34b1796 | 56 | /// previous method call. |
1a4d82fc JJ |
57 | #[inline] |
58 | pub unsafe fn try_write(&self) -> bool { self.0.try_write() } | |
59 | ||
9346a6ac | 60 | /// Unlocks previously acquired shared access to this lock. |
1a4d82fc JJ |
61 | /// |
62 | /// Behavior is undefined if the current thread does not have shared access. | |
63 | #[inline] | |
64 | pub unsafe fn read_unlock(&self) { self.0.read_unlock() } | |
65 | ||
9346a6ac | 66 | /// Unlocks previously acquired exclusive access to this lock. |
1a4d82fc JJ |
67 | /// |
68 | /// Behavior is undefined if the current thread does not currently have | |
69 | /// exclusive access. | |
70 | #[inline] | |
71 | pub unsafe fn write_unlock(&self) { self.0.write_unlock() } | |
72 | ||
9346a6ac | 73 | /// Destroys OS-related resources with this RWLock. |
1a4d82fc JJ |
74 | /// |
75 | /// Behavior is undefined if there are any currently active users of this | |
76 | /// lock. | |
77 | #[inline] | |
78 | pub unsafe fn destroy(&self) { self.0.destroy() } | |
79 | } |