For backtrace, use StaticMutex instead of a raw sys Mutex.

This commit is contained in:
Mara Bos 2020-10-07 13:54:16 +02:00
parent 5ded394553
commit 54a71e8954
2 changed files with 9 additions and 19 deletions

View File

@ -303,7 +303,8 @@ impl Backtrace {
// Capture a backtrace which start just before the function addressed by // Capture a backtrace which start just before the function addressed by
// `ip` // `ip`
fn create(ip: usize) -> Backtrace { fn create(ip: usize) -> Backtrace {
let _lock = lock(); // SAFETY: We don't attempt to lock this reentrantly.
let _lock = unsafe { lock() };
let mut frames = Vec::new(); let mut frames = Vec::new();
let mut actual_start = None; let mut actual_start = None;
unsafe { unsafe {
@ -408,7 +409,8 @@ impl Capture {
// Use the global backtrace lock to synchronize this as it's a // Use the global backtrace lock to synchronize this as it's a
// requirement of the `backtrace` crate, and then actually resolve // requirement of the `backtrace` crate, and then actually resolve
// everything. // everything.
let _lock = lock(); // SAFETY: We don't attempt to lock this reentrantly.
let _lock = unsafe { lock() };
for frame in self.frames.iter_mut() { for frame in self.frames.iter_mut() {
let symbols = &mut frame.symbols; let symbols = &mut frame.symbols;
let frame = match &frame.frame { let frame = match &frame.frame {

View File

@ -8,27 +8,15 @@ use crate::io;
use crate::io::prelude::*; use crate::io::prelude::*;
use crate::path::{self, Path, PathBuf}; use crate::path::{self, Path, PathBuf};
use crate::sync::atomic::{self, Ordering}; use crate::sync::atomic::{self, Ordering};
use crate::sys::mutex::Mutex; use crate::sys_common::mutex::StaticMutex;
/// Max number of frames to print. /// Max number of frames to print.
const MAX_NB_FRAMES: usize = 100; const MAX_NB_FRAMES: usize = 100;
pub fn lock() -> impl Drop { // SAFETY: Don't attempt to lock this reentrantly.
struct Guard; pub unsafe fn lock() -> impl Drop {
static LOCK: Mutex = Mutex::new(); static LOCK: StaticMutex = StaticMutex::new();
LOCK.lock()
impl Drop for Guard {
fn drop(&mut self) {
unsafe {
LOCK.unlock();
}
}
}
unsafe {
LOCK.lock();
Guard
}
} }
/// Prints the current backtrace. /// Prints the current backtrace.