Skip to content

Commit

Permalink
dbghlp: Make mutex name unique to the process
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Mar 31, 2023
1 parent a3406f9 commit befb41d
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ pub struct Init {
pub fn init() -> Result<Init, ()> {
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};

// Helper function for generating a name that's unique to the process.
fn mutex_name() -> [u8; 33] {
let mut name: [u8; 33] = *b"Local\\RustBacktraceMutex00000000\0";
let mut id = unsafe { GetCurrentProcessId() };
// Quick and dirty no alloc u32 to hex.
let mut index = name.len() - 1;
while id > 0 {
name[index - 1] = match id & 0xF {
h @ 0..=9 => 48 + h,
h => 65 + (h - 10),
} as u8;
id >>= 4;
index -= 1;
}
name
}

unsafe {
// First thing we need to do is to synchronize this function. This can
// be called concurrently from other threads or recursively within one
Expand Down Expand Up @@ -277,11 +294,8 @@ pub fn init() -> Result<Init, ()> {
static LOCK: AtomicUsize = AtomicUsize::new(0);
let mut lock = LOCK.load(SeqCst);
if lock == 0 {
lock = CreateMutexA(
ptr::null_mut(),
0,
"Local\\RustBacktraceMutex\0".as_ptr() as _,
) as usize;
let name = mutex_name();
lock = CreateMutexA(ptr::null_mut(), 0, name.as_ptr().cast::<i8>()) as usize;
if lock == 0 {
return Err(());
}
Expand Down

0 comments on commit befb41d

Please sign in to comment.