@@ -306,16 +306,34 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
306
306
return 0 ;
307
307
}
308
308
309
+ /* Helper to convert pthread_t to PyThread_ident_t. POSIX allows pthread_t to be
310
+ non-arithmetic, e.g., musl typedefs it as a pointer */
311
+ static PyThread_ident_t
312
+ _pthread_t_to_ident (pthread_t value ) {
313
+ #if SIZEOF_PTHREAD_T > SIZEOF_LONG
314
+ return (PyThread_ident_t ) * (unsigned long * ) & value ;
315
+ #else
316
+ PyThread_ident_t ident ;
317
+ #if defined(__linux__ ) && !defined(__GLIBC__ )
318
+ ident = (PyThread_ident_t ) (uintptr_t ) value ;
319
+ assert (pthread_equal (value , (pthread_t ) (uintptr_t ) ident ));
320
+ #else
321
+ ident = (PyThread_ident_t ) value ;
322
+ assert (pthread_equal (value , (pthread_t ) ident ));
323
+ #endif
324
+ return ident ;
325
+ #endif // SIZEOF_PTHREAD_T > SIZEOF_LONG
326
+ }
327
+
309
328
int
310
329
PyThread_start_joinable_thread (void (* func )(void * ), void * arg ,
311
330
PyThread_ident_t * ident , PyThread_handle_t * handle ) {
312
331
pthread_t th = (pthread_t ) 0 ;
313
332
if (do_start_joinable_thread (func , arg , & th )) {
314
333
return -1 ;
315
334
}
316
- * ident = ( PyThread_ident_t ) th ;
335
+ * ident = _pthread_t_to_ident ( th ) ;
317
336
* handle = (PyThread_handle_t ) th ;
318
- assert (th == (pthread_t ) * ident );
319
337
assert (th == (pthread_t ) * handle );
320
338
return 0 ;
321
339
}
@@ -328,11 +346,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
328
346
return PYTHREAD_INVALID_THREAD_ID ;
329
347
}
330
348
pthread_detach (th );
331
- #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
332
- return (unsigned long ) th ;
333
- #else
334
- return (unsigned long ) * (unsigned long * ) & th ;
335
- #endif
349
+ return (unsigned long ) _pthread_t_to_ident (th );;
336
350
}
337
351
338
352
int
@@ -357,8 +371,7 @@ PyThread_get_thread_ident_ex(void) {
357
371
if (!initialized )
358
372
PyThread_init_thread ();
359
373
threadid = pthread_self ();
360
- assert (threadid == (pthread_t ) (PyThread_ident_t ) threadid );
361
- return (PyThread_ident_t ) threadid ;
374
+ return _pthread_t_to_ident (threadid );
362
375
}
363
376
364
377
unsigned long
0 commit comments