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.
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.
11 use sys::rwlock as imp;
13 /// An OS-based reader-writer lock.
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);
21 /// Creates a new reader-writer lock for use.
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()) }
28 /// Acquires shared access to the underlying lock, blocking the current
31 /// Behavior is undefined if the rwlock has been moved between this and any
32 /// previous method call.
34 pub unsafe fn read(&self) { self.0.read() }
36 /// Attempts to acquire shared access to this lock, returning whether it
39 /// This function does not block the current thread.
41 /// Behavior is undefined if the rwlock has been moved between this and any
42 /// previous method call.
44 pub unsafe fn try_read(&self) -> bool { self.0.try_read() }
46 /// Acquires write access to the underlying lock, blocking the current thread
49 /// Behavior is undefined if the rwlock has been moved between this and any
50 /// previous method call.
52 pub unsafe fn write(&self) { self.0.write() }
54 /// Attempts to acquire exclusive access to this lock, returning whether it
57 /// This function does not block the current thread.
59 /// Behavior is undefined if the rwlock has been moved between this and any
60 /// previous method call.
62 pub unsafe fn try_write(&self) -> bool { self.0.try_write() }
64 /// Unlocks previously acquired shared access to this lock.
66 /// Behavior is undefined if the current thread does not have shared access.
68 pub unsafe fn read_unlock(&self) { self.0.read_unlock() }
70 /// Unlocks previously acquired exclusive access to this lock.
72 /// Behavior is undefined if the current thread does not currently have
75 pub unsafe fn write_unlock(&self) { self.0.write_unlock() }
77 /// Destroys OS-related resources with this RWLock.
79 /// Behavior is undefined if there are any currently active users of this
82 pub unsafe fn destroy(&self) { self.0.destroy() }