Skip to content

Use portable atomic types for the registry refcount and PC-Talker braille context - #157

Merged
ethindp merged 2 commits into
ethindp:masterfrom
trypsynth:portable-atomics
Oct 7, 2026
Merged

ethindp merged 2 commits into
ethindp:masterfrom
trypsynth:portable-atomics

Conversation

@trypsynth

Copy link
Copy Markdown
Contributor

std::atomic_unsigned_lock_free is not available in libstdc++ before GCC 13, so Prism does not compile with GCC 12. GCC 12 is the newest GCC that Ubuntu 22.04 supplies.

This change uses std::atomic<unsigned> for the registry reference count. The PC-Talker backend stores a ULONGLONG from QueryUnbiasedInterruptTime in its braille context. On Windows, std::atomic_unsigned_lock_free holds an unsigned long, which is 32 bits, so the value was cut short. The braille context now uses std::atomic<ULONGLONG>.

@ethindp

ethindp commented Oct 7, 2026

Copy link
Copy Markdown
Owner

I'm not entirely sure if this would unblock Ubuntu 22.04 (and I'm not even really sure why we would even want to support such old releases of Linux).

As for std::atomic_unsigned_lock_free, this is highly unusual, since the C++23 standard defines std::atomic_unsigned_lock_free as:

The type aliases atomic_signed_lock_free and atomic_unsigned_lock_free name specializations of atomic whose template arguments are integral types, respectively signed and unsigned, and whose is_always_lock_free property is true.

Hence why I used it. I'm a bit concerned that this may introduce false atomicity (i.e., the STL will convert it into multiple atomic loads or stores or will use locks in place of actual atomics). That said, the value of QueryUnbiasedInterruptTime being sliced in half is not at all acceptable. Your thoughts?

@ethindp

ethindp commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Also, is there a reason beyond GCC 12 compat you wanted the registry reference count to be std::atomic<unsigned>? If I remember right, the unsigned specifier without a type is implementation-defined. I selected std::atomic_unsigned_lock_free for the registry initially for a couple reasons:

  1. I wanted reference counting to always be lock-free with respect to all other atomic operations.
  2. I didn't really care about what "type" the variable was, necessarily; it is a counter only and acts as an indicator to when we should free the back-end from the cache.

I'm not necessarily against it, just a bit confused on why that specific change was made.

@trypsynth

Copy link
Copy Markdown
Contributor Author

Thanks for looking at it!

It does unblock 22.04: I'm working on integrating Prism into NVGT, and NVGT's CI now compiles it with GCC 12 on Ubuntu 22.04 with this change applied. NVGT builds its Linux releases on 22.04 on purpose, since binaries built against an older glibc run on more distros, so games made with it work for more players. Debian 12 also defaults to GCC 12.

On unsigned: it's exactly unsigned int by the standard, only its width is up to the implementation (32 bits on everything Prism targets). The aliases' underlying type is implementation-chosen too, which is how the PC-Talker value got cut in half (it's unsigned long on MSVC).

Your point about lock-freedom is fair though, I don't want to lose that guarantee. A few ways I could go:

  1. Keep std::atomic<unsigned> and std::atomic<ULONGLONG>, but add static_assert(...::is_always_lock_free) to each, so the build fails anywhere they wouldn't be lock-free.
  2. Keep std::atomic_unsigned_lock_free when __cpp_lib_atomic_lock_free_type_aliases is defined, and only fall back to the plain types (with the same static_assert) on older standard libraries. PC-Talker would still move to a 64-bit type either way.
  3. Drop the refcount change and only fix the PC-Talker truncation here, and I'll keep carrying the GCC 12 change in NVGT.

Which would you prefer? Happy to update the PR whichever way.

@ethindp

ethindp commented Oct 7, 2026

Copy link
Copy Markdown
Owner

I think option 2 might be a good middle ground. And glad to know it unblocks 22.04, I was worried I'd have to do a bunch of C++20 polyfilling or something. Which may still be needed as I hope to take advantage of more C++23 features in future since compilers have sufficiently implemented the standard that it is now a practical and reasonable target.

Feel free to give option 2 a try and let's see what breaks.

@ethindp
ethindp merged commit a5c45e6 into ethindp:master Oct 7, 2026
63 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants