Skip to content

MemPoolTypes: portable thread-id cast for logging (pointer pthread_t)#3040

Open
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:fix/mempool-pointer-threadid
Open

MemPoolTypes: portable thread-id cast for logging (pointer pthread_t)#3040
tomjn wants to merge 1 commit into
beyond-all-reason:masterfrom
tomjn:fix/mempool-pointer-threadid

Conversation

@tomjn

@tomjn tomjn commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

What

StablePosAllocator::Allocate logs the current thread id:

static_cast<uint32_t>(Threading::GetCurrentThreadId())

NativeThreadId is pthread_t — an integer on Linux but a pointer (_opaque_pthread_t*) on macOS. The direct static_cast to uint32_t is therefore invalid on macOS and fails to compile under GCC:

rts/System/MemPoolTypes.h:434: error: invalid 'static_cast' from type
'Threading::NativeThreadId' {aka '_opaque_pthread_t*'} to type 'uint32_t'

This casts via uintptr_t first (valid for both pointer and integer thread-id representations), then narrows to uint32_t for the log line.

Notes

  • The log line is debug-only (reportWork is false), but the template body still has to compile, so the cast must be valid on every platform.
  • No behavior change on Linux/Windows — uintptr_t(integer) is just a widening cast there.
  • Surfaced building the headless unitsync target on macOS with GCC.

@sprunk

sprunk commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Looks ok but I think the static_cast <uint32_t> (x) may have been there rather than the C-style uint32_t(x) because the C-style cast also fails to compile on some other target.

@tomjn tomjn force-pushed the fix/mempool-pointer-threadid branch from 2e7948c to f143b15 Compare June 20, 2026 15:10
@sprunk

sprunk commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Looks more ok than previous, but this kind of helper sounds like it belongs in the threading class (GetAsU32 or something)

StablePosAllocator::Allocate logs the current thread id. NativeThreadId is
pthread_t, an integer on Linux and a pointer (_opaque_pthread_t*) on macOS,
so static_cast<uint32_t>(GetCurrentThreadId()) failed to compile under GCC
on macOS:

  MemPoolTypes.h: error: invalid 'static_cast' from type
  'Threading::NativeThreadId' {aka '_opaque_pthread_t*'} to type 'uint32_t'

Add the conversion next to NativeThreadId in Threading: a small templated
ThreadIdAsU32() (so `if constexpr` actually selects a valid cast per target
-- it would not be discarded in a non-template function), plus an inline
GetCurrentThreadIdAsU32() wrapper. MemPoolTypes now calls that instead of
casting inline. reinterpret_cast for the pointer case, the original
static_cast otherwise; no codegen change on Linux/Windows.

Assisted by Claude Code; verified the cast compiles for pointer and
integer thread-id types on macOS GCC.
@tomjn tomjn force-pushed the fix/mempool-pointer-threadid branch from f143b15 to 69c26de Compare June 21, 2026 13:25

@sprunk sprunk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok. I feel like it could be cleaner still but let's not bother for something that is ultimately just printing a log.

@sprunk sprunk added refactor Internal code cleanup; paying off technical debt; janitorial work. big mac Part of the big push to support Mac. labels Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

big mac Part of the big push to support Mac. refactor Internal code cleanup; paying off technical debt; janitorial work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants