Skip to content

Commit 140a4d8

Browse files
committed
simplify conversion logic
1 parent dc7c497 commit 140a4d8

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Python/thread_pthread.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,21 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
307307
}
308308

309309
/* 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 */
310+
non-arithmetic, e.g., musl typedefs it as a pointer. */
311311
static PyThread_ident_t
312312
_pthread_t_to_ident(pthread_t value) {
313-
#if SIZEOF_PTHREAD_T > SIZEOF_LONG
314-
return (PyThread_ident_t) *(unsigned long *) &value;
313+
// Cast through an integer type of the same size to avoid sign-extension.
314+
#if SIZEOF_PTHREAD_T == SIZEOF_VOID_P
315+
return (uintptr_t) value;
316+
#elif SIZEOF_PTHREAD_T == SIZEOF_LONG
317+
return (unsigned long) value;
318+
#elif SIZEOF_PTHREAD_T == SIZEOF_INT
319+
return (unsigned int) value;
320+
#elif SIZEOF_PTHREAD_T == SIZEOF_LONG_LONG
321+
return (unsigned long long) value;
315322
#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+
#error "Unsupported SIZEOF_PTHREAD_T value"
323324
#endif
324-
return ident;
325-
#endif // SIZEOF_PTHREAD_T > SIZEOF_LONG
326325
}
327326

328327
int

0 commit comments

Comments
 (0)