www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - GetAndSet function (corresponding to cas function)

reply Adrian Mercieca <amercieca gmail.com> writes:
Hi folks,

Is there a GetAndSet function (corresponding to cas (compare and set) 
function) in D?

Thanks.
Dec 25 2011
parent reply Mike Wey <mike-wey example.com> writes:
On 12/25/2011 09:25 AM, Adrian Mercieca wrote:
 Hi folks,

 Is there a GetAndSet function (corresponding to cas (compare and set)
 function) in D?

 Thanks.
core.atomic.cas http://dlang.org/phobos/core_atomic.html#cas -- Mike Wey
Dec 25 2011
parent reply Adrian Mercieca <amercieca gmail.com> writes:
On Sun, 25 Dec 2011 13:37:32 +0100, Mike Wey wrote:

 On 12/25/2011 09:25 AM, Adrian Mercieca wrote:
 Hi folks,

 Is there a GetAndSet function (corresponding to cas (compare and set)
 function) in D?

 Thanks.
core.atomic.cas http://dlang.org/phobos/core_atomic.html#cas
I know of the cas function in D. I was asking if there was a getAndSet function. Tks.
Dec 25 2011
parent reply Adrian Mercieca <amercieca gmail.com> writes:
Hi folks,

Would anyone answer me on this please?

To clarify, in Java there is are getAndSet methods on Atomic type objects 
(along with compareAndSet).

I know that in D there is the cas function (equivalent to Java's 
compareAndSet); is there an equivalent D function for Java's getAndSet 
please?


Thanks.
Dec 26 2011
next sibling parent reply Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Mon, Dec 26, 2011 at 2:34 PM, Adrian Mercieca <amercieca gmail.com> wrote:
 Hi folks,

 Would anyone answer me on this please?

 To clarify, in Java there is are getAndSet methods on Atomic type objects
 (along with compareAndSet).

 I know that in D there is the cas function (equivalent to Java's
 compareAndSet); is there an equivalent D function for Java's getAndSet
 please?


 Thanks.
From Java:
--- public final boolean getAndSet(boolean newValue) { for (;;) { boolean current = get(); if (compareAndSet(current, newValue)) return current; } } --- getAndSet is just a wrapped version of compareAndSet.
Dec 26 2011
next sibling parent Adrian Mercieca <amercieca gmail.com> writes:
On Mon, 26 Dec 2011 15:06:57 -0600, Andrew Wiley wrote:

 On Mon, Dec 26, 2011 at 2:34 PM, Adrian Mercieca <amercieca gmail.com>
 wrote:
 Hi folks,

 Would anyone answer me on this please?

 To clarify, in Java there is are getAndSet methods on Atomic type
 objects (along with compareAndSet).

 I know that in D there is the cas function (equivalent to Java's
 compareAndSet); is there an equivalent D function for Java's getAndSet
 please?


 Thanks.
From Java:
--- public final boolean getAndSet(boolean newValue) { for (;;) { boolean current = get(); if (compareAndSet(current, newValue)) return current; } } --- getAndSet is just a wrapped version of compareAndSet.
Hi Andrew, It is indeed; should have thought of that. Anyway, thanks for pointing it out. Regards.
Dec 27 2011
prev sibling parent Adrian Mercieca <amercieca gmail.com> writes:
Hi Andrew,

Actually, what would be the equivalent of the 'get' function in D?

Thanks.
Dec 27 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, December 26, 2011 20:34:39 Adrian Mercieca wrote:
 Hi folks,
 
 Would anyone answer me on this please?
 
 To clarify, in Java there is are getAndSet methods on Atomic type objects
 (along with compareAndSet).
 
 I know that in D there is the cas function (equivalent to Java's
 compareAndSet); is there an equivalent D function for Java's getAndSet
 please?
If it's not in core.atomic, then probably not. It has atomicLoad and atomicStore, but I don't see anything that tries to combine the two, assuming that that's what you want be getAndSet. - Jonathan M Davis
Dec 26 2011
prev sibling parent Andrew Wiley <wiley.andrew.j gmail.com> writes:
On Mon, Dec 26, 2011 at 4:05 PM, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 On Monday, December 26, 2011 20:34:39 Adrian Mercieca wrote:
 Hi folks,

 Would anyone answer me on this please?

 To clarify, in Java there is are getAndSet methods on Atomic type objects
 (along with compareAndSet).

 I know that in D there is the cas function (equivalent to Java's
 compareAndSet); is there an equivalent D function for Java's getAndSet
 please?
If it's not in core.atomic, then probably not. It has atomicLoad and atomicStore, but I don't see anything that tries to combine the two, assuming that that's what you want be getAndSet.
getAndSet can easily be implemented as a utility method using cas and atomicLoad. It's not a primitive atomic operation, it's just a convenience function. T getAndSet(T)(shared(T)* location, T newValue) { while(1) { auto current = atomicLoad(*location); if(cas(location, current, newValue)) return current; } } Someone who knows more about the options to atomicLoad could probably make this faster, but because we're using cas, it's guaranteed to be correct.
Dec 26 2011