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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::{convert::From, marker::PhantomData, time::Duration};
use crate::{
binding::{thread, vm},
types::Value,
AnyException, AnyObject, Class, Float, NilClass, Object, VerifiedObject,
};
/// Ruby's `Mutex` (`Thread::Mutex`), for coordinating Ruby threads.
#[derive(Debug)]
#[repr(C)]
pub struct Mutex {
value: Value,
}
impl Mutex {
/// Creates an unlocked mutex (`rb_mutex_new`).
///
/// # Examples
///
/// ```
/// use rutie::{Mutex, VM};
/// # VM::init();
///
/// assert!(!Mutex::new().is_locked());
/// ```
pub fn new() -> Self {
Mutex::from(thread::mutex_new())
}
/// Locks the mutex, waiting for other threads to release it
/// (`rb_mutex_lock`), and returns a guard that unlocks it when dropped.
/// Returns the `ThreadError` when the current thread already holds it
/// or waiting would deadlock.
///
/// A Ruby exception unwinding through Rust frames does not run Rust
/// destructors, so when the code holding the lock calls Ruby methods
/// that may raise, prefer [`synchronize`](#method.synchronize).
///
/// # Examples
///
/// ```
/// use rutie::{Mutex, VM};
/// # VM::init();
///
/// let mutex = Mutex::new();
///
/// {
/// let _guard = mutex.lock().unwrap();
/// assert!(mutex.is_locked());
///
/// // Locking again from the same thread is an error, not a deadlock.
/// assert!(mutex.lock().is_err());
/// }
///
/// assert!(!mutex.is_locked());
/// ```
pub fn lock(&self) -> Result<MutexGuard<'_>, AnyException> {
let mutex = self.value();
vm::protect_value(|| thread::mutex_lock(mutex))
.map(|_| MutexGuard::new(self))
.map_err(AnyException::from)
}
/// Locks the mutex if it is free, returning `None` otherwise
/// (`rb_mutex_trylock`).
///
/// # Examples
///
/// ```
/// use rutie::{Mutex, VM};
/// # VM::init();
///
/// let mutex = Mutex::new();
/// let guard = mutex.try_lock().unwrap();
///
/// assert!(mutex.try_lock().is_none());
///
/// drop(guard);
/// assert!(mutex.try_lock().is_some());
/// ```
pub fn try_lock(&self) -> Option<MutexGuard<'_>> {
if thread::mutex_try_lock(self.value()) {
Some(MutexGuard::new(self))
} else {
None
}
}
/// Returns `true` if some thread holds the mutex (`rb_mutex_locked_p`).
///
/// # Examples
///
/// ```
/// use rutie::{Mutex, VM};
/// # VM::init();
///
/// let mutex = Mutex::new();
/// let _guard = mutex.lock().unwrap();
///
/// assert!(mutex.is_locked());
/// ```
pub fn is_locked(&self) -> bool {
thread::mutex_is_locked(self.value())
}
/// Runs `func` holding the mutex and returns its result
/// (`rb_mutex_synchronize`). The mutex is released even when `func`
/// raises a Ruby exception or panics; either is returned as `Err`.
///
/// # Examples
///
/// ```
/// use rutie::{Exception, Mutex, VM};
/// # VM::init();
///
/// let mutex = Mutex::new();
///
/// let answer = mutex.synchronize(|| {
/// assert!(mutex.is_locked());
/// 42
/// });
/// assert_eq!(answer.unwrap(), 42);
///
/// let failed = mutex.synchronize(|| unsafe { VM::eval_str("raise 'inside'") });
/// assert_eq!(failed.unwrap_err().message(), "inside");
/// assert!(!mutex.is_locked());
/// ```
pub fn synchronize<F, R>(&self, func: F) -> Result<R, AnyException>
where
F: FnOnce() -> R,
{
let mutex = self.value();
let mut result = None;
vm::protect_value(|| {
thread::mutex_synchronize(mutex, || {
result = Some(func());
NilClass::new().value()
})
})
.map_err(AnyException::from)?;
Ok(result.expect("synchronized block did not run"))
}
}
impl Default for Mutex {
fn default() -> Self {
Mutex::new()
}
}
/// Holds a [`Mutex`](struct.Mutex.html) locked; unlocks it when dropped.
///
/// It cannot be sent to another thread, since only the locking Ruby thread
/// may unlock.
#[derive(Debug)]
pub struct MutexGuard<'a> {
mutex: &'a Mutex,
_not_send: PhantomData<*const ()>,
}
impl<'a> MutexGuard<'a> {
fn new(mutex: &'a Mutex) -> Self {
MutexGuard {
mutex,
_not_send: PhantomData,
}
}
/// Releases the lock and sleeps until woken or until `timeout` passes,
/// then locks it again (Ruby's `Mutex#sleep`, `rb_mutex_sleep`).
///
/// # Examples
///
/// ```
/// use rutie::{Mutex, VM};
/// use std::time::Duration;
/// # VM::init();
///
/// let mutex = Mutex::new();
/// let guard = mutex.lock().unwrap();
///
/// guard.sleep(Some(Duration::from_millis(10))).unwrap();
///
/// assert!(mutex.is_locked());
/// ```
pub fn sleep(&self, timeout: Option<Duration>) -> Result<(), AnyException> {
let mutex = self.mutex.value();
let timeout = timeout
.map(|timeout| Float::new(timeout.as_secs_f64()).value())
.unwrap_or_else(|| NilClass::new().value());
vm::protect_value(|| thread::mutex_sleep(mutex, timeout))
.map(|_| ())
.map_err(AnyException::from)
}
}
impl<'a> Drop for MutexGuard<'a> {
fn drop(&mut self) {
let mutex = self.mutex.value();
// Only fails if the lock was already released some other way.
let _ = vm::protect_value(|| thread::mutex_unlock(mutex));
}
}
impl From<Value> for Mutex {
fn from(value: Value) -> Self {
Mutex { value }
}
}
impl Into<Value> for Mutex {
fn into(self) -> Value {
self.value
}
}
impl Into<AnyObject> for Mutex {
fn into(self) -> AnyObject {
AnyObject::from(self.value)
}
}
impl Object for Mutex {
#[inline]
fn value(&self) -> Value {
self.value
}
}
impl VerifiedObject for Mutex {
fn is_correct_type<T: Object>(object: &T) -> bool {
Class::from_existing("Mutex").case_equals(object)
}
fn error_message() -> &'static str {
"Error converting to Mutex"
}
}
impl PartialEq for Mutex {
fn eq(&self, other: &Self) -> bool {
self.equals(other)
}
}
#[cfg(test)]
mod tests {
use crate::{Exception, Mutex, Object, VM};
use std::time::Duration;
#[test]
fn test_mutex() {
crate::on_ruby_thread(|| {
let mutex = Mutex::new();
{
let guard = mutex.lock().unwrap();
assert!(mutex.try_lock().is_none());
guard.sleep(Some(Duration::from_millis(1))).unwrap();
assert!(mutex.is_locked());
}
assert!(!mutex.is_locked());
// Shared with Ruby code, which sees the same lock.
VM::global_set("$rutie_test_mutex", Mutex::from(mutex.value()));
let held = mutex
.synchronize(|| {
VM::eval("$rutie_test_mutex.owned?")
.unwrap()
.value()
.is_true()
})
.unwrap();
assert!(held);
let panicked = mutex.synchronize(|| -> i32 { panic!("in synchronize") });
assert_eq!(
panicked.unwrap_err().message(),
"Rust panic: in synchronize"
);
assert!(!mutex.is_locked());
assert!(VM::eval("Mutex.new")
.unwrap()
.try_convert_to::<Mutex>()
.is_ok());
VM::global_set("$rutie_test_mutex", crate::NilClass::new());
});
}
}