File tree Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -239,6 +239,23 @@ pub struct Init {
239
239
pub fn init ( ) -> Result < Init , ( ) > {
240
240
use core:: sync:: atomic:: { AtomicUsize , Ordering :: SeqCst } ;
241
241
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
+
242
259
unsafe {
243
260
// First thing we need to do is to synchronize this function. This can
244
261
// be called concurrently from other threads or recursively within one
@@ -277,11 +294,8 @@ pub fn init() -> Result<Init, ()> {
277
294
static LOCK : AtomicUsize = AtomicUsize :: new ( 0 ) ;
278
295
let mut lock = LOCK . load ( SeqCst ) ;
279
296
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 ;
285
299
if lock == 0 {
286
300
return Err ( ( ) ) ;
287
301
}
You can’t perform that action at this time.
0 commit comments