1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use core::cell::UnsafeCell;
use core::marker::Sync;
use core::ops::{Drop, Deref, DerefMut};
use core::fmt;
use core::option::Option::{self, None, Some};
use core::default::Default;
use crate::{arch, panic, process::IntrLockGuard};
use crate::process::my_cpu;
use crate::panic_println;
use crate::arch::hart_id;
use alloc::rc::Weak;
pub struct Mutex<T: ?Sized> {
lock: u32,
name: &'static str,
hart: UnsafeCell<i64>,
data: UnsafeCell<T>,
}
pub struct MutexGuard<'a, T: ?Sized + 'a> {
lock: &'a u32,
mutex: &'a Mutex<T>,
data: &'a mut T,
intr_lock: IntrLockGuard<'a>,
}
pub struct WeakMutexGuard<'a, T: ?Sized + 'a> {
mutex: &'a Mutex<T>
}
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
impl<T> Mutex<T> {
pub const fn new(user_data: T, name: &'static str) -> Mutex<T> {
Mutex {
lock: 0,
data: UnsafeCell::new(user_data),
hart: UnsafeCell::new(-1),
name,
}
}
pub fn into_inner(self) -> T {
let Mutex { data, .. } = self;
data.into_inner()
}
}
impl<T: ?Sized> Mutex<T> {
fn obtain_lock(&self) {
while arch::__sync_lock_test_and_set(&self.lock, 1) != 0 {}
arch::__sync_synchronize();
}
pub fn lock(&self) -> MutexGuard<T> {
let intr_lock = my_cpu().intr_lock.lock();
if unsafe { self.holding() } {
panic!("lock {}: hart {} already holding the lock!", self.name, arch::hart_id());
}
self.obtain_lock();
if self.lock != 1 {
panic!("lock {}: not locked!", self.name);
}
unsafe { *self.hart.get() = arch::hart_id() as i64; }
MutexGuard {
lock: &self.lock,
mutex: self,
data: unsafe { &mut *self.data.get() },
intr_lock,
}
}
unsafe fn holding(&self) -> bool {
let _intr_lock = my_cpu().intr_lock.lock();
if self.lock == 1 && *self.hart.get() == arch::hart_id() as i64 {
return true;
}
false
}
pub unsafe fn get(&self) -> &mut T {
&mut *self.data.get()
}
}
impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> {
type Target = T;
fn deref<'b>(&'b self) -> &'b T { &*self.data }
}
impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> {
fn deref_mut<'b>(&'b mut self) -> &'b mut T { &mut *self.data }
}
impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> {
fn drop(&mut self) {
if unsafe { !self.mutex.holding() } {
panic!("lock {}: unlock from another hart {}!", self.mutex.name, arch::hart_id());
}
unsafe { *self.mutex.hart.get() = -1; }
arch::__sync_synchronize();
arch::__sync_lock_release(&self.lock);
}
}
impl<'a, T: ?Sized> MutexGuard<'a, T> {
pub fn into_weak(self) -> WeakMutexGuard<'a, T> {
WeakMutexGuard {
mutex: self.mutex
}
}
}
impl<'a, T: ?Sized> WeakMutexGuard<'a, T> {
pub fn into_guard(self) -> MutexGuard<'a, T> {
self.mutex.lock()
}
}