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 all 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
117 changes: 117 additions & 0 deletions std/assembly/atomics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { ArrayBufferView } from "./arraybuffer";
import { E_INDEXOUTOFRANGE } from "./util/error";

export namespace Atomics {

// @ts-ignore: decorator
@inline
export function load<T extends ArrayBufferView>(array: T, index: i32): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.load<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset
);
}

// @ts-ignore: decorator
@inline
export function store<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): void {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
atomic.store<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function add<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.add<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function sub<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.sub<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function and<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.and<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function or<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.or<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function xor<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.xor<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function exchange<T extends ArrayBufferView>(array: T, index: i32, value: valueof<T>): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.xchg<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
value
);
}

// @ts-ignore: decorator
@inline
export function compareExchange<T extends ArrayBufferView>(
array: T,
index: i32,
expectedValue: valueof<T>,
replacementValue: valueof<T>
): valueof<T> {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.cmpxchg<valueof<T>>(
changetype<usize>(array.buffer) + (index << alignof<valueof<T>>()) + array.byteOffset,
expectedValue,
replacementValue
);
}

// @ts-ignore: decorator
@inline
export function wait<T extends ArrayBufferView>(array: T, value: valueof<T>, timeout: i64 = -1): AtomicWaitResult {
return atomic.wait<valueof<T>>(changetype<usize>(array.buffer) + array.byteOffset, value, timeout);
}

// @ts-ignore: decorator
@inline
export function notify<T extends ArrayBufferView>(array: T, index: i32, count: i32 = -1): i32 {
if (index < 0 || (index << alignof<valueof<T>>()) >= array.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
return atomic.notify(changetype<usize>(array.buffer) + (index << alignof<valueof<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 @@ -1116,6 +1116,22 @@ declare namespace table {
export function copy(dest: u32, src: u32, n: u32): void;
}

declare namespace Atomics {
export function load<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32): T;
export function store<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): void;
export function add<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function sub<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function and<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function or<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function xor<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function exchange<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, value: T): T;
export function compareExchange<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, index: i32, expectedValue: T, replacementValue: T): T;
export function wait<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<T>, value: T, timeout?: i64): AtomicWaitResult;
export function notify<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64>(array: TypedArray<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