Thread overview
GetAndSet function (corresponding to cas function)
Dec 25, 2011
Adrian Mercieca
Dec 25, 2011
Mike Wey
Dec 25, 2011
Adrian Mercieca
Dec 26, 2011
Adrian Mercieca
Dec 26, 2011
Andrew Wiley
Dec 27, 2011
Adrian Mercieca
Dec 27, 2011
Adrian Mercieca
Dec 26, 2011
Jonathan M Davis
Dec 26, 2011
Andrew Wiley
December 25, 2011
Hi folks,

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

Thanks.
December 25, 2011
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
December 25, 2011
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.
December 26, 2011
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.
December 26, 2011
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.
December 26, 2011
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
December 26, 2011
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.
December 27, 2011
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.
December 27, 2011
Hi Andrew,

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

Thanks.