Bug 3243 – instantiated 'static this()' bring on a compiler message ': __gate is thread local'
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-08-10T11:35:22Z
Last change time
2020-03-21T03:56:35Z
Keywords
diagnostic, pull
Assigned to
No Owner
Creator
HOSOKAWA Kenchi
Comments
Comment #0 by hskwk — 2009-08-10T11:35:22Z
template T() {
static this() {
}
}
class C {
mixin T;
}
Compiling this code (with -vtls) brings on a compiler message
: __gate is thread local
I do not know what does this message mean.
Please mark keywords for this issue if you know anything about this message.
Comment #1 by jeremiep — 2009-09-08T16:00:15Z
Here's the patch:
--- orig\func.cpp Thu Sep 03 01:01:40 2009
+++ dmd\func.cpp Tue Sep 08 18:54:46 2009
@@ -2759,7 +2759,7 @@
*/
Identifier *id = Lexer::idPool("__gate");
VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL);
- v->storage_class = STCstatic;
+ v->storage_class = STCstatic | STCgshared;
Statements *sa = new Statements();
Statement *s = new DeclarationStatement(0, v);
sa->push(s);
When a static constructor is coming from a template, a "gate" is added to prevent running it more than once. That gate was not marked with __gshared.
Comment #2 by bearophile_hugs — 2013-11-26T05:01:00Z
I have this "bug" with D2.064. However, I'm not sure it is a bug, and that it should be fixed the way Jeremie suggested, because static constructors initialize thread local data. If you make the gate gshared, then you will only initialize for one thread.
By the way, the line Jeremie gave for patching is now:
v->storage_class = isSharedStaticCtorDeclaration() ? STCstatic : STCtls;
There is also the same pattern below for the destructor (not in Jeremie's patch).
Comment #4 by code — 2015-08-10T16:43:45Z
This is indeed an invalid bug as far as D2 is concerned, because the static constructor is run once per thread and needs a gate variable to prevent being run multiple times in the face of separate compilation. I suppose we could improve the diagnostic message, though?
Comment #5 by k.hara.pg — 2015-08-11T01:47:32Z
(In reply to David Nadlinger from comment #4)
> This is indeed an invalid bug as far as D2 is concerned, because the static
> constructor is run once per thread and needs a gate variable to prevent
> being run multiple times in the face of separate compilation. I suppose we
> could improve the diagnostic message, though?
The original issue, the mixed-in `static this()` will report ': __gate is thread local' message with -vtls, is properly fixed from 2.065, by the side-effect of the commit:
https://github.com/D-Programming-Language/dmd/commit/9f9e4690d6abcfee5858f48a7eb80d1b7eca5a06
Because the mixin is not a proper template instance, the instantiated code is inserted in the enclosing scope, so the generated `static this()` will be a non-instantiated function and should not have gate.
But, if the template is properly instantiated, the static this() still needs TLS __gate variable implicitly as you said, and it will be listed by -vtls. For example:
template T()
{
static this() {}
}
class C
{
alias ti = T!();
}
With 2.068.0,
dmd -c -vtls test.d
Will show:
: __gate is thread local
I think this is a minor diagnostic issue. -vlts does not have to list the internal generated variables like __gate, because it's just confusing.
I change the title and reopen.