immutable, static, __gshared, TLS, and compile time allocation

Manu turkeyman at gmail.com
Fri Apr 20 13:45:11 PDT 2012


I need to clarify some things that have confused me a few times. I've
reconsidered these basic attributes a few times, and I thought I understood
them, but I obviously don't.

The case that has confused me is here:
http://d.puremagic.com/issues/show_bug.cgi?id=7897

In the global scope:

int x; <- x is TLS

but:

static int x;  <- this is... a 'static' global instance, whatever that
means? Is this TLS or not? If so, how is it distinct from 'int x;' above? I
presume it must still be TLS, and effectively meaningless at the global
scope; dlang.org states "static is ignored when applied to other
declarations". It is just used for members of struct/classes? Why not
produce a syntax error rather than ignore it?

immutable int x;  <- this can't possibly change, so why would it be TLS? it
must be a single static instance... right?

__gshared int x;  <- this should behave exactly like a C global right? ie,
no TLS + addressable at compile time.

static immutable x;  <- i've seen this a few times, what does it mean?


I'm concerned with which of these are addressable at compile time. By my
logic, all (well, perhaps not the first) should be allocated in the
datablock, addresses known at compile time, and therefore addressable at
compile time (ie, able to alias in template args).
That doesn't seem to be the case however in at least the __gshared case (in
my big report above), which is very surprising to me.

There's another thing that that's had me scratching my head for a while,
and gave me the contradictory feeling that the use of TLS data is fairly
relaxed:

struct S
{
   int x;
   static int y;
}

In this case y is static, ie, globally shared by all instances, but I
expect it SHOULD be TLS, since it is mutable, and therefore not thread
safe...
That said, I write code that passes static members of structs into
templates as alias parameters all the time, and generate code that writes
to them.
How is this possible? And given that it is, why is it not possible for at
least __gshared at global scope?


struct S
{
   static x;
   __gshared y;
}

What is the difference between x and y here?


I've obviously missed something rather fundamental, but the dlang
descriptions of each of these attributes are a little light, and leaves me
quite unclear on the details here.

I'm particularly confused that I CAN alias a static member in a struct,
which I suspect should be TLS for thread safety, but I CAN'T alias a
__gshared at global scope, which is not TLS by definition?

O_o
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120420/eda84a4a/attachment.html>


More information about the Digitalmars-d mailing list