is(Mutex == shared) == false?

Stanislav Blinov stanislav.blinov at gmail.com
Tue Feb 11 09:09:38 PST 2014


On Tuesday, 11 February 2014 at 05:58:15 UTC, Sean Kelly wrote:
> On Saturday, 8 February 2014 at 16:46:26 UTC, Stanislav Blinov

>> Maybe we should also consider making [core.sync primitives] 
>> final along the way?
>
> Probably.  The new std.concurrency patch overrides Condition, 
> but mostly for convenience.  I think those methods not being 
> virtual was an oversight on my part.

Great :)

Now to another issue, or possible enhancement. It would seem that 
we may benefit from some sort of relaxed shared ops.

For example, Condition on Windows has several int fields, which 
of course would become shared(int) due to transitivity. 
Currently, those ints are modified directly using ++, --, += and 
so on. As per 
https://d.puremagic.com/issues/show_bug.cgi?id=3672, such code 
would be illegal. A straightforward hack would be to move such 
code into non-shared methods and then call those methods by 
casting away shared on the this reference. A better, but still 
naive replacement would be to use atomicOp() for all those 
operations. The problem is that many of those operations happen 
under a lock, where a strong do {} while (!cas()) loop, to which 
atomicOp() currently translates, would be unnecessary 
pessimization.
Of course, they could be replaced manually with something like:

// atomicOp!"-="(m_numWaitersBlocked, 1);
atomicStore!(MemoryOrder.rel)(m_numWaitersBlocked, 
atomicLoad!(MemoryOrder.acq)(m_numWaitersBlocked) - 1);

But that is just tedious to type, hard to read, and still may be 
less efficient that a straightforward --.

Maybe an additional function like this could be helpful:

--8<--

HeadUnshared!T localOp(string op,T,V)(T what, V mod) if (is(T == 
shared))
{ ... }

-->8--

Or even something like this:

--8<--

ref auto assumeLocal(T)(ref T v) if (is(T == shared))
{
     // Cast via pointer to preserve lvalue
     return *cast(HeadUnshared!T*)&v;
}

-->8--

With which we can perform this:

--8<--

--assumeLocal(m_numWaitersBlocked);

-->8--

What do you think?


More information about the Digitalmars-d mailing list