Closed Bug 873800 Opened 11 years ago Closed 11 years ago

Add compare-and-swap to mfbt/Atomics.h

Categories

(Core :: MFBT, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla24

People

(Reporter: justin.lebar+bug, Unassigned)

References

Details

Attachments

(1 file)

I've wanted a pointer-sized CAS for a while now.

It returns a bool instead of returning the old value of *ptr because that's all we can do with <atomic>, afaict.  Windows and GCC both give us both options.
Blocks: 873801
Attached patch Patch, v1Splinter Review
Attachment #751404 - Flags: review?(nfroyd)
(In reply to Justin Lebar [:jlebar] from comment #0)
> I've wanted a pointer-sized CAS for a while now.
> 
> It returns a bool instead of returning the old value of *ptr because that's
> all we can do with <atomic>, afaict.  Windows and GCC both give us both
> options.

No, that's not true. The parameter T &expected in the <atomic> compare_exchange gets set to the value of the variable if the comparison failed.
Ah, okay!

I can replicate that interface in the patch, if we want.  I guess I'm a bit afraid of having tricky reference params, but it's not too bad...
Comment on attachment 751404 [details] [diff] [review]
Patch, v1

Review of attachment 751404 [details] [diff] [review]:
-----------------------------------------------------------------

Works for me.

::: mfbt/Atomics.h
@@ +550,5 @@
>        return _InterlockedExchange(ptr, val);
>      }
> +    static bool compareExchange(Type* ptr, Type oldVal, Type newVal) {
> +      return _InterlockedCompareExchange(ptr, newVal, oldVal) == oldVal;
> +    }

Please mention the new requirement of defining compareExchange in the block comment describing PrimitiveIntrinsics, with a brief blurb about it.

@@ +818,5 @@
>        return Intrinsics::exchange(mValue, aValue);
>      }
> +    bool compareExchange(T aOldValue, T aNewValue) {
> +      return Intrinsics::compareExchange(mValue, aOldValue, aNewValue);
> +    }

Please add a comment here describing the interface.
Attachment #751404 - Flags: review?(nfroyd) → review+
https://hg.mozilla.org/mozilla-central/rev/448e9861db9f
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla24
Made Win64 burn, https://tbpl.mozilla.org/php/getParsedLog.php?id=23411638&tree=Mozilla-Central, you'll probably be hearing about that eventually.
Depends on: 876691
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: