Skip to content

Atomics

Atomically load a 32-bit integer from memory. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32) (result i32)

Example:

;; Atomically load i32 from address
(i32.atomic.load (i32.const 0))

Atomically load a 64-bit integer from memory. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32) (result i64)

Example:

;; Atomically load i64 from address
(i64.atomic.load (i32.const 0))

Atomically load an 8-bit value from memory and zero-extend to i32. Requires shared memory.

Signature: (param i32) (result i32)

Example:

;; Atomically load byte and zero-extend to i32
(i32.atomic.load8_u (i32.const 0))

Atomically load a 16-bit value from memory and zero-extend to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32) (result i32)

Example:

;; Atomically load 16-bit value and zero-extend to i32
(i32.atomic.load16_u (i32.const 0))

Atomically load an 8-bit value from memory and zero-extend to i64. Requires shared memory.

Signature: (param i32) (result i64)

Example:

;; Atomically load byte and zero-extend to i64
(i64.atomic.load8_u (i32.const 0))

Atomically load a 16-bit value from memory and zero-extend to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32) (result i64)

Example:

;; Atomically load 16-bit value and zero-extend to i64
(i64.atomic.load16_u (i32.const 0))

Atomically load a 32-bit value from memory and zero-extend to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32) (result i64)

Example:

;; Atomically load 32-bit value and zero-extend to i64
(i64.atomic.load32_u (i32.const 0))

Atomically store a 32-bit integer to memory. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32)

Example:

;; Atomically store i32 value at address
(i32.atomic.store (i32.const 0) (i32.const 42))

Atomically store a 64-bit integer to memory. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64)

Example:

;; Atomically store i64 value at address
(i64.atomic.store (i32.const 0) (i64.const 42))

Atomically store the low 8 bits of an i32 to memory. Requires shared memory.

Signature: (param i32 i32)

Example:

;; Atomically store low byte of i32 at address
(i32.atomic.store8 (i32.const 0) (i32.const 255))

Atomically store the low 16 bits of an i32 to memory. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32)

Example:

;; Atomically store low 16 bits of i32 at address
(i32.atomic.store16 (i32.const 0) (i32.const 1000))

Atomically store the low 8 bits of an i64 to memory. Requires shared memory.

Signature: (param i32 i64)

Example:

;; Atomically store low byte of i64 at address
(i64.atomic.store8 (i32.const 0) (i64.const 255))

Atomically store the low 16 bits of an i64 to memory. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64)

Example:

;; Atomically store low 16 bits of i64 at address
(i64.atomic.store16 (i32.const 0) (i64.const 1000))

Atomically store the low 32 bits of an i64 to memory. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64)

Example:

;; Atomically store low 32 bits of i64 at address
(i64.atomic.store32 (i32.const 0) (i64.const 100000))

Atomically read a 32-bit value, add to it, and store the result. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically add 10 to value at address, return old value
(i32.atomic.rmw.add (i32.const 0) (i32.const 10))

Atomically read a 32-bit value, subtract from it, and store the result. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically subtract 5 from value at address, return old value
(i32.atomic.rmw.sub (i32.const 0) (i32.const 5))

Atomically read a 32-bit value, perform bitwise AND, and store the result. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically AND with mask, return old value
(i32.atomic.rmw.and (i32.const 0) (i32.const 0xFF))

Atomically read a 32-bit value, perform bitwise OR, and store the result. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically OR with flags, return old value
(i32.atomic.rmw.or (i32.const 0) (i32.const 0x80))

Atomically read a 32-bit value, perform bitwise XOR, and store the result. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically XOR with value, return old value
(i32.atomic.rmw.xor (i32.const 0) (i32.const 0xFF))

Atomically exchange (swap) a 32-bit value in memory. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically swap value at address, return old value
(i32.atomic.rmw.xchg (i32.const 0) (i32.const 100))

Atomically read a 64-bit value, add to it, and store the result. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically add 10 to i64 value at address, return old value
(i64.atomic.rmw.add (i32.const 0) (i64.const 10))

Atomically read a 64-bit value, subtract from it, and store the result. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically subtract 5 from i64 value at address, return old value
(i64.atomic.rmw.sub (i32.const 0) (i64.const 5))

Atomically read a 64-bit value, perform bitwise AND, and store the result. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically AND with mask, return old value
(i64.atomic.rmw.and (i32.const 0) (i64.const 0xFF))

Atomically read a 64-bit value, perform bitwise OR, and store the result. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically OR with flags, return old value
(i64.atomic.rmw.or (i32.const 0) (i64.const 0x80))

Atomically read a 64-bit value, perform bitwise XOR, and store the result. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically XOR with value, return old value
(i64.atomic.rmw.xor (i32.const 0) (i64.const 0xFF))

Atomically exchange (swap) a 64-bit value in memory. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically swap i64 value at address, return old value
(i64.atomic.rmw.xchg (i32.const 0) (i64.const 100))

Atomically read an 8-bit value, add to it, and store the result. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically add to byte at address, return old value
(i32.atomic.rmw8.add_u (i32.const 0) (i32.const 1))

Atomically read an 8-bit value, subtract from it, and store the result. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically subtract from byte at address, return old value
(i32.atomic.rmw8.sub_u (i32.const 0) (i32.const 1))

Atomically read an 8-bit value, perform bitwise AND, and store the result. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically AND byte at address with mask, return old value
(i32.atomic.rmw8.and_u (i32.const 0) (i32.const 0x0F))

Atomically read an 8-bit value, perform bitwise OR, and store the result. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically OR byte at address with flags, return old value
(i32.atomic.rmw8.or_u (i32.const 0) (i32.const 0x80))

Atomically read an 8-bit value, perform bitwise XOR, and store the result. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically XOR byte at address, return old value
(i32.atomic.rmw8.xor_u (i32.const 0) (i32.const 0xFF))

Atomically exchange an 8-bit value in memory. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically swap byte at address, return old value
(i32.atomic.rmw8.xchg_u (i32.const 0) (i32.const 42))

Atomically read a 16-bit value, add to it, and store the result. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically add to 16-bit value at address, return old value
(i32.atomic.rmw16.add_u (i32.const 0) (i32.const 100))

Atomically read a 16-bit value, subtract from it, and store the result. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically subtract from 16-bit value at address, return old value
(i32.atomic.rmw16.sub_u (i32.const 0) (i32.const 100))

Atomically read a 16-bit value, perform bitwise AND, and store the result. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically AND 16-bit value at address with mask, return old value
(i32.atomic.rmw16.and_u (i32.const 0) (i32.const 0x00FF))

Atomically read a 16-bit value, perform bitwise OR, and store the result. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically OR 16-bit value at address with flags, return old value
(i32.atomic.rmw16.or_u (i32.const 0) (i32.const 0x8000))

Atomically read a 16-bit value, perform bitwise XOR, and store the result. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically XOR 16-bit value at address, return old value
(i32.atomic.rmw16.xor_u (i32.const 0) (i32.const 0xFFFF))

Atomically exchange a 16-bit value in memory. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Atomically swap 16-bit value at address, return old value
(i32.atomic.rmw16.xchg_u (i32.const 0) (i32.const 1000))

Atomically read an 8-bit value, add to it, and store the result. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically add to byte at address, return old value as i64
(i64.atomic.rmw8.add_u (i32.const 0) (i64.const 1))

Atomically read an 8-bit value, subtract from it, and store the result. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically subtract from byte at address, return old value as i64
(i64.atomic.rmw8.sub_u (i32.const 0) (i64.const 1))

Atomically read an 8-bit value, perform bitwise AND, and store the result. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically AND byte at address with mask, return old value as i64
(i64.atomic.rmw8.and_u (i32.const 0) (i64.const 0x0F))

Atomically read an 8-bit value, perform bitwise OR, and store the result. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically OR byte at address with flags, return old value as i64
(i64.atomic.rmw8.or_u (i32.const 0) (i64.const 0x80))

Atomically read an 8-bit value, perform bitwise XOR, and store the result. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically XOR byte at address, return old value as i64
(i64.atomic.rmw8.xor_u (i32.const 0) (i64.const 0xFF))

Atomically exchange an 8-bit value in memory. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically swap byte at address, return old value as i64
(i64.atomic.rmw8.xchg_u (i32.const 0) (i64.const 42))

Atomically read a 16-bit value, add to it, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically add to 16-bit value at address, return old value as i64
(i64.atomic.rmw16.add_u (i32.const 0) (i64.const 100))

Atomically read a 16-bit value, subtract from it, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically subtract from 16-bit value at address, return old value as i64
(i64.atomic.rmw16.sub_u (i32.const 0) (i64.const 100))

Atomically read a 16-bit value, perform bitwise AND, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically AND 16-bit value at address with mask, return old value as i64
(i64.atomic.rmw16.and_u (i32.const 0) (i64.const 0x00FF))

Atomically read a 16-bit value, perform bitwise OR, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically OR 16-bit value at address with flags, return old value as i64
(i64.atomic.rmw16.or_u (i32.const 0) (i64.const 0x8000))

Atomically read a 16-bit value, perform bitwise XOR, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically XOR 16-bit value at address, return old value as i64
(i64.atomic.rmw16.xor_u (i32.const 0) (i64.const 0xFFFF))

Atomically exchange a 16-bit value in memory. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically swap 16-bit value at address, return old value as i64
(i64.atomic.rmw16.xchg_u (i32.const 0) (i64.const 1000))

Atomically read a 32-bit value, add to it, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically add to 32-bit value at address, return old value as i64
(i64.atomic.rmw32.add_u (i32.const 0) (i64.const 1000))

Atomically read a 32-bit value, subtract from it, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically subtract from 32-bit value at address, return old value as i64
(i64.atomic.rmw32.sub_u (i32.const 0) (i64.const 1000))

Atomically read a 32-bit value, perform bitwise AND, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically AND 32-bit value at address with mask, return old value as i64
(i64.atomic.rmw32.and_u (i32.const 0) (i64.const 0xFFFF0000))

Atomically read a 32-bit value, perform bitwise OR, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically OR 32-bit value at address with flags, return old value as i64
(i64.atomic.rmw32.or_u (i32.const 0) (i64.const 0x80000000))

Atomically read a 32-bit value, perform bitwise XOR, and store the result. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically XOR 32-bit value at address, return old value as i64
(i64.atomic.rmw32.xor_u (i32.const 0) (i64.const 0xFFFFFFFF))

Atomically exchange a 32-bit value in memory. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64) (result i64)

Example:

;; Atomically swap 32-bit value at address, return old value as i64
(i64.atomic.rmw32.xchg_u (i32.const 0) (i64.const 100000))

Atomically compare and exchange a 32-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32 i32) (result i32)

Example:

;; Compare and exchange: if [addr] == expected, set to replacement
;; Returns original value at address
(i32.atomic.rmw.cmpxchg
(i32.const 0) ;; address
(i32.const 0) ;; expected value
(i32.const 1)) ;; replacement value

Atomically compare and exchange a 64-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value. Requires shared memory. The address must be 8-byte aligned.

Signature: (param i32 i64 i64) (result i64)

Example:

;; Compare and exchange: if [addr] == expected, set to replacement
;; Returns original value at address
(i64.atomic.rmw.cmpxchg
(i32.const 0) ;; address
(i64.const 0) ;; expected value
(i64.const 1)) ;; replacement value

Atomically compare and exchange an 8-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value zero-extended to i32. Requires shared memory.

Signature: (param i32 i32 i32) (result i32)

Example:

;; Compare and exchange byte: if [addr] == expected, set to replacement
(i32.atomic.rmw8.cmpxchg_u
(i32.const 0) ;; address
(i32.const 0) ;; expected value
(i32.const 1)) ;; replacement value

Atomically compare and exchange a 16-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value zero-extended to i32. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i32 i32) (result i32)

Example:

;; Compare and exchange 16-bit: if [addr] == expected, set to replacement
(i32.atomic.rmw16.cmpxchg_u
(i32.const 0) ;; address
(i32.const 0) ;; expected value
(i32.const 1)) ;; replacement value

Atomically compare and exchange an 8-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value zero-extended to i64. Requires shared memory.

Signature: (param i32 i64 i64) (result i64)

Example:

;; Compare and exchange byte: if [addr] == expected, set to replacement
(i64.atomic.rmw8.cmpxchg_u
(i32.const 0) ;; address
(i64.const 0) ;; expected value
(i64.const 1)) ;; replacement value

Atomically compare and exchange a 16-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value zero-extended to i64. Requires shared memory. The address must be 2-byte aligned.

Signature: (param i32 i64 i64) (result i64)

Example:

;; Compare and exchange 16-bit: if [addr] == expected, set to replacement
(i64.atomic.rmw16.cmpxchg_u
(i32.const 0) ;; address
(i64.const 0) ;; expected value
(i64.const 1)) ;; replacement value

Atomically compare and exchange a 32-bit value. If the value at the address equals the expected value, replace it with the replacement value. Returns the original value zero-extended to i64. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i64 i64) (result i64)

Example:

;; Compare and exchange 32-bit: if [addr] == expected, set to replacement
(i64.atomic.rmw32.cmpxchg_u
(i32.const 0) ;; address
(i64.const 0) ;; expected value
(i64.const 1)) ;; replacement value

Suspend the current thread until notified or timeout. The thread waits if the 32-bit value at the address equals the expected value. Returns 0 if woken by notify, 1 if value did not match, 2 if timed out. Requires shared memory. The address must be 4-byte aligned. Timeout is in nanoseconds (-1 for infinite).

Signature: (param i32 i32 i64) (result i32)

Example:

;; Wait on address until value changes or timeout
;; Returns: 0 = woken, 1 = not equal, 2 = timed out
(memory.atomic.wait32
(i32.const 0) ;; address
(i32.const 0) ;; expected value
(i64.const -1)) ;; timeout (-1 = infinite)

Suspend the current thread until notified or timeout. The thread waits if the 64-bit value at the address equals the expected value. Returns 0 if woken by notify, 1 if value did not match, 2 if timed out. Requires shared memory. The address must be 8-byte aligned. Timeout is in nanoseconds (-1 for infinite).

Signature: (param i32 i64 i64) (result i32)

Example:

;; Wait on address until 64-bit value changes or timeout
;; Returns: 0 = woken, 1 = not equal, 2 = timed out
(memory.atomic.wait64
(i32.const 0) ;; address
(i64.const 0) ;; expected value
(i64.const 1000000000)) ;; timeout in nanoseconds (1 second)

Wake up threads waiting on an address. Returns the number of threads that were woken. Requires shared memory. The address must be 4-byte aligned.

Signature: (param i32 i32) (result i32)

Example:

;; Wake up to 1 waiter at address
(memory.atomic.notify
(i32.const 0) ;; address
(i32.const 1)) ;; max waiters to wake

Ensure memory ordering between atomic and non-atomic operations. This is a sequentially consistent fence that prevents reordering of memory accesses across the fence.

Signature: ()

Example:

;; Memory fence for ordering guarantees
(atomic.fence)