Skip to content

Commit 54a71e8

Browse files
committed
For backtrace, use StaticMutex instead of a raw sys Mutex.
1 parent 5ded394 commit 54a71e8

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

Diff for: library/std/src/backtrace.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ impl Backtrace {
303303
// Capture a backtrace which start just before the function addressed by
304304
// `ip`
305305
fn create(ip: usize) -> Backtrace {
306-
let _lock = lock();
306+
// SAFETY: We don't attempt to lock this reentrantly.
307+
let _lock = unsafe { lock() };
307308
let mut frames = Vec::new();
308309
let mut actual_start = None;
309310
unsafe {
@@ -408,7 +409,8 @@ impl Capture {
408409
// Use the global backtrace lock to synchronize this as it's a
409410
// requirement of the `backtrace` crate, and then actually resolve
410411
// everything.
411-
let _lock = lock();
412+
// SAFETY: We don't attempt to lock this reentrantly.
413+
let _lock = unsafe { lock() };
412414
for frame in self.frames.iter_mut() {
413415
let symbols = &mut frame.symbols;
414416
let frame = match &frame.frame {

Diff for: library/std/src/sys_common/backtrace.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,15 @@ use crate::io;
88
use crate::io::prelude::*;
99
use crate::path::{self, Path, PathBuf};
1010
use crate::sync::atomic::{self, Ordering};
11-
use crate::sys::mutex::Mutex;
11+
use crate::sys_common::mutex::StaticMutex;
1212

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

16-
pub fn lock() -> impl Drop {
17-
struct Guard;
18-
static LOCK: Mutex = Mutex::new();
19-
20-
impl Drop for Guard {
21-
fn drop(&mut self) {
22-
unsafe {
23-
LOCK.unlock();
24-
}
25-
}
26-
}
27-
28-
unsafe {
29-
LOCK.lock();
30-
Guard
31-
}
16+
// SAFETY: Don't attempt to lock this reentrantly.
17+
pub unsafe fn lock() -> impl Drop {
18+
static LOCK: StaticMutex = StaticMutex::new();
19+
LOCK.lock()
3220
}
3321

3422
/// Prints the current backtrace.

0 commit comments

Comments
 (0)