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

run destructors for .threadvars on thread exit #1308

Merged
merged 4 commits into from
May 13, 2024

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented May 12, 2024

Summary

All .threadvarss that have destructors are now destroyed on thread
exit (including the main thread). Module-level .threadvars are
destroyed before procedure-scoped .threadvars, mirroring the
destruction order of non-threadlocal globals.

Fixes #1301.

Details

Two new module operators are introduced:

  • ThreadDeinit (destroys all module-level .threadvars)
  • ThreadPostDeinit (destroys all procedure-scoped .threadvars)

They have the same role for .threadvars as Deinit and PostDeinit
do for normal globals. In order to make implementation slightly
simpler, destruction of procedure-scoped .threadvars is also done as
part PostDeinit. This means that ThreadPostDeinit doesn't have to
be called on program teardown.

Finally, cbackend (the other backends don't support threads at the
moment) generates and emits the nimTeardownThreadVars procedure,
which is then importc'ed by threads and run after the destruction
callbacks (which might use threadvars themselves).

Since all backends use backends.generateMain (either directly or
indirectly), destruction of .threadvars also automatically works with
the JS and VM backends.

Two new module operators are introduced:
* the `threadDeinit` operator (module-level threadvar destruction)
* the `threadPostDeinit` operator (procedure-scoped threadvar
  destruction)

Both are similar to the `Deinit` and `PostDeinit` operators. Module-
level threadvars are destroyed after top-level globals but before
procedure-scoped threadvars and globals.

For slightly simpler code generation, destruction of procedure-scoped
threadvars is intertwined with that of procedure-scoped globals (in
the `PostDeinit` procedure).
The procedure uses a fixed name so that the `threads` module can then
import it.
The destructors are called *after* manually registered thread
destruction handlers, as those could rely on threadvars.
@zerbina zerbina added bug Something isn't working compiler/backend Related to backend system of the compiler labels May 12, 2024
@saem
Copy link
Collaborator

saem commented May 12, 2024

  • the destruction order for threadvars (and globals in general) likely needs to be redone from first principles at a later stage

I was just about to ask about this, because the order seems backwards -- sounds good, it can be discussed elsewhere.

@saem
Copy link
Collaborator

saem commented May 13, 2024

/merge

Copy link

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

  • the destruction order for threadvars (and globals in general) likely needs to be redone from first principles at a later stage
  • more progress towards making thread support a bit more usable

@chore-runner chore-runner bot added this pull request to the merge queue May 13, 2024
Merged via the queue into nim-works:devel with commit af04de5 May 13, 2024
31 checks passed
@zerbina zerbina deleted the thread-destructors branch May 16, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/backend Related to backend system of the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.threadvars aren't destroyed on thread exit
2 participants