Skip to content

Conversation

@tqchen
Copy link
Member

@tqchen tqchen commented Dec 5, 2025

In DSL settings, sometimes it is helpful to have ability to do thread-safe initialization of static handles.
This PR adds two functions to make it easy to do so without stdcpp run_once support

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @tqchen, 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 mechanism for thread-safe, one-time initialization and deinitialization of static handles within the TVM FFI environment. This is particularly useful in DSL contexts where ensuring a resource is initialized and deinitialized exactly once, even across multiple threads, is critical, without relying on C++11 std::call_once features. The new functions, TVMFFIEnvHandleInitOnce and TVMFFIEnvHandleDeinitOnce, provide a portable and reliable solution for managing resource lifecycles in concurrent environments.

Highlights

  • New API for Thread-Safe Handle Initialization: Introduced TVMFFIEnvHandleInitOnce to provide a thread-safe mechanism for initializing static handles exactly once, particularly useful in DSL settings where stdcpp run_once support might not be available.
  • New API for Thread-Safe Handle Deinitialization: Added TVMFFIEnvHandleDeinitOnce to complement the initialization, ensuring thread-safe, once-only deinitialization of handles.
  • Robust Implementation: The implementation leverages platform-specific atomic operations (e.g., InterlockedCompareExchangePointer for MSVC, __atomic_load_n for GCC/Clang) and std::mutex to guarantee thread safety and correct 'once' semantics.
  • Comprehensive Testing: Extensive unit tests have been added to cover various scenarios, including successful initialization/deinitialization, idempotency, error handling, and multi-threaded race conditions, ensuring the reliability of the new functions.
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
Contributor

@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 thread-safe handle initialization and deinitialization functions, TVMFFIEnvHandleInitOnce and TVMFFIEnvHandleDeinitOnce. The intent is valuable, and the inclusion of comprehensive tests is a great step. However, there are critical flaws in the current implementation. TVMFFIEnvHandleInitOnce employs a single global mutex, which will serialize all initializations and create a significant performance bottleneck. TVMFFIEnvHandleDeinitOnce contains a race condition in its lock-free logic that could lead to severe issues like double-frees. I have provided suggestions to fix these critical problems. There is also a minor naming inconsistency that I've pointed out to improve code clarity.

@tqchen
Copy link
Member Author

tqchen commented Dec 5, 2025

/gemini review

Copy link
Contributor

@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 two new thread-safe helper functions, TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce, to manage the lifecycle of static handles. The implementation correctly uses a double-checked locking pattern for initialization and an atomic exchange for deinitialization, ensuring thread safety. The changes are accompanied by comprehensive single-threaded and multi-threaded tests. My feedback focuses on improving the clarity of function pointer parameter names in the implementation to align with the public C API header, which will enhance code readability and maintainability.

@tqchen
Copy link
Member Author

tqchen commented Dec 5, 2025

/gemini review

@tqchen tqchen changed the title [ENV] Introduce TVMFFIEnvHandleInitOnce [C-API] Introduce TVMFFIEnvHandleInitOnce Dec 5, 2025
@tqchen tqchen changed the title [C-API] Introduce TVMFFIEnvHandleInitOnce [C-API] Introduce TVMFFIHandleInitOnce Dec 5, 2025
Copy link
Contributor

@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 two new thread-safe helper functions, TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce, for one-time initialization and deinitialization of static handles. The implementation uses a double-checked locking pattern for initialization and an atomic exchange for deinitialization. The changes also include comprehensive unit tests, covering single-threaded and multi-threaded scenarios.

My review focuses on the thread-safety implementation and test quality. I've identified a potential performance issue in TVMFFIHandleInitOnce due to the use of a single global mutex, which could cause contention. I've also suggested an improvement to the multi-threaded tests to make them more robust. Overall, the changes are well-structured and the new functionality is a useful addition.

In DSL settings, sometimes it is helpful to have ability to do thread-safe initialization of static handles.
This PR adds two functions to make it easy to do so without stdcpp run_once support.
@tqchen
Copy link
Member Author

tqchen commented Dec 5, 2025

/gemini review

Copy link
Contributor

@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 TVMFFIHandleInitOnce and TVMFFIHandleDeinitOnce to provide thread-safe initialization and deinitialization of static handles, which is particularly useful for DSLs without native support for this. The implementation uses a double-checked locking pattern with a static mutex for initialization and atomic exchange for deinitialization. The changes are accompanied by a comprehensive set of single-threaded and multi-threaded tests.

The implementation is solid, but I have a couple of suggestions for improvement. The TVMFFIHandleInitOnce function uses a single global mutex, which could become a performance bottleneck if many different handles are initialized concurrently. In the tests, global static variables are used to share state with C-style callbacks, which could be problematic if tests are run in parallel. I've left specific comments with suggestions on how to address these points.

@tqchen tqchen merged commit 25c25ae into apache:main Dec 5, 2025
7 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