-
-
Notifications
You must be signed in to change notification settings - Fork 670
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7387b7d
Add atomic.isLockFree
MaxGraey a929b33
Merge branch 'master' into add-islockfree
MaxGraey f74e544
Add Atomics namespace (wip)
MaxGraey 871d054
Fixes
MaxGraey 2dfbd91
Merge branch 'master' into add-islockfree
MaxGraey 2666199
Add range & type validations
MaxGraey 8e02733
fix ArrayBufferView
MaxGraey 767e9b9
Merge branch 'master' into add-islockfree
MaxGraey 0e153b5
Simplify checks
MaxGraey 9d5e46c
trigger ci
MaxGraey 05ed8db
improve load using T exends ArrayBufferView
MaxGraey 119e65c
data -> buffer
MaxGraey 76c4ac2
improve definisions
MaxGraey d125b38
improve defs again
MaxGraey 1afa09b
index fixes + convert to T extends ArrayBufferView rest methods
MaxGraey cef93b0
use error from util
MaxGraey 68324a5
refactor
MaxGraey d5fd03a
Merge branch 'master' into add-islockfree
MaxGraey df36f28
reuse E_INDEXOUTOFRANGE
MaxGraey 339fce9
Merge branch 'master' into add-islockfree
MaxGraey e9ffe63
Merge branch 'master' into add-islockfree
MaxGraey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.