-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpthread_mutex.rs
173 lines (148 loc) · 4.41 KB
/
pthread_mutex.rs
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
171
172
173
use pin_init::*;
use pin_project::pin_project;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::{
cell::UnsafeCell,
ops::{Deref, DerefMut},
};
use std::{io::Error, marker::PhantomPinned};
#[repr(transparent)]
struct RawMutex {
pthread: UnsafeCell<libc::pthread_mutex_t>,
}
unsafe impl Send for RawMutex {}
unsafe impl Sync for RawMutex {}
struct RawMutexGuard<'a>(&'a RawMutex);
impl Drop for RawMutex {
fn drop(&mut self) {
// Thanks to pin_init, we are certain that self is initialized already.
unsafe {
println!("drop");
libc::pthread_mutex_destroy(self.pthread.get());
}
}
}
impl RawMutex {
// Use pin_init's abstraction to provide a safe initializaiton.
pub fn new() -> impl Init<Self, Error> {
init_from_closure(|mut this| {
let ptr = this.get_mut().as_mut_ptr() as *mut libc::pthread_mutex_t;
unsafe {
ptr.write(libc::PTHREAD_MUTEX_INITIALIZER);
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
let ret = libc::pthread_mutexattr_init(attr.as_mut_ptr());
if ret != 0 {
return Err(this.init_err(Error::from_raw_os_error(ret)));
}
let ret =
libc::pthread_mutexattr_settype(attr.as_mut_ptr(), libc::PTHREAD_MUTEX_NORMAL);
if ret != 0 {
libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
return Err(this.init_err(Error::from_raw_os_error(ret)));
}
let ret = libc::pthread_mutex_init(ptr, attr.as_ptr());
libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
if ret != 0 {
return Err(this.init_err(Error::from_raw_os_error(ret)));
}
Ok(this.init_ok())
}
})
}
pub fn lock(&self) -> RawMutexGuard<'_> {
unsafe {
libc::pthread_mutex_lock(self.pthread.get());
RawMutexGuard(self)
}
}
}
impl Drop for RawMutexGuard<'_> {
fn drop(&mut self) {
unsafe {
libc::pthread_mutex_unlock(self.0.pthread.get());
}
}
}
// Now we can use pin_init to define a Mutex that wraps the RawMutex
#[pin_init]
struct Mutex<T> {
#[pin]
mutex: RawMutex,
#[pin]
data: UnsafeCell<T>,
}
unsafe impl<T: Send> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}
struct MutexGuard<'a, T>(RawMutexGuard<'a>, &'a mut T);
impl<'a, T> Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
self.1
}
}
impl<'a, T> DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
self.1
}
}
impl<T> Mutex<T> {
pub fn new<F>(value: F) -> impl Init<Self, Error>
where
F: Init<T, Error>,
{
init_pin!(Mutex {
mutex: RawMutex::new(),
data: init_pin!(UnsafeCell(value)),
})
}
pub fn lock(&self) -> Pin<MutexGuard<'_, T>> {
let g = self.mutex.lock();
unsafe { Pin::new_unchecked(MutexGuard(g, &mut *self.data.get())) }
}
pub fn get_inner(self: Pin<&mut Self>) -> Pin<&mut T> {
unsafe { Pin::new_unchecked(&mut *self.data.get()) }
}
}
// Can be used on tuple structs.
// Can be used together with pin_project.
#[pin_init]
#[pin_project]
struct Pinned<T>(T, #[pin] PhantomPinned);
#[pin_init]
#[pin_project]
struct TwoMutex {
#[pin]
a: Mutex<i32>,
#[pin]
b: Mutex<Pinned<i32>>,
}
fn main() {
{
let m = Box::pin_with(Mutex::new(1)).unwrap();
println!("{}", *m.lock());
*m.lock() = 2;
println!("{}", *m.lock());
*m.lock() = 3;
println!("{}", *m.lock());
}
{
// Even complex, nested pinned data structure can be safely
// created and initialized on the stack.
init_stack!(
m = TwoMutex {
a: Mutex::new(1),
b: Mutex::new(Pinned(1, PhantomPinned))
}
);
let mut m = m.unwrap();
println!("{}", *m.a.lock());
*m.a.lock() = 2;
// Use pin-projection to access unpinned fields
*m.b.lock().as_mut().project().0 = 2;
println!("{}", m.b.lock().0);
// Use pin-projection to access pinned fields.
*m.as_mut().project().b.get_inner().project().0 = 3;
println!("{}", m.b.lock().0);
}
}