Skip to content

Feature: GC-safe TLS#23

Closed
cpunion wants to merge 5 commits into
goplus:mainfrom
cpunion:feature/gc-safe-tls
Closed

Feature: GC-safe TLS#23
cpunion wants to merge 5 commits into
goplus:mainfrom
cpunion:feature/gc-safe-tls

Conversation

@cpunion
Copy link
Copy Markdown
Contributor

@cpunion cpunion commented Oct 14, 2025

No description provided.

@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 and garbage collector-safe implementation of Thread Local Storage (TLS) within the c/tls package. The primary goal is to enable the safe use of GC-managed pointers in thread-specific data structures, ensuring they are properly scanned and retained by the Boehm garbage collector. This is achieved by integrating with the GC's root management system, allowing developers to store complex, GC-aware objects in thread-local slots without risking premature collection. The new package offers a flexible API with generic types and optional destructors, catering to both GC-enabled and nogc build configurations.

Highlights

  • New GC-safe TLS Package: Introduced a new c/tls package that provides a robust and garbage collector-safe implementation of Thread Local Storage (TLS).
  • Boehm GC Integration: Integrated with the Boehm GC by exposing GC_add_roots and GC_remove_roots functions in c/bdwgc, allowing thread-local memory regions to be registered and deregistered as GC roots.
  • Generic TLS Handle: Provides a generic Handle[T] type for managing thread-local values, including support for optional destructors that execute when a thread's TLS slot is destroyed.
  • Nogc Build Variant: Includes a nogc build variant of the c/tls package, which offers the same TLS API but bypasses GC root registration for environments where the Boehm GC is not used.
  • Documentation and Tests: Added comprehensive unit tests (c/tls/tls_test.go) to verify functionality and new documentation (docs/gc-safe-tls.md) explaining the package's purpose and usage.
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.

Copy link
Copy Markdown

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

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 thread-local storage (TLS) implementation, primarily for use with llgo. It correctly integrates with the Boehm GC by registering TLS slots as GC roots, ensuring that objects stored in TLS are not prematurely collected. The implementation is thoughtfully split into versions for builds with and without GC using build tags. The changes are well-structured and accompanied by tests and documentation. My review includes a couple of suggestions to improve the clarity and correctness of the example code in the documentation.

Comment thread docs/gc-safe-tls.md Outdated

var threadStats = tls.Alloc[*stats](func(s **stats) {
// Destructor runs when the thread exits.
if s != nil && *s != nil {
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

In the destructor example, the check s != nil is redundant. The destructor is designed to always be called with a valid pointer to the slot's value storage, so s will never be nil. You can simplify the condition to just check if the stored value itself is non-nil.

Suggested change
if s != nil && *s != nil {
if *s != nil {

Comment thread docs/gc-safe-tls.md
var threadStats = tls.Alloc[*stats](func(s **stats) {
// Destructor runs when the thread exits.
if s != nil && *s != nil {
log.Printf("thread handled %d requests", (*s).handled)
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 example code uses log.Printf, but the log package is not imported in the code snippet. This makes the example incomplete and could be confusing for users. Please consider adding the import to the example to make it self-contained and runnable.

@cpunion
Copy link
Copy Markdown
Contributor Author

cpunion commented Oct 14, 2025

/review

@cpunion cpunion closed this Oct 14, 2025
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.

1 participant