]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/rwlock.rs
Rollup merge of #37483 - xfix:patch-1, r=steveklabnik
[rust.git] / src / libstd / sys_common / rwlock.rs
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 impl RWLock {
21     /// Creates a new reader-writer lock for use.
22     ///
23     /// Behavior is undefined if the reader-writer lock is moved after it is
24     /// first used with any of the functions below.
25     pub const fn new() -> RWLock { RWLock(imp::RWLock::new()) }
26
27     /// Acquires shared access to the underlying lock, blocking the current
28     /// thread to do so.
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 read(&self) { self.0.read() }
34
35     /// Attempts to acquire shared access to this lock, returning whether it
36     /// succeeded or not.
37     ///
38     /// This function does not block the current thread.
39     ///
40     /// Behavior is undefined if the rwlock has been moved between this and any
41     /// previous method call.
42     #[inline]
43     pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
44
45     /// Acquires write access to the underlying lock, blocking the current thread
46     /// to do so.
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 write(&self) { self.0.write() }
52
53     /// Attempts to acquire exclusive access to this lock, returning whether it
54     /// succeeded or not.
55     ///
56     /// This function does not block the current thread.
57     ///
58     /// Behavior is undefined if the rwlock has been moved between this and any
59     /// previous method call.
60     #[inline]
61     pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
62
63     /// Unlocks previously acquired shared access to this lock.
64     ///
65     /// Behavior is undefined if the current thread does not have shared access.
66     #[inline]
67     pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
68
69     /// Unlocks previously acquired exclusive access to this lock.
70     ///
71     /// Behavior is undefined if the current thread does not currently have
72     /// exclusive access.
73     #[inline]
74     pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
75
76     /// Destroys OS-related resources with this RWLock.
77     ///
78     /// Behavior is undefined if there are any currently active users of this
79     /// lock.
80     #[inline]
81     pub unsafe fn destroy(&self) { self.0.destroy() }
82 }