Skip to content
Snippets Groups Projects
Commit 7c30ad5c authored by Rick Waldron's avatar Rick Waldron
Browse files

Atomics: expected return values for all atomic operations

parent a3b05ee8
No related branches found
No related tags found
No related merge requests found
Showing
with 418 additions and 0 deletions
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.add
description: >
Atomics.add returns the value that existed at the
index prior to the operation.
info: |
Atomics.add( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, add).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
assert.sameValue(Atomics.add(i32a, 0, value), 0);
assert.sameValue(i32a[0], value);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.and
description: >
Atomics.and returns the value that existed at the
index prior to the operation.
info: |
Atomics.and( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, and).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
var other = 0b00000001111111111000000011111111;
i32a[0] = value;
assert.sameValue(Atomics.and(i32a, 0, value), value);
assert.sameValue(i32a[0], value & other);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.compareExchange
description: >
Atomics.compareExchange returns the value that existed at the
index prior to the operation.
info: |
Atomics.compareExchange( typedArray, index, expectedValue, replacementValue )
...
12. Let compareExchange denote a semantic function of two List of
byte values arguments that returns the second argument if the
first argument is element-wise equal to expectedBytes.
13. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, replacement, compareExchange).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
i32a[0] = value;
assert.sameValue(Atomics.compareExchange(i32a, 0, value, 0), value);
assert.sameValue(i32a[0], 0);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.exchange
description: >
Atomics.and returns the value that existed at the
index prior to the operation.
info: |
Atomics.exchange( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, second).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
assert.sameValue(Atomics.exchange(i32a, 0, value), 0);
assert.sameValue(i32a[0], value);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.isLockFree
description: >
Atomics.isLockFree returns a boolean that indicates whether
operations on datum of size will be performed without the agent
acquiring a lock outside of size bytes.
info: |
Atomics.isLockFree( size )
1. Let n be ? ToInteger(size).
2. Let AR be the Agent Record of the surrounding agent.
3. If n equals 1, return AR.[[IsLockFree1]].
4. If n equals 2, return AR.[[IsLockFree2]].
5. If n equals 4, return true.
6. If n equals 8, return AR.[[IsLockFree8]].
7. Return false.
features: [Atomics, SharedArrayBuffer, TypedArray]
includes: [testBigIntTypedArray.js]
---*/
testWithBigIntTypedArrayConstructors(function(TA) {
assert.sameValue(Atomics.isLockFree(TA.BYTES_PER_ELEMENT), true);
});
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.isLockFree
description: >
Atomics.isLockFree returns a boolean that indicates whether
operations on datum of size will be performed without the agent
acquiring a lock outside of size bytes.
info: |
Atomics.isLockFree( size )
1. Let n be ? ToInteger(size).
2. Let AR be the Agent Record of the surrounding agent.
3. If n equals 1, return AR.[[IsLockFree1]].
4. If n equals 2, return AR.[[IsLockFree2]].
5. If n equals 4, return true.
6. Return false.
features: [Atomics, SharedArrayBuffer, TypedArray]
includes: [testTypedArray.js]
---*/
var views = intArrayConstructors.slice();
testWithTypedArrayConstructors(function(TA) {
assert.sameValue(Atomics.isLockFree(TA.BYTES_PER_ELEMENT), true);
}, views);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.load
description: >
Atomics.load returns the value that existed at the
index prior to the operation.
info: |
Atomics.load( typedArray, index, value )
1. Return ? AtomicLoad(typedArray, index).
AtomicLoad( typedArray, index )
1. Let buffer be ? ValidateSharedIntegerTypedArray(typedArray).
2. Let i be ? ValidateAtomicAccess(typedArray, index).
3. Let arrayTypeName be typedArray.[[TypedArrayName]].
4. Let elementSize be the Number value of the Element Size value
specified in Table 56 for arrayTypeName.
5. Let elementType be the String value of the Element Type value
in Table 56 for arrayTypeName.
6. Let offset be typedArray.[[ByteOffset]].
7. Let indexedPosition be (i × elementSize) + offset.
8. Return GetValueFromBuffer(buffer, indexedPosition, elementType,
true, "SeqCst").
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
assert.sameValue(Atomics.load(i32a, 0), 0);
i32a[0] = value;
assert.sameValue(Atomics.load(i32a, 0), value);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.or
description: >
Atomics.and returns the value that existed at the
index prior to the operation.
info: |
Atomics.or( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, or).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
assert.sameValue(Atomics.or(i32a, 0, value), 0);
assert.sameValue(i32a[0], 0 | value);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.store
description: >
Atomics.store returns the newly stored value
info: |
Atomics.store( typedArray, index, value )
...
3. Let v be ? ToInteger(value).
...
9. Perform SetValueInBuffer(buffer, indexedPosition,
elementType, v, true, "SeqCst").
10. Return v.
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var expect = 0b00000001000000001000000010000001;
assert.sameValue(Atomics.store(i32a, 0, expect), expect);
assert.sameValue(i32a[0], expect);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.sub
description: >
Atomics.and returns the value that existed at the
index prior to the operation.
info: |
Atomics.sub( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, subtract).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
i32a[0] = value;
assert.sameValue(Atomics.sub(i32a, 0, value), value);
assert.sameValue(i32a[0], 0);
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.wait
description: >
Demonstrates that Atomics.store(...) is causing a waiting
features: [Atomics, computed-property-names, SharedArrayBuffer, TypedArray]
---*/
function getReport() {
var r;
while ((r = $262.agent.getReport()) == null) {
$262.agent.sleep(10);
}
return r;
}
const TWO_SECOND_TIMEOUT = 2000;
const i32a = new Int32Array(
new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT)
);
$262.agent.start(`
$262.agent.receiveBroadcast(function(sab) {
var i32a = new Int32Array(sab);
var before = Date.now();
Atomics.wait(i32a, 0, 0, ${TWO_SECOND_TIMEOUT});
$262.agent.report("done");
$262.agent.leaving();
});
`);
$262.agent.broadcast(i32a.buffer);
$262.agent.sleep(10);
Atomics.store(i32a, 0, 0x111111);
assert.sameValue(getReport(), "done");
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.xor
description: >
Atomics.xor returns the value that existed at the
index prior to the operation.
info: |
Atomics.xor( typedArray, index, value )
1. Return ? AtomicReadModifyWrite(typedArray, index, value, xor).
AtomicReadModifyWrite( typedArray, index, value, op )
...
9. Return GetModifySetValueInBuffer(buffer, indexedPosition,
elementType, v, op).
GetModifySetValueInBuffer( arrayBuffer,
byteIndex, type, value, op [ , isLittleEndian ] )
...
16. Return RawBytesToNumber(type, rawBytesRead, isLittleEndian).
features: [Atomics, SharedArrayBuffer, TypedArray]
---*/
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
var i32a = new Int32Array(buffer);
var value = 0b00000001000000001000000010000001;
var other = 0b00000001111111111000000011111111;
i32a[0] = value;
assert.sameValue(Atomics.xor(i32a, 0, other), value);
assert.sameValue(i32a[0], value ^ other);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment