Skip to content

Add Atomics ns + .isLockFree #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions std/assembly/atomics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
export namespace Atomics {
// @ts-ignore: decorator
@inline
export function load<T>(array: ArrayBufferView<T>, index: i32): T {
if (isFloat<T>()) ERROR("Atomics.load accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.load<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset);
}

// @ts-ignore: decorator
@inline
export function store<T>(array: ArrayBufferView<T>, index: i32, value: T): void {
if (isFloat<T>()) ERROR("Atomics.store accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
atomic.store<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function add<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.add accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.add<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function sub<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.sub accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.sub<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function and<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.and accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.and<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function or<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.or accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.or<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function xor<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.xor accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.xor<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function exchange<T>(array: ArrayBufferView<T>, index: i32, value: T): T {
if (isFloat<T>()) ERROR("Atomics.exchange accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.xchg<T>(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, value, index);
}

// @ts-ignore: decorator
@inline
export function compareExchange<T>(array: ArrayBufferView<T>, index: i32, expectedValue: T, replacementValue: T): T {
if (isFloat<T>()) ERROR("Atomics.compareExchange accept only integer shared typed arrays");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.cmpxchg<T>(
changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset,
expectedValue,
replacementValue,
index
);
}

// @ts-ignore: decorator
@inline
export function wait<T>(array: ArrayBufferView<T>, value: T, timeout: i64 = -1): AtomicWaitResult {
if (sizeof<T>() < 4) ERROR("Atomics.wait accept only Int32Array/Uint32Array or Int64Array/Uint64Array shared typed array");
if (isFloat<T>()) ERROR("Atomics.wait accept only Int32Array/Uint32Array or Int64Array/Uint64Array shared typed array");
return atomic.wait<T>(changetype<usize>(array.buffer) + array.byteOffset, value, timeout);
}

// @ts-ignore: decorator
@inline
export function notify<T>(array: ArrayBufferView<T>, index: i32, count: i32 = -1): i32 {
if (sizeof<T>() < 4) ERROR("Atomics.notify accept only Int32Array/Uint32Array or Int64Array/Uint64Array shared typed array");
if (isFloat<T>()) ERROR("Atomics.notify accept only Int32Array/Uint32Array or Int64Array/Uint64Array shared typed array");
if (index < 0 || (index << alignof<T>()) >= array.byteLength) throw new RangeError("Invalid atomic access index");
return atomic.notify(changetype<usize>(array.buffer) + (index << alignof<T>()) + array.byteOffset, count);
}

export function isLockFree(size: usize): bool {
return size == 1 || size == 2 || size == 4;
}
}
16 changes: 16 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,22 @@ declare namespace table {
export function copy(dest: u32, src: u32, n: u32): void;
}

declare namespace Atomics {
export function load<T>(array: ArrayBufferView<T>, index: i32): T;
export function store<T>(array: ArrayBufferView<T>, index: i32, value: T): void;
export function add<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function sub<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function and<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function or<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function xor<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function exchange<T>(array: ArrayBufferView<T>, index: i32, value: T): T;
export function compareExchange<T>(array: ArrayBufferView<T>, index: i32, expectedValue: T, replacementValue: T): T;
export function wait<T>(array: ArrayBufferView<T>, value: T, timeout?: i64): AtomicWaitResult;
export function notify<T>(array: ArrayBufferView<T>, index: i32, count?: i32): i32;
/** The static Atomics.isLockFree() method is used to determine whether to use locks or atomic operations. It returns true, if the given size is one of the BYTES_PER_ELEMENT */
export function isLockFree(size: usize): bool;
}

/** Class representing a generic, fixed-length raw binary data buffer. */
declare class ArrayBuffer {
/** The size, in bytes, of the array. */
Expand Down
Loading