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 4 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
76 changes: 76 additions & 0 deletions std/assembly/atomics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export namespace Atomics {
// @ts-ignore: decorator
@inline
export function load<T>(array: ArrayBufferView<T>, index: i32): T {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 @@ -1054,6 +1054,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