mirror of
https://github.com/servo/servo.git
synced 2025-08-12 08:55:32 +01:00
Auto merge of #12637 - asajeffrey:constellation-use-reentrant-logging-mutex, r=emilio
Replaced mutex in constellation logging by a reentrant mutex. <!-- Please describe your changes on the following line: --> The double-panic in #12553 may be caused by using a non-reentrant lock, which panics on reetry. This PR adds a reentrant lock type (slightly annoyingly, the implementation in std isn't exported) and uses it for logging. cc @jdm --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #12619. - [X] These changes do not require tests because they are designed to remove a class of intermittents. <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12637) <!-- Reviewable:end -->
This commit is contained in:
commit
fdb1e511bd
6 changed files with 319 additions and 5 deletions
|
@ -8,4 +8,5 @@ extern crate util;
|
|||
|
||||
mod opts;
|
||||
mod prefs;
|
||||
mod remutex;
|
||||
mod thread;
|
||||
|
|
89
tests/unit/util/remutex.rs
Normal file
89
tests/unit/util/remutex.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// These tests came from https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use util::remutex::{ReentrantMutex, ReentrantMutexGuard};
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
let m = ReentrantMutex::new(());
|
||||
{
|
||||
let a = m.lock().unwrap();
|
||||
{
|
||||
let b = m.lock().unwrap();
|
||||
{
|
||||
let c = m.lock().unwrap();
|
||||
assert_eq!(*c, ());
|
||||
}
|
||||
assert_eq!(*b, ());
|
||||
}
|
||||
assert_eq!(*a, ());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_mutex() {
|
||||
let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
|
||||
let m2 = m.clone();
|
||||
let lock = m.lock().unwrap();
|
||||
let child = thread::spawn(move || {
|
||||
let lock = m2.lock().unwrap();
|
||||
assert_eq!(*lock.borrow(), 4950);
|
||||
});
|
||||
for i in 0..100 {
|
||||
let lock = m.lock().unwrap();
|
||||
*lock.borrow_mut() += i;
|
||||
}
|
||||
drop(lock);
|
||||
child.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trylock_works() {
|
||||
let m = Arc::new(ReentrantMutex::new(()));
|
||||
let m2 = m.clone();
|
||||
let _lock = m.try_lock().unwrap();
|
||||
let _lock2 = m.try_lock().unwrap();
|
||||
thread::spawn(move || {
|
||||
let lock = m2.try_lock();
|
||||
assert!(lock.is_err());
|
||||
}).join().unwrap();
|
||||
let _lock3 = m.try_lock().unwrap();
|
||||
}
|
||||
|
||||
pub struct Answer<'a>(pub ReentrantMutexGuard<'a, RefCell<u32>>);
|
||||
impl<'a> Drop for Answer<'a> {
|
||||
fn drop(&mut self) {
|
||||
*self.0.borrow_mut() = 42;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poison_works() {
|
||||
let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
|
||||
let mc = m.clone();
|
||||
let result = thread::spawn(move ||{
|
||||
let lock = mc.lock().unwrap();
|
||||
*lock.borrow_mut() = 1;
|
||||
let lock2 = mc.lock().unwrap();
|
||||
*lock.borrow_mut() = 2;
|
||||
let _answer = Answer(lock2);
|
||||
println!("Intentionally panicking.");
|
||||
panic!("What the answer to my lifetimes dilemma is?");
|
||||
}).join();
|
||||
assert!(result.is_err());
|
||||
let r = m.lock().err().unwrap().into_inner();
|
||||
assert_eq!(*r.borrow(), 42);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue