WebAssembly / WebAssembly/threads

Behavior of `t.atomic.rmw.cmpxchg` instructions

Open
#195 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
767
Forks
54
PR merge metrics
No merged PRs in 30d

Description

Hi,

As the 17. and 18. steps in the spec:

17. If c1 equals c2, then:
    Let c be c2.
18. Else:
    Let c be c3.

And the 21. step will write the c back to the memory.
Where c1 is the origin value on the memory, c2 is the expected value, c3 is the value to replace.

Maybe I can realize this as following simply:

if (memory[addr] != expected)
    memory[addr] = replacement;

But the closest function in C++ std::atomic<T>::compare_exchange_strong has the different behavior.

if (memory[addr] == expected)
   memory[addr] = replacement;

The atomic_rmw instructions are similar to the member functions of std::atomic<T> in C++ STL, such as fetch_add(), fetch_xor, etc.

Is it necessary to have the same behavior between cmpxchg instructions and std::atomic<T>::compare_exchange_strong in C++?

Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with steps 17–21 of the linked WebAssembly threads specification and compare their t.atomic.rmw.cmpxchg semantics with C++ std::atomic<T>::compare_exchange_strong. No repository files or tests are identified; done would require resolving whether the specified behavior is intentional and determining any resulting specification change.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.