If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

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

RESOLVED FIXED in mozilla24

Status

()

Core
MFBT
RESOLVED FIXED
4 years ago
4 years ago

People

(Reporter: Justin Lebar (not reading bugmail), Unassigned)

Tracking

Trunk
mozilla24
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

4 years ago
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.
(Reporter)

Updated

4 years ago
Blocks: 873801
(Reporter)

Comment 1

4 years ago
Created attachment 751404 [details] [diff] [review]
Patch, v1
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.
(Reporter)

Comment 3

4 years ago
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+
(Reporter)

Comment 5

4 years ago
https://hg.mozilla.org/integration/mozilla-inbound/rev/448e9861db9f
https://hg.mozilla.org/mozilla-central/rev/448e9861db9f
Status: NEW → RESOLVED
Last Resolved: 4 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.
(Reporter)

Updated

4 years ago
Depends on: 876691
You need to log in before you can comment on or make changes to this bug.