Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit init of Thread and Fiber class variables #15369

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Jan 24, 2025

Adds Thread.init, Fiber.init that explicitly initialize the class variables defined on Thread and Fiber.

In order to implement #14905 we'll need the ability to access Fiber.current which depends on Thread.current but if these accessors use the class getter/property macros then we'll enter an infinite recursion.

Adds Crystal.once_init to allow to remove the Crystal.once_mutex= setter. It also removes the need for the __crystal_once_init fun which won't be defined anymore —it's only kept for the legacy implementation.

Those initializers are now explicitly called from a new Crystal.init_runtime method called by Crystal.main, but also by the interpreter that doesn't call Crystal.main —not currently needed, but it will be to reuse crystal/once to implement #14905.

@straight-shoota
Copy link
Member

Can we split this into two PRs? I understand the changes are largely independent of each other.

@ysbaddaden ysbaddaden force-pushed the refactor/thread-and-fiber-explicit-class-vars-init branch from f2b1909 to a1cbb2f Compare January 24, 2025 14:21
Replaces the implicit initialization of Thread and Fiber class variables
with an explicit initializer since both should be initialized before
`__crystal_once_init` is called.

This is working for now because the `class_[getter|property]` macros
in Thread and Fiber don't protect against either recursion nor parallel
initializations, but we plan to protect them (using Mutex) which would
immediately break with an infinite recursion:

Mutex#lock -> Fiber#current -> Thread#current -> Mutex#lock -> ...
@ysbaddaden ysbaddaden force-pushed the refactor/thread-and-fiber-explicit-class-vars-init branch from a1cbb2f to 39d88fa Compare January 24, 2025 14:26
@ysbaddaden ysbaddaden changed the title Explicit initialization of thread, fiber and crystal/once class variables Explicit initialization of thread and fiber class variables Jan 24, 2025
@ysbaddaden ysbaddaden changed the title Explicit initialization of thread and fiber class variables Explicit init of Thread and Fiber class variables Jan 24, 2025
@straight-shoota straight-shoota added this to the 1.16.0 milestone Jan 24, 2025
@straight-shoota straight-shoota merged commit 9e475f6 into crystal-lang:master Jan 25, 2025
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants