[dmd-concurrency] synchronized, shared, and regular methods inside the same class
Kevin Bealer
kevinbealer at gmail.com
Tue Jan 5 16:37:53 PST 2010
I read through a few papers on hazard pointers, and I still only know enough
gun safety to know that the gun is always loaded. But I can't help but
ask...
What if this code:
void methodName(ref shared int a)
{
do_stuff();
int a1 = borrowShared(a);
auto x = computeX(a1);
auto y = computeY(a1);
auto z = computeZ(a1);
int xyz = combine(x, y, z);
returnShared(a, xyz);
more_stuff();
}
Was converted to this (borrowing Sean's example):
void methodName( ref shared int a )
{
do_stuff();
int temp1;
do {
int a1 = temp1 = a;
auto x = computeX(a1);
auto y = computeY(a1);
auto z = computeZ(a1);
a1 = combine(x, y, z);
} while( !atomicStoreIf( a, a1, temp1 ) ); // atomicStoreIf is a CAS
more_stuff();
}
It would only work for single variable modifications of course, and if
computeX,Y,Z is slow, it is probably pointless or a live-lock. The user
would need to understand that this is really a loop, so maybe a different
syntax is needed:
void methodName(ref shared int a)
{
do_stuff();
bool quit = false;
forshare(a; a1; ! quit) {
auto x = computeX(a1);
auto y = computeY(a1);
auto z = computeZ(a1);
quit = /* iteration count test or something */;
a1 = combine(x, y, z);
}
more_stuff();
}
I included the quit concept as an optional feature to remind users that this
might live-lock; they could of course say "if (...) break" at any time as
well.
(I'm sure there are some critics who will say that putting easy to reach
lock-free algorithm primitives next to simple to use language features is
like selling rubber snakes and real snakes from the same bin.)
Kevin
On Tue, Jan 5, 2010 at 6:58 PM, Andrei Alexandrescu <andrei at erdani.com>wrote:
> Kevin Bealer wrote:
>
>> And I guess getting lock free systems right is normally
>> considered an "experts only" kind of thing, except maybe the one-pointer
>> versions like linked list.
>>
>
> Even lock-free slists are heinously difficult to define correctly...
>
> Andrei
>
> _______________________________________________
> dmd-concurrency mailing list
> dmd-concurrency at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-concurrency
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/dmd-concurrency/attachments/20100105/6d818d05/attachment-0001.htm>
More information about the dmd-concurrency
mailing list