]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/mutex.rs
Add x86_64-fortanix-unknown-sgx target to libstd and dependencies
[rust.git] / src / libstd / sys / sgx / mutex.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 Mutex {
14     locked: UnsafeCell<bool>,
15 }
16
17 unsafe impl Send for Mutex {}
18 unsafe impl Sync for Mutex {} // FIXME
19
20 impl Mutex {
21     pub const fn new() -> Mutex {
22         Mutex { locked: UnsafeCell::new(false) }
23     }
24
25     #[inline]
26     pub unsafe fn init(&mut self) {
27     }
28
29     #[inline]
30     pub unsafe fn lock(&self) {
31         let locked = self.locked.get();
32         assert!(!*locked, "cannot recursively acquire mutex");
33         *locked = true;
34     }
35
36     #[inline]
37     pub unsafe fn unlock(&self) {
38         *self.locked.get() = false;
39     }
40
41     #[inline]
42     pub unsafe fn try_lock(&self) -> bool {
43         let locked = self.locked.get();
44         if *locked {
45             false
46         } else {
47             *locked = true;
48             true
49         }
50     }
51
52     #[inline]
53     pub unsafe fn destroy(&self) {
54     }
55 }
56
57 // FIXME
58 pub struct ReentrantMutex {
59 }
60
61 impl ReentrantMutex {
62     pub unsafe fn uninitialized() -> ReentrantMutex {
63         ReentrantMutex { }
64     }
65
66     pub unsafe fn init(&mut self) {}
67
68     pub unsafe fn lock(&self) {}
69
70     #[inline]
71     pub unsafe fn try_lock(&self) -> bool {
72         true
73     }
74
75     pub unsafe fn unlock(&self) {}
76
77     pub unsafe fn destroy(&self) {}
78 }