| Thread overview | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 25, 2011 GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Hi folks, Is there a GetAndSet function (corresponding to cas (compare and set) function) in D? Thanks. | ||||
December 25, 2011 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adrian Mercieca | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adrian Mercieca | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adrian Mercieca | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adrian Mercieca | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrew Wiley | 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 Re: GetAndSet function (corresponding to cas function) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrew Wiley | Hi Andrew, Actually, what would be the equivalent of the 'get' function in D? Thanks. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply