]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/rwlock.rs
2c0b1a45206c3345ee6fb2d8623d4290ade5fc69
[rust.git] / src / libstd / sys / sgx / rwlock.rs
1 // Copyright 2018 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 cell::UnsafeCell;
12
13 pub struct RWLock {
14     mode: UnsafeCell<isize>,
15 }
16
17 unsafe impl Send for RWLock {}
18 unsafe impl Sync for RWLock {} // FIXME
19
20 impl RWLock {
21     pub const fn new() -> RWLock {
22         RWLock {
23             mode: UnsafeCell::new(0),
24         }
25     }
26
27     #[inline]
28     pub unsafe fn read(&self) {
29         let mode = self.mode.get();
30         if *mode >= 0 {
31             *mode += 1;
32         } else {
33             rtabort!("rwlock locked for writing");
34         }
35     }
36
37     #[inline]
38     pub unsafe fn try_read(&self) -> bool {
39         let mode = self.mode.get();
40         if *mode >= 0 {
41             *mode += 1;
42             true
43         } else {
44             false
45         }
46     }
47
48     #[inline]
49     pub unsafe fn write(&self) {
50         let mode = self.mode.get();
51         if *mode == 0 {
52             *mode = -1;
53         } else {
54             rtabort!("rwlock locked for reading")
55         }
56     }
57
58     #[inline]
59     pub unsafe fn try_write(&self) -> bool {
60         let mode = self.mode.get();
61         if *mode == 0 {
62             *mode = -1;
63             true
64         } else {
65             false
66         }
67     }
68
69     #[inline]
70     pub unsafe fn read_unlock(&self) {
71         *self.mode.get() -= 1;
72     }
73
74     #[inline]
75     pub unsafe fn write_unlock(&self) {
76         *self.mode.get() += 1;
77     }
78
79     #[inline]
80     pub unsafe fn destroy(&self) {
81     }
82 }