Skip to content

Commit dab8b61

Browse files
Merge pull request #518 from ChrisDenton/mutex-name
dbghlp: Make mutex name unique to the process
2 parents 2eaa602 + e90def2 commit dab8b61

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/dbghelp.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ pub struct Init {
239239
pub fn init() -> Result<Init, ()> {
240240
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
241241

242+
// Helper function for generating a name that's unique to the process.
243+
fn mutex_name() -> [u8; 33] {
244+
let mut name: [u8; 33] = *b"Local\\RustBacktraceMutex00000000\0";
245+
let mut id = unsafe { GetCurrentProcessId() };
246+
// Quick and dirty no alloc u32 to hex.
247+
let mut index = name.len() - 1;
248+
while id > 0 {
249+
name[index - 1] = match (id & 0xF) as u8 {
250+
h @ 0..=9 => b'0' + h,
251+
h => b'A' + (h - 10),
252+
};
253+
id >>= 4;
254+
index -= 1;
255+
}
256+
name
257+
}
258+
242259
unsafe {
243260
// First thing we need to do is to synchronize this function. This can
244261
// be called concurrently from other threads or recursively within one
@@ -277,11 +294,8 @@ pub fn init() -> Result<Init, ()> {
277294
static LOCK: AtomicUsize = AtomicUsize::new(0);
278295
let mut lock = LOCK.load(SeqCst);
279296
if lock == 0 {
280-
lock = CreateMutexA(
281-
ptr::null_mut(),
282-
0,
283-
"Local\\RustBacktraceMutex\0".as_ptr() as _,
284-
) as usize;
297+
let name = mutex_name();
298+
lock = CreateMutexA(ptr::null_mut(), 0, name.as_ptr().cast::<i8>()) as usize;
285299
if lock == 0 {
286300
return Err(());
287301
}

0 commit comments

Comments
 (0)