Skip to content

tls: add GC-safe TLS helper - #1

Open
cpunion wants to merge 16 commits into
mainfrom
feature/gc-safe-tls
Open

tls: add GC-safe TLS helper#1
cpunion wants to merge 16 commits into
mainfrom
feature/gc-safe-tls

Conversation

@cpunion

@cpunion cpunion commented Oct 14, 2025

Copy link
Copy Markdown
Owner

Summary

  • add c/tls package that wraps pthread TLS and registers slots with Boehm when enabled
  • expose GC_add_roots/GC_remove_roots via c/bdwgc so slot registration compiles
  • document usage in docs/gc-safe-tls.md and add llgo-only tests

Testing

  • cd runtime && ../llgo.sh test ./internal/clite/tls
  • go test -tags llgo ./c/tls

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @cpunion, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a robust, garbage collector-aware Thread Local Storage (TLS) utility, refines C type mappings for enhanced cross-platform compatibility, and overhauls the c/clang bindings for greater efficiency. A major focus of this update is the substantial expansion of embedded USB device class implementations, adding comprehensive support for CDC, HID (keyboard, mouse, joystick), MIDI, and MSC, alongside a more modular and portable C standard library integration.

Highlights

  • GC-Safe Thread Local Storage (TLS): Introduced a new c/tls package that provides a helper for managing Thread Local Storage (TLS) in a garbage collector-safe manner. This ensures that pointers stored in TLS are visible to the Boehm GC, preventing premature reclamation of memory.
  • Improved C Type Definitions: Refactored C integer type definitions (Long, Ulong) in c/c.go to be platform-specific, using build tags to select appropriate sizes (e.g., int32 for 32-bit Unix/Windows/WASM, int64 for 64-bit Unix). This enhances portability and correctness across different architectures.
  • Simplified Clang Bindings: Streamlined the c/clang package by removing intermediate C++ wrapper files (_wrap/cursor.cpp, _wrap/llgo_check.cpp, _wrap/llgo_check.go) and directly linking Go methods to native clang_ functions. This reduces complexity and potential overhead.
  • Expanded USB Device Class Support: Significantly extended embedded USB device class support by adding new packages and descriptors for Communication Device Class (CDC), Human Interface Device (HID) including keyboard, mouse, and joystick, MIDI, and Mass Storage Class (MSC). This enables a wider range of USB peripheral functionalities.
  • Refactored OS and C Standard Library Bindings: Restructured the c/os and c/stdio packages to use platform-specific files, improving modularity and maintainability. This includes new files for Darwin, non-WASM, and WASM builds, and new functions for directory and file system operations.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/go.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a GC-safe TLS helper in the c/tls package, which is a significant enhancement for managing thread-local storage in a garbage-collected environment. The changes also include substantial refactoring in the c/clang and c/os packages to improve portability and simplify C interoperability, moving away from CGo-style wrappers to more direct llgo:link directives. The addition of platform-specific type definitions is a good step towards better cross-platform compatibility. Overall, the changes are well-structured and improve the codebase. I have a few minor suggestions for improvement.

Comment thread c/tls/tls_test.go
func TestAllocSetGetClear(t *testing.T) {
var mu sync.Mutex
var destructCount int
handle := tls.Alloc[*slotValue](func(v **slotValue) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The destructor's signature func(v **slotValue) is a bit confusing. While correct because T is *slotValue, it might be clearer to define T as slotValue and then use *slotValue in the destructor. This would make the API more intuitive for users who might not expect a double pointer. However, if the goal is to allow modification of the pointer itself (like nilling it out), the current approach is valid but could benefit from a comment explaining why a double pointer is used.

Comment thread c/os/os.go
Comment on lines +246 to +247
//go:linkname Syscall C.syscall
func Syscall(sysno c.Long, __llgo_va_list ...any) c.Long

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The Syscall function is a powerful but potentially unsafe feature. It would be beneficial to add a prominent warning in the documentation about its platform-dependent nature and the risks associated with its use, such as kernel panics or security vulnerabilities if used incorrectly. This will help prevent misuse by developers who may not be fully aware of the implications.

Comment thread c/os/os_darwin.go
Comment on lines +42 to +43
//go:linkname Clearenv C.llgoClearenv
func Clearenv() c.Int

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The function Clearenv is linked to C.llgoClearenv here, but in c/os/os_other.go it's linked to C.clearenv. This inconsistency could lead to confusion or maintenance issues. It would be better to use the same C function name across all platforms if possible, or if not, to document the reason for the difference.

@cpunion
cpunion force-pushed the feature/gc-safe-tls branch from 0e64df5 to d7433ee Compare October 14, 2025 22:46
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.

3 participants