View mode: basic / threaded / horizontal-split · Log in · Help
November 20, 2012
__gshared implicitly convertible to shared?
Would it make sense to make it so that __gshared implicitly converted to 
shared? It's my understanding that the main purpose of __gshared is to be able 
to better interact with C code, but given the issues with shared, lots of 
people have just used __gshared instead of shared. This causes a problem in 
some cases. For instance, none of Mutex's functions are currently shared in 
spite of the fact that it really doesn't make sense to have a Mutex which is 
thread-local, but it can't have all of its functions be only shared, because 
then it wouldn't work with __gshared. That means that in order to work with 
both __gshared and shared, all of its functions must be duplicated, which is 
obviously less than ideal.

So, given that __gshared is shared across threads like shared is (just with 
fewer protections), would it make sense to make it so that __gshared 
implicitly converts to shared? Then a type like Mutex which is intended to be 
shared, can just make all of its member functions shared, and it'll work with 
both __gshared and shared.

It may also be necessary to make shared implicitly convert to __gshared for 
that to work cleanly (particularly when you get stuff like a member function 
returning a reference variable which then must be shared, even if the original 
variable were __gshared - because the function itself is shared), and I don't 
know how big a problem that would be. But I think that the basic idea of 
allowing implicit conversions at least from __gshared to shared (if not shared 
to __gshared) is worth exploring. Is there a major reason why it would be a 
bad idea? My experience with both is limited, and I may just be completely 
missing something here.

- Jonathan M Davis
November 21, 2012
Re: __gshared implicitly convertible to shared?
On Tuesday, 20 November 2012 at 19:15:01 UTC, Jonathan M Davis 
wrote:
> Would it make sense to make it so that __gshared implicitly 
> converted to
> shared? It's my understanding that the main purpose of 
> __gshared is to be able
> to better interact with C code, but given the issues with 
> shared, lots of
> people have just used __gshared instead of shared. This causes 
> a problem in
> some cases. For instance, none of Mutex's functions are 
> currently shared in
> spite of the fact that it really doesn't make sense to have a 
> Mutex which is
> thread-local, but it can't have all of its functions be only 
> shared, because
> then it wouldn't work with __gshared. That means that in order 
> to work with
> both __gshared and shared, all of its functions must be 
> duplicated, which is
> obviously less than ideal.
>
> So, given that __gshared is shared across threads like shared 
> is (just with
> fewer protections), would it make sense to make it so that 
> __gshared
> implicitly converts to shared? Then a type like Mutex which is 
> intended to be
> shared, can just make all of its member functions shared, and 
> it'll work with
> both __gshared and shared.
>
> It may also be necessary to make shared implicitly convert to 
> __gshared for
> that to work cleanly (particularly when you get stuff like a 
> member function
> returning a reference variable which then must be shared, even 
> if the original
> variable were __gshared - because the function itself is 
> shared), and I don't
> know how big a problem that would be. But I think that the 
> basic idea of
> allowing implicit conversions at least from __gshared to shared 
> (if not shared
> to __gshared) is worth exploring. Is there a major reason why 
> it would be a
> bad idea? My experience with both is limited, and I may just be 
> completely
> missing something here.
>
> - Jonathan M Davis

I'm short on time, and so can't double-check the online docs or 
the book, but as I recall (having never actually used 
__gshared... yet) they differ in that shared is a type 
constructor and __gshared is a declaration attribute.  So I'm 
just not sure.  On the one hand, the compiler would always be 
able to see that either the left or right side of an assignment 
(or the inside of an argument list) includes a __gshared 
variable, and could do the "right" thing...  But on the other 
hand it would be an odd special case of a type qualifier being 
cast away because of a variable's attribute.  Is this something 
we want?  Or is it intended as a short-term workaround until 
shared is more fully/correctly defined semantically?
November 21, 2012
Re: __gshared implicitly convertible to shared?
On 2012-11-20 20:14, Jonathan M Davis wrote:
> Would it make sense to make it so that __gshared implicitly converted to
> shared? It's my understanding that the main purpose of __gshared is to be able
> to better interact with C code, but given the issues with shared, lots of
> people have just used __gshared instead of shared.

I don't think it's just because there are issues with "shared". If 
"shared" means other semantics than just a global variable the D code 
will have different semantics when accessing the variable compared to 
the C code. I don't think that's a good idea. If it's not safe to just 
access the variable as is, I think it's better to add a safe wrapper 
then changing the semantics of the bindings.

-- 
/Jacob Carlborg
Top | Discussion index | About this forum | D home