run destructors for .threadvar
s on thread exit
#1308
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
All
.threadvars
s that have destructors are now destroyed on threadexit (including the main thread). Module-level
.threadvar
s aredestroyed before procedure-scoped
.threadvar
s, mirroring thedestruction order of non-threadlocal globals.
Fixes #1301.
Details
Two new module operators are introduced:
ThreadDeinit
(destroys all module-level.threadvar
s)ThreadPostDeinit
(destroys all procedure-scoped.threadvar
s)They have the same role for
.threadvar
s asDeinit
andPostDeinit
do for normal globals. In order to make implementation slightly
simpler, destruction of procedure-scoped
.threadvar
s is also done aspart
PostDeinit
. This means thatThreadPostDeinit
doesn't have tobe called on program teardown.
Finally,
cbackend
(the other backends don't support threads at themoment) generates and emits the
nimTeardownThreadVars
procedure,which is then importc'ed by
threads
and run after the destructioncallbacks (which might use threadvars themselves).
Since all backends use
backends.generateMain
(either directly orindirectly), destruction of
.threadvar
s also automatically works withthe JS and VM backends.