Skip to content

Commit

Permalink
Rollup merge of rust-lang#77646 - fusion-engineering-forks:use-static…
Browse files Browse the repository at this point in the history
…-mutex, r=dtolnay

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

The code used the very unsafe `sys::mutex::Mutex` directly, and built its own unlock-on-drop wrapper around it. The StaticMutex wrapper already provides that and is easier to use safely.

@rustbot modify labels: +T-libs +C-cleanup
  • Loading branch information
Dylan-DPC committed Oct 15, 2020
2 parents 235fb8b + 54a71e8 commit 143aa29
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
6 changes: 4 additions & 2 deletions library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ impl Backtrace {
// Capture a backtrace which start just before the function addressed by
// `ip`
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 actual_start = None;
unsafe {
Expand Down Expand Up @@ -408,7 +409,8 @@ impl Capture {
// Use the global backtrace lock to synchronize this as it's a
// requirement of the `backtrace` crate, and then actually resolve
// everything.
let _lock = lock();
// SAFETY: We don't attempt to lock this reentrantly.
let _lock = unsafe { lock() };
for frame in self.frames.iter_mut() {
let symbols = &mut frame.symbols;
let frame = match &frame.frame {
Expand Down
22 changes: 5 additions & 17 deletions library/std/src/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,15 @@ use crate::io;
use crate::io::prelude::*;
use crate::path::{self, Path, PathBuf};
use crate::sync::atomic::{self, Ordering};
use crate::sys::mutex::Mutex;
use crate::sys_common::mutex::StaticMutex;

/// Max number of frames to print.
const MAX_NB_FRAMES: usize = 100;

pub fn lock() -> impl Drop {
struct Guard;
static LOCK: Mutex = Mutex::new();

impl Drop for Guard {
fn drop(&mut self) {
unsafe {
LOCK.unlock();
}
}
}

unsafe {
LOCK.lock();
Guard
}
// SAFETY: Don't attempt to lock this reentrantly.
pub unsafe fn lock() -> impl Drop {
static LOCK: StaticMutex = StaticMutex::new();
LOCK.lock()
}

/// Prints the current backtrace.
Expand Down

0 comments on commit 143aa29

Please sign in to comment.