]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/rwlock.rs
SGX target: implement synchronization primitives and threading
[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     #[unstable(feature = "sys_internals", issue = "0")] // FIXME: min_const_fn
26     pub const fn new() -> RWLock { RWLock(imp::RWLock::new()) }
27
28     /// Acquires shared access to the underlying lock, blocking the current
29     /// thread to do so.
30     ///
31     /// Behavior is undefined if the rwlock has been moved between this and any
32     /// previous method call.
33     #[inline]
34     pub unsafe fn read(&self) { self.0.read() }
35
36     /// Attempts to acquire shared access to this lock, returning whether it
37     /// succeeded or not.
38     ///
39     /// This function does not block the current thread.
40     ///
41     /// Behavior is undefined if the rwlock has been moved between this and any
42     /// previous method call.
43     #[inline]
44     pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
45
46     /// Acquires write access to the underlying lock, blocking the current thread
47     /// to do so.
48     ///
49     /// Behavior is undefined if the rwlock has been moved between this and any
50     /// previous method call.
51     #[inline]
52     pub unsafe fn write(&self) { self.0.write() }
53
54     /// Attempts to acquire exclusive access to this lock, returning whether it
55     /// succeeded or not.
56     ///
57     /// This function does not block the current thread.
58     ///
59     /// Behavior is undefined if the rwlock has been moved between this and any
60     /// previous method call.
61     #[inline]
62     pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
63
64     /// Unlocks previously acquired shared access to this lock.
65     ///
66     /// Behavior is undefined if the current thread does not have shared access.
67     #[inline]
68     pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
69
70     /// Unlocks previously acquired exclusive access to this lock.
71     ///
72     /// Behavior is undefined if the current thread does not currently have
73     /// exclusive access.
74     #[inline]
75     pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
76
77     /// Destroys OS-related resources with this RWLock.
78     ///
79     /// Behavior is undefined if there are any currently active users of this
80     /// lock.
81     #[inline]
82     pub unsafe fn destroy(&self) { self.0.destroy() }
83 }