Thread overview
Serious extern(C) bug
Sep 25, 2013
Luís Marques
Sep 25, 2013
growler
Sep 25, 2013
growler
Sep 25, 2013
Walter Bright
Sep 25, 2013
Luís Marques
Sep 25, 2013
Jacob Carlborg
Sep 25, 2013
Iain Buclaw
September 25, 2013
Have you seen this one before? Do you know a workaround? (DMD  v2.063.2, on OX X 10.9)

file.d:

    extern(C)
    {
        int x = 0;
        void setx();
        void printx();
    }

    void main()
    {
        setx(); // sets x = 42
        writeln(x); // prints x = 0
        printx(); // prints x = 42
        x = 7;
        printx(); // prints x = 42
    }



file.c:

    #include <stdio.h>

    extern int x;

    void setx()
    {
        x = 42;
    }

    void printx()
    {
        printf("%d\n", x);
    }

Output:

    0
    42
    42
September 25, 2013
On Wednesday, 25 September 2013 at 04:13:02 UTC, Luís Marques wrote:
> Have you seen this one before? Do you know a workaround? (DMD  v2.063.2, on OX X 10.9)
>
> file.d:
>
>     extern(C)
>     {
>         int x = 0;
>         void setx();
>         void printx();
>     }
>
>     void main()
>     {
>         setx(); // sets x = 42
>         writeln(x); // prints x = 0
>         printx(); // prints x = 42
>         x = 7;
>         printx(); // prints x = 42
>     }
>
>
>
> file.c:
>
>     #include <stdio.h>
>
>     extern int x;
>
>     void setx()
>     {
>         x = 42;
>     }
>
>     void printx()
>     {
>         printf("%d\n", x);
>     }
>
> Output:
>
>     0
>     42
>     42

don't you need extern __gshared on the var?

extern __gshared int x;



September 25, 2013
On Wednesday, 25 September 2013 at 04:35:35 UTC, growler wrote:
> On Wednesday, 25 September 2013 at 04:13:02 UTC, Luís Marques wrote:
>> Have you seen this one before? Do you know a workaround? (DMD  v2.063.2, on OX X 10.9)
>>
>> file.d:
>>
>>    extern(C)
>>    {
>>        int x = 0;
>>        void setx();
>>        void printx();
>>    }
>>
>>    void main()
>>    {
>>        setx(); // sets x = 42
>>        writeln(x); // prints x = 0
>>        printx(); // prints x = 42
>>        x = 7;
>>        printx(); // prints x = 42
>>    }
>>
>>
>>
>> file.c:
>>
>>    #include <stdio.h>
>>
>>    extern int x;
>>
>>    void setx()
>>    {
>>        x = 42;
>>    }
>>
>>    void printx()
>>    {
>>        printf("%d\n", x);
>>    }
>>
>> Output:
>>
>>    0
>>    42
>>    42
>
> don't you need extern __gshared on the var?
>
> extern __gshared int x;

http://dlang.org/interfaceToC.html (see last section at bottom of
page)
September 25, 2013
On 9/24/2013 9:13 PM, "Luís Marques" <luis@luismarques.eu>" wrote:
> Have you seen this one before? Do you know a workaround? (DMD v2.063.2, on OX X
> 10.9)
>
> file.d:
>
>      extern(C)
>      {
>          int x = 0;

This x is put in thread local storage.

>          void setx();
>          void printx();
>      }
>
>      void main()
>      {
>          setx(); // sets x = 42
>          writeln(x); // prints x = 0
>          printx(); // prints x = 42
>          x = 7;
>          printx(); // prints x = 42
>      }
>
>
>
> file.c:
>
>      #include <stdio.h>
>
>      extern int x;

This x is put in thread global storage.

>
>      void setx()
>      {
>          x = 42;
>      }
>
>      void printx()
>      {
>          printf("%d\n", x);
>      }
>
> Output:
>
>      0
>      42
>      42

So you've got two different x's here.

Declare the first one as:

    __gshared int x = 0;
September 25, 2013
Oops :-)

I just assumed too quickly that it was another consequence of the DMD / (GCC, Clang) section mismatch I reported some days ago (http://forum.dlang.org/thread/gqxtzdbhnzkslnltqrmo@forum.dlang.org), I didn't even think it was a basic PEBCAK.

My bad --;;

I guess I also implicitly assumed extern(C) implied __gshared... ("make it the same as C, not just the mangling"). And TLS is so implicit in D2 that one can even forget about it :-)
September 25, 2013
On 2013-09-25 06:49, "Luís Marques" <luis@luismarques.eu>" wrote:

> I guess I also implicitly assumed extern(C) implied __gshared... ("make
> it the same as C, not just the mangling"). And TLS is so implicit in D2
> that one can even forget about it :-)

Well, C has thread local, via the extension "__thread" or via C++ "thread_local". So if extern (C) implied "__gshared", how would one declare an extern (C) thread local variable?

-- 
/Jacob Carlborg
September 25, 2013
On 25 September 2013 10:40, Jacob Carlborg <doob@me.com> wrote:
> On 2013-09-25 06:49, "Luís Marques" <luis@luismarques.eu>" wrote:
>
>> I guess I also implicitly assumed extern(C) implied __gshared... ("make
>> it the same as C, not just the mangling"). And TLS is so implicit in D2
>> that one can even forget about it :-)
>
>
> Well, C has thread local, via the extension "__thread" or via C++
> "thread_local". So if extern (C) implied "__gshared", how would one declare
> an extern (C) thread local variable?
>
> --
> /Jacob Carlborg

Especially considering that __thread is deprecated/error in D now. ;)

-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';